|
| 1 | +import type { ClientOptions, MeasurementUnit, Primitive } from '@sentry/types'; |
| 2 | +import { logger } from '@sentry/utils'; |
| 3 | +import type { BaseClient } from '../baseclient'; |
| 4 | +import { DEBUG_BUILD } from '../debug-build'; |
| 5 | +import { getCurrentHub } from '../hub'; |
| 6 | +import type { MetricType } from './types'; |
| 7 | + |
| 8 | +interface MetricData { |
| 9 | + unit?: MeasurementUnit; |
| 10 | + tags?: { [key: string]: Primitive }; |
| 11 | + timestamp?: number; |
| 12 | +} |
| 13 | + |
| 14 | +function addToMetricsAggregator(metricType: MetricType, name: string, value: number, data: MetricData = {}): void { |
| 15 | + const hub = getCurrentHub(); |
| 16 | + const client = hub.getClient() as BaseClient<ClientOptions>; |
| 17 | + const scope = hub.getScope(); |
| 18 | + if (client) { |
| 19 | + if (!client.metricsAggregator) { |
| 20 | + DEBUG_BUILD && |
| 21 | + logger.warn('No metrics aggregator enabled. Please add the Metrics integration to use metrics APIs'); |
| 22 | + return; |
| 23 | + } |
| 24 | + const { unit, tags, timestamp } = data; |
| 25 | + const { release, environment } = client.getOptions(); |
| 26 | + const transaction = scope.getTransaction(); |
| 27 | + const metricTags = { |
| 28 | + ...tags, |
| 29 | + }; |
| 30 | + if (release) { |
| 31 | + metricTags.release = release; |
| 32 | + } |
| 33 | + if (environment) { |
| 34 | + metricTags.environment = environment; |
| 35 | + } |
| 36 | + if (transaction) { |
| 37 | + metricTags.transaction = transaction.name; |
| 38 | + } |
| 39 | + |
| 40 | + DEBUG_BUILD && logger.log(`Adding value of ${value} to ${metricType} metric ${name}`); |
| 41 | + client.metricsAggregator.add(metricType, name, value, unit, metricTags, timestamp); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Adds a value to a counter metric |
| 47 | + * |
| 48 | + * @experimental This API is experimental and might having breaking changes in the future. |
| 49 | + */ |
| 50 | +export function incr(name: string, value: number, data?: MetricData): void { |
| 51 | + addToMetricsAggregator('c', name, value, data); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Adds a value to a distribution metric |
| 56 | + * |
| 57 | + * @experimental This API is experimental and might having breaking changes in the future. |
| 58 | + */ |
| 59 | +export function distribution(name: string, value: number, data?: MetricData): void { |
| 60 | + addToMetricsAggregator('d', name, value, data); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Adds a value to a set metric |
| 65 | + * |
| 66 | + * @experimental This API is experimental and might having breaking changes in the future. |
| 67 | + */ |
| 68 | +export function set(name: string, value: number, data?: MetricData): void { |
| 69 | + addToMetricsAggregator('s', name, value, data); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Adds a value to a gauge metric |
| 74 | + * |
| 75 | + * @experimental This API is experimental and might having breaking changes in the future. |
| 76 | + */ |
| 77 | +export function gauge(name: string, value: number, data?: MetricData): void { |
| 78 | + addToMetricsAggregator('s', name, value, data); |
| 79 | +} |
0 commit comments