Effect CommunityEC
Effect Community2y ago
41 replies
phischer

Ingest Metrics in local Prometheus/Grafana

With the tutorial on the effect.website I got traces to work on my local tempo/prometheus/grafana docker. Very cool! I tried to get the metrics to work with the same docker containers, but can't get it to work. I must say, that I'm a complete beginner and never used any of these tools before effect. My changes after the traces tutorial are the following:

// test-metrics.ts
import { Metric, Effect, Console, Layer } from 'effect'
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http'
import { NodeSdk } from '@effect/opentelemetry'
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics'

const NodeSdkLive = Layer.unwrapEffect(
  Effect.gen(function* ($) {
    const metricExporter = new OTLPMetricExporter({
      url: 'http://127.0.0.1:9090/api/v1/otlp/v1/metrics',
    })

    return NodeSdk.layer(() => ({
      resource: {
        serviceName: 'nextjs',
      },
      metricReader: new PeriodicExportingMetricReader({
        exporter: metricExporter,
        exportIntervalMillis: 1000,
      }),
    }))
  }),
)

// Create a counter named 'task_count' and increment it by 1 every time it's invoked
const taskCount = Metric.counter('task_count').pipe(Metric.withConstantInput(1))

const task1 = Effect.succeed(1).pipe(Effect.delay('100 millis'))
const task2 = Effect.succeed(2).pipe(Effect.delay('200 millis'))

const program = Effect.gen(function* (_) {
  const a = yield* _(taskCount(task1))
  const b = yield* _(taskCount(task2))
  return a + b
})

// const showMetric = Metric.value(taskCount).pipe(Effect.flatMap(Console.log))

Effect.runPromise(program.pipe(Effect.provide(NodeSdkLive))).then(console.log)
Was this page helpful?