2.1.0 • Published 9 months ago

@well-known-components/metrics v2.1.0

Weekly downloads
21
License
Apache-2.0
Repository
github
Last release
9 months ago

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 {}
}
2.1.0

9 months ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago