@@ -4,8 +4,10 @@ import type {
4
4
ClientOptions ,
5
5
Event ,
6
6
EventHint ,
7
+ Log ,
7
8
MonitorConfig ,
8
9
ParameterizedString ,
10
+ Primitive ,
9
11
SerializedCheckIn ,
10
12
SeverityLevel ,
11
13
} from './types-hoist' ;
@@ -20,6 +22,8 @@ import { eventFromMessage, eventFromUnknownInput } from './utils-hoist/eventbuil
20
22
import { logger } from './utils-hoist/logger' ;
21
23
import { uuid4 } from './utils-hoist/misc' ;
22
24
import { resolvedSyncPromise } from './utils-hoist/syncpromise' ;
25
+ import { _INTERNAL_flushLogsBuffer } from './logs' ;
26
+ import { isPrimitive } from './utils-hoist' ;
23
27
24
28
export interface ServerRuntimeClientOptions extends ClientOptions < BaseTransportOptions > {
25
29
platform ?: string ;
@@ -33,6 +37,8 @@ export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportO
33
37
export class ServerRuntimeClient <
34
38
O extends ClientOptions & ServerRuntimeClientOptions = ServerRuntimeClientOptions ,
35
39
> extends Client < O > {
40
+ private _logWeight : number ;
41
+
36
42
/**
37
43
* Creates a new Edge SDK instance.
38
44
* @param options Configuration options for this SDK.
@@ -42,6 +48,26 @@ export class ServerRuntimeClient<
42
48
registerSpanErrorInstrumentation ( ) ;
43
49
44
50
super ( options ) ;
51
+
52
+ this . _logWeight = 0 ;
53
+
54
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
55
+ const client = this ;
56
+ this . on ( 'flush' , ( ) => {
57
+ _INTERNAL_flushLogsBuffer ( client ) ;
58
+ } ) ;
59
+
60
+ this . on ( 'beforeCaptureLog' , log => {
61
+ client . _logWeight += estimateLogSizeInBytes ( log ) ;
62
+
63
+ // We flush the logs buffer if it exceeds 0.8 MB
64
+ // The log weight is a rough estimate, so we flush way before
65
+ // the payload gets too big.
66
+ if ( client . _logWeight > 800_000 ) {
67
+ _INTERNAL_flushLogsBuffer ( client ) ;
68
+ client . _logWeight = 0 ;
69
+ }
70
+ } ) ;
45
71
}
46
72
47
73
/**
@@ -196,3 +222,45 @@ function setCurrentRequestSessionErroredOrCrashed(eventHint?: EventHint): void {
196
222
}
197
223
}
198
224
}
225
+
226
+ /**
227
+ * Estimate the size of a log in bytes.
228
+ *
229
+ * @param log - The log to estimate the size of.
230
+ * @returns The estimated size of the log in bytes.
231
+ */
232
+ function estimateLogSizeInBytes ( log : Log ) : number {
233
+ let weight = 0 ;
234
+
235
+ // Estimate byte size of 2 bytes per character. This is a rough estimate JS strings are stored as UTF-16.
236
+ if ( log . message ) {
237
+ weight += log . message . length * 2 ;
238
+ }
239
+
240
+ if ( log . attributes ) {
241
+ Object . values ( log . attributes ) . forEach ( value => {
242
+ if ( Array . isArray ( value ) ) {
243
+ weight += value . length * estimatePrimitiveSizeInBytes ( value [ 0 ] ) ;
244
+ } else if ( isPrimitive ( value ) ) {
245
+ weight += estimatePrimitiveSizeInBytes ( value ) ;
246
+ } else {
247
+ // For objects values, we estimate the size of the object as 100 bytes
248
+ weight += 100 ;
249
+ }
250
+ } ) ;
251
+ }
252
+
253
+ return weight ;
254
+ }
255
+
256
+ function estimatePrimitiveSizeInBytes ( value : Primitive ) : number {
257
+ if ( typeof value === 'string' ) {
258
+ return value . length * 2 ;
259
+ } else if ( typeof value === 'number' ) {
260
+ return 8 ;
261
+ } else if ( typeof value === 'boolean' ) {
262
+ return 4 ;
263
+ }
264
+
265
+ return 0 ;
266
+ }
0 commit comments