Skip to content

Commit 2a0fd6b

Browse files
committed
fix: remove trace id and respect logging span id field.
1 parent 518dd73 commit 2a0fd6b

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

src/execution_context.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +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.spanId = spanId;
31+
req.spanId = match.groups.spanId;
3332
}
3433
}
3534
next();

src/functions.ts

Lines changed: 0 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ function getJSONWithContext(json: any, context: ExecutionContext) {
164164
} else {
165165
json[EXECUTION_CONTEXT_LABELS_KEY] = {execution_id: context.executionId};
166166
}
167-
return {
168-
...json,
169-
[EXECUTION_CONTEXT_SPAN_ID_KEY]: context.spanId,
170-
};
167+
if (!(EXECUTION_CONTEXT_SPAN_ID_KEY in json)) {
168+
json[EXECUTION_CONTEXT_SPAN_ID_KEY] = context.spanId;
169+
}
170+
return json;
171171
}
172172

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

test/logger.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ describe('getModifiedData', () => {
106106
assert.equal(modifiedData, expectedOutput);
107107
});
108108

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+
109128
it('uint8array', () => {
110129
const modifiedData = getModifiedData(sampleUint8Arr);
111130
assert.equal(modifiedData, expectedTextOutput);

0 commit comments

Comments
 (0)