Reference library

Observability & SRE

Structured logging, metrics, tracing, health checks, and SLO-friendly instrumentation.

1 match
Observability & SRE easy

How to Calculate Percentile Latency in Python

Generate mock latency samples with occasional spikes and compute 50th, 90th, 95th, and 99th percentile values in milliseconds.

percentile latency slo
Python
import random
import statistics

def generate_latency_samples(n=1000):
    """Generate realistic mock latency data (ms) with occasional spikes."""
    samples = []
    for _ in range(n):
        # Normal case: ~50ms with jitter
        base = random.gauss(50, 5)
        # 2% spike chance: slow downstream or GC pause
 …
13 0 Open

Browse by section

Each section groups closely related Python snippets.

Observability & SRE — Python code examples

What you will find here

This page collects observability & sre snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.