I'm working with the OpenTelemetry SDK in typescript.
I want to have a simple class like this:
import { MeterProvider } from "@opentelemetry/sdk-metrics"
class MyClass {
private meter : Meter | undefined; // <--- cannot specify this type
private startup(provider : MeterProvider) {
this.meter = provider.getMeter("my-meter");
}
}
I also want to have some functions, external to the class, which would be defined as follows:
doStuffWithMeter(meter : Meter) { // <--- same here
meter.doThings() // ...
}
The problem is, as far as I can tell, the otel SDK does not export the Meter
type, so I cannot use it to specify the type of the meter
member/argument. Is there any way I can get my hands on this type and/or any workaround to this?
Since the getMeter()
method of a MeterProvider
is not generic and returns the desired unexported Meter
type, you can just use the ReturnType
utility type to make a type alias:
type Meter = ReturnType<MeterProvider["getMeter"]>
// type Meter = Meter
(In case it's confusing: when you use IntelliSense to view it it looks like a circular definition: type Meter = Meter
. But the type named Meter
on the right of the equals sign is the anonymous unexported Meter
from the module, while the one on the left is the type alias you're assigning it to.)