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' ;
3
5
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' ;
5
7
6
8
/**
7
9
* Creates envelope item for a single log
@@ -26,74 +28,59 @@ function addLog(log: Log): void {
26
28
const client = getClient ( ) ;
27
29
28
30
if ( ! client ) {
31
+ DEBUG_BUILD && logger . warn ( 'No client available, log will not be captured.' ) ;
29
32
return ;
30
33
}
31
34
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.' ) ;
33
37
return ;
34
38
}
35
39
36
- const globalScope = getGlobalScope ( ) ;
40
+ const scope = getCurrentScope ( ) ;
41
+ const dsc = getDynamicSamplingContextFromScope ( client , scope ) ;
42
+
37
43
const dsn = client . getDsn ( ) ;
38
44
39
45
const headers : LogEnvelope [ 0 ] = {
40
- trace : {
41
- trace_id : globalScope . getPropagationContext ( ) . traceId ,
42
- public_key : dsn ?. publicKey ,
43
- } ,
46
+ trace : dropUndefinedKeys ( dsc ) as DynamicSamplingContext ,
44
47
...( dsn ? { dsn : dsnToString ( dsn ) } : { } ) ,
45
48
} ;
46
49
if ( ! log . traceId ) {
47
- log . traceId = globalScope . getPropagationContext ( ) . traceId || '00000000-0000-0000-0000-000000000000' ;
50
+ log . traceId = dsc . trace_id ;
48
51
}
49
52
if ( ! log . timeUnixNano ) {
50
53
log . timeUnixNano = `${ new Date ( ) . getTime ( ) . toString ( ) } 000000` ;
51
54
}
52
55
53
56
const envelope = createEnvelope < LogEnvelope > ( headers , [ createLogEnvelopeItem ( log ) ] ) ;
54
57
55
- // sendEnvelope should not throw
56
58
// eslint-disable-next-line @typescript-eslint/no-floating-promises
57
- client . sendEnvelope ( envelope ) ;
59
+ void client . sendEnvelope ( envelope ) ;
58
60
}
59
61
60
62
function valueToAttribute ( key : string , value : unknown ) : LogAttribute {
61
- if ( typeof value === 'number' ) {
62
- if ( Number . isInteger ( value ) ) {
63
+ switch ( typeof value ) {
64
+ case 'number' :
63
65
return {
64
66
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 ) } ,
68
83
} ;
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
- } ;
97
84
}
98
85
}
99
86
0 commit comments