Skip to content

Commit 01f2b1c

Browse files
committed
Added trace state data header extraction from GCP function requests and injection into transaction logic + test changes
1 parent 8443739 commit 01f2b1c

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

packages/serverless/src/gcpfunction/http.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { captureException, flush, getCurrentHub, Handlers, startTransaction } from '@sentry/node';
2-
import { extractTraceparentData } from '@sentry/tracing';
2+
import { extractSentrytraceData, extractTracestateData } from '@sentry/tracing';
33
import { isString, logger, stripUrlQueryAndFragment } from '@sentry/utils';
44

55
import { domainify, getActiveDomain, proxyFunction } from './../utils';
@@ -50,15 +50,22 @@ function _wrapHttpFunction(fn: HttpFunction, wrapOptions: Partial<HttpFunctionWr
5050
const reqUrl = stripUrlQueryAndFragment(req.originalUrl || req.url || '');
5151

5252
// Applying `sentry-trace` to context
53-
let traceparentData;
53+
let traceparentData, tracestateData;
5454
const reqWithHeaders = req as { headers?: { [key: string]: string } };
5555
if (reqWithHeaders.headers && isString(reqWithHeaders.headers['sentry-trace'])) {
56-
traceparentData = extractTraceparentData(reqWithHeaders.headers['sentry-trace'] as string);
56+
traceparentData = extractSentrytraceData(reqWithHeaders.headers['sentry-trace'] as string);
5757
}
58+
if (reqWithHeaders.headers?.tracestate) {
59+
tracestateData = extractTracestateData(reqWithHeaders.headers.tracestate as string);
60+
}
61+
5862
const transaction = startTransaction({
5963
name: `${reqMethod} ${reqUrl}`,
6064
op: 'gcp.function.http',
6165
...traceparentData,
66+
// ToDo check with Katie if this should be here even if undefined
67+
// metadata: { tracestate: tracestateData },
68+
...(tracestateData && { metadata: { tracestate: tracestateData } }),
6269
});
6370

6471
// getCurrentHub() is expected to use current active domain as a carrier

packages/serverless/test/gcpfunction.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ describe('GCPFunction', () => {
131131

132132
const trace_headers: { [key: string]: string } = {
133133
'sentry-trace': '12312012123120121231201212312012-1121201211212012-0',
134+
tracestate: 'sentry=doGsaREgReaT,maisey=silly,charlie=goofy',
134135
};
135136

136137
await handleHttp(wrappedHandler, trace_headers);
@@ -140,6 +141,12 @@ describe('GCPFunction', () => {
140141
traceId: '12312012123120121231201212312012',
141142
parentSpanId: '1121201211212012',
142143
parentSampled: false,
144+
metadata: {
145+
tracestate: {
146+
sentry: 'sentry=doGsaREgReaT',
147+
thirdparty: 'maisey=silly,charlie=goofy',
148+
},
149+
},
143150
});
144151
// @ts-ignore see "Why @ts-ignore" note
145152
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);

0 commit comments

Comments
 (0)