Skip to content

Commit d27b99b

Browse files
committed
feat(core): Apply scope attributes to logs
1 parent 69ffa8e commit d27b99b

File tree

2 files changed

+12
-59
lines changed

2 files changed

+12
-59
lines changed

packages/core/src/logs/internal.ts

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import type { TypedAttributes } from '../attributes';
2+
import { attributeValueToTypedAttributeValue } from '../attributes';
13
import { getGlobalSingleton } from '../carrier';
24
import type { Client } from '../client';
35
import { getClient, getCurrentScope, getGlobalScope, getIsolationScope } from '../currentScopes';
46
import { DEBUG_BUILD } from '../debug-build';
57
import type { Scope, ScopeData } from '../scope';
68
import type { Integration } from '../types-hoist/integration';
7-
import type { Log, SerializedLog, SerializedLogAttributeValue } from '../types-hoist/log';
9+
import type { Log, SerializedLog } from '../types-hoist/log';
810
import { mergeScopeData } from '../utils/applyScopeDataToEvent';
911
import { consoleSandbox, debug } from '../utils/debug-logger';
1012
import { isParameterizedString } from '../utils/is';
@@ -16,51 +18,6 @@ import { createLogEnvelope } from './envelope';
1618

1719
const MAX_LOG_BUFFER_SIZE = 100;
1820

19-
/**
20-
* Converts a log attribute to a serialized log attribute.
21-
*
22-
* @param key - The key of the log attribute.
23-
* @param value - The value of the log attribute.
24-
* @returns The serialized log attribute.
25-
*/
26-
export function logAttributeToSerializedLogAttribute(value: unknown): SerializedLogAttributeValue {
27-
switch (typeof value) {
28-
case 'number':
29-
if (Number.isInteger(value)) {
30-
return {
31-
value,
32-
type: 'integer',
33-
};
34-
}
35-
return {
36-
value,
37-
type: 'double',
38-
};
39-
case 'boolean':
40-
return {
41-
value,
42-
type: 'boolean',
43-
};
44-
case 'string':
45-
return {
46-
value,
47-
type: 'string',
48-
};
49-
default: {
50-
let stringValue = '';
51-
try {
52-
stringValue = JSON.stringify(value) ?? '';
53-
} catch {
54-
// Do nothing
55-
}
56-
return {
57-
value: stringValue,
58-
type: 'string',
59-
};
60-
}
61-
}
62-
}
63-
6421
/**
6522
* Sets a log attribute if the value exists and the attribute key is not already present.
6623
*
@@ -139,6 +96,7 @@ export function _INTERNAL_captureLog(
13996

14097
const {
14198
user: { id, email, username },
99+
attributes: scopeAttributes,
142100
} = getMergedScopeData(currentScope);
143101
setLogAttribute(processedLogAttributes, 'user.id', id, false);
144102
setLogAttribute(processedLogAttributes, 'user.email', email, false);
@@ -201,13 +159,13 @@ export function _INTERNAL_captureLog(
201159
body: message,
202160
trace_id: traceContext?.trace_id,
203161
severity_number: severityNumber ?? SEVERITY_TEXT_TO_SEVERITY_NUMBER[level],
204-
attributes: Object.keys(attributes).reduce(
205-
(acc, key) => {
206-
acc[key] = logAttributeToSerializedLogAttribute(attributes[key]);
162+
attributes: {
163+
...scopeAttributes,
164+
...Object.keys(attributes).reduce((acc, key) => {
165+
acc[key] = attributeValueToTypedAttributeValue(attributes[key]);
207166
return acc;
208-
},
209-
{} as Record<string, SerializedLogAttributeValue>,
210-
),
167+
}, {} as TypedAttributes),
168+
},
211169
};
212170

213171
captureSerializedLog(client, serializedLog);

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { TypedAttributes } from '../attributes';
12
import type { ParameterizedString } from './parameterize';
23

34
export type LogSeverityLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
@@ -30,12 +31,6 @@ export interface Log {
3031
severityNumber?: number;
3132
}
3233

33-
export type SerializedLogAttributeValue =
34-
| { value: string; type: 'string' }
35-
| { value: number; type: 'integer' }
36-
| { value: number; type: 'double' }
37-
| { value: boolean; type: 'boolean' };
38-
3934
export interface SerializedLog {
4035
/**
4136
* Timestamp in seconds (epoch time) indicating when the log occurred.
@@ -60,7 +55,7 @@ export interface SerializedLog {
6055
/**
6156
* Arbitrary structured data that stores information about the log - e.g., userId: 100.
6257
*/
63-
attributes?: Record<string, SerializedLogAttributeValue>;
58+
attributes?: TypedAttributes;
6459

6560
/**
6661
* The severity number.

0 commit comments

Comments
 (0)