[go: up one dir, main page]

Draft: feat: Add the new HTTPServer type.

This code adds a new HTTP server component to the labkit package that makes it easier to build web services with built-in monitoring and observability features.

HTTPServer

The main addition is an HTTPServer that automatically wraps incoming requests with useful middleware for collecting metrics, distributed tracing, and correlation IDs (which help track requests across multiple services). It runs two servers simultaneously - the main HTTP server for handling requests and a separate monitoring server for health checks and metrics.

The server is designed to be developer-friendly with sensible defaults: it listens on port 8080 (or uses the PORT environment variable), runs monitoring on port 8082, and can be configured through environment variables or explicit options. Developers can easily register request handlers and choose which observability features to enable or disable per endpoint.

Key features include graceful shutdown handling, automatic port assignment for testing, and integration with GitLab's existing observability tools. The code also includes comprehensive tests that verify the server starts correctly and can handle requests.

This is part of GitLab's effort to standardize how services are built, providing a consistent foundation that includes monitoring, tracing, and metrics collection out of the box, so developers don't have to implement these common requirements from scratch.

Other improvements

In order to implement the new HTTPServer type and tests, the existing packages had to be extended to make the implementation possible:

  • Package metrics: The new WithPrometheusRegisterer option allows to provide a Prometheus registry to use. Previously, the package always used the (global) default registry, which breaks unit tests, which execute this code multiple times in a single process. The new option uses the same name as monitoring.WithPrometheusRegisterer.
  • Package monitoring: The new Server type was introduced. This is necessary to get access to the HTTP server's Shutdown function. monitoring.Start remains for backwards compatibility, but has been marked as deprecated.

Future work

  • GRPC: We should add a similar GRPCServer type.

    I opted to implement the ServerOption type as an interface, instead of (the more common) function pointer. The idea is that options such as WithAddress can be passed to NewHTTPServer and NewGRPCServer, avoiding repetitive naming (e.g. WithHTTPAddress, WithGRPCAddress).

Issue: Discussion: Unified HTTP Server API for Labkit (#77)

Edited by Florian Forster

Merge request reports

Loading