-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): Add interfaces for metrics (#9698)
Not adding any tests for `createMetricEnvelope` yet because the type for `metricAggregate` is going to change, just added it as boilerplate. In the next PR I'll be implementing a metrics aggregator class which lives on the client. This is responsible for bucketing metrics and incrementally flushing them out. https://develop.sentry.dev/delightful-developer-metrics/ https://develop.sentry.dev/delightful-developer-metrics/sending-metrics-sdk/
- Loading branch information
1 parent
27f6ae1
commit 59db749
Showing
4 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { DsnComponents, DynamicSamplingContext, SdkMetadata, StatsdEnvelope, StatsdItem } from '@sentry/types'; | ||
import { createEnvelope, dropUndefinedKeys, dsnToString } from '@sentry/utils'; | ||
|
||
/** | ||
* Create envelope from a metric aggregate. | ||
*/ | ||
export function createMetricEnvelope( | ||
// TODO(abhi): Add type for this | ||
metricAggregate: string, | ||
dynamicSamplingContext?: Partial<DynamicSamplingContext>, | ||
metadata?: SdkMetadata, | ||
tunnel?: string, | ||
dsn?: DsnComponents, | ||
): StatsdEnvelope { | ||
const headers: StatsdEnvelope[0] = { | ||
sent_at: new Date().toISOString(), | ||
}; | ||
|
||
if (metadata && metadata.sdk) { | ||
headers.sdk = { | ||
name: metadata.sdk.name, | ||
version: metadata.sdk.version, | ||
}; | ||
} | ||
|
||
if (!!tunnel && !!dsn) { | ||
headers.dsn = dsnToString(dsn); | ||
} | ||
|
||
if (dynamicSamplingContext) { | ||
headers.trace = dropUndefinedKeys(dynamicSamplingContext) as DynamicSamplingContext; | ||
} | ||
|
||
const item = createMetricEnvelopeItem(metricAggregate); | ||
return createEnvelope<StatsdEnvelope>(headers, [item]); | ||
} | ||
|
||
function createMetricEnvelopeItem(metricAggregate: string): StatsdItem { | ||
const metricHeaders: StatsdItem[0] = { | ||
type: 'statsd', | ||
}; | ||
return [metricHeaders, metricAggregate]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { MeasurementUnit } from './measurement'; | ||
import type { Primitive } from './misc'; | ||
|
||
export interface BaseMetric { | ||
name: string; | ||
timestamp: number; | ||
unit?: MeasurementUnit; | ||
tags?: { [key: string]: Primitive }; | ||
} | ||
|
||
export interface CounterMetric extends BaseMetric { | ||
value: number; | ||
} | ||
|
||
export interface GaugeMetric extends BaseMetric { | ||
value: number; | ||
first: number; | ||
min: number; | ||
max: number; | ||
sum: number; | ||
count: number; | ||
} | ||
|
||
export interface DistributionMetric extends BaseMetric { | ||
value: number[]; | ||
} | ||
|
||
export interface SetMetric extends BaseMetric { | ||
value: Set<number>; | ||
} | ||
|
||
export type Metric = CounterMetric | GaugeMetric | DistributionMetric | SetMetric; |