Skip to main content

OpenTelemetry

  • OpenTelemetry 是什么?
    • 定义 spec
    • OpenTracing 和 OpenCensus 合并后的项目
    • API -> SDK -> Processing -> Exporter -out of app-> Collector -> Backend
  • 参考
  • otlp
    • /v1/traces
    • /v1/metrics
    • /v1/logs
docker run --rm -it \
--name opentelemetry-collector otel/opentelemetry-collector

collector

# 数据源定义 - 在 pipeline/service 中使用
# push or pull
receivers:
otlp:
protocols:
grpc:
http:

processors:
batch:

exporters:
otlp:
endpoint: otelcol:55680

extensions:
health_check:
pprof:
zpages:

service:
extensions: [health_check, pprof, zpages]
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [otlp]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [otlp]
logs:
receivers: [otlp]
processors: [batch]
exporters: [otlp]

Integration

envfor
OTEL_EXPORTER_OTLP_ENDPOINT
OTEL_LOG_LEVEL

NodeJS

npm add @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node

# @opentelemetry/sdk-trace-web
# @opentelemetry/sdk-metrics
npm add @opentelemetry/sdk-trace-node @opentelemetry/exporter-trace-otlp-proto
tracing.ts
'use strict';

import process from 'node:process';
import opentelemetry from '@opentelemetry/sdk-node';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { ConsoleSpanExporter } from '@opentelemetry/sdk-trace-base';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { PeriodicExportingMetricReader, ConsoleMetricExporter } from '@opentelemetry/sdk-metrics';

const sdk = new NodeSDK({
traceExporter: new ConsoleSpanExporter(),
metricReader: new PeriodicExportingMetricReader({
exporter: new ConsoleMetricExporter(),
}),
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'my-service',
}),
instrumentations: [getNodeAutoInstrumentations()],
});

// initialize the SDK and register with the OpenTelemetry API
// this enables the API to record telemetry
sdk.start();

// gracefully shut down the SDK on process exit
process.on('SIGTERM', () => {
sdk
.shutdown()
.then(() => console.log('Tracing terminated'))
.catch((error) => console.log('Error terminating tracing', error))
.finally(() => process.exit(0));
});