@@ -4,8 +4,10 @@ import type {
44 ClientOptions ,
55 Event ,
66 EventHint ,
7+ Log ,
78 MonitorConfig ,
89 ParameterizedString ,
10+ Primitive ,
911 SerializedCheckIn ,
1012 SeverityLevel ,
1113} from './types-hoist' ;
@@ -20,6 +22,8 @@ import { eventFromMessage, eventFromUnknownInput } from './utils-hoist/eventbuil
2022import { logger } from './utils-hoist/logger' ;
2123import { uuid4 } from './utils-hoist/misc' ;
2224import { resolvedSyncPromise } from './utils-hoist/syncpromise' ;
25+ import { _INTERNAL_flushLogsBuffer } from './logs' ;
26+ import { isPrimitive } from './utils-hoist' ;
2327
2428export interface ServerRuntimeClientOptions extends ClientOptions < BaseTransportOptions > {
2529 platform ?: string ;
@@ -33,6 +37,8 @@ export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportO
3337export class ServerRuntimeClient <
3438 O extends ClientOptions & ServerRuntimeClientOptions = ServerRuntimeClientOptions ,
3539> extends Client < O > {
40+ private _logWeight : number ;
41+
3642 /**
3743 * Creates a new Edge SDK instance.
3844 * @param options Configuration options for this SDK.
@@ -42,6 +48,26 @@ export class ServerRuntimeClient<
4248 registerSpanErrorInstrumentation ( ) ;
4349
4450 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+ } ) ;
4571 }
4672
4773 /**
@@ -196,3 +222,45 @@ function setCurrentRequestSessionErroredOrCrashed(eventHint?: EventHint): void {
196222 }
197223 }
198224}
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