autotelic / fastify-opentelemetry

A Fastify plugin that utilizes the OpenTelemetry API to provide request tracing.
MIT License
72 stars 12 forks source link

How do you expose OpenTelemetry metrics? #34

Closed 2color closed 3 years ago

2color commented 3 years ago

Thank you @HW13 for the work creating this plugin.

I'm trying to export metrics (request (with status code) and db query counts and request latency) in a project using this plugin.

How would you recommend doing this?

I am still navigating my way through the OpenTelemetry standard.

HW13 commented 3 years ago

Thanks @2color!

Currently this plugin doesn't provide any OpenTelemetry/Metrics functionality.

But you could use @opentelemetry/api-metrics to set a global meter provider:

// metrics.js
// Just like opentelemetry-api this file needs to be loaded before the app.
const { metrics } = require('@opentelemetry/api-metrics')
const { MeterProvider, ConsoleMetricExporter } = require('@opentelemetry/metrics')

const meterProvider = new MeterProvider({
  exporter: new ConsoleMetricExporter(),
  interval: 1000,
});

metrics.setGlobalMeterProvider(meterProvider)

Then use the metrics API to get your meter into a request decorator:

// plugins/dbQueryCounter.js
const { metrics } = require('@opentelemetry/api-metrics')

async function dbQueryCounterPlugin (fastify, opts) {
  const dbQueryCounter = metrics.getMeter('appMetrics').createCounter('dbQueryCounter', {
    description: 'DB Query Counter'
  })
  fastify.decorateRequest('dbQueryCounter', null)
  fastify.addHook('onRequest', async (req) => {
    req.dbQueryCounter = dbQueryCounter
  })
}

Since metrics can be fairly app specific, we haven't attempted to provide any sort of integration via this plugin. But we're definitely open to suggestions.

2color commented 3 years ago

Thank you for the example.

Yeah, I realise that metrics are typically be app-specific.

Coming from Prometheus and entering the world of OpenTelemetry still leaves me wondering whether it's common to alert based on traces.

I had in mind some generic metrics (requests, latency histograms, etc.) that would replicate a lot of the information in the traces but be more suited for alerting. Especially since it's common for sampling to only collect a subset of the traces.

turneand commented 3 years ago

There are some "standard" metrics that could be captured using a fastify plugin including request counts, duration, grouped by status code, buckets, etc. I've used fastify-metrics successfully pre-opentelemetry and they expose a good starting point for the metrics required.

HW13 commented 3 years ago

I had in mind some generic metrics (requests, latency histograms, etc.) that would replicate a lot of the information in the traces but be more suited for alerting. Especially since it's common for sampling to only collect a subset of the traces.

Something like this would definitely be useful, but it should probably be in a separate plugin - similar to how OpenTelemetry has split their metrics API into a separate package. I would really like to make that plugin - just not sure when or if I'll have time to do so.

I'm going to close this issue for now @2color, but I'll be sure to post here if we do get a chance to write an OpenTelemetry metrics plugin.

2color commented 3 years ago

That makes sense @HW13 Thanks for the input!