-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathlog.ts
91 lines (83 loc) · 2.17 KB
/
log.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import type {
DsnComponents,
LogSeverityLevel,
SdkMetadata,
SerializedLogAttribute,
SerializedOtelLog,
} from './types-hoist';
import type { OtelLogItem, OtelLogEnvelope } from './types-hoist/envelope';
import { createEnvelope, dsnToString } from './utils-hoist';
export const SEVERITY_TEXT_TO_SEVERITY_NUMBER: Partial<Record<LogSeverityLevel, number>> = {
trace: 1,
debug: 5,
info: 9,
warn: 13,
error: 17,
fatal: 21,
};
/**
* Convert a log attribute to a serialized log attribute
*
* @param key - The key of the log attribute
* @param value - The value of the log attribute
* @returns The serialized log attribute
*/
export function logAttributeToSerializedLogAttribute(key: string, value: unknown): SerializedLogAttribute {
switch (typeof value) {
case 'number':
return {
key,
value: { doubleValue: value },
};
case 'boolean':
return {
key,
value: { boolValue: value },
};
case 'string':
return {
key,
value: { stringValue: value },
};
default:
return {
key,
value: { stringValue: JSON.stringify(value) ?? '' },
};
}
}
/**
* Creates envelope item for a single log
*/
export function createOtelLogEnvelopeItem(log: SerializedOtelLog): OtelLogItem {
const headers: OtelLogItem[0] = {
type: 'otel_log',
};
return [headers, log];
}
/**
* Records a log and sends it to sentry.
*
* Logs represent a message (and optionally some structured data) which provide context for a trace or error.
* Ex: sentry.addLog({level: 'warning', message: `user ${user} just bought ${item}`, attributes: {user, item}}
*
* @params log - the log object which will be sent
*/
export function createOtelLogEnvelope(
logs: Array<SerializedOtelLog>,
metadata?: SdkMetadata,
tunnel?: string,
dsn?: DsnComponents,
): OtelLogEnvelope {
const headers: OtelLogEnvelope[0] = {};
if (metadata?.sdk) {
headers.sdk = {
name: metadata.sdk.name,
version: metadata.sdk.version,
};
}
if (!!tunnel && !!dsn) {
headers.dsn = dsnToString(dsn);
}
return createEnvelope<OtelLogEnvelope>(headers, logs.map(createOtelLogEnvelopeItem));
}