Skip to content

Commit 63926bf

Browse files
committed
rework: export getTraceData from core
1 parent 63283d0 commit 63926bf

File tree

14 files changed

+53
-52
lines changed

14 files changed

+53
-52
lines changed

packages/astro/src/index.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export {
5555
getSentryRelease,
5656
getSpanDescendants,
5757
getSpanStatusFromHttpCode,
58-
getTracingMetaTagValues,
58+
getTraceData,
5959
graphqlIntegration,
6060
hapiIntegration,
6161
httpIntegration,

packages/astro/src/server/middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type { Client, Scope, Span, SpanAttributes } from '@sentry/types';
1414
import { addNonEnumerableProperty, objectify, stripUrlQueryAndFragment } from '@sentry/utils';
1515
import type { APIContext, MiddlewareResponseHandler } from 'astro';
1616

17-
import { getTracingMetaTagValues } from '@sentry/node';
17+
import { getTraceData } from '@sentry/node';
1818

1919
type MiddlewareOptions = {
2020
/**
@@ -189,7 +189,7 @@ function addMetaTagToHead(htmlChunk: string, scope: Scope, client: Client, span?
189189
if (typeof htmlChunk !== 'string') {
190190
return htmlChunk;
191191
}
192-
const { 'sentry-trace': sentryTrace, baggage } = getTracingMetaTagValues(span, scope, client);
192+
const { 'sentry-trace': sentryTrace, baggage } = getTraceData(span, scope, client);
193193

194194
const sentryTraceMeta = `<meta name="sentry-trace" content="${sentryTrace}"/>`;
195195
const baggageMeta = baggage && `<meta name="baggage" content="${baggage}"/>`;

packages/astro/test/server/middleware.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ describe('sentryMiddleware', () => {
3434
});
3535
vi.spyOn(SentryNode, 'getActiveSpan').mockImplementation(getSpanMock);
3636
vi.spyOn(SentryNode, 'getClient').mockImplementation(() => ({}) as Client);
37+
vi.spyOn(SentryNode, 'getTraceData').mockImplementation(() => ({
38+
'sentry-trace': '123',
39+
baggage: 'abc',
40+
}));
3741
vi.spyOn(SentryCore, 'getDynamicSamplingContextFromSpan').mockImplementation(() => ({
3842
transaction: 'test',
3943
}));

packages/aws-serverless/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export {
2020
getCurrentScope,
2121
getGlobalScope,
2222
getIsolationScope,
23-
getTracingMetaTagValues,
23+
getTraceData,
2424
setCurrentClient,
2525
Scope,
2626
SDK_VERSION,

packages/bun/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export {
4040
getCurrentScope,
4141
getGlobalScope,
4242
getIsolationScope,
43-
getTracingMetaTagValues,
43+
getTraceData,
4444
setCurrentClient,
4545
Scope,
4646
SDK_VERSION,

packages/cloudflare/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export {
5555
setMeasurement,
5656
getActiveSpan,
5757
getRootSpan,
58+
getTraceData,
5859
startSpan,
5960
startInactiveSpan,
6061
startSpanManual,

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export {
8282
} from './utils/spanUtils';
8383
export { parseSampleRate } from './utils/parseSampleRate';
8484
export { applySdkMetadata } from './utils/sdkMetadata';
85+
export { getTraceData } from './utils/traceData';
8586
export { DEFAULT_ENVIRONMENT } from './constants';
8687
export { addBreadcrumb } from './breadcrumbs';
8788
export { functionToStringIntegration } from './integrations/functiontostring';

packages/node/src/utils/meta.ts renamed to packages/core/src/utils/traceData.ts

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,56 @@
1-
import {
2-
getDynamicSamplingContextFromClient,
3-
getDynamicSamplingContextFromSpan,
4-
getRootSpan,
5-
spanToTraceHeader,
6-
} from '@sentry/core';
71
import type { Client, Scope, Span } from '@sentry/types';
82
import {
93
TRACEPARENT_REGEXP,
104
dynamicSamplingContextToSentryBaggageHeader,
115
generateSentryTraceHeader,
126
logger,
137
} from '@sentry/utils';
8+
import { getClient, getCurrentScope } from '../currentScopes';
9+
import { getDynamicSamplingContextFromClient, getDynamicSamplingContextFromSpan } from '../tracing';
10+
import { getActiveSpan, getRootSpan, spanToTraceHeader } from './spanUtils';
1411

1512
/**
1613
* Extracts trace propagation data from the current span or from the client's scope (via transaction or propagation
17-
* context) and serializes it to meta tag content values.
18-
*
19-
* Use this function to obtain data for the tracing meta tags you can inject when rendering an HTML response to
20-
* continue the server-initiated trace on the client.
14+
* context) and serializes it to `sentry-trace` and `baggage` values to strings. These values can be used to propagate
15+
* a trace via our tracing Http headers or Html `<meta>` tags.
2116
*
22-
* Example usage:
17+
* This function also applies some validation to the generated sentry-trace and baggage values to ensure that
18+
* only valid strings are returned.
2319
*
24-
* ```js
25-
* // render meta tags as html
26-
* const tagValues = getTracingMetaTagValues(span, scope, client);
27-
* return `
28-
* <meta name="sentry-trace" content="${tagValues['sentry-trace']}"/>
29-
* ${tagValues.baggage ? `<meta name="baggage" content="${tagValues.baggage}"/>` : ''}`
30-
* ```
20+
* @param span a span to take the trace data from. By default, the currently active span is used.
21+
* @param scope the scope to take trace data from By default, the active current scope is used.
22+
* @param client the SDK's client to take trace data from. By default, the current client is used.
3123
*
32-
* @param span the currently active span
33-
* @param client the SDK's client
34-
*
35-
* @returns an object with the values of the tracing meta tags. The object keys are the name of the meta tag,
36-
* the respective value is the content.
24+
* @returns an object with the tracing data values. The object keys are the name of the tracing key to be used as header
25+
* or meta tag name.
3726
*/
38-
export function getTracingMetaTagValues(
39-
span: Span | undefined,
40-
scope: Scope,
41-
client: Client | undefined,
27+
export function getTraceData(
28+
span?: Span | undefined,
29+
scope?: Scope,
30+
client?: Client,
4231
): { 'sentry-trace': string; baggage?: string } {
43-
const { dsc, sampled, traceId } = scope.getPropagationContext();
44-
const rootSpan = span && getRootSpan(span);
32+
const clientToUse = client || getClient();
33+
const scopeToUser = scope || getCurrentScope();
34+
const spanToUse = span || getActiveSpan();
35+
36+
const { dsc, sampled, traceId } = scopeToUser.getPropagationContext();
37+
const rootSpan = spanToUse && getRootSpan(spanToUse);
4538

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

4841
const dynamicSamplingContext = rootSpan
4942
? getDynamicSamplingContextFromSpan(rootSpan)
5043
: dsc
5144
? dsc
52-
: client
53-
? getDynamicSamplingContextFromClient(traceId, client)
45+
: clientToUse
46+
? getDynamicSamplingContextFromClient(traceId, clientToUse)
5447
: undefined;
5548

5649
const baggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
5750

5851
const isValidSentryTraceHeader = TRACEPARENT_REGEXP.test(sentryTrace);
5952
if (!isValidSentryTraceHeader) {
60-
logger.warn('Invalid sentry-trace data. Returning empty "sentry-trace" meta tag');
53+
logger.warn('Invalid sentry-trace data. Returning empty "sentry-trace" value');
6154
}
6255

6356
const validBaggage = isValidBaggageString(baggage);

packages/node/test/utils/meta.test.ts renamed to packages/core/test/lib/utils/traceData.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as SentryCore from '@sentry/core';
2-
import { SentrySpan } from '@sentry/core';
1+
import { SentrySpan, getTraceData } from '../../../src/';
2+
import * as SentryCoreTracing from '../../../src/tracing';
33

4-
import { getTracingMetaTagValues, isValidBaggageString } from '../../src/utils/meta';
4+
import { isValidBaggageString } from '../../../src/utils/traceData';
55

66
const TRACE_FLAG_SAMPLED = 1;
77

@@ -19,14 +19,14 @@ const mockedScope = {
1919
}),
2020
} as any;
2121

22-
describe('getTracingMetaTagValues', () => {
22+
describe('getTraceData', () => {
2323
it('returns the tracing meta tags from the span, if it is provided', () => {
2424
{
25-
jest.spyOn(SentryCore, 'getDynamicSamplingContextFromSpan').mockReturnValueOnce({
25+
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromSpan').mockReturnValueOnce({
2626
environment: 'production',
2727
});
2828

29-
const tags = getTracingMetaTagValues(mockedSpan, mockedScope, mockedClient);
29+
const tags = getTraceData(mockedSpan, mockedScope, mockedClient);
3030

3131
expect(tags).toEqual({
3232
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
@@ -36,7 +36,7 @@ describe('getTracingMetaTagValues', () => {
3636
});
3737

3838
it('returns propagationContext DSC data if no span is available', () => {
39-
const tags = getTracingMetaTagValues(
39+
const tags = getTraceData(
4040
undefined,
4141
{
4242
getPropagationContext: () => ({
@@ -60,12 +60,12 @@ describe('getTracingMetaTagValues', () => {
6060
});
6161

6262
it('returns only the `sentry-trace` tag if no DSC is available', () => {
63-
jest.spyOn(SentryCore, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({
63+
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({
6464
trace_id: '',
6565
public_key: undefined,
6666
});
6767

68-
const tags = getTracingMetaTagValues(
68+
const tags = getTraceData(
6969
// @ts-expect-error - we don't need to provide all the properties
7070
{
7171
isRecording: () => true,
@@ -87,12 +87,12 @@ describe('getTracingMetaTagValues', () => {
8787
});
8888

8989
it('returns only the `sentry-trace` tag if no DSC is available without a client', () => {
90-
jest.spyOn(SentryCore, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({
90+
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({
9191
trace_id: '',
9292
public_key: undefined,
9393
});
9494

95-
const tags = getTracingMetaTagValues(
95+
const tags = getTraceData(
9696
// @ts-expect-error - we don't need to provide all the properties
9797
{
9898
isRecording: () => true,

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export {
5555
setMeasurement,
5656
getActiveSpan,
5757
getRootSpan,
58+
getTraceData,
5859
startSpan,
5960
startInactiveSpan,
6061
startSpanManual,

packages/google-cloud-serverless/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export {
2020
getCurrentScope,
2121
getGlobalScope,
2222
getIsolationScope,
23-
getTracingMetaTagValues,
23+
getTraceData,
2424
setCurrentClient,
2525
Scope,
2626
SDK_VERSION,

packages/node/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export { initOpenTelemetry, preloadOpenTelemetry } from './sdk/initOtel';
4040
export { getAutoPerformanceIntegrations } from './integrations/tracing';
4141
export { getSentryRelease, defaultStackParser } from './sdk/api';
4242
export { createGetModuleFromFilename } from './utils/module';
43-
export { getTracingMetaTagValues } from './utils/meta';
4443
export { makeNodeTransport } from './transports';
4544
export { NodeClient } from './sdk/client';
4645
export { cron } from './cron';
@@ -96,6 +95,7 @@ export {
9695
getCurrentHub,
9796
getCurrentScope,
9897
getIsolationScope,
98+
getTraceData,
9999
withScope,
100100
withIsolationScope,
101101
captureException,

packages/sveltekit/src/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export {
5151
getSentryRelease,
5252
getSpanDescendants,
5353
getSpanStatusFromHttpCode,
54-
getTracingMetaTagValues,
54+
getTraceData,
5555
graphqlIntegration,
5656
hapiIntegration,
5757
httpIntegration,

packages/vercel-edge/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export {
5555
setMeasurement,
5656
getActiveSpan,
5757
getRootSpan,
58+
getTraceData,
5859
startSpan,
5960
startInactiveSpan,
6061
startSpanManual,

0 commit comments

Comments
 (0)