Lab 13: Observability

Time: 60 minutes | Level: Architect | Docker: node:20-alpine

Overview

Typed observability stack: OpenTelemetry TypeScript SDK (typed spans/attributes/metrics), pino structured logging with type-safe bindings, AsyncLocalStorage<T> typed context propagation, and distributed tracing type patterns.


Step 1: OpenTelemetry TypeScript Setup

// src/telemetry/sdk.ts
import { NodeSDK }           from '@opentelemetry/sdk-node';
import { Resource }           from '@opentelemetry/resources';
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
import { OTLPTraceExporter }  from '@opentelemetry/exporter-trace-otlp-http';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';

const sdk = new NodeSDK({
  resource: new Resource({
    [ATTR_SERVICE_NAME]:    'my-service',
    [ATTR_SERVICE_VERSION]: '1.0.0',
    'deployment.environment': process.env.NODE_ENV,
  }),
  traceExporter: new OTLPTraceExporter({
    url: 'http://otel-collector:4318/v1/traces',
  }),
  metricReader: new PeriodicExportingMetricReader({
    exporter: new OTLPMetricExporter({
      url: 'http://otel-collector:4318/v1/metrics',
    }),
    exportIntervalMillis: 30_000,
  }),
});

sdk.start();
process.on('SIGTERM', () => sdk.shutdown());

Step 2: Typed Spans


Step 3: AsyncLocalStorage for Request Context


Step 4: Pino — Type-Safe Structured Logging


Step 5: Typed Metrics


Step 6: Distributed Tracing — Context Propagation


Step 7: Error Tracking with Types


Step 8: Capstone — OpenTelemetry Typed Spans

📸 Verified Output:


Summary

Tool
Type Feature
Benefit

OpenTelemetry

Attributes type

Type-checked span attrs

AsyncLocalStorage<T>

Generic type param

Type-safe request context

pino

Logger<K>

Typed log bindings

Semantic conventions

ATTR_* constants

Standard attribute names

Custom metrics

Histogram/Counter

Typed instrument API

W3C propagation

propagation.inject

Distributed trace context

Last updated