1
+ import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-proto' ;
1
2
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto' ;
2
3
import {
4
+ Detector ,
5
+ DetectorSync ,
3
6
envDetectorSync ,
4
7
hostDetectorSync ,
8
+ IResource ,
5
9
osDetectorSync ,
6
10
processDetectorSync ,
11
+ ResourceDetectionConfig ,
7
12
} from '@opentelemetry/resources' ;
8
13
import { containerDetector } from '@opentelemetry/resource-detector-container' ;
9
14
import { gcpDetector } from '@opentelemetry/resource-detector-gcp' ;
@@ -23,7 +28,7 @@ import type { ConfigurationSchema } from '../config/schema.js';
23
28
import { getAutoInstrumentations } from './instrumentations.js' ;
24
29
import { DummySpanExporter } from './DummyExporter.js' ;
25
30
26
- function getExporter ( ) {
31
+ function getSpanExporter ( ) {
27
32
if (
28
33
! process . env . DISABLE_OLTP_EXPORTER &&
29
34
( [ 'production' , 'staging' ] . includes ( process . env . APP_ENV || process . env . NODE_ENV || '' ) ||
@@ -39,9 +44,36 @@ function getExporter() {
39
44
return new DummySpanExporter ( ) ;
40
45
}
41
46
47
+ function getLogExporter ( ) {
48
+ if (
49
+ ! process . env . DISABLE_OLTP_EXPORTER &&
50
+ ( [ 'production' , 'staging' ] . includes ( process . env . APP_ENV || process . env . NODE_ENV || '' ) ||
51
+ process . env . OTLP_EXPORTER )
52
+ ) {
53
+ return new OTLPLogExporter ( {
54
+ url : process . env . OTLP_EXPORTER || 'http://otlp-exporter:4318/v1/logs' ,
55
+ } ) ;
56
+ }
57
+ if ( process . env . ENABLE_CONSOLE_OLTP_EXPORTER ) {
58
+ return new opentelemetry . logs . ConsoleLogRecordExporter ( ) ;
59
+ }
60
+ return undefined ;
61
+ }
62
+
42
63
let prometheusExporter : PrometheusExporter | undefined ;
43
64
let telemetrySdk : opentelemetry . NodeSDK | undefined ;
44
65
66
+ function awaitAttributes ( detector : DetectorSync ) : Detector {
67
+ return {
68
+ async detect ( config ?: ResourceDetectionConfig ) : Promise < IResource > {
69
+ const resource = detector . detect ( config )
70
+ await resource . waitForAsyncAttributes ?.( )
71
+
72
+ return resource
73
+ } ,
74
+ }
75
+ }
76
+
45
77
/**
46
78
* OpenTelemetry is not friendly to the idea of stopping
47
79
* and starting itself, it seems. So we can only keep a global
@@ -52,31 +84,31 @@ let telemetrySdk: opentelemetry.NodeSDK | undefined;
52
84
*/
53
85
export async function startGlobalTelemetry ( serviceName : string ) {
54
86
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 ) ;
87
+ const { metrics, logs, NodeSDK } = opentelemetry ;
57
88
58
89
prometheusExporter = new PrometheusExporter ( { preventServerStart : true } ) ;
59
90
const instrumentations = getAutoInstrumentations ( ) ;
60
- telemetrySdk = new opentelemetry . NodeSDK ( {
91
+ const logExporter = getLogExporter ( ) ;
92
+ telemetrySdk = new NodeSDK ( {
61
93
serviceName,
62
94
autoDetectResources : false ,
63
- traceExporter : getExporter ( ) ,
64
95
resourceDetectors : [
65
- envDetectorSync ,
66
- hostDetectorSync ,
67
- osDetectorSync ,
68
- processDetectorSync ,
69
- containerDetector ,
70
- gcpDetector ,
96
+ awaitAttributes ( envDetectorSync ) ,
97
+ awaitAttributes ( hostDetectorSync ) ,
98
+ awaitAttributes ( osDetectorSync ) ,
99
+ awaitAttributes ( processDetectorSync ) ,
100
+ awaitAttributes ( containerDetector ) ,
101
+ awaitAttributes ( gcpDetector ) ,
71
102
] ,
103
+ traceExporter : getSpanExporter ( ) ,
72
104
metricReader : prometheusExporter ,
73
105
instrumentations,
74
- logRecordProcessors : [ ] ,
106
+ logRecordProcessors : logExporter ? [ new logs . BatchLogRecordProcessor ( logExporter ) ] : [ ] ,
75
107
views : [
76
- new opentelemetry . metrics . View ( {
108
+ new metrics . View ( {
77
109
instrumentName : 'http_request_duration_seconds' ,
78
- instrumentType : opentelemetry . metrics . InstrumentType . HISTOGRAM ,
79
- aggregation : new opentelemetry . metrics . ExplicitBucketHistogramAggregation (
110
+ instrumentType : metrics . InstrumentType . HISTOGRAM ,
111
+ aggregation : new metrics . ExplicitBucketHistogramAggregation (
80
112
[ 0.003 , 0.03 , 0.1 , 0.3 , 1.5 , 10 ] ,
81
113
true ,
82
114
) ,
0 commit comments