JSPM

@well-known-components/metrics

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2742
  • Score
    100M100P100Q118612F
  • License Apache-2.0

metrics component

Package Exports

  • @well-known-components/metrics

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@well-known-components/metrics) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

metrics

npm i @well-known-components/metrics

Define your metrics in a static file

// src/metrics.ts

import { IMetricsComponent } from "@well-known-components/interfaces"
import { validateMetricsDeclaration } from "@well-known-components/metrics"

export const metricDeclarations = {
  // IMetricsComponent.SummaryType
  // IMetricsComponent.HistogramType
  // IMetricsComponent.GaugeType
  // IMetricsComponent.CounterType
  user_counter: {
    type: IMetricsComponent.CounterType,
    help: "Count calls to /user/:userId",
    labelNames: ["userId"],
  }
}

// type assertions
validateMetricsDeclaration(metricDeclarations)

Define the component

// src/components.ts

import { metricDeclarations } from "./metrics"

export async function initComponents(): Promise<AppComponents> {
  ...
  // const config
  // const server
  const metrics = await createMetricsComponent(metricDeclarations, { server, config })

  return { ...components, metrics }
}

Register metrics

export async function userIdHandler(context: {
  components: Pick<AppComponents, "metrics">
}) {
  const { components: { metrics } } = context

  metrics.increment("user_counter", { userId: Math.random() })
  // metrics.decrement("user_counter", { userId: Math.random() })
  // metrics.observe("user_counter", { userId: Math.random() }, 1)
  // metrics.reset("user_counter")

  return {}
}