-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathenvelope.ts
More file actions
46 lines (40 loc) · 1.67 KB
/
envelope.ts
File metadata and controls
46 lines (40 loc) · 1.67 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
import type { Client } from '../../client';
import type { DynamicSamplingContext, SpanContainerItem, StreamedSpanEnvelope } from '../../types-hoist/envelope';
import type { SerializedStreamedSpan } from '../../types-hoist/span';
import { dsnToString } from '../../utils/dsn';
import { createEnvelope, getSdkMetadataForEnvelopeHeader } from '../../utils/envelope';
/**
* Creates a span v2 span streaming envelope
*/
export function createStreamedSpanEnvelope(
serializedSpans: Array<SerializedStreamedSpan>,
dsc: Partial<DynamicSamplingContext>,
client: Client,
): StreamedSpanEnvelope {
const options = client.getOptions();
const dsn = client.getDsn();
const tunnel = options.tunnel;
const sdk = getSdkMetadataForEnvelopeHeader(options._metadata);
const headers: StreamedSpanEnvelope[0] = {
sent_at: new Date().toISOString(),
...(dscHasRequiredProps(dsc) && { trace: dsc }),
...(sdk && { sdk }),
...(!!tunnel && dsn && { dsn: dsnToString(dsn) }),
};
const isBrowserSdk = options._metadata?.sdk?.name === 'sentry.javascript.browser';
const inferSetting = options.sendDefaultPii ? 'auto' : 'never';
const spanContainer: SpanContainerItem = [
{ type: 'span', item_count: serializedSpans.length, content_type: 'application/vnd.sentry.items.span.v2+json' },
{
version: 2,
...(isBrowserSdk && {
ingest_settings: { infer_ip: inferSetting, infer_useragent: inferSetting },
}),
items: serializedSpans,
},
];
return createEnvelope<StreamedSpanEnvelope>(headers, [spanContainer]);
}
function dscHasRequiredProps(dsc: Partial<DynamicSamplingContext>): dsc is DynamicSamplingContext {
return !!dsc.trace_id && !!dsc.public_key;
}