Skip to content

Commit

Permalink
Switched to OTel + Sentry custom setup
Browse files Browse the repository at this point in the history
ref https://docs.sentry.io/platforms/javascript/guides/node/opentelemetry/custom-setup/

- I feel like I'm going back and forth with this but nothing seems to
  work flawlessly and it's quite frustrating
- I've copied the following from the Sentry docs, so if it doesn't work
  here, I can open an issue on their repo
  • Loading branch information
daniellockyer committed Nov 18, 2024
1 parent 1d19687 commit 4fbb87b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 486 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
"@js-temporal/polyfill": "0.4.4",
"@logtape/logtape": "0.7.1",
"@opentelemetry/api": "1.9.0",
"@opentelemetry/auto-instrumentations-node": "0.52.1",
"@opentelemetry/core": "1.27.0",
"@opentelemetry/exporter-trace-otlp-proto": "0.54.2",
"@opentelemetry/sdk-node": "0.54.2",
"@opentelemetry/sdk-trace-base": "1.27.0",
"@opentelemetry/sdk-trace-node": "1.27.0",
"@sentry/node": "8.37.1",
"@sentry/opentelemetry": "8.38.0",
"hono": "4.6.9",
"jsonwebtoken": "9.0.2",
"knex": "3.1.0",
Expand Down
120 changes: 58 additions & 62 deletions src/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import { CloudPropagator } from '@google-cloud/opentelemetry-cloud-trace-propagator';
import {
DiagConsoleLogger,
DiagLogLevel,
type TextMapPropagator,
diag,
trace,
} from '@opentelemetry/api';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { CompositePropagator } from '@opentelemetry/core';
import {
BatchSpanProcessor,
SimpleSpanProcessor,
} 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;
import {
SentryPropagator,
SentrySampler,
SentrySpanProcessor,
} from '@sentry/opentelemetry';

if (process.env.NODE_ENV === 'production') {
if (process.env.OTEL_DEBUG_LOGGING) {
Expand All @@ -33,65 +25,69 @@ if (process.env.NODE_ENV === 'production') {
}
}

if (process.env.K_SERVICE) {
const { TraceExporter } = await import(
'@google-cloud/opentelemetry-cloud-trace-exporter'
);
provider.addSpanProcessor(new BatchSpanProcessor(new TraceExporter()));
if (process.env.SENTRY_DSN) {
const sentryClient = Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.NODE_ENV || 'unknown',
release: process.env.K_REVISION,

propagator = new CloudPropagator();
}
skipOpenTelemetrySetup: true,
tracesSampleRate: 1.0,
});

const provider = new NodeTracerProvider({
sampler: sentryClient ? new SentrySampler(sentryClient) : undefined,
});

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

provider.addSpanProcessor(
new SimpleSpanProcessor(
new OTLPTraceExporter({
url: 'http://jaeger:4318/v1/traces',
}),
),
);
}
const propagators: TextMapPropagator[] = [new SentryPropagator()];

provider.register({
propagator,
});
if (process.env.K_SERVICE) {
const { TraceExporter } = await import(
'@google-cloud/opentelemetry-cloud-trace-exporter'
);
provider.addSpanProcessor(
new BatchSpanProcessor(new TraceExporter({})),
);

export const tracer = trace.getTracer('activitypub');
const { CloudPropagator } = await import(
'@google-cloud/opentelemetry-cloud-trace-propagator'
);
propagators.push(new CloudPropagator());
}

try {
sdk.start();
} catch (e) {
console.error(e);
}
if (process.env.NODE_ENV === 'development') {
const { OTLPTraceExporter } = await import(
'@opentelemetry/exporter-trace-otlp-proto'
);
provider.addSpanProcessor(
new SimpleSpanProcessor(
new OTLPTraceExporter({
url: 'http://jaeger:4318/v1/traces',
}),
),
);
}

if (process.env.SENTRY_DSN) {
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.NODE_ENV || 'unknown',
release: process.env.K_REVISION,
skipOpenTelemetrySetup: true,
provider.register({
propagator: new CompositePropagator({ propagators }),
contextManager: new Sentry.SentryContextManager(),
});

Sentry.validateOpenTelemetrySetup();
}

export function spanWrapper<TArgs extends unknown[], TReturn>(
fn: (...args: TArgs) => TReturn | Promise<TReturn>,
fn: (...args: TArgs) => TReturn,
) {
return (...args: TArgs) => {
return tracer.startActiveSpan(fn.name || 'anonymous', async (span) => {
try {
const result = await Promise.resolve(fn(...args));
span.end();
return result;
} catch (error) {
span.recordException(error as Error);
span.setStatus({ code: 2 }); // OpenTelemetry ERROR status
span.end();
throw error;
}
});
return Sentry.startSpan(
{
op: 'fn',
name: fn.name,
},
() => fn(...args),
);
};
}
Loading

0 comments on commit 4fbb87b

Please sign in to comment.