From 387075ebe1cef98a902a5e570d519a1d892a98a3 Mon Sep 17 00:00:00 2001 From: Charly Gomez Date: Fri, 10 Oct 2025 14:02:55 +0200 Subject: [PATCH 1/3] fix types --- packages/core/src/types-hoist/metric.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/types-hoist/metric.ts b/packages/core/src/types-hoist/metric.ts index 9201243c4a38..6ac63da6032b 100644 --- a/packages/core/src/types-hoist/metric.ts +++ b/packages/core/src/types-hoist/metric.ts @@ -9,7 +9,7 @@ export interface Metric { /** * The value of the metric. */ - value: number | string; + value: number; /** * The type of metric. @@ -42,7 +42,7 @@ export interface SerializedMetric { /** * The trace ID for this metric. */ - trace_id?: string; + trace_id: string; /** * The span ID for this metric. @@ -67,7 +67,7 @@ export interface SerializedMetric { /** * The value of the metric. */ - value: number | string; + value: number; /** * Arbitrary structured data that stores information about the metric. From 3b7a096adcfd2c5749fa07a122034cf465537bfe Mon Sep 17 00:00:00 2001 From: Charly Gomez Date: Fri, 10 Oct 2025 14:12:20 +0200 Subject: [PATCH 2/3] update tracing_id logic and update func signature --- packages/core/src/metrics/internal.ts | 5 +++++ packages/core/src/metrics/public-api.ts | 2 +- packages/core/src/server-runtime-client.ts | 6 +----- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/core/src/metrics/internal.ts b/packages/core/src/metrics/internal.ts index 0f16d98b790e..0c08a56ca246 100644 --- a/packages/core/src/metrics/internal.ts +++ b/packages/core/src/metrics/internal.ts @@ -199,6 +199,11 @@ export function _INTERNAL_captureMetric(beforeMetric: Metric, options?: Internal const traceId = span ? span.spanContext().traceId : traceContext?.trace_id; const spanId = span ? span.spanContext().spanId : undefined; + if (!traceId) { + DEBUG_BUILD && debug.warn('[Metric] No trace ID available, will not send metric.'); + return; + } + const serializedMetric: SerializedMetric = { timestamp: timestampInSeconds(), trace_id: traceId, diff --git a/packages/core/src/metrics/public-api.ts b/packages/core/src/metrics/public-api.ts index e508fcb9e6d0..7dcfe74dfdb0 100644 --- a/packages/core/src/metrics/public-api.ts +++ b/packages/core/src/metrics/public-api.ts @@ -30,7 +30,7 @@ export interface MetricOptions { * @param value - The value of the metric. * @param options - Options for capturing the metric. */ -function captureMetric(type: MetricType, name: string, value: number | string, options?: MetricOptions): void { +function captureMetric(type: MetricType, name: string, value: number, options?: MetricOptions): void { _INTERNAL_captureMetric( { type, name, value, unit: options?.unit, attributes: options?.attributes }, { scope: options?.scope }, diff --git a/packages/core/src/server-runtime-client.ts b/packages/core/src/server-runtime-client.ts index 761d4aca7cd7..cafb7fce3d86 100644 --- a/packages/core/src/server-runtime-client.ts +++ b/packages/core/src/server-runtime-client.ts @@ -283,11 +283,7 @@ function estimateMetricSizeInBytes(metric: Metric): number { } // Add weight for the value - if (typeof metric.value === 'string') { - weight += metric.value.length * 2; - } else { - weight += 8; // number - } + weight += 8; // number if (metric.attributes) { Object.values(metric.attributes).forEach(value => { From 2448d0eea5c1ed138bbb78a6e0d9daf59e213610 Mon Sep 17 00:00:00 2001 From: Charly Gomez Date: Thu, 16 Oct 2025 13:57:33 +0200 Subject: [PATCH 3/3] cleanup --- packages/core/src/client.ts | 8 +-- packages/core/src/metrics/internal.ts | 7 +- packages/core/src/server-runtime-client.ts | 76 ---------------------- packages/core/test/lib/client.test.ts | 5 +- 4 files changed, 7 insertions(+), 89 deletions(-) diff --git a/packages/core/src/client.ts b/packages/core/src/client.ts index 6a269a969c8d..e0294ef26c59 100644 --- a/packages/core/src/client.ts +++ b/packages/core/src/client.ts @@ -1521,12 +1521,8 @@ function estimateMetricSizeInBytes(metric: Metric): number { weight += metric.name.length * 2; } - // Add weight for the value - if (typeof metric.value === 'string') { - weight += metric.value.length * 2; - } else { - weight += 8; // number - } + // Add weight for number + weight += 8; return weight + estimateAttributesSizeInBytes(metric.attributes); } diff --git a/packages/core/src/metrics/internal.ts b/packages/core/src/metrics/internal.ts index a461f8665ca0..f8e40b5fc0b4 100644 --- a/packages/core/src/metrics/internal.ts +++ b/packages/core/src/metrics/internal.ts @@ -199,14 +199,9 @@ export function _INTERNAL_captureMetric(beforeMetric: Metric, options?: Internal const traceId = span ? span.spanContext().traceId : traceContext?.trace_id; const spanId = span ? span.spanContext().spanId : undefined; - if (!traceId) { - DEBUG_BUILD && debug.warn('[Metric] No trace ID available, will not send metric.'); - return; - } - const serializedMetric: SerializedMetric = { timestamp: timestampInSeconds(), - trace_id: traceId, + trace_id: traceId ?? '', span_id: spanId, name: processedMetric.name, type: processedMetric.type, diff --git a/packages/core/src/server-runtime-client.ts b/packages/core/src/server-runtime-client.ts index 2ab384079ba1..9d037eb3b7c3 100644 --- a/packages/core/src/server-runtime-client.ts +++ b/packages/core/src/server-runtime-client.ts @@ -191,79 +191,3 @@ function setCurrentRequestSessionErroredOrCrashed(eventHint?: EventHint): void { } } } - -/** - * Estimate the size of a metric in bytes. - * - * @param metric - The metric to estimate the size of. - * @returns The estimated size of the metric in bytes. - */ -function estimateMetricSizeInBytes(metric: Metric): number { - let weight = 0; - - // Estimate byte size of 2 bytes per character. This is a rough estimate JS strings are stored as UTF-16. - if (metric.name) { - weight += metric.name.length * 2; - } - - // Add weight for the value - weight += 8; // number - - if (metric.attributes) { - Object.values(metric.attributes).forEach(value => { - if (Array.isArray(value)) { - weight += value.length * estimatePrimitiveSizeInBytes(value[0]); - } else if (isPrimitive(value)) { - weight += estimatePrimitiveSizeInBytes(value); - } else { - // For objects values, we estimate the size of the object as 100 bytes - weight += 100; - } - }); - } - - return weight; -} - -/** - * Estimate the size of a log in bytes. - * - * @param log - The log to estimate the size of. - * @returns The estimated size of the log in bytes. - */ -function estimateLogSizeInBytes(log: Log): number { - let weight = 0; - - // Estimate byte size of 2 bytes per character. This is a rough estimate JS strings are stored as UTF-16. - if (log.message) { - weight += log.message.length * 2; - } - - if (log.attributes) { - Object.values(log.attributes).forEach(value => { - if (Array.isArray(value)) { - weight += value.length * estimatePrimitiveSizeInBytes(value[0]); - } else if (isPrimitive(value)) { - weight += estimatePrimitiveSizeInBytes(value); - } else { - // For objects values, we estimate the size of the object as 100 bytes - weight += 100; - } - }); - } - - return weight; -} - -function estimatePrimitiveSizeInBytes(value: Primitive): number { - if (typeof value === 'string') { - return value.length * 2; - } else if (typeof value === 'number') { - return 8; - } else if (typeof value === 'boolean') { - return 4; - } - - return 0; -} - diff --git a/packages/core/test/lib/client.test.ts b/packages/core/test/lib/client.test.ts index ae324aa40f9f..d50d6da911b8 100644 --- a/packages/core/test/lib/client.test.ts +++ b/packages/core/test/lib/client.test.ts @@ -2763,7 +2763,10 @@ describe('Client', () => { // Create large metrics that will exceed the 800KB threshold const largeValue = 'x'.repeat(400_000); // 400KB string - _INTERNAL_captureMetric({ name: 'large_metric', value: largeValue, type: 'counter', attributes: {} }, { scope }); + _INTERNAL_captureMetric( + { name: 'large_metric', value: 1, type: 'counter', attributes: { large_value: largeValue } }, + { scope }, + ); expect(sendEnvelopeSpy).toHaveBeenCalledTimes(1); });