Skip to content

Proposal to possibly fix @microlabs/otel-cf-workers conflics #17399

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

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
57 changes: 53 additions & 4 deletions packages/cloudflare/src/opentelemetry/tracer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Context, Span, SpanOptions, Tracer, TracerProvider } from '@opentelemetry/api';
import type { Context, ProxyTracerProvider, Span, SpanOptions, Tracer, TracerProvider } from '@opentelemetry/api';
import { trace } from '@opentelemetry/api';
import { startInactiveSpan, startSpanManual } from '@sentry/core';

Expand All @@ -7,16 +7,24 @@ import { startInactiveSpan, startSpanManual } from '@sentry/core';
* This is not perfect but handles easy/common use cases.
*/
export function setupOpenTelemetryTracer(): void {
trace.setGlobalTracerProvider(new SentryCloudflareTraceProvider());
const result = trace.setGlobalTracerProvider(new SentryCloudflareTraceProvider());
if (result) {
return;
}
const current = trace.getTracerProvider() as ProxyTracerProvider;
current.setDelegate(new SentryCloudflareTraceProvider(current.getDelegate()));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Tracer Provider Verification Missing

The code assumes the existing global tracer provider is a ProxyTracerProvider and calls setDelegate without verification. If the current provider doesn't implement setDelegate, this will cause a runtime error.

Fix in Cursor Fix in Web

}

class SentryCloudflareTraceProvider implements TracerProvider {
private readonly _tracers: Map<string, Tracer> = new Map();

public constructor(private readonly _provider?: TracerProvider) {}

public getTracer(name: string, version?: string, options?: { schemaUrl?: string }): Tracer {
const key = `${name}@${version || ''}:${options?.schemaUrl || ''}`;
if (!this._tracers.has(key)) {
this._tracers.set(key, new SentryCloudflareTracer());
const tracer = this._provider?.getTracer?.(key, version, options);
this._tracers.set(key, new SentryCloudflareTracer(tracer));
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand All @@ -25,15 +33,56 @@ class SentryCloudflareTraceProvider implements TracerProvider {
}

class SentryCloudflareTracer implements Tracer {
public constructor(private readonly _tracer?: Tracer) {}
public startSpan(name: string, options?: SpanOptions): Span {
return startInactiveSpan({
const topSpan = this._tracer?.startSpan?.(name, options);
const sentrySpan = startInactiveSpan({
name,
...options,
attributes: {
...options?.attributes,
'sentry.cloudflare_tracer': true,
},
});
if (!topSpan) {
return sentrySpan;
}
const _proxied = new WeakMap<CallableFunction, CallableFunction>();
return new Proxy(sentrySpan, {
set: (target, p, newValue, receiver) => {
try {
Reflect.set(topSpan, p, newValue);
} catch {
//
}
return Reflect.set(target, p, newValue, receiver);
},
get: (target, p) => {
const propertyValue = Reflect.get(target, p);
if (typeof propertyValue !== 'function') {
return propertyValue;
}
const proxyTo = Reflect.get(topSpan, p);
if (typeof proxyTo !== 'function') {
return propertyValue;
}
if (_proxied.has(propertyValue)) {
return _proxied.get(propertyValue);
}
const proxy = new Proxy(propertyValue, {
apply: (target, thisArg, argArray) => {
try {
Reflect.apply(proxyTo, topSpan, argArray);
} catch {
//
}
return Reflect.apply(target, thisArg, argArray);
},
});
_proxied.set(propertyValue, proxy);
return proxy;
},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Proxy Issues Cause Telemetry and Memory Problems

The Proxy implementation for SentryCloudflareTracer's spans has a few issues. It causes methods to execute twice, leading to inconsistent telemetry. It also swallows errors from the underlying OpenTelemetry tracer. Additionally, accessing functions on the proxied span creates new proxies every time, which can lead to memory leaks.

Fix in Cursor Fix in Web

});
}

/**
Expand Down
56 changes: 55 additions & 1 deletion packages/cloudflare/test/opentelemetry.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { trace } from '@opentelemetry/api';
import type { TransactionEvent } from '@sentry/core';
import { startSpan } from '@sentry/core';
import { beforeEach, describe, expect, test } from 'vitest';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { init } from '../src/sdk';
import { resetSdk } from './testUtils';

Expand Down Expand Up @@ -132,6 +132,60 @@ describe('opentelemetry compatibility', () => {
'sentry.source': 'custom',
});

expect(transactionEvent?.spans).toEqual([
expect.objectContaining({
description: 'otel span',
data: {
'sentry.cloudflare_tracer': true,
'sentry.origin': 'manual',
},
}),
]);
});
test('Ensure that sentry spans works over other opentelemetry implementations', async () => {
const transactionEvents: TransactionEvent[] = [];
const end = vi.fn();
const _startSpan = vi.fn().mockImplementation(() => ({ end }));

const getTracer = vi.fn().mockImplementation(() => ({
startSpan: _startSpan,
}));
trace.setGlobalTracerProvider({
getTracer,
});

const client = init({
dsn: 'https://username@domain/123',
tracesSampleRate: 1,
beforeSendTransaction: event => {
transactionEvents.push(event);
return null;
},
});

const tracer = trace.getTracer('test');

expect(getTracer).toBeCalledWith('test@:', undefined, undefined);
startSpan({ name: 'sentry span' }, () => {
const span = tracer.startSpan('otel span');
span.end();
});
expect(_startSpan).toBeCalledWith('otel span', undefined);
expect(end).toBeCalled();

await client!.flush();

expect(transactionEvents).toHaveLength(1);
const [transactionEvent] = transactionEvents;

expect(transactionEvent?.spans?.length).toBe(1);
expect(transactionEvent?.transaction).toBe('sentry span');
expect(transactionEvent?.contexts?.trace?.data).toEqual({
'sentry.origin': 'manual',
'sentry.sample_rate': 1,
'sentry.source': 'custom',
});

expect(transactionEvent?.spans).toEqual([
expect.objectContaining({
description: 'otel span',
Expand Down