Skip to content

Commit 1c95817

Browse files
committed
ref: Improve log creation logic and update option name
1 parent 4fd0f8a commit 1c95817

File tree

2 files changed

+37
-45
lines changed

2 files changed

+37
-45
lines changed

packages/core/src/log.ts

+32-45
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { getClient, getGlobalScope } from './currentScopes';
2-
import type { LogEnvelope, LogItem } from './types-hoist/envelope';
1+
import { getClient, getCurrentScope } from './currentScopes';
2+
import { DEBUG_BUILD } from './debug-build';
3+
import { getDynamicSamplingContextFromScope } from './tracing';
4+
import type { DynamicSamplingContext, LogEnvelope, LogItem } from './types-hoist/envelope';
35
import type { Log, LogAttribute, LogSeverityLevel } from './types-hoist/log';
4-
import { createEnvelope, dsnToString } from './utils-hoist';
6+
import { createEnvelope, dropUndefinedKeys, dsnToString, logger } from './utils-hoist';
57

68
/**
79
* Creates envelope item for a single log
@@ -26,74 +28,59 @@ function addLog(log: Log): void {
2628
const client = getClient();
2729

2830
if (!client) {
31+
DEBUG_BUILD && logger.warn('No client available, log will not be captured.');
2932
return;
3033
}
3134

32-
if (!client.getOptions()._experiments?.logSupport) {
35+
if (!client.getOptions()._experiments?.enableLogs) {
36+
DEBUG_BUILD && logger.warn('logging option not enabled, log will not be captured.');
3337
return;
3438
}
3539

36-
const globalScope = getGlobalScope();
40+
const scope = getCurrentScope();
41+
const dsc = getDynamicSamplingContextFromScope(client, scope);
42+
3743
const dsn = client.getDsn();
3844

3945
const headers: LogEnvelope[0] = {
40-
trace: {
41-
trace_id: globalScope.getPropagationContext().traceId,
42-
public_key: dsn?.publicKey,
43-
},
46+
trace: dropUndefinedKeys(dsc) as DynamicSamplingContext,
4447
...(dsn ? { dsn: dsnToString(dsn) } : {}),
4548
};
4649
if (!log.traceId) {
47-
log.traceId = globalScope.getPropagationContext().traceId || '00000000-0000-0000-0000-000000000000';
50+
log.traceId = dsc.trace_id;
4851
}
4952
if (!log.timeUnixNano) {
5053
log.timeUnixNano = `${new Date().getTime().toString()}000000`;
5154
}
5255

5356
const envelope = createEnvelope<LogEnvelope>(headers, [createLogEnvelopeItem(log)]);
5457

55-
// sendEnvelope should not throw
5658
// eslint-disable-next-line @typescript-eslint/no-floating-promises
57-
client.sendEnvelope(envelope);
59+
void client.sendEnvelope(envelope);
5860
}
5961

6062
function valueToAttribute(key: string, value: unknown): LogAttribute {
61-
if (typeof value === 'number') {
62-
if (Number.isInteger(value)) {
63+
switch (typeof value) {
64+
case 'number':
6365
return {
6466
key,
65-
value: {
66-
intValue: value,
67-
},
67+
value: { doubleValue: value },
68+
};
69+
case 'boolean':
70+
return {
71+
key,
72+
value: { boolValue: value },
73+
};
74+
case 'string':
75+
return {
76+
key,
77+
value: { stringValue: value },
78+
};
79+
default:
80+
return {
81+
key,
82+
value: { stringValue: JSON.stringify(value) },
6883
};
69-
}
70-
return {
71-
key,
72-
value: {
73-
doubleValue: value,
74-
},
75-
};
76-
} else if (typeof value === 'boolean') {
77-
return {
78-
key,
79-
value: {
80-
boolValue: value,
81-
},
82-
};
83-
} else if (typeof value === 'string') {
84-
return {
85-
key,
86-
value: {
87-
stringValue: value,
88-
},
89-
};
90-
} else {
91-
return {
92-
key,
93-
value: {
94-
stringValue: JSON.stringify(value),
95-
},
96-
};
9784
}
9885
}
9986

packages/core/src/types-hoist/options.ts

+5
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,12 @@ export interface ClientOptions<TO extends BaseTransportOptions = BaseTransportOp
182182
* Options which are in beta, or otherwise not guaranteed to be stable.
183183
*/
184184
_experiments?: {
185+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
185186
[key: string]: any;
187+
/**
188+
* If logs support should be enabled. Defaults to false.
189+
*/
190+
enableLogs?: boolean;
186191
};
187192

188193
/**

0 commit comments

Comments
 (0)