cs.oldnet

All sheets / Observability

OpenTelemetry

Vendor-neutral traces, metrics and logs: concepts, SDK setup and the Collector.

Concepts

  • Trace: one request's journey; tree of spans.
  • Span: name, start/end, attributes, events, status, parent span id, kind (server, client, producer, consumer, internal).
  • Context propagation: trace context passed across services via W3C traceparent header (and message attributes for queues).
  • Resource: who emits telemetry (service.name, version, env).
  • Semantic conventions: standard attribute names (http.request.method, db.system).
  • Baggage: user key-values propagated with context.

traceparent

traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
             ver-trace-id (16 bytes)           -parent span id -flags (sampled)

Node.js setup

// tracing.js, load before the app: node -r ./tracing.js app.js
import { NodeSDK } from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';

new NodeSDK({
  serviceName: 'checkout',
  traceExporter: new OTLPTraceExporter({ url: 'http://collector:4318/v1/traces' }),
  instrumentations: [getNodeAutoInstrumentations()],
}).start();

Or zero-code: OTEL_SERVICE_NAME=checkout node --require @opentelemetry/auto-instrumentations-node/register app.js.

Manual spans

import { trace, SpanStatusCode } from '@opentelemetry/api';
const tracer = trace.getTracer('checkout');

await tracer.startActiveSpan('charge-card', async (span) => {
  try {
    span.setAttribute('order.id', id);
    await charge();
  } catch (e) {
    span.recordException(e);
    span.setStatus({ code: SpanStatusCode.ERROR });
    throw e;
  } finally { span.end(); }
});

Metrics

import { metrics } from '@opentelemetry/api';
const meter = metrics.getMeter('checkout');
const orders = meter.createCounter('orders.placed');
const latency = meter.createHistogram('checkout.duration', { unit: 'ms' });
orders.add(1, { region: 'us' });

Instruments: Counter, UpDownCounter, Histogram, Gauge (observable).

Collector

Receives → processes → exports. Decouples apps from vendors.

receivers:
  otlp: { protocols: { grpc: {}, http: {} } }
processors:
  batch: {}
  memory_limiter: { check_interval: 1s, limit_mib: 512 }
  tail_sampling:
    policies:
      - { name: errors, type: status_code, status_code: { status_codes: [ERROR] } }
      - { name: slow, type: latency, latency: { threshold_ms: 1000 } }
exporters:
  otlphttp: { endpoint: https://tenant.live.dynatrace.com/api/v2/otlp }
  awsxray: {}
service:
  pipelines:
    traces: { receivers: [otlp], processors: [memory_limiter, tail_sampling, batch], exporters: [otlphttp] }

Deploy as agent (sidecar/daemon) and/or gateway (central cluster). ADOT = AWS distro.

Sampling

  • Head sampling: decide at trace start (TraceIdRatioBased 10%); cheap, may miss errors.
  • Tail sampling: decide after the trace completes (keep errors/slow); needs the Collector.
  • ParentBased sampler respects upstream decisions.

Env vars

OTEL_SERVICE_NAME=checkout
OTEL_RESOURCE_ATTRIBUTES=deployment.environment=prod,service.version=1.4.2
OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4318
OTEL_TRACES_SAMPLER=parentbased_traceidratio
OTEL_TRACES_SAMPLER_ARG=0.1

OTLP ports: 4317 gRPC, 4318 HTTP.