Skip to content

Commit b19d6bd

Browse files
lilickakkoyun
authored andcommitted
examples: Follow best practices and established naming conventions (prometheus#1650)
* 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 <[email protected]> * examples: Switch custom labels example Signed-off-by: Lili Cosic <[email protected]> --------- Signed-off-by: Lili Cosic <[email protected]> Co-authored-by: Kemal Akkoyun <[email protected]>
1 parent 3bc2fa6 commit b19d6bd

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

examples/customlabels/main.go

+53-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020
"flag"
2121
"log"
2222
"net/http"
23+
"time"
2324

2425
"github.com/prometheus/client_golang/prometheus"
2526
"github.com/prometheus/client_golang/prometheus/collectors"
27+
"github.com/prometheus/client_golang/prometheus/promauto"
2628
"github.com/prometheus/client_golang/prometheus/promhttp"
2729
)
2830

@@ -33,12 +35,61 @@ func main() {
3335

3436
// Create a new registry.
3537
reg := prometheus.NewRegistry()
36-
prometheus.WrapRegistererWith(prometheus.Labels{"serviceName": "my-service-name"}, reg).MustRegister(
38+
reg.MustRegister(
3739
collectors.NewGoCollector(),
3840
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
3941
)
4042

41-
// Expose the registered metrics via HTTP.
43+
// We should see the following metrics with an extra source label. But
44+
// other collectors registered above are expected not to have the extra
45+
// label.
46+
// See also https://prometheus.io/docs/instrumenting/writing_exporters/#target-labels-not-static-scraped-labels
47+
startFireKeeper(prometheus.WrapRegistererWith(prometheus.Labels{"component": "FireKeeper"}, reg))
48+
startSparkForge(prometheus.WrapRegistererWith(prometheus.Labels{"component": "SparkForge"}, reg))
49+
4250
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
4351
log.Fatal(http.ListenAndServe(*addr, nil))
4452
}
53+
54+
func startFireKeeper(reg prometheus.Registerer) {
55+
firesMaintained := promauto.With(reg).NewCounter(prometheus.CounterOpts{
56+
Name: "fires_maintained_total",
57+
Help: "Total number of fires maintained",
58+
})
59+
60+
sparksDistributed := promauto.With(reg).NewCounter(prometheus.CounterOpts{
61+
Name: "sparks_distributed_total",
62+
Help: "Total number of sparks distributed",
63+
})
64+
65+
go func() {
66+
for {
67+
time.Sleep(5 * time.Second)
68+
firesMaintained.Inc()
69+
log.Println("FireKeeper maintained a fire")
70+
}
71+
}()
72+
73+
go func() {
74+
for {
75+
time.Sleep(7 * time.Second)
76+
sparksDistributed.Inc()
77+
log.Println("FireKeeper distributed a spark")
78+
}
79+
}()
80+
}
81+
82+
func startSparkForge(reg prometheus.Registerer) {
83+
itemsForged := promauto.With(reg).NewCounter(prometheus.CounterOpts{
84+
Name: "items_forged_total",
85+
Help: "Total number of items forged",
86+
})
87+
88+
go func() {
89+
for {
90+
time.Sleep(6 * time.Second)
91+
itemsForged.Inc()
92+
log.Println("SparkForge forged an item")
93+
}
94+
}()
95+
}

0 commit comments

Comments
 (0)