@cerbos/opentelemetry
Version:
OpenTelemetry instrumentation for the @cerbos/grpc and @cerbos/http client libraries
98 lines • 3.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CerbosInstrumentation = void 0;
const api_1 = require("@opentelemetry/api");
const core_1 = require("@cerbos/core");
const instruments_1 = require("./instruments");
const metadata_1 = require("./metadata");
const transport_1 = require("./transport");
/**
* OpenTelemetry instrumentation for Cerbos clients.
*
* @example
* ```typescript
* import { CerbosInstrumentation } from "@cerbos/opentelemetry";
* import { registerInstrumentations } from "@opentelemetry/instrumentation";
*
* registerInstrumentations({
* instrumentations: [...yourOtherInstrumentations, new CerbosInstrumentation()],
* });
* ```
*
* @public
*/
class CerbosInstrumentation {
/**
* Name of the instrumentation.
*/
instrumentationName = metadata_1.name;
/**
* Version of the instrumentation.
*/
instrumentationVersion = metadata_1.version;
/** @internal */
_tracer;
diag;
instrumenter;
config;
instruments;
constructor(config = {}) {
this.diag = api_1.diag.createComponentLogger({
namespace: metadata_1.name,
});
this.config = { enabled: true, ...config };
this._tracer = api_1.trace.getTracer(metadata_1.name, metadata_1.version);
this.instrumenter = (transport) => new transport_1.Transport(this, transport);
if (this.config.enabled) {
this.enable();
}
}
/**
* Gets the instrumentation configuration.
*/
getConfig() {
return this.config;
}
/**
* Sets the instrumentation configuration.
*
* @remarks
* Changing `enabled` via this method has no effect.
* Use the {@link CerbosInstrumentation.disable} and {@link CerbosInstrumentation.enable} methods instead.
*/
setConfig(config) {
this.config = config;
}
/**
* Override the meter provider, which otherwise defaults to the global meter provider.
*/
setMeterProvider(meterProvider) {
this.instruments = new instruments_1.Instruments(meterProvider);
}
/**
* Override the tracer provider, which otherwise defaults to the global tracer provider.
*/
setTracerProvider(tracerProvider) {
this._tracer = tracerProvider.getTracer(metadata_1.name, metadata_1.version);
}
/**
* Enables the instrumentation.
*/
enable() {
this.diag.debug("Enabling Cerbos client instrumentation");
(0, core_1._addInstrumenter)(this.instrumenter);
}
/**
* Disables the instrumentation.
*/
disable() {
this.diag.debug("Disabling Cerbos client instrumentation");
(0, core_1._removeInstrumenter)(this.instrumenter);
}
/** @internal */
get _instruments() {
return (this.instruments ??= new instruments_1.Instruments(api_1.metrics));
}
}
exports.CerbosInstrumentation = CerbosInstrumentation;
//# sourceMappingURL=instrumentation.js.map