-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathenvelope.ts
More file actions
68 lines (63 loc) · 2.22 KB
/
envelope.ts
File metadata and controls
68 lines (63 loc) · 2.22 KB
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
import type { DsnComponents } from '../types-hoist/dsn';
import type { LogContainerItem, LogEnvelope } from '../types-hoist/envelope';
import type { SerializedLog } from '../types-hoist/log';
import type { SdkMetadata } from '../types-hoist/sdkmetadata';
import { dsnToString } from '../utils/dsn';
import { createEnvelope } from '../utils/envelope';
import { isBrowser } from '../utils/isBrowser';
/**
* Creates a log container envelope item for a list of logs.
*
* @param items - The logs to include in the envelope.
* @param inferUserData - If true, tells Relay to infer the end-user IP and User-Agent from the incoming request.
* Only emitted as `ingest_settings` in browser environments.
* @returns The created log container envelope item.
*/
export function createLogContainerEnvelopeItem(items: Array<SerializedLog>, inferUserData?: boolean): LogContainerItem {
const inferSetting = inferUserData ? 'auto' : 'never';
return [
{
type: 'log',
item_count: items.length,
content_type: 'application/vnd.sentry.items.log+json',
},
{
version: 2,
...(isBrowser() && {
ingest_settings: { infer_ip: inferSetting, infer_user_agent: inferSetting },
}),
items,
},
];
}
/**
* Creates an envelope for a list of logs.
*
* Logs from multiple traces can be included in the same envelope.
*
* @param logs - The logs to include in the envelope.
* @param metadata - The metadata to include in the envelope.
* @param tunnel - The tunnel to include in the envelope.
* @param dsn - The DSN to include in the envelope.
* @param inferUserData - If true, tells Relay to infer the end-user IP and User-Agent from the incoming request.
* @returns The created envelope.
*/
export function createLogEnvelope(
logs: Array<SerializedLog>,
metadata?: SdkMetadata,
tunnel?: string,
dsn?: DsnComponents,
inferUserData?: boolean,
): LogEnvelope {
const headers: LogEnvelope[0] = {};
if (metadata?.sdk) {
headers.sdk = {
name: metadata.sdk.name,
version: metadata.sdk.version,
};
}
if (!!tunnel && !!dsn) {
headers.dsn = dsnToString(dsn);
}
return createEnvelope<LogEnvelope>(headers, [createLogContainerEnvelopeItem(logs, inferUserData)]);
}