9
9
trace ,
10
10
} from "@opentelemetry/api" ;
11
11
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http" ;
12
- import { registerInstrumentations } from "@opentelemetry/instrumentation" ;
12
+ import { InstrumentationOption , registerInstrumentations } from "@opentelemetry/instrumentation" ;
13
13
import { ExpressInstrumentation } from "@opentelemetry/instrumentation-express" ;
14
14
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http" ;
15
15
import { Resource } from "@opentelemetry/resources" ;
@@ -23,12 +23,13 @@ import {
23
23
TraceIdRatioBasedSampler ,
24
24
} from "@opentelemetry/sdk-trace-base" ;
25
25
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node" ;
26
- import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions" ;
26
+ import { SEMRESATTRS_SERVICE_NAME } from "@opentelemetry/semantic-conventions" ;
27
27
import { PrismaInstrumentation } from "@prisma/instrumentation" ;
28
28
import { env } from "~/env.server" ;
29
29
import { AuthenticatedEnvironment } from "~/services/apiAuth.server" ;
30
30
import { singleton } from "~/utils/singleton" ;
31
31
import { LoggerSpanExporter } from "./telemetry/loggerExporter.server" ;
32
+
32
33
class CustomWebappSampler implements Sampler {
33
34
constructor ( private readonly _baseSampler : Sampler ) { }
34
35
@@ -44,10 +45,7 @@ class CustomWebappSampler implements Sampler {
44
45
const parentContext = trace . getSpanContext ( context ) ;
45
46
46
47
// Exclude Prisma spans (adjust this logic as needed for your use case)
47
- if (
48
- ! parentContext &&
49
- ( ( attributes && attributes [ "model" ] && attributes [ "method" ] ) || name . includes ( "prisma" ) )
50
- ) {
48
+ if ( ! parentContext && name . includes ( "prisma" ) ) {
51
49
return { decision : SamplingDecision . NOT_RECORD } ;
52
50
}
53
51
@@ -65,29 +63,46 @@ export const tracer = singleton("tracer", getTracer);
65
63
function getTracer ( ) {
66
64
diag . setLogger ( new DiagConsoleLogger ( ) , DiagLogLevel . ERROR ) ;
67
65
66
+ const samplingRate = 1.0 / Math . max ( parseInt ( env . INTERNAL_OTEL_TRACE_SAMPING_RATE , 10 ) , 1 ) ;
67
+
68
68
const provider = new NodeTracerProvider ( {
69
69
forceFlushTimeoutMillis : 500 ,
70
70
resource : new Resource ( {
71
- [ SemanticResourceAttributes . SERVICE_NAME ] : "trigger.dev" ,
71
+ [ SEMRESATTRS_SERVICE_NAME ] : "trigger.dev" ,
72
72
} ) ,
73
73
sampler : new ParentBasedSampler ( {
74
- root : new CustomWebappSampler (
75
- new TraceIdRatioBasedSampler ( env . APP_ENV === "development" ? 1.0 : 0.05 )
76
- ) , // 5% sampling
74
+ root : new CustomWebappSampler ( new TraceIdRatioBasedSampler ( samplingRate ) ) , // 5% sampling
77
75
} ) , // 5% sampling
78
76
} ) ;
79
77
80
- if ( env . OTLP_EXPORTER_TRACES_URL ) {
78
+ if ( env . INTERNAL_OTEL_TRACE_EXPORTER_URL ) {
81
79
const exporter = new OTLPTraceExporter ( {
82
- url : env . OTLP_EXPORTER_TRACES_URL ,
80
+ url : env . INTERNAL_OTEL_TRACE_EXPORTER_URL ,
83
81
timeoutMillis : 1000 ,
82
+ headers :
83
+ env . INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADER_NAME &&
84
+ env . INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADER_VALUE
85
+ ? {
86
+ [ env . INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADER_NAME ] :
87
+ env . INTERNAL_OTEL_TRACE_EXPORTER_AUTH_HEADER_VALUE ,
88
+ }
89
+ : undefined ,
84
90
} ) ;
85
91
86
- provider . addSpanProcessor ( new BatchSpanProcessor ( exporter ) ) ;
92
+ provider . addSpanProcessor (
93
+ new BatchSpanProcessor ( exporter , {
94
+ maxExportBatchSize : 512 ,
95
+ scheduledDelayMillis : 200 ,
96
+ exportTimeoutMillis : 30000 ,
97
+ maxQueueSize : 2048 ,
98
+ } )
99
+ ) ;
87
100
88
- console . log ( `⚡ Tracer: OTLP exporter enabled to ${ env . OTLP_EXPORTER_TRACES_URL } ` ) ;
101
+ console . log ( `🔦 Tracer: OTLP exporter enabled to ${ env . INTERNAL_OTEL_TRACE_EXPORTER_URL } ` ) ;
89
102
} else {
90
- if ( env . LOG_TELEMETRY === "true" ) {
103
+ if ( env . INTERNAL_OTEL_TRACE_LOGGING_ENABLED === "1" ) {
104
+ console . log ( `🔦 Tracer: Logger exporter enabled` ) ;
105
+
91
106
const loggerExporter = new LoggerSpanExporter ( ) ;
92
107
93
108
provider . addSpanProcessor ( new SimpleSpanProcessor ( loggerExporter ) ) ;
@@ -96,13 +111,18 @@ function getTracer() {
96
111
97
112
provider . register ( ) ;
98
113
114
+ let instrumentations : InstrumentationOption [ ] = [
115
+ new HttpInstrumentation ( ) ,
116
+ new ExpressInstrumentation ( ) ,
117
+ ] ;
118
+
119
+ if ( env . INTERNAL_OTEL_TRACE_INSTRUMENT_PRISMA_ENABLED === "1" ) {
120
+ instrumentations . push ( new PrismaInstrumentation ( ) ) ;
121
+ }
122
+
99
123
registerInstrumentations ( {
100
124
tracerProvider : provider ,
101
- instrumentations : [
102
- new HttpInstrumentation ( ) ,
103
- new ExpressInstrumentation ( ) ,
104
- new PrismaInstrumentation ( ) ,
105
- ] ,
125
+ instrumentations,
106
126
} ) ;
107
127
108
128
return provider . getTracer ( "trigger.dev" , "3.0.0.dp.1" ) ;
0 commit comments