Skip to content

Commit ab78ba8

Browse files
committed
⚗️ try adding traceinfo to bridge logs
1 parent 46ef3f2 commit ab78ba8

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/util/logger.util.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { context, trace } from '@opentelemetry/api';
1+
import { context, propagation, trace } from '@opentelemetry/api';
22

33
import { anonymizeKey } from './anonymize-key';
44
import { userIdStorage } from '../middlewares';
@@ -83,23 +83,37 @@ const logger = (
8383
logFn(formatedMessage, ...args);
8484
return;
8585
}
86-
8786
const data: Record<string, unknown> = { ...args };
88-
const userId = userIdStorage.getStore();
89-
90-
if (userId) {
91-
data.userId = userId;
92-
}
9387

9488
const anonymizedApiKey = apiKey ? anonymizeKey(apiKey) : undefined;
9589

9690
if (anonymizedApiKey) {
9791
data.apiKey = anonymizedApiKey;
9892
}
9993

94+
const object: Record<string, unknown> = {};
95+
propagation.inject(context.active(), object);
96+
97+
// format tracing info for gcloud logging
98+
let traceProp = {};
99+
if (object.traceparent && typeof object.traceparent === 'string') {
100+
const [, traceId, spanId] = object.traceparent.split('-');
101+
102+
traceProp =
103+
process.env.NODE_ENV === 'production'
104+
? {
105+
'logging.googleapis.com/trace': `projects/clinq-services/traces/${traceId}`,
106+
'logging.googleapis.com/spanId': spanId,
107+
'logging.googleapis.com/sampled': false,
108+
}
109+
: {};
110+
}
111+
100112
logFn(
101113
JSON.stringify({
114+
...traceProp,
102115
message: formatedMessage,
116+
userId: userIdStorage.getStore(),
103117
...data,
104118
}),
105119
);

0 commit comments

Comments
 (0)