From 1afd16afc21965168b52b8248ad94924ca15e3dc Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 14 Jul 2025 11:39:56 -0400 Subject: [PATCH 1/2] ref(opentelemetry): Use `debug` instead of `logger` --- packages/opentelemetry/src/propagator.ts | 13 +++++------ packages/opentelemetry/src/sampler.ts | 10 ++++----- packages/opentelemetry/src/spanExporter.ts | 6 ++--- .../opentelemetry/test/helpers/initOtel.ts | 22 +++++++++++-------- .../test/integration/transactions.test.ts | 12 +++++----- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/packages/opentelemetry/src/propagator.ts b/packages/opentelemetry/src/propagator.ts index 30429a490b14..40afc10120df 100644 --- a/packages/opentelemetry/src/propagator.ts +++ b/packages/opentelemetry/src/propagator.ts @@ -4,13 +4,13 @@ import { isTracingSuppressed, W3CBaggagePropagator } from '@opentelemetry/core'; import { ATTR_URL_FULL, SEMATTRS_HTTP_URL } from '@opentelemetry/semantic-conventions'; import type { Client, continueTrace, DynamicSamplingContext, Options, Scope } from '@sentry/core'; import { + debug, generateSentryTraceHeader, getClient, getCurrentScope, getDynamicSamplingContextFromScope, getDynamicSamplingContextFromSpan, getIsolationScope, - logger, LRUMap, parseBaggageHeader, propagationContextFromHeaders, @@ -45,7 +45,7 @@ export class SentryPropagator extends W3CBaggagePropagator { */ public inject(context: Context, carrier: unknown, setter: TextMapSetter): void { if (isTracingSuppressed(context)) { - DEBUG_BUILD && logger.log('[Tracing] Not injecting trace data for url because tracing is suppressed.'); + DEBUG_BUILD && debug.log('[Tracing] Not injecting trace data for url because tracing is suppressed.'); return; } @@ -55,10 +55,7 @@ export class SentryPropagator extends W3CBaggagePropagator { const tracePropagationTargets = getClient()?.getOptions()?.tracePropagationTargets; if (!shouldPropagateTraceForUrl(url, tracePropagationTargets, this._urlMatchesTargetsMap)) { DEBUG_BUILD && - logger.log( - '[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:', - url, - ); + debug.log('[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:', url); return; } @@ -139,14 +136,14 @@ export function shouldPropagateTraceForUrl( const cachedDecision = decisionMap?.get(url); if (cachedDecision !== undefined) { - DEBUG_BUILD && !cachedDecision && logger.log(NOT_PROPAGATED_MESSAGE, url); + DEBUG_BUILD && !cachedDecision && debug.log(NOT_PROPAGATED_MESSAGE, url); return cachedDecision; } const decision = stringMatchesSomePattern(url, tracePropagationTargets); decisionMap?.set(url, decision); - DEBUG_BUILD && !decision && logger.log(NOT_PROPAGATED_MESSAGE, url); + DEBUG_BUILD && !decision && debug.log(NOT_PROPAGATED_MESSAGE, url); return decision; } diff --git a/packages/opentelemetry/src/sampler.ts b/packages/opentelemetry/src/sampler.ts index 73c568152b97..e06fe51bfd2a 100644 --- a/packages/opentelemetry/src/sampler.ts +++ b/packages/opentelemetry/src/sampler.ts @@ -13,8 +13,8 @@ import { import type { Client, SpanAttributes } from '@sentry/core'; import { baggageHeaderToDynamicSamplingContext, + debug, hasSpansEnabled, - logger, parseSampleRate, sampleSpan, SEMANTIC_ATTRIBUTE_SENTRY_OP, @@ -137,7 +137,7 @@ export class SentrySampler implements Sampler { const method = `${maybeSpanHttpMethod}`.toUpperCase(); if (method === 'OPTIONS' || method === 'HEAD') { - DEBUG_BUILD && logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`); + DEBUG_BUILD && debug.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`); return wrapSamplingDecision({ decision: SamplingDecision.NOT_RECORD, @@ -153,7 +153,7 @@ export class SentrySampler implements Sampler { // We check for `parentSampled === undefined` because we only want to record client reports for spans that are trace roots (ie. when there was incoming trace) parentSampled === undefined ) { - DEBUG_BUILD && logger.log('[Tracing] Discarding root span because its trace was not chosen to be sampled.'); + DEBUG_BUILD && debug.log('[Tracing] Discarding root span because its trace was not chosen to be sampled.'); this._client.recordDroppedEvent('sample_rate', 'transaction'); } @@ -187,12 +187,12 @@ function getParentSampled(parentSpan: Span, traceId: string, spanName: string): if (parentContext.isRemote) { const parentSampled = getSamplingDecision(parentSpan.spanContext()); DEBUG_BUILD && - logger.log(`[Tracing] Inheriting remote parent's sampled decision for ${spanName}: ${parentSampled}`); + debug.log(`[Tracing] Inheriting remote parent's sampled decision for ${spanName}: ${parentSampled}`); return parentSampled; } const parentSampled = getSamplingDecision(parentContext); - DEBUG_BUILD && logger.log(`[Tracing] Inheriting parent's sampled decision for ${spanName}: ${parentSampled}`); + DEBUG_BUILD && debug.log(`[Tracing] Inheriting parent's sampled decision for ${spanName}: ${parentSampled}`); return parentSampled; } diff --git a/packages/opentelemetry/src/spanExporter.ts b/packages/opentelemetry/src/spanExporter.ts index 6430f0f23da5..18fbad4fafd7 100644 --- a/packages/opentelemetry/src/spanExporter.ts +++ b/packages/opentelemetry/src/spanExporter.ts @@ -15,10 +15,10 @@ import { captureEvent, convertSpanLinksForEnvelope, debounce, + debug, getCapturedScopesOnSpan, getDynamicSamplingContextFromSpan, getStatusMessage, - logger, SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, @@ -105,7 +105,7 @@ export class SentrySpanExporter { }); if (droppedSpanCount > 0) { DEBUG_BUILD && - logger.log( + debug.log( `SpanExporter dropped ${droppedSpanCount} spans because they were pending for more than ${this._finishedSpanBucketSize} seconds.`, ); } @@ -142,7 +142,7 @@ export class SentrySpanExporter { const sentSpanCount = sentSpans.size; const remainingOpenSpanCount = finishedSpans.length - sentSpanCount; DEBUG_BUILD && - logger.log( + debug.log( `SpanExporter exported ${sentSpanCount} spans, ${remainingOpenSpanCount} spans are waiting for their parent spans to finish`, ); diff --git a/packages/opentelemetry/test/helpers/initOtel.ts b/packages/opentelemetry/test/helpers/initOtel.ts index dd797f44ffb2..fd7a33884b5c 100644 --- a/packages/opentelemetry/test/helpers/initOtel.ts +++ b/packages/opentelemetry/test/helpers/initOtel.ts @@ -7,7 +7,7 @@ import { ATTR_SERVICE_VERSION, SEMRESATTRS_SERVICE_NAMESPACE, } from '@opentelemetry/semantic-conventions'; -import { getClient, logger, SDK_VERSION } from '@sentry/core'; +import { debug, getClient, SDK_VERSION } from '@sentry/core'; import { wrapContextManagerClass } from '../../src/contextManager'; import { DEBUG_BUILD } from '../../src/debug-build'; import { SentryPropagator } from '../../src/propagator'; @@ -25,21 +25,25 @@ export function initOtel(): void { if (!client) { DEBUG_BUILD && - logger.warn( + debug.warn( 'No client available, skipping OpenTelemetry setup. This probably means that `Sentry.init()` was not called before `initOtel()`.', ); return; } if (client.getOptions().debug) { - const otelLogger = new Proxy(logger as typeof logger & { verbose: (typeof logger)['debug'] }, { - get(target, prop, receiver) { - const actualProp = prop === 'verbose' ? 'debug' : prop; - return Reflect.get(target, actualProp, receiver); + // Disable diag, to ensure this works even if called multiple times + diag.disable(); + diag.setLogger( + { + error: debug.error, + warn: debug.warn, + info: debug.log, + debug: debug.log, + verbose: debug.log, }, - }); - - diag.setLogger(otelLogger, DiagLogLevel.DEBUG); + DiagLogLevel.DEBUG, + ); } setupEventContextTrace(client); diff --git a/packages/opentelemetry/test/integration/transactions.test.ts b/packages/opentelemetry/test/integration/transactions.test.ts index 9bc1847b422b..c6292bebc50f 100644 --- a/packages/opentelemetry/test/integration/transactions.test.ts +++ b/packages/opentelemetry/test/integration/transactions.test.ts @@ -5,8 +5,8 @@ import type { SpanProcessor } from '@opentelemetry/sdk-trace-base'; import type { Event, TransactionEvent } from '@sentry/core'; import { addBreadcrumb, + debug, getClient, - logger, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, setTag, @@ -440,7 +440,7 @@ describe('Integration | Transactions', () => { vi.setSystemTime(now); const logs: unknown[] = []; - vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg)); + vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg)); mockSdkInit({ tracesSampleRate: 1, beforeSendTransaction }); @@ -510,7 +510,7 @@ describe('Integration | Transactions', () => { vi.setSystemTime(now); const logs: unknown[] = []; - vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg)); + vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg)); const transactions: Event[] = []; @@ -568,7 +568,7 @@ describe('Integration | Transactions', () => { vi.setSystemTime(now); const logs: unknown[] = []; - vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg)); + vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg)); const transactions: Event[] = []; @@ -625,7 +625,7 @@ describe('Integration | Transactions', () => { vi.setSystemTime(now); const logs: unknown[] = []; - vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg)); + vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg)); const transactions: Event[] = []; @@ -687,7 +687,7 @@ describe('Integration | Transactions', () => { vi.setSystemTime(now); const logs: unknown[] = []; - vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg)); + vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg)); const transactions: Event[] = []; From b7ab6a817ffaebc06c6adaf1965323eff55c68ad Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 14 Jul 2025 12:18:32 -0400 Subject: [PATCH 2/2] temp patch for node --- packages/node-core/test/integration/transactions.test.ts | 6 +++++- packages/node/test/integration/transactions.test.ts | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/node-core/test/integration/transactions.test.ts b/packages/node-core/test/integration/transactions.test.ts index db499cd368df..2fd27789f1ef 100644 --- a/packages/node-core/test/integration/transactions.test.ts +++ b/packages/node-core/test/integration/transactions.test.ts @@ -1,7 +1,7 @@ import { context, trace, TraceFlags } from '@opentelemetry/api'; import type { SpanProcessor } from '@opentelemetry/sdk-trace-base'; import type { TransactionEvent } from '@sentry/core'; -import { logger, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core'; +import { debug, logger, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core'; import { SentrySpanProcessor } from '@sentry/opentelemetry'; import { afterEach, describe, expect, it, vi } from 'vitest'; import * as Sentry from '../../src'; @@ -558,7 +558,9 @@ describe('Integration | Transactions', () => { vi.setSystemTime(now); const logs: unknown[] = []; + // TODO: Remove this once node is using debug instead of logger vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg)); + vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg)); mockSdkInit({ tracesSampleRate: 1, beforeSendTransaction }); @@ -636,7 +638,9 @@ describe('Integration | Transactions', () => { vi.setSystemTime(now); const logs: unknown[] = []; + // TODO: Remove this once node is using debug instead of logger vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg)); + vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg)); mockSdkInit({ tracesSampleRate: 1, diff --git a/packages/node/test/integration/transactions.test.ts b/packages/node/test/integration/transactions.test.ts index db499cd368df..2fd27789f1ef 100644 --- a/packages/node/test/integration/transactions.test.ts +++ b/packages/node/test/integration/transactions.test.ts @@ -1,7 +1,7 @@ import { context, trace, TraceFlags } from '@opentelemetry/api'; import type { SpanProcessor } from '@opentelemetry/sdk-trace-base'; import type { TransactionEvent } from '@sentry/core'; -import { logger, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core'; +import { debug, logger, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core'; import { SentrySpanProcessor } from '@sentry/opentelemetry'; import { afterEach, describe, expect, it, vi } from 'vitest'; import * as Sentry from '../../src'; @@ -558,7 +558,9 @@ describe('Integration | Transactions', () => { vi.setSystemTime(now); const logs: unknown[] = []; + // TODO: Remove this once node is using debug instead of logger vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg)); + vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg)); mockSdkInit({ tracesSampleRate: 1, beforeSendTransaction }); @@ -636,7 +638,9 @@ describe('Integration | Transactions', () => { vi.setSystemTime(now); const logs: unknown[] = []; + // TODO: Remove this once node is using debug instead of logger vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg)); + vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg)); mockSdkInit({ tracesSampleRate: 1,