forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example of adding sentry to an existing OTEL app
- Loading branch information
1 parent
01a2c35
commit cf9aa82
Showing
5 changed files
with
2,546 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
@@ -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; | ||
|
||
|
Oops, something went wrong.