Skip to content

Commit 4c8c2a0

Browse files
committed
feat(metrics): Add timings method to metrics
1 parent b188e61 commit 4c8c2a0

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

.size-limit.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ module.exports = [
5757
gzip: true,
5858
limit: '87 KB',
5959
},
60+
{
61+
name: '@sentry/browser (incl. Tracing, Replay, Feedback, metrics)',
62+
path: 'packages/browser/build/npm/esm/index.js',
63+
import: createImport('init', 'browserTracingIntegration', 'replayIntegration', 'feedbackIntegration', 'metrics'),
64+
gzip: true,
65+
limit: '100 KB',
66+
},
67+
{
68+
name: '@sentry/browser (incl. metrics)',
69+
path: 'packages/browser/build/npm/esm/index.js',
70+
import: createImport('init', 'metrics'),
71+
gzip: true,
72+
limit: '40 KB',
73+
},
6074
{
6175
name: '@sentry/browser (incl. Feedback)',
6276
path: 'packages/browser/build/npm/esm/index.js',

packages/core/src/metrics/exports.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import type {
22
Client,
3+
DurationUnit,
34
MeasurementUnit,
45
MetricsAggregator as MetricsAggregatorInterface,
56
Primitive,
67
} from '@sentry/types';
7-
import { getGlobalSingleton, logger } from '@sentry/utils';
8+
import { getGlobalSingleton, logger, timestampInSeconds } from '@sentry/utils';
89
import { getClient } from '../currentScopes';
910
import { DEBUG_BUILD } from '../debug-build';
11+
import { startInactiveSpan } from '../tracing';
12+
import { handleCallbackErrors } from '../utils/handleCallbackErrors';
1013
import { getActiveSpan, getRootSpan, spanToJSON } from '../utils/spanUtils';
1114
import { COUNTER_METRIC_TYPE, DISTRIBUTION_METRIC_TYPE, GAUGE_METRIC_TYPE, SET_METRIC_TYPE } from './constants';
1215
import type { MetricType } from './types';
@@ -102,6 +105,50 @@ function distribution(aggregator: MetricsAggregatorConstructor, name: string, va
102105
addToMetricsAggregator(aggregator, DISTRIBUTION_METRIC_TYPE, name, ensureNumber(value), data);
103106
}
104107

108+
function timing(
109+
aggregator: MetricsAggregatorConstructor,
110+
name: string,
111+
value: number,
112+
unit?: DurationUnit,
113+
data?: Omit<MetricData, 'unit'>,
114+
): void;
115+
function timing<T>(
116+
aggregator: MetricsAggregatorConstructor,
117+
name: string,
118+
value: () => T,
119+
unit?: DurationUnit,
120+
data?: Omit<MetricData, 'unit'>,
121+
): T;
122+
function timing<T = void>(
123+
aggregator: MetricsAggregatorConstructor,
124+
name: string,
125+
value: number | (() => T),
126+
unit: DurationUnit = 'second',
127+
data?: Omit<MetricData, 'unit'>,
128+
): T | void {
129+
// callback form
130+
if (typeof value === 'function') {
131+
const startTime = timestampInSeconds();
132+
const span = startInactiveSpan({ op: 'metrics.timing', name, startTime });
133+
134+
return handleCallbackErrors(
135+
() => value(),
136+
() => {
137+
// no special error handling necessary
138+
},
139+
() => {
140+
const endTime = timestampInSeconds();
141+
const timeDiff = endTime - startTime;
142+
distribution(aggregator, name, timeDiff, { ...data, unit });
143+
span.end(endTime);
144+
},
145+
);
146+
}
147+
148+
// value form
149+
distribution(aggregator, name, value, { ...data, unit });
150+
}
151+
105152
/**
106153
* Adds a value to a set metric. Value must be a string or integer.
107154
*
@@ -125,6 +172,7 @@ export const metrics = {
125172
distribution,
126173
set,
127174
gauge,
175+
timing,
128176
/**
129177
* @ignore This is for internal use only.
130178
*/

0 commit comments

Comments
 (0)