effect-io-ai

Package: effect
Module: Metric

Metric.Metric.Attributes

Union type for metric attributes that can be provided as either an object or array of tuples.

Example (Providing attributes in different formats)

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

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

const program = Effect.gen(function*() {
  // Different ways to specify attributes
  const attributesAsObject = {
    service: "api",
    environment: "production",
    version: "1.2.3"
  }

  const attributesAsArray: ReadonlyArray<[string, string]> = [
    ["service", "api"],
    ["environment", "production"],
    ["version", "1.2.3"]
  ]

  // Create metrics with different attribute formats
  const requestCounter1 = Metric.counter("requests", {
    description: "Total requests",
    attributes: attributesAsObject // Using object format
  })

  const requestCounter2 = Metric.counter("requests", {
    description: "Total requests",
    attributes: attributesAsArray // Using array format
  })

  // Function to normalize attributes to object format
  const normalizeAttributes = (
    attrs: typeof attributesAsObject | ReadonlyArray<[string, string]>
  ) => {
    if (Array.isArray(attrs)) {
      return Object.fromEntries(attrs)
    }
    return attrs
  }

  // Add runtime attributes using withAttributes
  const contextualCounter = Metric.withAttributes(requestCounter1, {
    method: "GET",
    endpoint: "/api/users"
  })

  // Update metrics with different attribute combinations
  yield* Metric.update(contextualCounter, 1)

  // Both formats result in the same internal representation
  const normalizedObject = normalizeAttributes(attributesAsObject)
  const normalizedArray = normalizeAttributes(attributesAsArray)

  return {
    attributeFormats: {
      object: normalizedObject, // { service: "api", environment: "production", version: "1.2.3" }
      array: normalizedArray, // { service: "api", environment: "production", version: "1.2.3" }
      areEqual:
        JSON.stringify(normalizedObject) === JSON.stringify(normalizedArray) // true
    }
  }
})

Signature

type Attributes = AttributeSet | ReadonlyArray<[string, string]>

Source

Since v4.0.0