7070 */
7171
7272import { AsyncLocalStorage } from 'node:async_hooks' ;
73- import { Logger , Duration , LogLevel , LogMetadata , Priority } from '@temporalio/common' ;
73+ import { Logger , Duration , LogLevel , LogMetadata , MetricMeter , Priority } from '@temporalio/common' ;
7474import { msToNumber } from '@temporalio/common/lib/time' ;
7575import { SymbolBasedInstanceOfError } from '@temporalio/common/lib/type-helpers' ;
7676
@@ -281,6 +281,14 @@ export class Context {
281281 */
282282 public log : Logger ;
283283
284+ /**
285+ * Get the metric meter for this activity with activity-specific tags.
286+ *
287+ * To add custom tags, register a {@link ActivityOutboundCallsInterceptor} that
288+ * intercepts the `getMetricTags()` method.
289+ */
290+ public readonly metricMeter : MetricMeter ;
291+
284292 /**
285293 * **Not** meant to instantiated by Activity code, used by the worker.
286294 *
@@ -291,13 +299,15 @@ export class Context {
291299 cancelled : Promise < never > ,
292300 cancellationSignal : AbortSignal ,
293301 heartbeat : ( details ?: any ) => void ,
294- log : Logger
302+ log : Logger ,
303+ metricMeter : MetricMeter
295304 ) {
296305 this . info = info ;
297306 this . cancelled = cancelled ;
298307 this . cancellationSignal = cancellationSignal ;
299308 this . heartbeatFn = heartbeat ;
300309 this . log = log ;
310+ this . metricMeter = metricMeter ;
301311 }
302312
303313 /**
@@ -434,3 +444,26 @@ export function cancelled(): Promise<never> {
434444export function cancellationSignal ( ) : AbortSignal {
435445 return Context . current ( ) . cancellationSignal ;
436446}
447+
448+ /**
449+ * Get the metric meter for the current activity, with activity-specific tags.
450+ *
451+ * To add custom tags, register a {@link ActivityOutboundCallsInterceptor} that
452+ * intercepts the `getMetricTags()` method.
453+ *
454+ * This is a shortcut for `Context.current().metricMeter` (see {@link Context.metricMeter}).
455+ */
456+ export const metricMeter : MetricMeter = {
457+ createCounter ( name , unit , description ) {
458+ return Context . current ( ) . metricMeter . createCounter ( name , unit , description ) ;
459+ } ,
460+ createHistogram ( name , valueType = 'int' , unit , description ) {
461+ return Context . current ( ) . metricMeter . createHistogram ( name , valueType , unit , description ) ;
462+ } ,
463+ createGauge ( name , valueType = 'int' , unit , description ) {
464+ return Context . current ( ) . metricMeter . createGauge ( name , valueType , unit , description ) ;
465+ } ,
466+ withTags ( tags ) {
467+ return Context . current ( ) . metricMeter . withTags ( tags ) ;
468+ } ,
469+ } ;
0 commit comments