pub struct MetricsEndpoint { /* private fields */ }Expand description
Lightweight HTTP server for exposing Prometheus metrics and health check endpoints.
MetricsEndpoint provides a simple, efficient HTTP server specifically
designed for serving metrics to monitoring systems like Prometheus. It
handles concurrent requests asynchronously and provides both metrics and
health check endpoints.
§Features
§Prometheus Integration
- Standard Format: Serves metrics in Prometheus exposition format
- Content-Type: Proper content-type headers for Prometheus compatibility
- Fresh Data: Generates metrics fresh on each request
- Error Handling: Graceful error responses for metrics generation failures
§Health Monitoring
- Health Endpoint: Simple health check endpoint for load balancers
- Fast Response: Low-latency health check responses
- Status Codes: Proper HTTP status codes for health states
- Monitoring Integration: Compatible with various monitoring systems
§Performance Optimized
- Async Processing: Non-blocking request handling
- Concurrent Connections: Handles multiple simultaneous requests
- Low Overhead: Minimal resource usage
- Efficient Parsing: Simple HTTP request parsing
§Usage Examples
§Basic Server Setup
§Integration with Tokio Runtime
§Configuration
The server is configured through the ConfigService:
- Port number for the HTTP server
- Bind address (currently localhost only)
- Request handling timeouts
§Error Handling
The server handles various error conditions gracefully:
- Metrics Generation Errors: Returns HTTP 500 with error details
- Connection Errors: Logs errors and continues serving other requests
- Invalid Requests: Returns HTTP 404 for unknown endpoints
- Resource Exhaustion: Graceful degradation under high load
§Thread Safety
The endpoint is thread-safe and can be used concurrently:
- Immutable after construction
- Shared metrics service through Arc
- Safe concurrent request handling
Implementations§
Source§impl MetricsEndpoint
impl MetricsEndpoint
Sourcepub fn new(metrics_service: Arc<MetricsService>) -> Self
pub fn new(metrics_service: Arc<MetricsService>) -> Self
Sourcepub async fn start(&self) -> Result<(), PipelineError>
pub async fn start(&self) -> Result<(), PipelineError>
Starts the metrics endpoint HTTP server.
This method starts the HTTP server and begins accepting connections. It runs indefinitely, handling incoming requests concurrently. The server will bind to the configured port and serve metrics and health check endpoints.
§Returns
Ok(())- Never returns normally (runs indefinitely)Err(PipelineError)- If server fails to start or bind to port
§Endpoints Served
GET /metrics- Prometheus metrics in text formatGET /health- Simple health check returning “OK”- Other paths return 404 Not Found
§Examples
§Error Handling
The server handles individual request errors gracefully:
- Connection errors are logged but don’t stop the server
- Metrics generation errors return HTTP 500
- Invalid requests return HTTP 404
§Performance
- Handles requests concurrently using Tokio tasks
- Low memory overhead per connection
- Efficient request parsing and response generation
Auto Trait Implementations§
impl Freeze for MetricsEndpoint
impl !RefUnwindSafe for MetricsEndpoint
impl Send for MetricsEndpoint
impl Sync for MetricsEndpoint
impl Unpin for MetricsEndpoint
impl !UnwindSafe for MetricsEndpoint
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more