Code Explainers

Code explainers tagged #observability

python
import time
import uuid
 
from django.utils.deprecation import MiddlewareMixin

Attaching per-request context in Django

middleware request lifecycle multi-tenancy
Intermediate 7 steps
java
package com.example.actuator;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;

Building a custom Spring Boot Actuator endpoint

actuator dependency-injection observability
Intermediate 7 steps
php
<?php
 
namespace App\Http\Controllers;
 

Building a health check endpoint in Laravel

health-check invokable-controller error-handling
Intermediate 8 steps
python
from flask import Flask, request, Response
from prometheus_client import Counter, Histogram, generate_latest, CONTENT_TYPE_LATEST
import time
 

Instrumenting a Flask app with Prometheus

metrics observability middleware
Intermediate 6 steps
python
import logging
import uuid
from flask import Flask, request, g
 

Adding correlation IDs to Flask logs

logging request-lifecycle observability
Intermediate 7 steps
python
from fastapi import APIRouter, status
from fastapi.responses import JSONResponse
from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError

Liveness vs readiness health checks in FastAPI

health-checks dependency-injection async
Intermediate 6 steps
java
@Configuration
public class DataSourceLoggingConfig {
 
    @Bean

Wrapping a Spring DataSource for query tracing

proxy pattern observability data source
Intermediate 7 steps
typescript
import {
  CallHandler,
  ExecutionContext,
  Injectable,

Recording HTTP metrics with a NestJS interceptor

interceptor observability prometheus
Intermediate 5 steps
php
namespace App\Providers;
 
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

Catching N+1 queries with Eloquent strict mode in Laravel

n+1 queries strict mode eager loading
Intermediate 7 steps
ruby
module SlowQueryLogger
  SLOW_QUERY_THRESHOLD_MS = 200.0
  IGNORED_PAYLOAD_NAMES = %w[SCHEMA TRANSACTION].freeze
 

Logging slow SQL queries in Rails

instrumentation logging pub-sub
Intermediate 7 steps
javascript
const express = require('express');
const router = express.Router();
const { pool } = require('../db');
const redis = require('../redis');

Building a health check endpoint in Express

health-check timeouts promise-race
Intermediate 9 steps
javascript
'use client';
 
import { useEffect } from 'react';
import * as Sentry from '@sentry/nextjs';

How a Next.js error boundary recovers

error-boundary error-handling observability
Intermediate 8 steps
rust
use std::time::Instant;
 
use axum::{
    body::Body,

Prometheus request metrics in Axum

middleware observability prometheus
Intermediate 7 steps
rust
use std::time::Duration;
 
use axum::{
    body::Body,

Structured request tracing in Axum

middleware observability tracing
Intermediate 7 steps
go
package middleware
 
import (
	"strconv"

Instrumenting a Gin API with Prometheus

middleware observability metrics
Intermediate 6 steps
typescript
import { Controller, Get } from '@nestjs/common';
import {
  HealthCheck,
  HealthCheckService,

Liveness vs readiness probes in NestJS

health-checks dependency-injection observability
Intermediate 6 steps
java
@Service
public class OrderMetricsService {
 
    private final MeterRegistry registry;

Instrumenting orders with Micrometer in Spring

metrics micrometer observability
Intermediate 8 steps
go
package middleware
 
import (
	"time"

Building a request logger middleware in Gin

middleware structured-logging closures
Intermediate 6 steps