|
| 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 { COUNTER_METRIC_TYPE, DISTRIBUTION_METRIC_TYPE, GAUGE_METRIC_TYPE, SET_METRIC_TYPE } from './constants'; |
| 7 | +import { MetricsAggregator } from './integration'; |
| 8 | +import type { MetricType } from './types'; |
| 9 | + |
| 10 | +interface MetricData { |
| 11 | + unit?: MeasurementUnit; |
| 12 | + tags?: Record<string, Primitive>; |
| 13 | + timestamp?: number; |
| 14 | +} |
| 15 | + |
| 16 | +function addToMetricsAggregator( |
| 17 | + metricType: MetricType, |
| 18 | + name: string, |
| 19 | + value: number | string, |
| 20 | + data: MetricData = {}, |
| 21 | +): void { |
| 22 | + const hub = getCurrentHub(); |
| 23 | + const client = hub.getClient() as BaseClient<ClientOptions>; |
| 24 | + const scope = hub.getScope(); |
| 25 | + if (client) { |
| 26 | + if (!client.metricsAggregator) { |
| 27 | + DEBUG_BUILD && |
| 28 | + logger.warn('No metrics aggregator enabled. Please add the MetricsAggregator integration to use metrics APIs'); |
| 29 | + return; |
| 30 | + } |
| 31 | + const { unit, tags, timestamp } = data; |
| 32 | + const { release, environment } = client.getOptions(); |
| 33 | + const transaction = scope.getTransaction(); |
| 34 | + const metricTags: Record<string, string> = {}; |
| 35 | + if (release) { |
| 36 | + metricTags.release = release; |
| 37 | + } |
| 38 | + if (environment) { |
| 39 | + metricTags.environment = environment; |
| 40 | + } |
| 41 | + if (transaction) { |
| 42 | + metricTags.transaction = transaction.name; |
| 43 | + } |
| 44 | + |
| 45 | + DEBUG_BUILD && logger.log(`Adding value of ${value} to ${metricType} metric ${name}`); |
| 46 | + client.metricsAggregator.add(metricType, name, value, unit, { ...metricTags, ...tags }, timestamp); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Adds a value to a counter metric |
| 52 | + * |
| 53 | + * @experimental This API is experimental and might having breaking changes in the future. |
| 54 | + */ |
| 55 | +export function increment(name: string, value: number = 1, data?: MetricData): void { |
| 56 | + addToMetricsAggregator(COUNTER_METRIC_TYPE, name, value, data); |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Adds a value to a distribution metric |
| 61 | + * |
| 62 | + * @experimental This API is experimental and might having breaking changes in the future. |
| 63 | + */ |
| 64 | +export function distribution(name: string, value: number, data?: MetricData): void { |
| 65 | + addToMetricsAggregator(DISTRIBUTION_METRIC_TYPE, name, value, data); |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Adds a value to a set metric. Value must be a string or integer. |
| 70 | + * |
| 71 | + * @experimental This API is experimental and might having breaking changes in the future. |
| 72 | + */ |
| 73 | +export function set(name: string, value: number | string, data?: MetricData): void { |
| 74 | + addToMetricsAggregator(SET_METRIC_TYPE, name, value, data); |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Adds a value to a gauge metric |
| 79 | + * |
| 80 | + * @experimental This API is experimental and might having breaking changes in the future. |
| 81 | + */ |
| 82 | +export function gauge(name: string, value: number, data?: MetricData): void { |
| 83 | + addToMetricsAggregator(GAUGE_METRIC_TYPE, name, value, data); |
| 84 | +} |
| 85 | + |
| 86 | +export const metrics = { |
| 87 | + increment, |
| 88 | + distribution, |
| 89 | + set, |
| 90 | + gauge, |
| 91 | + MetricsAggregator, |
| 92 | +}; |
0 commit comments