Sign inSign up

chiefj/latency-engine

By chiefj

Updated 3 months ago

The latency-engine is a high-performance Kafka consumer worker service

Image
Developer tools
0

116

chiefj/latency-engine repository overview

Latency Engine

The latency-engine is a high-performance Kafka consumer worker service that processes raw LLM span events to track and store latency metrics, Service Level Objective (SLO) errors, and latency attribution information in real-time.


🌲 Business Design Tree

Below is the decision path followed for every span event processed by the latency engine:

                          [Raw Span Event Consumed]
                                      │
                                      ▼
               [Validate Span: Has model & latency_ms_total?]
                               /             \
                       (No)   /               \   (Yes)
                             ▼                 ▼
                     [Skip Span]       [Parse UTC Timestamp]
                                               │
           ┌───────────────────────────────────┼──────────────────────────────────┐
           │                                   │                                  │
           ▼                                   ▼                                  ▼
[latency_ms_ttft exists?]             [Is retry_count > 0?]             [SLO threshold check]
      /         \                           /         \                       /         \
(No) /           \ (Yes)             (Yes) /           \ (No)           (Yes)/           \(No)
    ▼             ▼                       ▼             ▼                   ▼             ▼
[Skip]     [Update TTFT Sketch]     [Update Retry]  [Update Total]     [Incr Errors    [Incr Total
           (sketch:ttft:{m}:{h})     (sketch:retry)  (sketch:total)     & Total]        Only]
                  │                                                         │               │
                  ▼                                                         ▼               ▼
           [TPOT Eligible?]                                                 └───────┬───────┘
         (TTFT & Tokens > 0,                                                        │
          Reason != timeout)                                                        │
              /         \                                                           ▼
      (No)   /           \ (Yes)                                           [Attribution tags?]
            ▼             ▼                                                    /          \
         [Skip]     [Calc TPOT]                                         (Yes) /            \ (No)
                    (tpot:latest)                                            ▼              ▼
                                                                     [Store Hash    [Skip]
                                                                      & Agg Avg]

🚀 How to Use the Package

This worker consumes raw LLM span payloads from the llm.spans.raw Kafka topic, aggregates latency metrics into DDSketch representations, and persists the serialized sketches to Redis.

Setup and Installation
  1. Development Environment Setup Using Python 3.11/3.12, install package dependencies in development mode:

    pip install -e ".[dev]"
    
  2. Environment Variables Config Configure the worker using the following environment variables:

    Environment VariableDescriptionDefault
    KAFKA_BOOTSTRAP_SERVERSKafka brokers connection stringlocalhost:9092
    KAFKA_CONSUMER_GROUPConsumer group for the workerlatency-engine-cg
    KAFKA_TOPIC_INPUTKafka topic containing raw span eventsllm.spans.raw
    REDIS_URLRedis URL for stats storageredis://localhost:6379/0
    SLO_CONFIG_PATHPath to the SLO threshold YAML configurationsrc/slo_config.yaml
    HEALTH_PORTHTTP port for the /health endpoint8002
    SKIP_CONSOLE_EXPORTERSkip writing OpenTelemetry traces to stdoutfalse
    SKIP_OTLP_EXPORTERSkip exporting traces to an OTLP collectortrue

🛠️ Tracing & Instrumentation

The latency engine has native, high-depth OpenTelemetry tracing built-in:

  • Automatic Context Propagation: Extracts traceparent and tracestate from Kafka metadata headers to parent-link spans across network boundaries.
  • Granular Spans:
    • latency_handler.handle_spans: Traces the execution of batch processing.
    • latency_handler.process_span: Traces individual span metrics aggregation and includes detailed attributes (net.dns.latency_ms, net.tcp.latency_ms, llm.queue.latency_ms, llm.inference.latency_ms) for deep latency attribution.

🐳 Docker Deployment & Testing

We provide a production-ready, multi-stage build setup to construct lightweight runtime images and execute self-tests.

1. Build Docker Image

Build the Docker image locally. This triggers internal unit tests automatically:

docker build -t latency-engine:latest -f build/Dockerfile .
2. Deploy via Docker Compose

To boot up the complete pipeline (Kafka/Redpanda, Redis, and the Latency Engine worker):

docker compose -f deploy/docker/docker-compose.yaml up -d
3. Verification & Live Testing

To verify the engine is alive and healthy:

./scripts/health-check.sh

Or execute unit tests inside the package:

./scripts/test.sh

Tag summary

Content type

Image

Digest

sha256:18d1dc159

Size

106.3 MB

Last updated

3 months ago

docker pull chiefj/latency-engine