|
1 | | -import { context, trace } from '@opentelemetry/api'; |
| 1 | +import { context, propagation, trace } from '@opentelemetry/api'; |
2 | 2 |
|
3 | 3 | import { anonymizeKey } from './anonymize-key'; |
4 | 4 | import { userIdStorage } from '../middlewares'; |
@@ -83,23 +83,37 @@ const logger = ( |
83 | 83 | logFn(formatedMessage, ...args); |
84 | 84 | return; |
85 | 85 | } |
86 | | - |
87 | 86 | const data: Record<string, unknown> = { ...args }; |
88 | | - const userId = userIdStorage.getStore(); |
89 | | - |
90 | | - if (userId) { |
91 | | - data.userId = userId; |
92 | | - } |
93 | 87 |
|
94 | 88 | const anonymizedApiKey = apiKey ? anonymizeKey(apiKey) : undefined; |
95 | 89 |
|
96 | 90 | if (anonymizedApiKey) { |
97 | 91 | data.apiKey = anonymizedApiKey; |
98 | 92 | } |
99 | 93 |
|
| 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 | + |
100 | 112 | logFn( |
101 | 113 | JSON.stringify({ |
| 114 | + ...traceProp, |
102 | 115 | message: formatedMessage, |
| 116 | + userId: userIdStorage.getStore(), |
103 | 117 | ...data, |
104 | 118 | }), |
105 | 119 | ); |
|
0 commit comments