Skip to content

Commit

Permalink
feat(metrics): support metrics timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePlenkov committed Dec 16, 2024
1 parent 84cce75 commit ae80565
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
28 changes: 24 additions & 4 deletions api/src/metrics/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ export interface Counter<
/**
* Increment value of counter by the input. Inputs must not be negative.
*/
add(value: number, attributes?: AttributesTypes, context?: Context): void;
add(
value: number,
attributes?: AttributesTypes,
context?: Context,
timestamp?: number
): void;
}

/**
Expand All @@ -109,7 +114,12 @@ export interface UpDownCounter<
/**
* Increment value of counter by the input. Inputs may be negative.
*/
add(value: number, attributes?: AttributesTypes, context?: Context): void;
add(
value: number,
attributes?: AttributesTypes,
context?: Context,
timestamp?: number
): void;
}

/**
Expand All @@ -121,7 +131,12 @@ export interface Gauge<
/**
* Records a measurement.
*/
record(value: number, attributes?: AttributesTypes, context?: Context): void;
record(
value: number,
attributes?: AttributesTypes,
context?: Context,
timestamp?: number
): void;
}

/**
Expand All @@ -133,7 +148,12 @@ export interface Histogram<
/**
* Records a measurement. Value of the measurement must not be negative.
*/
record(value: number, attributes?: AttributesTypes, context?: Context): void;
record(
value: number,
attributes?: AttributesTypes,
context?: Context,
timestamp?: number
): void;
}

/**
Expand Down
41 changes: 31 additions & 10 deletions packages/sdk-metrics/src/Instruments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export class SyncInstrument {
protected _record(
value: number,
attributes: Attributes = {},
context: Context = contextApi.active()
context: Context = contextApi.active(),
timestamp: number
) {
if (typeof value !== 'number') {
diag.warn(
Expand All @@ -72,7 +73,7 @@ export class SyncInstrument {
value,
attributes,
context,
millisToHrTime(Date.now())
millisToHrTime(timestamp)
);
}
}
Expand All @@ -87,8 +88,13 @@ export class UpDownCounterInstrument
/**
* Increment value of counter by the input. Inputs may be negative.
*/
add(value: number, attributes?: Attributes, ctx?: Context): void {
this._record(value, attributes, ctx);
add(
value: number,
attributes?: Attributes,
ctx?: Context,
timestamp = Date.now()
): void {
this._record(value, attributes, ctx, timestamp);
}
}

Expand All @@ -99,15 +105,20 @@ export class CounterInstrument extends SyncInstrument implements Counter {
/**
* Increment value of counter by the input. Inputs may not be negative.
*/
add(value: number, attributes?: Attributes, ctx?: Context): void {
add(
value: number,
attributes?: Attributes,
ctx?: Context,
timestamp = Date.now()
): void {
if (value < 0) {
diag.warn(
`negative value provided to counter ${this._descriptor.name}: ${value}`
);
return;
}

this._record(value, attributes, ctx);
this._record(value, attributes, ctx, timestamp);
}
}

Expand All @@ -118,8 +129,13 @@ export class GaugeInstrument extends SyncInstrument implements Gauge {
/**
* Records a measurement.
*/
record(value: number, attributes?: Attributes, ctx?: Context): void {
this._record(value, attributes, ctx);
record(
value: number,
attributes?: Attributes,
ctx?: Context,
timestamp = Date.now()
): void {
this._record(value, attributes, ctx, timestamp);
}
}

Expand All @@ -130,14 +146,19 @@ export class HistogramInstrument extends SyncInstrument implements Histogram {
/**
* Records a measurement. Value of the measurement must not be negative.
*/
record(value: number, attributes?: Attributes, ctx?: Context): void {
record(
value: number,
attributes?: Attributes,
ctx?: Context,
timestamp = Date.now()
): void {
if (value < 0) {
diag.warn(
`negative value provided to histogram ${this._descriptor.name}: ${value}`
);
return;
}
this._record(value, attributes, ctx);
this._record(value, attributes, ctx, timestamp);
}
}

Expand Down

0 comments on commit ae80565

Please sign in to comment.