Skip to content

Conversation

@matiasperrone-exo
Copy link

@matiasperrone-exo matiasperrone-exo commented Dec 18, 2025

Overview

This PR adds comprehensive OpenTelemetry (OTEL) middleware integration to the IdP API, enabling distributed tracing, metrics collection, and centralized logging through OTLP (OpenTelemetry Protocol).

Key Features

  • Request Tracking Middleware: TrackRequestMiddleware that captures request/response metadata including timing, CloudFlare Ray ID, and baggage context
  • OpenTelemetry Integration: Full integration with keepsuit/laravel-opentelemetry package (v1.14)
  • Trace Context Propagation: Support for W3C Trace Context and Baggage standards
  • OTEL Collector Stack: Docker Compose configuration with OpenTelemetry Collector and Elasticsearch backend
  • Configurable Exporters: Support for OTLP gRPC, OTLP HTTP, and Zipkin exporters
  • Elasticsearch Storage: Centralized logging and trace storage in Elasticsearch
  • Performance Monitoring: Response time tracking and HTTP status code attributes
  • Instrumentation: Pre-configured instrumentations for HTTP, database queries, Redis, queues, cache, events, views, and console commands

Files Added

  • app/Http/Middleware/TrackRequestMiddleware.php - Request/response tracking middleware
  • config/opentelemetry.php - OpenTelemetry configuration file
  • docker-compose/opentelemetry/otel-collector-config.yaml - OTEL Collector configuration
  • .env.example - Updated with OpenTelemetry environment variables

Files Modified

  • app/Http/Kernel.php - Added TrackRequestMiddleware to middleware stack
  • composer.json - Added OpenTelemetry dependencies and PSR-compatible autoloaders
  • composer.lock - Updated dependencies
  • docker-compose.yml - Added Elasticsearch and OpenTelemetry Collector services

Configuration Highlights

Environment Variables

  • OTEL_SERVICE_ENABLED - Enable/disable OpenTelemetry
  • OTEL_SERVICE_NAME - Service name (defaults to app name)
  • OTEL_EXPORTER_OTLP_ENDPOINT - Collector endpoint (default: http://otel-collector:4318)
  • OTEL_EXPORTER_OTLP_PROTOCOL - Protocol: grpc, http/protobuf, http/json
  • OTEL_TRACES_SAMPLER_TYPE - Sampler: always_on, always_off, traceidratio
  • OTEL_INSTRUMENTATION_* - Toggle individual instrumentations

Docker Services

  • OpenTelemetry Collector: Receives traces, metrics, logs via OTLP
  • Elasticsearch: Stores traces, metrics, logs for analysis
  • Ports: 4318 (OTLP HTTP), 4317 (OTLP gRPC), 9200 (Elasticsearch), 8888/8889 (Prometheus)

Architecture

  • TrackRequestMiddleware: Runs early in middleware stack to track request lifecycle
  • Baggage Context: CloudFlare Ray ID and User-Agent propagated via OpenTelemetry Baggage
  • Span Lifecycle: Events for request start/finished with duration and status
  • Graceful Degradation: Disabled in testing environment
  • Error Handling: Comprehensive error logging without breaking request flow

Dependencies Added

  • keepsuit/laravel-opentelemetry ^1.14 - Laravel OpenTelemetry wrapper
  • open-telemetry/api ^1.4 - OTEL API
  • open-telemetry/sdk ^1.9 - OTEL SDK
  • open-telemetry/exporter-otlp ^1.3 - OTLP exporter
  • google/protobuf ^4.33 - Protocol Buffers support
  • composer/semver ^3.4.4 - Version constraint handling

Task:

Ref: https://app.clickup.com/t/86b7ccrnm

@matiasperrone-exo matiasperrone-exo self-assigned this Dec 26, 2025
@matiasperrone-exo matiasperrone-exo marked this pull request as ready for review December 30, 2025 19:28
@matiasperrone-exo
Copy link
Author

@smarcet please review again as all the requested changes were incorporated.

@matiasperrone-exo matiasperrone-exo added documentation Improvements or additions to documentation and removed documentation Improvements or additions to documentation labels Dec 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants