docs/docs/cn/plugin-development/server/telemetry.md
:::warning{title=实验性} :::
NocoBase 的遥测 (Telemetry) 模块基于 <a href="https://opentelemetry.io/" target="_blank">OpenTelemetry</a> 封装。本文介绍如何在使用遥测模块收集链路 (Trace) 和监控指标 (Metric) 数据来增强 NocoBase 系统的可观测性 (Observability)。
const meter = app.telemetry.metric.getMeter();
const counter = meter.createCounter('event_counter', {});
counter.add(1);
参考:
const tracer = app.telemetry.trace.getTracer();
tracer.startActiveSpan();
tracer.startSpan();
参考:
import { Plugin } from '@nocobase/server';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
class InstrumentationPlugin extends Plugin {
afterAdd() {
this.app.on('beforeLoad', (app) => {
app.telemetry.addInstrumentation(getNodeAutoInstrumentations());
});
}
}
:::warning{title=注意}
NocoBase 中遥测模块的初始化位置为 app.beforeLoad. 因此并不是所有插桩库都适用于 NocoBase.
例如:<a href="https://www.npmjs.com/package/@opentelemetry/instrumentation-koa" target="_blank">instrumentation-koa</a> 需要在 Koa 实例化之前引入,而 NocoBase 的 Application 虽然基于 Koa, 但是遥测模块是在 Application 实例化之后才初始化的,则不能适用。
:::
参考:
import { Plugin } from '@nocobase/server';
import {
PeriodicExportingMetricReader,
ConsoleMetricExporter,
} from '@opentelemetry/sdk-metrics';
class MetricReaderPlugin extends Plugin {
afterAdd() {
this.app.on('beforeLoad', (app) => {
app.telemetry.metric.registerReader(
'console',
() =>
new PeriodicExportingMetricReader({
exporter: new ConsoleMetricExporter(),
}),
);
});
}
}
import { Plugin } from '@nocobase/server';
import {
BatchSpanProcessor,
ConsoleSpanExporter,
} from '@opentelemetry/sdk-trace-base';
class TraceSpanProcessorPlugin extends Plugin {
afterAdd() {
this.app.on('beforeLoad', (app) => {
app.telemetry.trace.registerProcessor(
'console',
() => new BatchSpanProcessor(new ConsoleSpanExporter()),
);
});
}
}
参考: