Skip to content

Commit 0fb00a5

Browse files
authoredDec 26, 2024··
fix: fix trace context pattern, remove trace id and respect logging span id field. (#667)
1 parent a40b32a commit 0fb00a5

9 files changed

+36
-60
lines changed
 

‎docs/generated/api.json

-27
Original file line numberDiff line numberDiff line change
@@ -1952,33 +1952,6 @@
19521952
"startIndex": 1,
19531953
"endIndex": 2
19541954
}
1955-
},
1956-
{
1957-
"kind": "PropertySignature",
1958-
"canonicalReference": "@google-cloud/functions-framework!Request_2#traceId:member",
1959-
"docComment": "/**\n * Cloud Trace trace ID.\n */\n",
1960-
"excerptTokens": [
1961-
{
1962-
"kind": "Content",
1963-
"text": "traceId?: "
1964-
},
1965-
{
1966-
"kind": "Content",
1967-
"text": "string"
1968-
},
1969-
{
1970-
"kind": "Content",
1971-
"text": ";"
1972-
}
1973-
],
1974-
"isReadonly": false,
1975-
"isOptional": true,
1976-
"releaseTag": "Public",
1977-
"name": "traceId",
1978-
"propertyTypeTokenRange": {
1979-
"startIndex": 1,
1980-
"endIndex": 2
1981-
}
19821955
}
19831956
],
19841957
"extendsTokenRanges": [

‎docs/generated/api.md.api.md

-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ interface Request_2 extends Request_3 {
116116
executionId?: string;
117117
rawBody?: Buffer;
118118
spanId?: string;
119-
traceId?: string;
120119
}
121120
export { Request_2 as Request }
122121

‎src/async_local_storage.ts

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type {AsyncLocalStorage} from 'node:async_hooks';
66

77
export interface ExecutionContext {
88
executionId?: string;
9-
traceId?: string;
109
spanId?: string;
1110
}
1211

@@ -32,7 +31,6 @@ export async function asyncLocalStorageMiddleware(
3231
asyncLocalStorage.run(
3332
{
3433
executionId: req.executionId,
35-
traceId: req.traceId,
3634
spanId: req.spanId,
3735
},
3836
() => {

‎src/execution_context.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const FUNCTION_EXECUTION_ID_HEADER_KEY = 'function-execution-id';
55
const TRACE_CONTEXT_HEADER_KEY = 'X-Cloud-Trace-Context';
66

77
const TRACE_CONTEXT_PATTERN =
8-
/^(?<traceId>\w+)\/(?<spanId>\d+);o=(?<options>.+)$/;
8+
/^(?<traceId>\w+)\/(?<spanId>\d+)(?:;o=(?<options>.+))?$/;
99

1010
function generateExecutionId() {
1111
const timestampPart = Date.now().toString(36).slice(-6);
@@ -28,9 +28,7 @@ export const executionContextMiddleware = (
2828
if (cloudTraceContext) {
2929
const match = cloudTraceContext.match(TRACE_CONTEXT_PATTERN);
3030
if (match?.groups) {
31-
const {traceId, spanId} = match.groups;
32-
req.traceId = traceId;
33-
req.spanId = spanId;
31+
req.spanId = match.groups.spanId;
3432
}
3533
}
3634
next();

‎src/functions.ts

-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ export interface Request extends ExpressRequest {
3636
* Request-specified execution ID.
3737
*/
3838
executionId?: string;
39-
/**
40-
* Cloud Trace trace ID.
41-
*/
42-
traceId?: string;
4339
/**
4440
* Cloud Trace span ID.
4541
*/

‎src/logger.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {getCurrentContext, ExecutionContext} from './async_local_storage';
1818
import {Buffer} from 'buffer';
1919

2020
export const EXECUTION_CONTEXT_LABELS_KEY = 'logging.googleapis.com/labels';
21-
export const EXECUTION_CONTEXT_TRACE_KEY = 'logging.googleapis.com/trace';
2221
export const EXECUTION_CONTEXT_SPAN_ID_KEY = 'logging.googleapis.com/spanId';
2322
const SEVERITY = 'severity';
2423

@@ -132,7 +131,6 @@ export function getModifiedData(
132131
let dataWithContext: {
133132
message: string | Uint8Array;
134133
'logging.googleapis.com/labels': {execution_id: string | undefined};
135-
'logging.googleapis.com/trace': string | undefined;
136134
'logging.googleapis.com/spanId': string | undefined;
137135
severity?: string | undefined;
138136
};
@@ -155,7 +153,6 @@ function getTextWithContext(
155153
return {
156154
message: data,
157155
[EXECUTION_CONTEXT_LABELS_KEY]: {execution_id: context.executionId},
158-
[EXECUTION_CONTEXT_TRACE_KEY]: context.traceId,
159156
[EXECUTION_CONTEXT_SPAN_ID_KEY]: context.spanId,
160157
};
161158
}
@@ -167,11 +164,10 @@ function getJSONWithContext(json: any, context: ExecutionContext) {
167164
} else {
168165
json[EXECUTION_CONTEXT_LABELS_KEY] = {execution_id: context.executionId};
169166
}
170-
return {
171-
...json,
172-
[EXECUTION_CONTEXT_TRACE_KEY]: context.traceId,
173-
[EXECUTION_CONTEXT_SPAN_ID_KEY]: context.spanId,
174-
};
167+
if (!(EXECUTION_CONTEXT_SPAN_ID_KEY in json)) {
168+
json[EXECUTION_CONTEXT_SPAN_ID_KEY] = context.spanId;
169+
}
170+
return json;
175171
}
176172

177173
function processData(data: Uint8Array | string, encoding?: BufferEncoding) {

‎test/async_local_storage.ts

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ describe('asyncLocalStorageMiddleware', () => {
1515
const req = {
1616
body: 'test body',
1717
executionId: 'testExecutionId',
18-
traceId: 'testtrace',
1918
spanId: 'testSpanId',
2019
};
2120
let executionContext;
@@ -25,7 +24,6 @@ describe('asyncLocalStorageMiddleware', () => {
2524
assert(executionContext);
2625
assert.strictEqual(executionContext.executionId, req.executionId);
2726
assert.strictEqual(executionContext.spanId, req.spanId);
28-
assert.strictEqual(executionContext.traceId, req.traceId);
2927
};
3028

3129
await asyncLocalStorageMiddleware(

‎test/execution_context.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,32 @@ describe('executionContextMiddleware', () => {
1919
const testSpanId = '123';
2020
const testTrace = 'testtrace';
2121
const validExecutionId = 'xn1h9xdgv6zw';
22-
const headers = {
23-
'X-Cloud-Trace-Context': `${testTrace}/${testSpanId};o=1`,
24-
'function-execution-id': validExecutionId,
25-
};
2622

2723
it('uses execution ID in header', () => {
28-
const req = createRequest({}, headers);
24+
const req = createRequest(
25+
{},
26+
{
27+
'X-Cloud-Trace-Context': `${testTrace}/${testSpanId};o=1`,
28+
'function-execution-id': validExecutionId,
29+
}
30+
);
2931

3032
executionContextMiddleware(req as Request, {} as Response, next);
3133

3234
assert.strictEqual(req.executionId, validExecutionId);
3335
assert.strictEqual(req.spanId, testSpanId);
34-
assert.strictEqual(req.traceId, testTrace);
3536
});
3637

3738
it('generates execution ID if not in header', () => {
38-
const req = createRequest({}, headers);
39+
const req = createRequest(
40+
{},
41+
{'X-Cloud-Trace-Context': `${testTrace}/${testSpanId}`}
42+
);
3943

4044
executionContextMiddleware(req as Request, {} as Response, next);
4145

4246
assert(req.executionId);
4347
assert.strictEqual(req.spanId, testSpanId);
44-
assert.strictEqual(req.traceId, testTrace);
4548
});
4649

4750
it('req trace undefined if not in header', () => {
@@ -51,6 +54,5 @@ describe('executionContextMiddleware', () => {
5154

5255
assert(req.executionId);
5356
assert.strictEqual(req.spanId, undefined);
54-
assert.strictEqual(req.traceId, undefined);
5557
});
5658
});

‎test/logger.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,12 @@ describe('getModifiedData', () => {
4747
const sampleUint8Arr = new Uint8Array(Buffer.from(sampleText));
4848
const expectedExecutionContext = {
4949
executionId: 'testExecutionId',
50-
traceId: 'testTraceId',
5150
spanId: 'testSpanId',
5251
};
5352
const expectedMetadata = {
5453
'logging.googleapis.com/labels': {
5554
execution_id: 'testExecutionId',
5655
},
57-
'logging.googleapis.com/trace': 'testTraceId',
5856
'logging.googleapis.com/spanId': 'testSpanId',
5957
};
6058
const expectedTextOutput =
@@ -102,13 +100,31 @@ describe('getModifiedData', () => {
102100
user_label_1: 'value_1',
103101
execution_id: 'testExecutionId',
104102
},
105-
'logging.googleapis.com/trace': 'testTraceId',
106103
'logging.googleapis.com/spanId': 'testSpanId',
107104
}) + '\n';
108105
const modifiedData = getModifiedData(data);
109106
assert.equal(modifiedData, expectedOutput);
110107
});
111108

109+
it('json with user span id', () => {
110+
const data = JSON.stringify({
111+
text: 'default text.',
112+
component: 'arbitrary-property',
113+
'logging.googleapis.com/spanId': 'mySpanId',
114+
});
115+
const expectedOutput =
116+
JSON.stringify({
117+
text: 'default text.',
118+
component: 'arbitrary-property',
119+
'logging.googleapis.com/spanId': 'mySpanId',
120+
'logging.googleapis.com/labels': {
121+
execution_id: 'testExecutionId',
122+
},
123+
}) + '\n';
124+
const modifiedData = getModifiedData(data);
125+
assert.equal(modifiedData, expectedOutput);
126+
});
127+
112128
it('uint8array', () => {
113129
const modifiedData = getModifiedData(sampleUint8Arr);
114130
assert.equal(modifiedData, expectedTextOutput);

0 commit comments

Comments
 (0)
Please sign in to comment.