@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
131 lines (130 loc) • 2.78 kB
JavaScript
import { _INTERNAL_captureMetric } from "./internal.js";
// src/metrics/public-api.ts
/**
* Capture a metric with the given type, name, and value.
*
* @param type - The type of the metric.
* @param name - The name of the metric.
* @param value - The value of the metric.
* @param options - Options for capturing the metric.
*/
function captureMetric(type, name, value, options) {
_INTERNAL_captureMetric({
type,
name,
value,
unit: options?.unit,
attributes: options?.attributes
}, { scope: options?.scope });
}
/**
* @summary Increment a counter metric.
*
* @param name - The name of the counter metric.
* @param value - The value to increment by (defaults to 1).
* @param options - Options for capturing the metric.
*
* @example
*
* ```
* Sentry.metrics.count('api.requests', 1, {
* attributes: {
* endpoint: '/api/users',
* method: 'GET',
* status: 200
* }
* });
* ```
*
* @example With custom value
*
* ```
* Sentry.metrics.count('items.processed', 5, {
* attributes: {
* processor: 'batch-processor',
* queue: 'high-priority'
* }
* });
* ```
*/
function count(name, value = 1, options) {
captureMetric("counter", name, value, options);
}
/**
* @summary Set a gauge metric to a specific value.
*
* @param name - The name of the gauge metric.
* @param value - The current value of the gauge.
* @param options - Options for capturing the metric.
*
* @example
*
* ```
* Sentry.metrics.gauge('memory.usage', 1024, {
* unit: 'megabyte',
* attributes: {
* process: 'web-server',
* region: 'us-east-1'
* }
* });
* ```
*
* @example Without unit
*
* ```
* Sentry.metrics.gauge('active.connections', 42, {
* attributes: {
* server: 'api-1',
* protocol: 'websocket'
* }
* });
* ```
*/
function gauge(name, value, options) {
captureMetric("gauge", name, value, options);
}
/**
* @summary Record a value in a distribution metric.
*
* @param name - The name of the distribution metric.
* @param value - The value to record in the distribution.
* @param options - Options for capturing the metric.
*
* @example
*
* ```
* Sentry.metrics.distribution('task.duration', 500, {
* unit: 'millisecond',
* attributes: {
* task: 'data-processing',
* priority: 'high'
* }
* });
* ```
*
* @example Without unit
*
* ```
* Sentry.metrics.distribution('batch.size', 100, {
* attributes: {
* processor: 'batch-1',
* type: 'async'
* }
* });
* ```
*/
function distribution(name, value, options) {
captureMetric("distribution", name, value, options);
}
/**
* The public metrics API.
*
* A plain object rather than a module namespace so its methods can be spied on in tests.
*/
const metrics = {
count,
gauge,
distribution
};
export { count, distribution, gauge, metrics };
//# sourceMappingURL=public-api.js.map