1
+ /* eslint-disable max-params */
2
+ import { Context , Link , SpanAttributes , SpanKind } from "@opentelemetry/api" ;
3
+ import { ParentBasedSampler , TraceIdRatioBasedSampler } from "@opentelemetry/sdk-trace-node" ;
1
4
import { ATTR_SERVICE_VERSION } from "@opentelemetry/semantic-conventions" ;
2
5
import { ATTR_DEPLOYMENT_ENVIRONMENT_NAME } from "@opentelemetry/semantic-conventions/incubating" ;
3
6
import { createAwsInstrumentation } from "@saleor/apps-otel/src/aws-instrumentation-factory" ;
4
7
import { createBatchSpanProcessor } from "@saleor/apps-otel/src/batch-span-processor-factory" ;
5
8
import { createHttpInstrumentation } from "@saleor/apps-otel/src/http-instrumentation-factory" ;
6
9
import { ObservabilityAttributes } from "@saleor/apps-otel/src/observability-attributes" ;
10
+ import * as Sentry from "@sentry/nextjs" ;
11
+ import { SentryPropagator , wrapSamplingDecision } from "@sentry/opentelemetry" ;
7
12
import { registerOTel } from "@vercel/otel" ;
8
13
9
14
import { env } from "@/env" ;
15
+ import { createLogger } from "@/logger" ;
10
16
11
17
import pkg from "../../package.json" ;
12
18
19
+ const logger = createLogger ( "instrumentations/otel-node" ) ;
20
+
21
+ // Use ParentBasedSampler to make decision and then wrap it with Sentry wrapSamplingDecision
22
+ class AppSampler extends ParentBasedSampler {
23
+ shouldSample (
24
+ context : Context ,
25
+ traceId : string ,
26
+ spanName : string ,
27
+ spanKind : SpanKind ,
28
+ attributes : SpanAttributes ,
29
+ links : Link [ ] ,
30
+ ) {
31
+ const { decision } = super . shouldSample (
32
+ context ,
33
+ traceId ,
34
+ spanName ,
35
+ spanKind ,
36
+ attributes ,
37
+ links ,
38
+ ) ;
39
+
40
+ logger . info ( "AppSampler.shouldSample.decision" , { decision } ) ;
41
+
42
+ return wrapSamplingDecision ( {
43
+ decision,
44
+ context,
45
+ spanAttributes : attributes ,
46
+ } ) ;
47
+ }
48
+ toString ( ) {
49
+ return "AppSampler" ;
50
+ }
51
+ }
52
+
53
+ Sentry . init ( {
54
+ dsn : env . NEXT_PUBLIC_SENTRY_DSN ,
55
+ environment : env . ENV ,
56
+ includeLocalVariables : true ,
57
+ skipOpenTelemetrySetup : true ,
58
+ registerEsmLoaderHooks : false ,
59
+ integrations : [
60
+ Sentry . localVariablesIntegration ( {
61
+ captureAllExceptions : true ,
62
+ } ) ,
63
+ Sentry . extraErrorDataIntegration ( ) ,
64
+ Sentry . httpIntegration ( { spans : false } ) ,
65
+ ] ,
66
+ } ) ;
67
+
13
68
registerOTel ( {
14
69
serviceName : env . OTEL_SERVICE_NAME ,
15
70
attributes : {
@@ -27,4 +82,9 @@ registerOTel({
27
82
} ) ,
28
83
] ,
29
84
instrumentations : [ createAwsInstrumentation ( ) , createHttpInstrumentation ( ) ] ,
85
+ traceSampler : new AppSampler ( {
86
+ root : new TraceIdRatioBasedSampler ( env . OTEL_TRACES_SAMPLER_ARG ) ,
87
+ } ) ,
88
+ propagators : [ new SentryPropagator ( ) ] ,
89
+ contextManager : new Sentry . SentryContextManager ( ) ,
30
90
} ) ;
0 commit comments