refactor(telemetry): do not init telemetry if disabled (#1437)
Reviewed-on: daoyoucloud/tachybase#1437 Reviewed-by: sealday <zhanglin@daoyoucloud.com> Co-authored-by: TomyJan <TomyJan6@gmail.com> Co-committed-by: TomyJan <TomyJan6@gmail.com>
This commit is contained in:
parent
d5a0f5640c
commit
90ce46221b
@ -17,6 +17,7 @@ export type MetricOptions = {
|
|||||||
meterName?: string;
|
meterName?: string;
|
||||||
version?: string;
|
version?: string;
|
||||||
readerName?: string | string[];
|
readerName?: string | string[];
|
||||||
|
resource?: Resource;
|
||||||
};
|
};
|
||||||
|
|
||||||
type GetMetricReader = () => MetricReader;
|
type GetMetricReader = () => MetricReader;
|
||||||
@ -30,10 +31,15 @@ export class Metric {
|
|||||||
views: View[] = [];
|
views: View[] = [];
|
||||||
|
|
||||||
constructor(options?: MetricOptions) {
|
constructor(options?: MetricOptions) {
|
||||||
const { meterName, readerName, version } = options || {};
|
const { meterName, readerName, version, resource } = options || {};
|
||||||
this.readerName = readerName || 'console';
|
this.readerName = readerName || 'console';
|
||||||
this.meterName = meterName || 'tachybase-meter';
|
this.meterName = meterName || 'tachybase-meter';
|
||||||
this.version = version || '';
|
this.version = version || '';
|
||||||
|
this.provider = new MeterProvider({ resource, views: this.views });
|
||||||
|
opentelemetry.metrics.setGlobalMeterProvider(this.provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
init() {
|
||||||
this.registerReader(
|
this.registerReader(
|
||||||
'console',
|
'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) {
|
registerReader(name: string, reader: GetMetricReader) {
|
||||||
console.log('register metric reader:', name);
|
console.log('register metric reader:', name);
|
||||||
this.readers.register(name, reader);
|
this.readers.register(name, reader);
|
||||||
|
@ -4,8 +4,7 @@ import { InstrumentationOption, registerInstrumentations } from '@opentelemetry/
|
|||||||
import { Resource } from '@opentelemetry/resources';
|
import { Resource } from '@opentelemetry/resources';
|
||||||
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
|
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
|
||||||
|
|
||||||
// 防止加载什么奇怪的东西
|
import packageJson from '../../../../package.json'; // 防止加载什么奇怪的东西
|
||||||
import packageJson from '../../../../package.json';
|
|
||||||
import { telemetryOptions } from './config';
|
import { telemetryOptions } from './config';
|
||||||
import { Metric, MetricOptions } from './metric';
|
import { Metric, MetricOptions } from './metric';
|
||||||
import { Trace, TraceOptions } from './trace';
|
import { Trace, TraceOptions } from './trace';
|
||||||
@ -34,10 +33,20 @@ export class Telemetry {
|
|||||||
constructor(options?: TelemetryOptions) {
|
constructor(options?: TelemetryOptions) {
|
||||||
console.log('Create telemetry with options', options);
|
console.log('Create telemetry with options', options);
|
||||||
const { trace, metric, serviceName, version } = 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.serviceName = serviceName || 'tachybase-main';
|
||||||
this.version = version || '';
|
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 日志等级
|
// 设置 OTel 日志等级
|
||||||
const diagLogLevel = process.env.OTEL_LOG_LEVEL;
|
const diagLogLevel = process.env.OTEL_LOG_LEVEL;
|
||||||
@ -59,20 +68,9 @@ export class Telemetry {
|
|||||||
registerInstrumentations({
|
registerInstrumentations({
|
||||||
instrumentations: this.instrumentations,
|
instrumentations: this.instrumentations,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
init() {
|
this.trace.init();
|
||||||
console.log('Start init telemetry', this.serviceName, this.version);
|
this.metric.init();
|
||||||
|
|
||||||
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() {
|
start() {
|
||||||
@ -108,7 +106,9 @@ export const getTelemetry = () => {
|
|||||||
version: packageJson.version || 'UnkVer',
|
version: packageJson.version || 'UnkVer',
|
||||||
...telemetryOptions,
|
...telemetryOptions,
|
||||||
});
|
});
|
||||||
_telemetry.init();
|
if (telemetryOptions.enabled) {
|
||||||
|
_telemetry.init();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// 这里只需要 init,start 保留在 application 里
|
// 这里只需要 init,start 保留在 application 里
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ export type TraceOptions = {
|
|||||||
tracerName?: string;
|
tracerName?: string;
|
||||||
version?: string;
|
version?: string;
|
||||||
processorName?: string | string[];
|
processorName?: string | string[];
|
||||||
|
resource?: Resource;
|
||||||
};
|
};
|
||||||
|
|
||||||
type GetSpanProcessor = () => SpanProcessor;
|
type GetSpanProcessor = () => SpanProcessor;
|
||||||
@ -21,10 +22,17 @@ export class Trace {
|
|||||||
provider: NodeTracerProvider;
|
provider: NodeTracerProvider;
|
||||||
|
|
||||||
constructor(options?: TraceOptions) {
|
constructor(options?: TraceOptions) {
|
||||||
const { processorName, tracerName, version } = options || {};
|
const { processorName, tracerName, version, resource } = options || {};
|
||||||
this.processorName = processorName || 'console';
|
this.processorName = processorName || 'console';
|
||||||
this.tracerName = tracerName || 'tachybase-trace';
|
this.tracerName = tracerName || 'tachybase-trace';
|
||||||
this.version = version || '';
|
this.version = version || '';
|
||||||
|
this.provider = new NodeTracerProvider({
|
||||||
|
resource,
|
||||||
|
});
|
||||||
|
this.provider.register();
|
||||||
|
}
|
||||||
|
|
||||||
|
init() {
|
||||||
this.registerProcessor('console', () => new BatchSpanProcessor(new ConsoleSpanExporter()));
|
this.registerProcessor('console', () => new BatchSpanProcessor(new ConsoleSpanExporter()));
|
||||||
// 初始化 OTLP 作为链路追踪 Processor
|
// 初始化 OTLP 作为链路追踪 Processor
|
||||||
const newOtlpExporter = new OTLPTraceExporter({
|
const newOtlpExporter = new OTLPTraceExporter({
|
||||||
@ -35,13 +43,6 @@ export class Trace {
|
|||||||
this.registerProcessor('otlp', () => new BatchSpanProcessor(newOtlpExporter));
|
this.registerProcessor('otlp', () => new BatchSpanProcessor(newOtlpExporter));
|
||||||
}
|
}
|
||||||
|
|
||||||
init(resource: Resource) {
|
|
||||||
this.provider = new NodeTracerProvider({
|
|
||||||
resource,
|
|
||||||
});
|
|
||||||
this.provider.register();
|
|
||||||
}
|
|
||||||
|
|
||||||
registerProcessor(name: string, processor: GetSpanProcessor) {
|
registerProcessor(name: string, processor: GetSpanProcessor) {
|
||||||
console.log('register trace processor:', name);
|
console.log('register trace processor:', name);
|
||||||
this.processors.register(name, processor);
|
this.processors.register(name, processor);
|
||||||
|
Loading…
Reference in New Issue
Block a user