nocobase/packages/core/telemetry/src/telemetry.ts
YANG QIA 0dbc01c330
feat: telemetry (#3279)
* feat: telemetry

* fix: build

* chore: update

* refactor: improve api

* fix: test

* fix: version

* fix: build

* feat: support for adding views

* fix: typo

* fix: version

* chore: update

* chore(env): `true` -> `on`

* fix: metric version
2024-01-02 12:17:46 +08:00

63 lines
1.7 KiB
TypeScript

import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { Resource } from '@opentelemetry/resources';
import { InstrumentationOption, registerInstrumentations } from '@opentelemetry/instrumentation';
import { Metric, MetricOptions } from './metric';
import { Trace, TraceOptions } from './trace';
export interface TelemetryOptions {
serviceName?: string;
version?: string;
trace?: TraceOptions;
metric?: MetricOptions;
}
export class Telemetry {
serviceName: string;
version: string;
instrumentations: InstrumentationOption[] = [];
trace: Trace;
metric: Metric;
started = false;
constructor(options?: TelemetryOptions) {
const { trace, metric, serviceName, version } = options || {};
this.trace = new Trace({ tracerName: `${serviceName}-trace`, version, ...trace });
this.metric = new Metric({ meterName: `${serviceName}-meter`, version, ...metric });
this.serviceName = serviceName || 'nocobase';
this.version = version || '';
}
init() {
registerInstrumentations({
instrumentations: this.instrumentations,
});
const resource = Resource.default().merge(
new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: this.serviceName,
[SemanticResourceAttributes.SERVICE_VERSION]: this.version,
}),
);
this.trace.init(resource);
this.metric.init(resource);
}
start() {
if (!this.started) {
this.trace.start();
this.metric.start();
}
this.started = true;
}
async shutdown() {
await Promise.all([this.trace.shutdown(), this.metric.shutdown()]);
this.started = false;
}
addInstrumentation(...instrumentation: InstrumentationOption[]) {
this.instrumentations.push(...instrumentation);
}
}