loggingctx

package
v1.29.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package loggingctx provides request ID middleware and context utilities for distributed tracing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID extracts the request ID from the given context. Returns an empty string if no request ID is found.

func LogCall

func LogCall(r *http.Request, component, action string, kv ...interface{})

LogCall logs a standardized line for handler/API invocations with critical details. Always logs at INFO; include additional method/path/query and req_id. Extra kv pairs are appended.

Parameters:

  • r: HTTP request
  • component: Component name (e.g., "api", "web", "worker")
  • action: Action name (e.g., "GetUser", "CreateInteraction")
  • kv: Optional key-value pairs for additional context (must be even number of arguments)

func LogRequest

func LogRequest(ctx *http.Request, status int, latency time.Duration, bytes int)

LogRequest logs a standardized HTTP request with key metrics. Extracts request ID from context for distributed tracing.

Parameters:

  • ctx: Request context (should contain request ID from RequestIDMiddleware)
  • r: HTTP request
  • status: HTTP status code
  • latency: Request processing duration
  • bytes: Response size in bytes

func RequestIDMiddleware

func RequestIDMiddleware(next http.Handler) http.Handler

RequestIDMiddleware is HTTP middleware that generates a unique request ID for each request and stores it in the request context. The request ID is also added to the response headers for client-side correlation.

Request ID format: ULID-like (26 characters, timestamp-prefixed, URL-safe) Example: 01HQZX9P7J8K5M6N4Q3R2T1W0V

If the request already has an X-Request-ID header, it will be preserved. Otherwise, a new ULID is generated.

Usage:

mux := http.NewServeMux()
// ... register handlers ...
handler := loggingctx.RequestIDMiddleware(mux)
http.ListenAndServe(":8080", handler)

func WithRequestID

func WithRequestID(ctx context.Context, requestID string) context.Context

WithRequestID returns a new context with the given request ID.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL