Metric - Direct Prometheus Integration Example
Code Source: https://github.com/gogf/examples/tree/main/observability/metric/prometheus
Description
This example demonstrates how to directly integrate Prometheus
metrics in GoFrame
without using OpenTelemetry
. It shows how to:
- Create Prometheus metrics directly
- Register metrics with Prometheus registry
- Expose metrics via HTTP endpoint
- Update metric values dynamically
Structure
- `go.mod`: The Go module file for dependency management
- `go.sum`: The Go module checksums file
- `prometheus.go`: The main application demonstrating direct Prometheus integration
Features
The example showcases the following features:
-
Metric Types
- Counter: Monotonically increasing value
- Gauge: Value that can go up and down
-
Metric Operations
- Metric registration
- Value updates
- HTTP exposure
- Random value generation
-
HTTP Endpoints
/
: Triggers metric updates/metrics
: Exposes Prometheus metrics
Requirements
- Go 1.22 or higher
- Git
- GoFrame
- GoFrame OpenTelemetry Metric
Usage
-
Run the example:
go run prometheus.go
-
Test the application:
# Generate some metric values
curl http://localhost:8000/
# View metrics
curl http://localhost:8000/metrics -
Example metrics output:
# HELP demo_counter A demo counter.
# TYPE demo_counter counter
demo_counter 1
# HELP demo_gauge A demo gauge.
# TYPE demo_gauge gauge
demo_gauge 42
Implementation Details
The example demonstrates:
- Direct Prometheus metric creation
- Manual metric registration
- HTTP server configuration
- Dynamic value updates
- Metric exposure
Notes
- Uses native Prometheus client
- No OpenTelemetry dependency
- Simpler but less feature-rich
- Manual metric management
- Good for basic use cases
- Default port is 8000
- Metrics endpoint at /metrics
- Consider security when exposing metrics
- Random values for demonstration
- Counter only increases
- Gauge can increase or decrease