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;
|
||||
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);
|
||||
|
@ -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 里
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user