[JS] Telemetry does not work with Sentry or Elastic APM
Describe the bug If I want to use Sentry or Elastic APM with the firebase genkit Telemetry in local or in production, no trace are sent.
To Reproduce
NODE_OPTIONS="-r elastic-apm-node/start.js" genkit start -- nest start --watch
Expected behavior It should work
Screenshots
Runtime (please complete the following information):
- devcontainer : mcr.microsoft.com/devcontainers/typescript-node:1-22-bookworm
** Node version Node v22.9.0
Additional context Add any other context about the problem here.
I think I fixed my project's backend telemetry with Sentry + Genkit:
Context
Per Sentry docs (https://github.com/getsentry/sentry-javascript/blob/develop/docs/v8-node.md#using-a-custom-opentelemetry-setup), Sentry.init() creates its own OpenTelemetry provider.
So, to use a custom OpenTelemetry setup (like genkit), you need to opt out with skipOpenTelemetrySetup so that Sentry's initialization doesn't interfere on Genkit's setup!
Minimal example
This is a simplified version of what I tried (if it doesn't work out of the box, let me know):
import {
SentrySpanProcessor,
SentryPropagator,
setOpenTelemetryContextAsyncContextStrategy,
setupEventContextTrace,
} from '@sentry/opentelemetry';
import { context, propagation, trace } from '@opentelemetry/api';
Sentry.init({
dsn: '…',
environment: 'development',
skipOpenTelemetrySetup: true,
});
enableFirebaseTelemetry({
// forceDevExport: true // Enable to test in Firebase emulator
}).then(async () => {
const provider: any = trace.getTracerProvider();
provider.addSpanProcessor(new SentrySpanProcessor());
propagation.setGlobalPropagator(new SentryPropagator());
setOpenTelemetryContextAsyncContextStrategy();
setupEventContextTrace(Sentry.getClient());
});
I'm guessing Elastic APM would need the same pattern: let Genkit own the provider and have the third-party SDK add its processor?
For use calling disableOTelRootSpanDetection() is sufficient to use it with sentry.