1+ import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-proto' ;
12import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto' ;
23import {
4+ Detector ,
5+ DetectorSync ,
36 envDetectorSync ,
47 hostDetectorSync ,
8+ IResource ,
59 osDetectorSync ,
610 processDetectorSync ,
11+ ResourceDetectionConfig ,
712} from '@opentelemetry/resources' ;
813import { containerDetector } from '@opentelemetry/resource-detector-container' ;
914import { gcpDetector } from '@opentelemetry/resource-detector-gcp' ;
@@ -23,14 +28,18 @@ import type { ConfigurationSchema } from '../config/schema.js';
2328import { getAutoInstrumentations } from './instrumentations.js' ;
2429import { DummySpanExporter } from './DummyExporter.js' ;
2530
26- function getExporter ( ) {
31+ // OTLP seems to only support http, and this is a default on the local network so I'm keeping it.
32+ // NOSONAR
33+ const baseDefaultOtlpUrl = new URL ( 'http://otlp-exporter:4318/v1' ) . toString ( ) ;
34+
35+ function getSpanExporter ( ) {
2736 if (
2837 ! process . env . DISABLE_OLTP_EXPORTER &&
2938 ( [ 'production' , 'staging' ] . includes ( process . env . APP_ENV || process . env . NODE_ENV || '' ) ||
3039 process . env . OTLP_EXPORTER )
3140 ) {
3241 return new OTLPTraceExporter ( {
33- url : process . env . OTLP_EXPORTER || 'http://otlp-exporter:4318/v1/ traces' ,
42+ url : process . env . OTLP_EXPORTER || ` ${ baseDefaultOtlpUrl } / traces` ,
3443 } ) ;
3544 }
3645 if ( process . env . ENABLE_CONSOLE_OLTP_EXPORTER ) {
@@ -39,9 +48,36 @@ function getExporter() {
3948 return new DummySpanExporter ( ) ;
4049}
4150
51+ function getLogExporter ( ) {
52+ if (
53+ ! process . env . DISABLE_OLTP_EXPORTER &&
54+ ( [ 'production' , 'staging' ] . includes ( process . env . APP_ENV || process . env . NODE_ENV || '' ) ||
55+ process . env . OTLP_EXPORTER )
56+ ) {
57+ return new OTLPLogExporter ( {
58+ url : process . env . OTLP_EXPORTER || `${ baseDefaultOtlpUrl } /logs` ,
59+ } ) ;
60+ }
61+ if ( process . env . ENABLE_CONSOLE_OLTP_EXPORTER ) {
62+ return new opentelemetry . logs . ConsoleLogRecordExporter ( ) ;
63+ }
64+ return undefined ;
65+ }
66+
4267let prometheusExporter : PrometheusExporter | undefined ;
4368let telemetrySdk : opentelemetry . NodeSDK | undefined ;
4469
70+ function awaitAttributes ( detector : DetectorSync ) : Detector {
71+ return {
72+ async detect ( config ?: ResourceDetectionConfig ) : Promise < IResource > {
73+ const resource = detector . detect ( config )
74+ await resource . waitForAsyncAttributes ?.( )
75+
76+ return resource
77+ } ,
78+ }
79+ }
80+
4581/**
4682 * OpenTelemetry is not friendly to the idea of stopping
4783 * and starting itself, it seems. So we can only keep a global
@@ -52,31 +88,31 @@ let telemetrySdk: opentelemetry.NodeSDK | undefined;
5288 */
5389export async function startGlobalTelemetry ( serviceName : string ) {
5490 if ( ! prometheusExporter ) {
55- // For troubleshooting, set the log level to DiagLogLevel.DEBUG
56- opentelemetry . api . diag . setLogger ( new ( opentelemetry . api . DiagConsoleLogger ) ( ) , opentelemetry . api . DiagLogLevel . INFO ) ;
91+ const { metrics, logs, NodeSDK } = opentelemetry ;
5792
5893 prometheusExporter = new PrometheusExporter ( { preventServerStart : true } ) ;
5994 const instrumentations = getAutoInstrumentations ( ) ;
60- telemetrySdk = new opentelemetry . NodeSDK ( {
95+ const logExporter = getLogExporter ( ) ;
96+ telemetrySdk = new NodeSDK ( {
6197 serviceName,
6298 autoDetectResources : false ,
63- traceExporter : getExporter ( ) ,
6499 resourceDetectors : [
65- envDetectorSync ,
66- hostDetectorSync ,
67- osDetectorSync ,
68- processDetectorSync ,
69- containerDetector ,
70- gcpDetector ,
100+ awaitAttributes ( envDetectorSync ) ,
101+ awaitAttributes ( hostDetectorSync ) ,
102+ awaitAttributes ( osDetectorSync ) ,
103+ awaitAttributes ( processDetectorSync ) ,
104+ awaitAttributes ( containerDetector ) ,
105+ awaitAttributes ( gcpDetector ) ,
71106 ] ,
107+ traceExporter : getSpanExporter ( ) ,
72108 metricReader : prometheusExporter ,
73109 instrumentations,
74- logRecordProcessors : [ ] ,
110+ logRecordProcessors : logExporter ? [ new logs . BatchLogRecordProcessor ( logExporter ) ] : [ ] ,
75111 views : [
76- new opentelemetry . metrics . View ( {
112+ new metrics . View ( {
77113 instrumentName : 'http_request_duration_seconds' ,
78- instrumentType : opentelemetry . metrics . InstrumentType . HISTOGRAM ,
79- aggregation : new opentelemetry . metrics . ExplicitBucketHistogramAggregation (
114+ instrumentType : metrics . InstrumentType . HISTOGRAM ,
115+ aggregation : new metrics . ExplicitBucketHistogramAggregation (
80116 [ 0.003 , 0.03 , 0.1 , 0.3 , 1.5 , 10 ] ,
81117 true ,
82118 ) ,
0 commit comments