Skip to content

Commit cab0799

Browse files
committed
the simpler, the better
1 parent aa83a81 commit cab0799

File tree

4 files changed

+9
-24
lines changed

4 files changed

+9
-24
lines changed

packages/opentelemetry/src/utils/getTraceData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function getTraceData({
2424
if (span) {
2525
const { scope } = getCapturedScopesOnSpan(span);
2626
// fall back to current context if for whatever reason we can't find the one of the span
27-
ctx = api.trace.setSpan(api.context.active(), span) || (scope && getContextFromScope(scope));
27+
ctx = (scope && getContextFromScope(scope)) || api.trace.setSpan(api.context.active(), span);
2828
}
2929

3030
const { traceId, spanId, sampled, dynamicSamplingContext } = getInjectionData(ctx, { scope, client });

packages/remix/src/server/instrumentServer.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,13 @@ function getTraceAndBaggage(): {
115115
function makeWrappedDocumentRequestFunction(instrumentTracing?: boolean) {
116116
return function (origDocumentRequestFunction: HandleDocumentRequestFunction): HandleDocumentRequestFunction {
117117
return async function (this: unknown, request: Request, ...args: unknown[]): Promise<Response> {
118-
const activeSpan = getActiveSpan();
119-
const rootSpan = activeSpan && getRootSpan(activeSpan);
120-
121-
const serverTimingHeader = rootSpan ? generateSentryServerTimingHeader(rootSpan) : null;
118+
const serverTimingHeader = generateSentryServerTimingHeader();
122119

123120
let response: Response;
124121

125122
if (instrumentTracing) {
123+
const activeSpan = getActiveSpan();
124+
const rootSpan = activeSpan && getRootSpan(activeSpan);
126125
const name = rootSpan ? spanToJSON(rootSpan).description : undefined;
127126

128127
response = await startSpan(
@@ -220,13 +219,9 @@ function makeWrappedDataFunction(
220219

221220
// Redirects bypass makeWrappedDocumentRequestFunction, so we inject Server-Timing here.
222221
if (isResponse(res) && isRedirectResponse(res)) {
223-
const activeSpan = getActiveSpan();
224-
const rootSpan = activeSpan && getRootSpan(activeSpan);
225-
if (rootSpan) {
226-
const serverTimingHeader = generateSentryServerTimingHeader(rootSpan);
227-
if (serverTimingHeader) {
228-
return injectServerTimingHeaderValue(res, serverTimingHeader);
229-
}
222+
const serverTimingHeader = generateSentryServerTimingHeader();
223+
if (serverTimingHeader) {
224+
return injectServerTimingHeaderValue(res, serverTimingHeader);
230225
}
231226
}
232227

packages/remix/src/server/serverTimingTracePropagation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { DEBUG_BUILD } from '../utils/debug-build';
44
import { isCloudflareEnv } from '../utils/utils';
55

66
/** Generate a Server-Timing header value containing Sentry trace context. */
7-
export function generateSentryServerTimingHeader(span?: Span): string | null {
7+
export function generateSentryServerTimingHeader(): string | null {
88
if (!isNodeEnv() && !isCloudflareEnv()) {
99
return null;
1010
}
1111

1212
try {
13-
const traceData = getTraceData({ span });
13+
const traceData = getTraceData();
1414
const sentryTrace = traceData['sentry-trace'];
1515
const baggage = traceData.baggage;
1616

packages/remix/test/server/serverTimingTracePropagation.test.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,6 @@ describe('serverTimingTracePropagation', () => {
7272
expect(result).toContain('sentry-fallback=true');
7373
});
7474

75-
it('uses the provided span directly instead of resolving from active span', () => {
76-
const directSpan = { spanId: 'direct-span-id', spanContext: () => ({ traceId: 'abc' }) };
77-
78-
const result = generateSentryServerTimingHeader(directSpan as any);
79-
80-
expect(spanToTraceHeader).toHaveBeenCalledWith(directSpan);
81-
expect(spanToBaggageHeader).toHaveBeenCalledWith(directSpan);
82-
expect(result).toBeDefined();
83-
});
84-
8575
it('generates header in Cloudflare environment when isNodeEnv is false', () => {
8676
vi.mocked(isNodeEnv).mockReturnValue(false);
8777

0 commit comments

Comments
 (0)