Prometheus Metrics in Go: A Simple Application Monitor
This challenge focuses on implementing Prometheus metrics within a Go application. Prometheus is a popular monitoring system and open-source systems monitoring and alerting toolkit. This exercise will help you understand how to expose application-specific metrics in a format Prometheus can scrape and use for dashboards and alerting.
Problem Description
You are tasked with creating a simple Go application that exposes several Prometheus metrics related to a hypothetical web server. The application should track the following metrics:
http_requests_total: A counter representing the total number of HTTP requests received.http_requests_duration_seconds: A histogram representing the duration of HTTP requests in seconds.api_errors_total: A counter representing the total number of API errors encountered.
The application should expose these metrics via an HTTP endpoint at /metrics. The metrics should be updated periodically (e.g., every 5 seconds) to simulate real-time monitoring data. The application should also simulate some API errors to demonstrate the api_errors_total counter.
Key Requirements:
- Use the
github.com/prometheus/client_golang/prometheuspackage to define and manage the metrics. - Implement a function to update the metrics periodically.
- Create an HTTP handler that serves the
/metricsendpoint, returning the Prometheus-formatted metrics. - Include a main function that starts the HTTP server and begins the metric update loop.
- The histogram should have a specified number of buckets to provide meaningful duration data.
Expected Behavior:
When you access /metrics in your browser or using a tool like curl, you should see a text-based output formatted according to the Prometheus exposition format. The values of the metrics should increase over time as the application runs, reflecting the simulated HTTP requests and API errors.
Edge Cases to Consider:
- Error handling: Gracefully handle errors during metric updates or HTTP serving.
- Concurrency: Ensure that metric updates are thread-safe if your application handles concurrent requests. (This challenge doesn't require complex concurrency, but be mindful of it).
- Metric names and labels: Use descriptive metric names and consider adding labels to provide more context (e.g.,
http_requests_total{method="GET", path="/api/v1/users"}). For this challenge, keep it simple.
Examples
Example 1:
Input: Application running for 10 seconds, receiving 5 HTTP requests and encountering 2 API errors.
Output: (A snippet of the /metrics endpoint output)
# HELP http_requests_total Total number of HTTP requests received.
# TYPE http_requests_total counter
http_requests_total 5
# HELP http_requests_duration_seconds HTTP request duration in seconds.
# TYPE http_requests_duration_seconds histogram
http_requests_duration_seconds_bucket{le="0.005"} 4
http_requests_duration_seconds_bucket{le="0.01"} 4
http_requests_duration_seconds_bucket{le="0.025"} 4
http_requests_duration_seconds_bucket{le="0.05"} 4
http_requests_duration_seconds_bucket{le="0.1"} 4
http_requests_duration_seconds_count 5
# HELP api_errors_total Total number of API errors encountered.
# TYPE api_errors_total counter
api_errors_total 2
Explanation: The output shows the current values of the three metrics. The http_requests_total is 5, api_errors_total is 2, and the histogram buckets reflect the distribution of request durations.
Constraints
- The application should be able to handle at least 100 concurrent HTTP requests without significant performance degradation. (This is a general guideline; focus on correctness first).
- The histogram should have at least 5 buckets.
- The metric update interval should be between 3 and 7 seconds.
- The application should expose the metrics on port 8080.
Notes
- The
github.com/prometheus/client_golang/prometheuspackage provides a convenient way to define and manage Prometheus metrics in Go. Refer to the package documentation for details on available types and functions. - Consider using a
time.Tickerto schedule the metric updates. - The Prometheus exposition format is a simple text-based format. The package you are using will handle the formatting for you.
- Focus on creating a functional application that correctly exposes the specified metrics. Advanced features like custom labels or more complex metric types are not required for this challenge.
- Remember to import the necessary packages.
- The goal is to demonstrate a basic understanding of Prometheus metrics in Go.