Skip to content
Merged
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
43 changes: 43 additions & 0 deletions packages/core/src/metrics/index.ts
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];
}
8 changes: 7 additions & 1 deletion packages/types/src/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,11 @@ export type ReplayEnvelope = [ReplayEnvelopeHeaders, [ReplayEventItem, ReplayRec
export type CheckInEnvelope = BaseEnvelope<CheckInEnvelopeHeaders, CheckInItem>;
export type StatsdEnvelope = BaseEnvelope<StatsdEnvelopeHeaders, StatsdItem>;

export type Envelope = EventEnvelope | SessionEnvelope | ClientReportEnvelope | ReplayEnvelope | CheckInEnvelope;
export type Envelope =
| EventEnvelope
| SessionEnvelope
| ClientReportEnvelope
| ReplayEnvelope
| CheckInEnvelope
| StatsdEnvelope;
export type EnvelopeItem = Envelope[1][number];
3 changes: 3 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export type {
UserFeedbackItem,
CheckInItem,
CheckInEnvelope,
StatsdItem,
StatsdEnvelope,
} from './envelope';
export type { ExtendedError } from './error';
export type { Event, EventHint, EventType, ErrorEvent, TransactionEvent } from './event';
Expand Down Expand Up @@ -136,3 +138,4 @@ export type {

export type { BrowserClientReplayOptions, BrowserClientProfilingOptions } from './browseroptions';
export type { CheckIn, MonitorConfig, FinishedCheckIn, InProgressCheckIn, SerializedCheckIn } from './checkin';
export type { Metric, CounterMetric, GaugeMetric, DistributionMetric, SetMetric } from './metrics';
32 changes: 32 additions & 0 deletions packages/types/src/metrics.ts
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;