This repo contains Prometheus instrumentation for Go applications.
This is a simple Go application that exposes Prometheus metrics via HTTP.
Install the prometheus, promauto, and promhttp libraries necessary for this demo.
go get github.com/prometheus/client_golang/prometheus
go get github.com/prometheus/client_golang/prometheus/promauto
go get github.com/prometheus/client_golang/prometheus/promhttp
To expose Prometheus metrics in a Go application, you need to provide a /metrics HTTP endpoint. You can use the prometheus/promhttp library's HTTP Handler as the handler function.
The code in main.go would expose the default metrics for Go applications via http://localhost:2112/metrics
go run main.go
curl http://localhost:2112/metrics
This was a minimal go application demo of Prometheus Instrumentation.The application above exposes only the default Go metrics.
In this demo we will be using Counter metric type for our go application.
What is a counter metric type?
|Checkout my blog on [Prometheus]
|(https://varshaun.hashnode.dev/introduction-to-prometheus) to know more!
This main.go application exposes a myapp_processed_ops_total counter that counts the number of operations that have been processed thus far. Every 2 seconds, the counter is incremented by one.
go run main.go
curl http://localhost:2112/metrics
In the metrics output, you'll see the help text, type information, and current value of the myapp_processed_ops_total counter:
# HELP myapp_processed_ops_total The total number of processed events
# TYPE myapp_processed_ops_total counter
myapp_processed_ops_total 5
You can configure a locally running Prometheus instance to scrape metrics from the application. Here's an example prometheus.yml configuration:
scrape_configs:
- job_name: myapp
scrape_interval: 10s
static_configs:
- targets:
- localhost:2112