Skip to content

Commit

Permalink
Example of adding sentry to an existing OTEL app
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Jun 26, 2024
1 parent 01a2c35 commit cf9aa82
Show file tree
Hide file tree
Showing 5 changed files with 2,546 additions and 39 deletions.
38 changes: 36 additions & 2 deletions examples/esm-http-ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
import * as Sentry from "@sentry/node";
import {
SentrySpanProcessor,
SentryPropagator,
SentrySampler,
} from "@sentry/opentelemetry";

// Make sure `Sentry.init` is called before any other OTEL code
Sentry.init({
// fake DSN
dsn: "https://[email protected]/1337",
skipOpenTelemetrySetup: true,

beforeSendTransaction: (transaction) => {
// Log out transactions for debugging, don't send any data to Sentry
console.log(transaction);
return null;
},

// The SentrySampler will use this to determine which traces to sample
tracesSampleRate: 1.0,
});

import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { trace, DiagConsoleLogger, DiagLogLevel, diag } from '@opentelemetry/api';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
Expand All @@ -10,21 +33,32 @@ import { Resource } from '@opentelemetry/resources';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import http from 'http';

diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
// Turn of OTEL debug logging in favour of Sentry debug logging
// diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

const sentryClient = Sentry.getClient();

const tracerProvider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: 'esm-http-ts-example',
}),
sampler: sentryClient ? new SentrySampler(sentryClient) : undefined,
});
const exporter = new ConsoleSpanExporter();
const processor = new SimpleSpanProcessor(exporter);
tracerProvider.addSpanProcessor(new SentrySpanProcessor());
tracerProvider.addSpanProcessor(processor);
tracerProvider.register();
tracerProvider.register({
propagator: new SentryPropagator(),
contextManager: new Sentry.SentryContextManager(),
});

registerInstrumentations({
instrumentations: [new HttpInstrumentation()],
});

Sentry.validateOpenTelemetrySetup();

const hostname = '0.0.0.0';
const port = 3000;

Expand Down
Loading

0 comments on commit cf9aa82

Please sign in to comment.