From 90ce46221bd5d052a0805fb4165253dc2de87b42 Mon Sep 17 00:00:00 2001 From: TomyJan Date: Fri, 2 Aug 2024 15:59:52 +0800 Subject: [PATCH] refactor(telemetry): do not init telemetry if disabled (#1437) Reviewed-on: https://git.daoyoucloud.com/daoyoucloud/tachybase/pulls/1437 Reviewed-by: sealday Co-authored-by: TomyJan Co-committed-by: TomyJan --- packages/core/telemetry/src/metric.ts | 13 +++++---- packages/core/telemetry/src/telemetry.ts | 36 ++++++++++++------------ packages/core/telemetry/src/trace.ts | 17 +++++------ 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/packages/core/telemetry/src/metric.ts b/packages/core/telemetry/src/metric.ts index 3b8b719c8..cee5d8cf2 100644 --- a/packages/core/telemetry/src/metric.ts +++ b/packages/core/telemetry/src/metric.ts @@ -17,6 +17,7 @@ export type MetricOptions = { meterName?: string; version?: string; readerName?: string | string[]; + resource?: Resource; }; type GetMetricReader = () => MetricReader; @@ -30,10 +31,15 @@ export class Metric { views: View[] = []; constructor(options?: MetricOptions) { - const { meterName, readerName, version } = options || {}; + const { meterName, readerName, version, resource } = options || {}; this.readerName = readerName || 'console'; this.meterName = meterName || 'tachybase-meter'; this.version = version || ''; + this.provider = new MeterProvider({ resource, views: this.views }); + opentelemetry.metrics.setGlobalMeterProvider(this.provider); + } + + init() { this.registerReader( 'console', () => @@ -43,11 +49,6 @@ export class Metric { ); } - init(resource: Resource) { - this.provider = new MeterProvider({ resource, views: this.views }); - opentelemetry.metrics.setGlobalMeterProvider(this.provider); - } - registerReader(name: string, reader: GetMetricReader) { console.log('register metric reader:', name); this.readers.register(name, reader); diff --git a/packages/core/telemetry/src/telemetry.ts b/packages/core/telemetry/src/telemetry.ts index 535894b1f..459eb300c 100644 --- a/packages/core/telemetry/src/telemetry.ts +++ b/packages/core/telemetry/src/telemetry.ts @@ -4,8 +4,7 @@ import { InstrumentationOption, registerInstrumentations } from '@opentelemetry/ import { Resource } from '@opentelemetry/resources'; import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; -// 防止加载什么奇怪的东西 -import packageJson from '../../../../package.json'; +import packageJson from '../../../../package.json'; // 防止加载什么奇怪的东西 import { telemetryOptions } from './config'; import { Metric, MetricOptions } from './metric'; import { Trace, TraceOptions } from './trace'; @@ -34,10 +33,20 @@ export class Telemetry { constructor(options?: TelemetryOptions) { console.log('Create telemetry with options', options); 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 || 'tachybase-main'; this.version = version || ''; + const resource = Resource.default().merge( + new Resource({ + [SemanticResourceAttributes.SERVICE_NAME]: this.serviceName, + [SemanticResourceAttributes.SERVICE_VERSION]: this.version, + }), + ); + this.trace = new Trace({ tracerName: `${serviceName}-trace`, version, ...trace, resource }); + this.metric = new Metric({ meterName: `${serviceName}-meter`, version, ...metric, resource }); + } + + init() { + console.log('Start init telemetry', this.serviceName, this.version); // 设置 OTel 日志等级 const diagLogLevel = process.env.OTEL_LOG_LEVEL; @@ -59,20 +68,9 @@ export class Telemetry { registerInstrumentations({ instrumentations: this.instrumentations, }); - } - init() { - console.log('Start init telemetry', this.serviceName, this.version); - - 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); + this.trace.init(); + this.metric.init(); } start() { @@ -108,7 +106,9 @@ export const getTelemetry = () => { version: packageJson.version || 'UnkVer', ...telemetryOptions, }); - _telemetry.init(); + if (telemetryOptions.enabled) { + _telemetry.init(); + } } // 这里只需要 init,start 保留在 application 里 diff --git a/packages/core/telemetry/src/trace.ts b/packages/core/telemetry/src/trace.ts index 8d7c8d6c7..0b9df8236 100644 --- a/packages/core/telemetry/src/trace.ts +++ b/packages/core/telemetry/src/trace.ts @@ -9,6 +9,7 @@ export type TraceOptions = { tracerName?: string; version?: string; processorName?: string | string[]; + resource?: Resource; }; type GetSpanProcessor = () => SpanProcessor; @@ -21,10 +22,17 @@ export class Trace { provider: NodeTracerProvider; constructor(options?: TraceOptions) { - const { processorName, tracerName, version } = options || {}; + const { processorName, tracerName, version, resource } = options || {}; this.processorName = processorName || 'console'; this.tracerName = tracerName || 'tachybase-trace'; this.version = version || ''; + this.provider = new NodeTracerProvider({ + resource, + }); + this.provider.register(); + } + + init() { this.registerProcessor('console', () => new BatchSpanProcessor(new ConsoleSpanExporter())); // 初始化 OTLP 作为链路追踪 Processor const newOtlpExporter = new OTLPTraceExporter({ @@ -35,13 +43,6 @@ export class Trace { this.registerProcessor('otlp', () => new BatchSpanProcessor(newOtlpExporter)); } - init(resource: Resource) { - this.provider = new NodeTracerProvider({ - resource, - }); - this.provider.register(); - } - registerProcessor(name: string, processor: GetSpanProcessor) { console.log('register trace processor:', name); this.processors.register(name, processor);