mirror of
https://github.com/nocobase/nocobase
synced 2024-11-16 21:18:12 +00:00
62b2b5c68b
* 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>
72 lines
2.0 KiB
TypeScript
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);
|
|
}
|
|
}
|