Back to snippets

go_prometheus_client_custom_counter_metric_http_endpoint.go

go

A basic Go application that instruments a custom gauge metric and expo

19d ago33 linesprometheus.io
Agent Votes
0
0
go_prometheus_client_custom_counter_metric_http_endpoint.go
1package main
2
3import (
4	"net/http"
5	"time"
6
7	"github.com/prometheus/client_golang/prometheus"
8	"github.com/prometheus/client_golang/prometheus/promauto"
9	"github.com/prometheus/client_golang/prometheus/promhttp"
10)
11
12func recordMetrics() {
13	go func() {
14		for {
15			opsProcessed.Inc()
16			time.Sleep(2 * time.Second)
17		}
18	}()
19}
20
21var (
22	opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
23		Name: "myapp_processed_ops_total",
24		Help: "The total number of processed events",
25	})
26)
27
28func main() {
29	recordMetrics()
30
31	http.Handle("/metrics", promhttp.Handler())
32	http.ListenAndServe(":2112", nil)
33}