Hyperlinkv0.8.0-beta.28

Metric

Metric.CurrentMetricAttributesconsteffect/Metric.ts:1684
Context.Reference<Readonly<Record<string, string>>>

Context reference for metric attributes applied from the current Effect context.

When to use

Use to provide default attributes that should be merged into metric updates and reads in a scoped part of a program.

Details

The default value is an empty attribute set. Metric reads and updates merge these contextual attributes with the metric's own attributes to select the metric series being accessed.

Example (Providing current metric attributes)

import { Data, Effect, Metric } from "effect"

class AttributesError extends Data.TaggedError("AttributesError")<{
  readonly operation: string
}> {}

const program = Effect.gen(function*() {
  // Access current metric attributes
  const attributes = yield* Metric.CurrentMetricAttributes
  console.log("Current attributes:", attributes)

  // Set new attributes context
  const newAttributes = { service: "api", version: "1.0" }
  const result = yield* Effect.provideService(
    Effect.gen(function*() {
      const updatedAttributes = yield* Metric.CurrentMetricAttributes
      return updatedAttributes
    }),
    Metric.CurrentMetricAttributes,
    newAttributes
  )

  return result
})
references
Source effect/Metric.ts:16843 lines
export const CurrentMetricAttributes = Context.Reference<Metric.AttributeSet>(CurrentMetricAttributesKey, {
  defaultValue: () => ({})
})