Skip to content

Commit 3ce020d

Browse files
committed
remove params
1 parent b484ad5 commit 3ce020d

File tree

10 files changed

+99
-97
lines changed

10 files changed

+99
-97
lines changed

packages/astro/src/server/middleware.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
startSpan,
1212
withIsolationScope,
1313
} from '@sentry/node';
14-
import type { Client, Scope, Span, SpanAttributes } from '@sentry/types';
14+
import type { Scope, SpanAttributes } from '@sentry/types';
1515
import { addNonEnumerableProperty, objectify, stripUrlQueryAndFragment } from '@sentry/utils';
1616
import type { APIContext, MiddlewareResponseHandler } from 'astro';
1717

@@ -136,7 +136,6 @@ async function instrumentRequest(
136136
setHttpStatus(span, originalResponse.status);
137137
}
138138

139-
const scope = getCurrentScope();
140139
const client = getClient();
141140
const contentType = originalResponse.headers.get('content-type');
142141

@@ -160,7 +159,7 @@ async function instrumentRequest(
160159
start: async controller => {
161160
for await (const chunk of originalBody) {
162161
const html = typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
163-
const modifiedHtml = addMetaTagToHead(html, scope, client, span);
162+
const modifiedHtml = addMetaTagToHead(html);
164163
controller.enqueue(new TextEncoder().encode(modifiedHtml));
165164
}
166165
controller.close();
@@ -184,11 +183,11 @@ async function instrumentRequest(
184183
* This function optimistically assumes that the HTML coming in chunks will not be split
185184
* within the <head> tag. If this still happens, we simply won't replace anything.
186185
*/
187-
function addMetaTagToHead(htmlChunk: string, scope: Scope, client: Client, span?: Span): string {
186+
function addMetaTagToHead(htmlChunk: string): string {
188187
if (typeof htmlChunk !== 'string') {
189188
return htmlChunk;
190189
}
191-
const metaTags = getTraceMetaTags(span, scope, client);
190+
const metaTags = getTraceMetaTags();
192191

193192
if (!metaTags) {
194193
return htmlChunk;

packages/core/src/asyncContext/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ export interface AsyncContextStrategy {
6666
/** Suppress tracing in the given callback, ensuring no spans are generated inside of it. */
6767
suppressTracing?: typeof suppressTracing;
6868

69-
/** get trace data as serialized string values for propagation via `sentry-trace` and `baggage` */
69+
/** Get trace data as serialized string values for propagation via `sentry-trace` and `baggage`. */
7070
getTraceData?: typeof getTraceData;
7171
}

packages/core/src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
export type { ClientClass } from './sdk';
1+
export type { ClientClass as SentryCoreCurrentScopes } from './sdk';
22
export type { AsyncContextStrategy } from './asyncContext/types';
33
export type { Carrier } from './carrier';
44
export type { OfflineStore, OfflineTransportOptions } from './transports/offline';
55
export type { ServerRuntimeClientOptions } from './server-runtime-client';
66
export type { RequestDataIntegrationOptions } from './integrations/requestdata';
77
export type { IntegrationIndex } from './integration';
8-
export type { TraceData } from './utils/traceData';
98

109
export * from './tracing';
1110
export * from './semanticAttributes';

packages/core/src/utils/meta.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Client, Scope, Span } from '@sentry/types';
21
import { getTraceData } from './traceData';
32

43
/**
@@ -22,8 +21,8 @@ import { getTraceData } from './traceData';
2221
* ```
2322
*
2423
*/
25-
export function getTraceMetaTags(span?: Span, scope?: Scope, client?: Client): string {
26-
return Object.entries(getTraceData(span, scope, client))
24+
export function getTraceMetaTags(): string {
25+
return Object.entries(getTraceData())
2726
.map(([key, value]) => `<meta name="${key}" content="${value}"/>`)
2827
.join('\n');
2928
}

packages/core/src/utils/traceData.ts

+11-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Client, Scope, Span } from '@sentry/types';
1+
import type { SerializedTraceData } from '@sentry/types';
22
import {
33
TRACEPARENT_REGEXP,
44
dynamicSamplingContextToSentryBaggageHeader,
@@ -11,11 +11,6 @@ import { getClient, getCurrentScope } from '../currentScopes';
1111
import { getDynamicSamplingContextFromClient, getDynamicSamplingContextFromSpan } from '../tracing';
1212
import { getActiveSpan, getRootSpan, spanToTraceHeader } from './spanUtils';
1313

14-
export type TraceData = {
15-
'sentry-trace'?: string;
16-
baggage?: string;
17-
};
18-
1914
/**
2015
* Extracts trace propagation data from the current span or from the client's scope (via transaction or propagation
2116
* context) and serializes it to `sentry-trace` and `baggage` values to strings. These values can be used to propagate
@@ -24,35 +19,31 @@ export type TraceData = {
2419
* This function also applies some validation to the generated sentry-trace and baggage values to ensure that
2520
* only valid strings are returned.
2621
*
27-
* @param span a span to take the trace data from. By default, the currently active span is used.
28-
* @param scope the scope to take trace data from By default, the active current scope is used.
29-
* @param client the SDK's client to take trace data from. By default, the current client is used.
30-
*
3122
* @returns an object with the tracing data values. The object keys are the name of the tracing key to be used as header
3223
* or meta tag name.
3324
*/
34-
export function getTraceData(span?: Span, scope?: Scope, client?: Client): TraceData {
25+
export function getTraceData(): SerializedTraceData {
3526
const carrier = getMainCarrier();
3627
const acs = getAsyncContextStrategy(carrier);
3728
if (acs.getTraceData) {
38-
return acs.getTraceData(span, scope, client);
29+
return acs.getTraceData();
3930
}
4031

41-
const clientToUse = client || getClient();
42-
const scopeToUse = scope || getCurrentScope();
43-
const spanToUse = span || getActiveSpan();
32+
const client = getClient();
33+
const scope = getCurrentScope();
34+
const span = getActiveSpan();
4435

45-
const { dsc, sampled, traceId } = scopeToUse.getPropagationContext();
46-
const rootSpan = spanToUse && getRootSpan(spanToUse);
36+
const { dsc, sampled, traceId } = scope.getPropagationContext();
37+
const rootSpan = span && getRootSpan(span);
4738

48-
const sentryTrace = spanToUse ? spanToTraceHeader(spanToUse) : generateSentryTraceHeader(traceId, undefined, sampled);
39+
const sentryTrace = span ? spanToTraceHeader(span) : generateSentryTraceHeader(traceId, undefined, sampled);
4940

5041
const dynamicSamplingContext = rootSpan
5142
? getDynamicSamplingContextFromSpan(rootSpan)
5243
: dsc
5344
? dsc
54-
: clientToUse
55-
? getDynamicSamplingContextFromClient(traceId, clientToUse)
45+
: client
46+
? getDynamicSamplingContextFromClient(traceId, client)
5647
: undefined;
5748

5849
const baggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);

packages/core/test/lib/utils/traceData.test.ts

+65-59
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { SentrySpan, getTraceData } from '../../../src/';
2+
import * as SentryCoreCurrentScopes from '../../../src/currentScopes';
23
import * as SentryCoreTracing from '../../../src/tracing';
4+
import * as SentryCoreSpanUtils from '../../../src/utils/spanUtils';
35

46
import { isValidBaggageString } from '../../../src/utils/traceData';
57

@@ -25,33 +27,38 @@ describe('getTraceData', () => {
2527
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromSpan').mockReturnValueOnce({
2628
environment: 'production',
2729
});
30+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => mockedSpan);
31+
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
2832

29-
const tags = getTraceData(mockedSpan, mockedScope, mockedClient);
33+
const data = getTraceData();
3034

31-
expect(tags).toEqual({
35+
expect(data).toEqual({
3236
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
3337
baggage: 'sentry-environment=production',
3438
});
3539
}
3640
});
3741

3842
it('returns propagationContext DSC data if no span is available', () => {
39-
const traceData = getTraceData(
40-
undefined,
41-
{
42-
getPropagationContext: () => ({
43-
traceId: '12345678901234567890123456789012',
44-
sampled: true,
45-
spanId: '1234567890123456',
46-
dsc: {
47-
environment: 'staging',
48-
public_key: 'key',
49-
trace_id: '12345678901234567890123456789012',
50-
},
51-
}),
52-
} as any,
53-
mockedClient,
43+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => undefined);
44+
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(
45+
() =>
46+
({
47+
getPropagationContext: () => ({
48+
traceId: '12345678901234567890123456789012',
49+
sampled: true,
50+
spanId: '1234567890123456',
51+
dsc: {
52+
environment: 'staging',
53+
public_key: 'key',
54+
trace_id: '12345678901234567890123456789012',
55+
},
56+
}),
57+
}) as any,
5458
);
59+
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => mockedClient);
60+
61+
const traceData = getTraceData();
5562

5663
expect(traceData).toEqual({
5764
'sentry-trace': expect.stringMatching(/12345678901234567890123456789012-(.{16})-1/),
@@ -65,21 +72,22 @@ describe('getTraceData', () => {
6572
public_key: undefined,
6673
});
6774

68-
const traceData = getTraceData(
69-
// @ts-expect-error - we don't need to provide all the properties
70-
{
71-
isRecording: () => true,
72-
spanContext: () => {
73-
return {
74-
traceId: '12345678901234567890123456789012',
75-
spanId: '1234567890123456',
76-
traceFlags: TRACE_FLAG_SAMPLED,
77-
};
78-
},
75+
// @ts-expect-error - we don't need to provide all the properties
76+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
77+
isRecording: () => true,
78+
spanContext: () => {
79+
return {
80+
traceId: '12345678901234567890123456789012',
81+
spanId: '1234567890123456',
82+
traceFlags: TRACE_FLAG_SAMPLED,
83+
};
7984
},
80-
mockedScope,
81-
mockedClient,
82-
);
85+
}));
86+
87+
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
88+
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => mockedClient);
89+
90+
const traceData = getTraceData();
8391

8492
expect(traceData).toEqual({
8593
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
@@ -92,21 +100,21 @@ describe('getTraceData', () => {
92100
public_key: undefined,
93101
});
94102

95-
const traceData = getTraceData(
96-
// @ts-expect-error - we don't need to provide all the properties
97-
{
98-
isRecording: () => true,
99-
spanContext: () => {
100-
return {
101-
traceId: '12345678901234567890123456789012',
102-
spanId: '1234567890123456',
103-
traceFlags: TRACE_FLAG_SAMPLED,
104-
};
105-
},
103+
// @ts-expect-error - we don't need to provide all the properties
104+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
105+
isRecording: () => true,
106+
spanContext: () => {
107+
return {
108+
traceId: '12345678901234567890123456789012',
109+
spanId: '1234567890123456',
110+
traceFlags: TRACE_FLAG_SAMPLED,
111+
};
106112
},
107-
mockedScope,
108-
undefined,
109-
);
113+
}));
114+
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
115+
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => undefined);
116+
117+
const traceData = getTraceData();
110118

111119
expect(traceData).toEqual({
112120
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
@@ -115,21 +123,19 @@ describe('getTraceData', () => {
115123
});
116124

117125
it('returns an empty object if the `sentry-trace` value is invalid', () => {
118-
const traceData = getTraceData(
119-
// @ts-expect-error - we don't need to provide all the properties
120-
{
121-
isRecording: () => true,
122-
spanContext: () => {
123-
return {
124-
traceId: '1234567890123456789012345678901+',
125-
spanId: '1234567890123456',
126-
traceFlags: TRACE_FLAG_SAMPLED,
127-
};
128-
},
126+
// @ts-expect-error - we don't need to provide all the properties
127+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
128+
isRecording: () => true,
129+
spanContext: () => {
130+
return {
131+
traceId: '1234567890123456789012345678901+',
132+
spanId: '1234567890123456',
133+
traceFlags: TRACE_FLAG_SAMPLED,
134+
};
129135
},
130-
mockedScope,
131-
mockedClient,
132-
);
136+
}));
137+
138+
const traceData = getTraceData();
133139

134140
expect(traceData).toEqual({});
135141
});

packages/opentelemetry/src/asyncContextStrategy.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as api from '@opentelemetry/api';
22
import { getDefaultCurrentScope, getDefaultIsolationScope, setAsyncContextStrategy } from '@sentry/core';
3-
import type { getTraceData as defaultGetTraceData, withActiveSpan as defaultWithActiveSpan } from '@sentry/core';
3+
import type { withActiveSpan as defaultWithActiveSpan } from '@sentry/core';
44
import type { Scope } from '@sentry/types';
55

66
import {
@@ -104,9 +104,9 @@ export function setOpenTelemetryContextAsyncContextStrategy(): void {
104104
startInactiveSpan,
105105
getActiveSpan,
106106
suppressTracing,
107+
getTraceData,
107108
// The types here don't fully align, because our own `Span` type is narrower
108109
// than the OTEL one - but this is OK for here, as we now we'll only have OTEL spans passed around
109110
withActiveSpan: withActiveSpan as typeof defaultWithActiveSpan,
110-
getTraceData: getTraceData as typeof defaultGetTraceData,
111111
});
112112
}

packages/opentelemetry/src/utils/getTraceData.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import * as api from '@opentelemetry/api';
2-
import type { Span } from '@opentelemetry/api';
3-
import type { TraceData } from '@sentry/core';
42
import { getCurrentScope } from '@sentry/core';
3+
import type { SerializedTraceData } from '@sentry/types';
54
import { getPropagationContextFromSpan } from '../propagator';
65
import { generateSpanContextForPropagationContext } from './generateSpanContextForPropagationContext';
76

87
/**
98
* Otel-specific implementation of `getTraceData`.
109
* @see `@sentry/core` version of `getTraceData` for more information
1110
*/
12-
export function getTraceData(span?: Span): TraceData {
11+
export function getTraceData(): SerializedTraceData {
1312
const ctx = api.context.active();
14-
const spanToUse = span || api.trace.getSpan(ctx);
13+
const spanToUse = api.trace.getSpan(ctx);
1514

1615
// This should never happen, given we always create an ambient non-recording span if there's no active span.
1716
if (!spanToUse) {

packages/types/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export type { SpanStatus } from './spanStatus';
121121
export type { TimedEvent } from './timedEvent';
122122
export type { StackFrame } from './stackframe';
123123
export type { Stacktrace, StackParser, StackLineParser, StackLineParserFn } from './stacktrace';
124-
export type { PropagationContext, TracePropagationTargets } from './tracing';
124+
export type { PropagationContext, TracePropagationTargets, SerializedTraceData } from './tracing';
125125
export type { StartSpanOptions } from './startSpanOptions';
126126
export type {
127127
TraceparentData,

packages/types/src/tracing.ts

+9
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,12 @@ export interface PropagationContext {
4242
*/
4343
dsc?: Partial<DynamicSamplingContext>;
4444
}
45+
46+
/**
47+
* An object holding trace data, like span and trace ids, sampling decision, and dynamic sampling context
48+
* in a serialized form. Both keys are expected to be used as Http headers or Html meta tags.
49+
*/
50+
export interface SerializedTraceData {
51+
'sentry-trace'?: string;
52+
baggage?: string;
53+
}

0 commit comments

Comments
 (0)