Is it safe to create a new metric on every request? #3616
-
BackgroundThe JS metrics instrumentation docs say the following:
I'm definitely encountering these "trickier application load issues" in my application. I originally created metrics at the top-level scope of my modules. For instance, a small Express router that I might use in my application: import express from 'express';
import { metrics } from "@opentelemetry/api";
const meter = metrics.getMeter('my-service');
const count = meter.createCounter('events');
const router = Router();
router.get('/', (req, res, next) => {
count.add(1);
res.send(204);
});
export default router; However, due to the way the meter etc. are configured (I need to asynchronously load config to know if it's enabled and which exporter to use, and I also need to asynchronously detect resource attributes for the QuestionIs it safe to change the above code to the following? import express from 'express';
import { metrics } from "@opentelemetry/api";
const router = Router();
router.get('/', (req, res, next) => {
const meter = metrics.getMeter('my-service');
const count = meter.createCounter('events');
count.add(1);
res.send(204);
});
export default router; This would match my reading of "call If this isn't considered a best practice, I'd love to hear recommendations about how to handle a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Meters and metric instruments are safe to be retrieved multiple times with identical configurations (names, types, etc.). However, it is still recommended to keep a reference to their instances as de-duplication comes with performance penalties. |
Beta Was this translation helpful? Give feedback.
Meters and metric instruments are safe to be retrieved multiple times with identical configurations (names, types, etc.). However, it is still recommended to keep a reference to their instances as de-duplication comes with performance penalties.