Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: Follow best practices and established naming conventions #1650

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions examples/customlabels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

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

// Create a new registry.
reg := prometheus.NewRegistry()
prometheus.WrapRegistererWith(prometheus.Labels{"serviceName": "my-service-name"}, reg).MustRegister(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not sure but it might be worth including the note and link that is mentioned in the WrapRegistererWith description, thoughts?

// WrapRegistererWith provides a way to add fixed labels to a subset of
// Collectors. It should not be used to add fixed labels to all metrics
// exposed. See also
// https://prometheus.io/docs/instrumenting/writing_exporters/#target-labels-not-static-scraped-labels

Copy link
Member

@bwplotka bwplotka Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm good point. One way of doing this is to use a bit more realistic label name and situation. Service name could mean like a gRPC service (ok practice?) or application/service name (anti-pattern, should be a SD role). We clearly wrap GLOBAL process metrics with this label which feels like an anti pattern.

I would be keen on switching to e.g. component (quite often representing code component) e.g. something like

func main() {
	flag.Parse()

	// Create a new registry.
	reg := prometheus.NewRegistry()
	reg.MustRegister(
		collectors.NewGoCollector(),
		collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
	)
	
	startComponentA(prometheus.WrapRegistererWith(prometheus.Labels{"component": "A"}, reg))

	// Expose the registered metrics via HTTP.
	// We should see my_counter_objects_total with an extra label.
	// 	# HELP my_counter_objects_total Counter of objects
	//	# TYPE my_counter_objects_total counter
	//	my_counter_objects_total{component="A"} 1
	http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
	log.Fatal(http.ListenAndServe(*addr, nil))
}

func startComponentA(reg prometheus.Registerer) {
	ct := promauto.With(reg).NewCounter(prometheus.CounterOpts{
		Name: "my_counter_objects_total",
		Help: "Counter of objects",
	})

	// ...
	ct.Inc()
}

Would you like to update this example here, or should we do it in other PRs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm good point. One way of doing this is to use a bit more realistic label name and situation.

I think thats a good suggestion. It illustrates best practice with an example! 👍

Would you like to update this example here, or should we do it in other PRs?

I can update it, will try to get to it this week still.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bwplotka I might have gone overboard and adjusted the example to the point where it doesn't make sense or maybe it does? 🫠 Please take a look thanks! Otherwise will adjust to a more simple example. 😄

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")
}
}()
}