11import type { Client } from '../client' ;
2+ import type { SerializedLog , SerializedLogAttributeValue } from '../types-hoist/log' ;
3+ import type { Log } from '../types-hoist/log' ;
4+
25import { _getTraceInfoFromScope } from '../client' ;
36import { getClient , getCurrentScope } from '../currentScopes' ;
47import { DEBUG_BUILD } from '../debug-build' ;
58import { SEVERITY_TEXT_TO_SEVERITY_NUMBER } from './constants' ;
6- import type { SerializedLogAttribute , SerializedOtelLog } from '../types-hoist/log' ;
7- import type { Log } from '../types-hoist/log' ;
89import { _getSpanForScope } from '../utils/spanOnScope' ;
9- import { createOtelLogEnvelope } from './envelope' ;
10+ import { createLogEnvelope } from './envelope' ;
1011import { logger } from '../utils-hoist/logger' ;
1112import { isParameterizedString } from '../utils-hoist/is' ;
13+ import { timestampInSeconds } from '../utils-hoist/time' ;
1214
1315const MAX_LOG_BUFFER_SIZE = 100 ;
1416
15- const CLIENT_TO_LOG_BUFFER_MAP = new WeakMap < Client , Array < SerializedOtelLog > > ( ) ;
17+ const CLIENT_TO_LOG_BUFFER_MAP = new WeakMap < Client , Array < SerializedLog > > ( ) ;
1618
1719/**
1820 * Converts a log attribute to a serialized log attribute.
@@ -21,22 +23,28 @@ const CLIENT_TO_LOG_BUFFER_MAP = new WeakMap<Client, Array<SerializedOtelLog>>()
2123 * @param value - The value of the log attribute.
2224 * @returns The serialized log attribute.
2325 */
24- export function logAttributeToSerializedLogAttribute ( key : string , value : unknown ) : SerializedLogAttribute {
26+ export function logAttributeToSerializedLogAttribute ( value : unknown ) : SerializedLogAttributeValue {
2527 switch ( typeof value ) {
2628 case 'number' :
29+ if ( Number . isInteger ( value ) ) {
30+ return {
31+ value,
32+ type : 'integer' ,
33+ } ;
34+ }
2735 return {
28- key ,
29- value : { doubleValue : value } ,
36+ value ,
37+ type : 'double' ,
3038 } ;
3139 case 'boolean' :
3240 return {
33- key ,
34- value : { boolValue : value } ,
41+ value ,
42+ type : 'boolean' ,
3543 } ;
3644 case 'string' :
3745 return {
38- key ,
39- value : { stringValue : value } ,
46+ value ,
47+ type : 'string' ,
4048 } ;
4149 default : {
4250 let stringValue = '' ;
@@ -46,8 +54,8 @@ export function logAttributeToSerializedLogAttribute(key: string, value: unknown
4654 // Do nothing
4755 }
4856 return {
49- key ,
50- value : { stringValue } ,
57+ value : stringValue ,
58+ type : 'string' ,
5159 } ;
5260 }
5361 }
@@ -128,15 +136,19 @@ export function _INTERNAL_captureLog(
128136
129137 const { level, message, attributes = { } , severityNumber } = log ;
130138
131- const serializedLog : SerializedOtelLog = {
132- severityText : level ,
133- body : {
134- stringValue : message ,
135- } ,
136- attributes : Object . entries ( attributes ) . map ( ( [ key , value ] ) => logAttributeToSerializedLogAttribute ( key , value ) ) ,
137- timeUnixNano : `${ new Date ( ) . getTime ( ) . toString ( ) } 000000` ,
138- traceId : traceContext ?. trace_id ,
139- severityNumber : severityNumber ?? SEVERITY_TEXT_TO_SEVERITY_NUMBER [ level ] ,
139+ const serializedLog : SerializedLog = {
140+ timestamp : timestampInSeconds ( ) ,
141+ level,
142+ body : message ,
143+ trace_id : traceContext ?. trace_id ,
144+ severity_number : severityNumber ?? SEVERITY_TEXT_TO_SEVERITY_NUMBER [ level ] ,
145+ attributes : Object . keys ( attributes ) . reduce (
146+ ( acc , key ) => {
147+ acc [ key ] = logAttributeToSerializedLogAttribute ( attributes [ key ] ) ;
148+ return acc ;
149+ } ,
150+ { } as Record < string , SerializedLogAttributeValue > ,
151+ ) ,
140152 } ;
141153
142154 const logBuffer = CLIENT_TO_LOG_BUFFER_MAP . get ( client ) ;
@@ -161,14 +173,14 @@ export function _INTERNAL_captureLog(
161173 * @experimental This method will experience breaking changes. This is not yet part of
162174 * the stable Sentry SDK API and can be changed or removed without warning.
163175 */
164- export function _INTERNAL_flushLogsBuffer ( client : Client , maybeLogBuffer ?: Array < SerializedOtelLog > ) : void {
176+ export function _INTERNAL_flushLogsBuffer ( client : Client , maybeLogBuffer ?: Array < SerializedLog > ) : void {
165177 const logBuffer = maybeLogBuffer ?? CLIENT_TO_LOG_BUFFER_MAP . get ( client ) ?? [ ] ;
166178 if ( logBuffer . length === 0 ) {
167179 return ;
168180 }
169181
170182 const clientOptions = client . getOptions ( ) ;
171- const envelope = createOtelLogEnvelope ( logBuffer , clientOptions . _metadata , clientOptions . tunnel , client . getDsn ( ) ) ;
183+ const envelope = createLogEnvelope ( logBuffer , clientOptions . _metadata , clientOptions . tunnel , client . getDsn ( ) ) ;
172184
173185 // Clear the log buffer after envelopes have been constructed.
174186 logBuffer . length = 0 ;
@@ -188,6 +200,6 @@ export function _INTERNAL_flushLogsBuffer(client: Client, maybeLogBuffer?: Array
188200 * @param client - The client to get the log buffer for.
189201 * @returns The log buffer for the given client.
190202 */
191- export function _INTERNAL_getLogBuffer ( client : Client ) : Array < SerializedOtelLog > | undefined {
203+ export function _INTERNAL_getLogBuffer ( client : Client ) : Array < SerializedLog > | undefined {
192204 return CLIENT_TO_LOG_BUFFER_MAP . get ( client ) ;
193205}
0 commit comments