Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of adding sentry to an existing OTEL app #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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