From 582e0a32d35278a4da4ecbb082ef03f4b6770e39 Mon Sep 17 00:00:00 2001 From: Lili Cosic Date: Tue, 15 Oct 2024 14:16:39 +0200 Subject: [PATCH 1/2] examples: Follow best practices and established naming conventions This is a nitpick but from my experience and understanding the best practice for label key naming is to use one word, otherwise using an underscore. Since this is an example users tend to copy, I think correcting it might be a good idea. Signed-off-by: Lili Cosic --- examples/customlabels/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/customlabels/main.go b/examples/customlabels/main.go index fcfce295e..966544a05 100644 --- a/examples/customlabels/main.go +++ b/examples/customlabels/main.go @@ -33,7 +33,7 @@ func main() { // Create a new registry. reg := prometheus.NewRegistry() - prometheus.WrapRegistererWith(prometheus.Labels{"serviceName": "my-service-name"}, reg).MustRegister( + prometheus.WrapRegistererWith(prometheus.Labels{"service": "my-service-name"}, reg).MustRegister( collectors.NewGoCollector(), collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), ) From fd683b88b4d64eb085f3d7fd32599e859b8a0b6b Mon Sep 17 00:00:00 2001 From: Lili Cosic Date: Tue, 29 Oct 2024 11:25:37 +0100 Subject: [PATCH 2/2] examples: Switch custom labels example Signed-off-by: Lili Cosic --- examples/customlabels/main.go | 55 +++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/examples/customlabels/main.go b/examples/customlabels/main.go index 966544a05..799e05292 100644 --- a/examples/customlabels/main.go +++ b/examples/customlabels/main.go @@ -20,9 +20,11 @@ import ( "flag" "log" "net/http" + "time" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" ) @@ -33,12 +35,61 @@ func main() { // Create a new registry. reg := prometheus.NewRegistry() - prometheus.WrapRegistererWith(prometheus.Labels{"service": "my-service-name"}, reg).MustRegister( + reg.MustRegister( collectors.NewGoCollector(), collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), ) - // Expose the registered metrics via HTTP. + // We should see the following metrics with an extra source label. But + // other collectors registered above are expected not to have the extra + // label. + // See also https://prometheus.io/docs/instrumenting/writing_exporters/#target-labels-not-static-scraped-labels + startFireKeeper(prometheus.WrapRegistererWith(prometheus.Labels{"component": "FireKeeper"}, reg)) + startSparkForge(prometheus.WrapRegistererWith(prometheus.Labels{"component": "SparkForge"}, reg)) + http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) log.Fatal(http.ListenAndServe(*addr, nil)) } + +func startFireKeeper(reg prometheus.Registerer) { + firesMaintained := promauto.With(reg).NewCounter(prometheus.CounterOpts{ + Name: "fires_maintained_total", + Help: "Total number of fires maintained", + }) + + sparksDistributed := promauto.With(reg).NewCounter(prometheus.CounterOpts{ + Name: "sparks_distributed_total", + Help: "Total number of sparks distributed", + }) + + go func() { + for { + time.Sleep(5 * time.Second) + firesMaintained.Inc() + log.Println("FireKeeper maintained a fire") + } + }() + + go func() { + for { + time.Sleep(7 * time.Second) + sparksDistributed.Inc() + log.Println("FireKeeper distributed a spark") + } + }() +} + +func startSparkForge(reg prometheus.Registerer) { + itemsForged := promauto.With(reg).NewCounter(prometheus.CounterOpts{ + Name: "items_forged_total", + Help: "Total number of items forged", + }) + + go func() { + for { + time.Sleep(6 * time.Second) + itemsForged.Inc() + log.Println("SparkForge forged an item") + } + }() +}