Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/core/src/metrics/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't do this for logs. When can this ever happen?

return;
}

const serializedMetric: SerializedMetric = {
timestamp: timestampInSeconds(),
trace_id: traceId,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/metrics/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
76 changes: 76 additions & 0 deletions packages/core/src/server-runtime-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,79 @@ 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;
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Unused Functions Cause Compilation Errors

The estimateMetricSizeInBytes, estimateLogSizeInBytes, and estimatePrimitiveSizeInBytes functions seem accidentally committed. They are unused, unrelated to the PR's purpose, and cause compilation errors due to undefined types (Metric, Log, Primitive) and the isPrimitive function. Furthermore, their array size estimation logic accesses value[0] without checking for empty arrays and assumes homogeneous element types.

Fix in Cursor Fix in Web

6 changes: 3 additions & 3 deletions packages/core/src/types-hoist/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Metric {
/**
* The value of the metric.
*/
value: number | string;
value: number;

/**
* The type of metric.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
Loading