Skip to content

Commit 1afd16a

Browse files
committed
ref(opentelemetry): Use debug instead of logger
1 parent cc8cd7e commit 1afd16a

File tree

5 files changed

+32
-31
lines changed

5 files changed

+32
-31
lines changed

packages/opentelemetry/src/propagator.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { isTracingSuppressed, W3CBaggagePropagator } from '@opentelemetry/core';
44
import { ATTR_URL_FULL, SEMATTRS_HTTP_URL } from '@opentelemetry/semantic-conventions';
55
import type { Client, continueTrace, DynamicSamplingContext, Options, Scope } from '@sentry/core';
66
import {
7+
debug,
78
generateSentryTraceHeader,
89
getClient,
910
getCurrentScope,
1011
getDynamicSamplingContextFromScope,
1112
getDynamicSamplingContextFromSpan,
1213
getIsolationScope,
13-
logger,
1414
LRUMap,
1515
parseBaggageHeader,
1616
propagationContextFromHeaders,
@@ -45,7 +45,7 @@ export class SentryPropagator extends W3CBaggagePropagator {
4545
*/
4646
public inject(context: Context, carrier: unknown, setter: TextMapSetter): void {
4747
if (isTracingSuppressed(context)) {
48-
DEBUG_BUILD && logger.log('[Tracing] Not injecting trace data for url because tracing is suppressed.');
48+
DEBUG_BUILD && debug.log('[Tracing] Not injecting trace data for url because tracing is suppressed.');
4949
return;
5050
}
5151

@@ -55,10 +55,7 @@ export class SentryPropagator extends W3CBaggagePropagator {
5555
const tracePropagationTargets = getClient()?.getOptions()?.tracePropagationTargets;
5656
if (!shouldPropagateTraceForUrl(url, tracePropagationTargets, this._urlMatchesTargetsMap)) {
5757
DEBUG_BUILD &&
58-
logger.log(
59-
'[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:',
60-
url,
61-
);
58+
debug.log('[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:', url);
6259
return;
6360
}
6461

@@ -139,14 +136,14 @@ export function shouldPropagateTraceForUrl(
139136

140137
const cachedDecision = decisionMap?.get(url);
141138
if (cachedDecision !== undefined) {
142-
DEBUG_BUILD && !cachedDecision && logger.log(NOT_PROPAGATED_MESSAGE, url);
139+
DEBUG_BUILD && !cachedDecision && debug.log(NOT_PROPAGATED_MESSAGE, url);
143140
return cachedDecision;
144141
}
145142

146143
const decision = stringMatchesSomePattern(url, tracePropagationTargets);
147144
decisionMap?.set(url, decision);
148145

149-
DEBUG_BUILD && !decision && logger.log(NOT_PROPAGATED_MESSAGE, url);
146+
DEBUG_BUILD && !decision && debug.log(NOT_PROPAGATED_MESSAGE, url);
150147
return decision;
151148
}
152149

packages/opentelemetry/src/sampler.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import {
1313
import type { Client, SpanAttributes } from '@sentry/core';
1414
import {
1515
baggageHeaderToDynamicSamplingContext,
16+
debug,
1617
hasSpansEnabled,
17-
logger,
1818
parseSampleRate,
1919
sampleSpan,
2020
SEMANTIC_ATTRIBUTE_SENTRY_OP,
@@ -137,7 +137,7 @@ export class SentrySampler implements Sampler {
137137

138138
const method = `${maybeSpanHttpMethod}`.toUpperCase();
139139
if (method === 'OPTIONS' || method === 'HEAD') {
140-
DEBUG_BUILD && logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
140+
DEBUG_BUILD && debug.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
141141

142142
return wrapSamplingDecision({
143143
decision: SamplingDecision.NOT_RECORD,
@@ -153,7 +153,7 @@ export class SentrySampler implements Sampler {
153153
// 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)
154154
parentSampled === undefined
155155
) {
156-
DEBUG_BUILD && logger.log('[Tracing] Discarding root span because its trace was not chosen to be sampled.');
156+
DEBUG_BUILD && debug.log('[Tracing] Discarding root span because its trace was not chosen to be sampled.');
157157
this._client.recordDroppedEvent('sample_rate', 'transaction');
158158
}
159159

@@ -187,12 +187,12 @@ function getParentSampled(parentSpan: Span, traceId: string, spanName: string):
187187
if (parentContext.isRemote) {
188188
const parentSampled = getSamplingDecision(parentSpan.spanContext());
189189
DEBUG_BUILD &&
190-
logger.log(`[Tracing] Inheriting remote parent's sampled decision for ${spanName}: ${parentSampled}`);
190+
debug.log(`[Tracing] Inheriting remote parent's sampled decision for ${spanName}: ${parentSampled}`);
191191
return parentSampled;
192192
}
193193

194194
const parentSampled = getSamplingDecision(parentContext);
195-
DEBUG_BUILD && logger.log(`[Tracing] Inheriting parent's sampled decision for ${spanName}: ${parentSampled}`);
195+
DEBUG_BUILD && debug.log(`[Tracing] Inheriting parent's sampled decision for ${spanName}: ${parentSampled}`);
196196
return parentSampled;
197197
}
198198

packages/opentelemetry/src/spanExporter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import {
1515
captureEvent,
1616
convertSpanLinksForEnvelope,
1717
debounce,
18+
debug,
1819
getCapturedScopesOnSpan,
1920
getDynamicSamplingContextFromSpan,
2021
getStatusMessage,
21-
logger,
2222
SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME,
2323
SEMANTIC_ATTRIBUTE_SENTRY_OP,
2424
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
@@ -105,7 +105,7 @@ export class SentrySpanExporter {
105105
});
106106
if (droppedSpanCount > 0) {
107107
DEBUG_BUILD &&
108-
logger.log(
108+
debug.log(
109109
`SpanExporter dropped ${droppedSpanCount} spans because they were pending for more than ${this._finishedSpanBucketSize} seconds.`,
110110
);
111111
}
@@ -142,7 +142,7 @@ export class SentrySpanExporter {
142142
const sentSpanCount = sentSpans.size;
143143
const remainingOpenSpanCount = finishedSpans.length - sentSpanCount;
144144
DEBUG_BUILD &&
145-
logger.log(
145+
debug.log(
146146
`SpanExporter exported ${sentSpanCount} spans, ${remainingOpenSpanCount} spans are waiting for their parent spans to finish`,
147147
);
148148

packages/opentelemetry/test/helpers/initOtel.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ATTR_SERVICE_VERSION,
88
SEMRESATTRS_SERVICE_NAMESPACE,
99
} from '@opentelemetry/semantic-conventions';
10-
import { getClient, logger, SDK_VERSION } from '@sentry/core';
10+
import { debug, getClient, SDK_VERSION } from '@sentry/core';
1111
import { wrapContextManagerClass } from '../../src/contextManager';
1212
import { DEBUG_BUILD } from '../../src/debug-build';
1313
import { SentryPropagator } from '../../src/propagator';
@@ -25,21 +25,25 @@ export function initOtel(): void {
2525

2626
if (!client) {
2727
DEBUG_BUILD &&
28-
logger.warn(
28+
debug.warn(
2929
'No client available, skipping OpenTelemetry setup. This probably means that `Sentry.init()` was not called before `initOtel()`.',
3030
);
3131
return;
3232
}
3333

3434
if (client.getOptions().debug) {
35-
const otelLogger = new Proxy(logger as typeof logger & { verbose: (typeof logger)['debug'] }, {
36-
get(target, prop, receiver) {
37-
const actualProp = prop === 'verbose' ? 'debug' : prop;
38-
return Reflect.get(target, actualProp, receiver);
35+
// Disable diag, to ensure this works even if called multiple times
36+
diag.disable();
37+
diag.setLogger(
38+
{
39+
error: debug.error,
40+
warn: debug.warn,
41+
info: debug.log,
42+
debug: debug.log,
43+
verbose: debug.log,
3944
},
40-
});
41-
42-
diag.setLogger(otelLogger, DiagLogLevel.DEBUG);
45+
DiagLogLevel.DEBUG,
46+
);
4347
}
4448

4549
setupEventContextTrace(client);

packages/opentelemetry/test/integration/transactions.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import type { SpanProcessor } from '@opentelemetry/sdk-trace-base';
55
import type { Event, TransactionEvent } from '@sentry/core';
66
import {
77
addBreadcrumb,
8+
debug,
89
getClient,
9-
logger,
1010
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
1111
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
1212
setTag,
@@ -440,7 +440,7 @@ describe('Integration | Transactions', () => {
440440
vi.setSystemTime(now);
441441

442442
const logs: unknown[] = [];
443-
vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg));
443+
vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg));
444444

445445
mockSdkInit({ tracesSampleRate: 1, beforeSendTransaction });
446446

@@ -510,7 +510,7 @@ describe('Integration | Transactions', () => {
510510
vi.setSystemTime(now);
511511

512512
const logs: unknown[] = [];
513-
vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg));
513+
vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg));
514514

515515
const transactions: Event[] = [];
516516

@@ -568,7 +568,7 @@ describe('Integration | Transactions', () => {
568568
vi.setSystemTime(now);
569569

570570
const logs: unknown[] = [];
571-
vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg));
571+
vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg));
572572

573573
const transactions: Event[] = [];
574574

@@ -625,7 +625,7 @@ describe('Integration | Transactions', () => {
625625
vi.setSystemTime(now);
626626

627627
const logs: unknown[] = [];
628-
vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg));
628+
vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg));
629629

630630
const transactions: Event[] = [];
631631

@@ -687,7 +687,7 @@ describe('Integration | Transactions', () => {
687687
vi.setSystemTime(now);
688688

689689
const logs: unknown[] = [];
690-
vi.spyOn(logger, 'log').mockImplementation(msg => logs.push(msg));
690+
vi.spyOn(debug, 'log').mockImplementation(msg => logs.push(msg));
691691

692692
const transactions: Event[] = [];
693693

0 commit comments

Comments
 (0)