nocobase/packages/core/telemetry/src/telemetry.ts
jack zhang 62b2b5c68b
chore: add copyright information to the file header (#4028)
* fix: add license code

* fix: bug

* fix: bug

* fix: upgrade

* fix: improve

* chore: add copyright information to the file header

* fix: d.ts bug

* fix: bug

* fix: e2e bug

* fix: merge main

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
2024-04-30 15:51:31 +08:00

72 lines
2.0 KiB
TypeScript

/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
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);
}
}