Skip to content

Commit

Permalink
Fixed duplicate initialization of OTel API
Browse files Browse the repository at this point in the history
- I misread the docs here and didn't realise you shouldn't set up a
  provider and an SDK separately
- this fixes that by just switching to the SDK
  • Loading branch information
daniellockyer committed Nov 14, 2024
1 parent 2eabf20 commit 06e0d8e
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CloudPropagator } from '@google-cloud/opentelemetry-cloud-trace-propagator';
import {
DiagConsoleLogger,
DiagLogLevel,
type TextMapPropagator,
diag,
trace,
} from '@opentelemetry/api';
Expand All @@ -10,20 +10,12 @@ import { NodeSDK } from '@opentelemetry/sdk-node';
import {
BatchSpanProcessor,
SimpleSpanProcessor,
type SpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import * as Sentry from '@sentry/node';

const sdk = new NodeSDK({
instrumentations: getNodeAutoInstrumentations({
'@opentelemetry/instrumentation-mysql2': {
addSqlCommenterCommentToQueries: true,
},
}),
});

const provider = new NodeTracerProvider();
let propagator: CloudPropagator | undefined;
let textMapPropagator: TextMapPropagator | undefined;
const spanProcessors: SpanProcessor[] = [];

if (process.env.NODE_ENV === 'production') {
if (process.env.OTEL_DEBUG_LOGGING) {
Expand All @@ -37,17 +29,20 @@ if (process.env.K_SERVICE) {
const { TraceExporter } = await import(
'@google-cloud/opentelemetry-cloud-trace-exporter'
);
provider.addSpanProcessor(new BatchSpanProcessor(new TraceExporter()));
spanProcessors.push(new BatchSpanProcessor(new TraceExporter()));

propagator = new CloudPropagator();
const { CloudPropagator } = await import(
'@google-cloud/opentelemetry-cloud-trace-propagator'
);
textMapPropagator = new CloudPropagator();
}

if (process.env.NODE_ENV === 'development') {
const { OTLPTraceExporter } = await import(
'@opentelemetry/exporter-trace-otlp-proto'
);

provider.addSpanProcessor(
spanProcessors.push(
new SimpleSpanProcessor(
new OTLPTraceExporter({
url: 'http://jaeger:4318/v1/traces',
Expand All @@ -56,12 +51,18 @@ if (process.env.NODE_ENV === 'development') {
);
}

provider.register({
propagator,
});

export const tracer = trace.getTracer('activitypub');

const sdk = new NodeSDK({
instrumentations: getNodeAutoInstrumentations({
'@opentelemetry/instrumentation-mysql2': {
addSqlCommenterCommentToQueries: true,
},
}),
spanProcessors,
textMapPropagator,
});

try {
sdk.start();
} catch (e) {
Expand Down

0 comments on commit 06e0d8e

Please sign in to comment.