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 1 commit
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
2 changes: 1 addition & 1 deletion examples/customlabels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ 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. 😄

prometheus.WrapRegistererWith(prometheus.Labels{"service": "my-service-name"}, reg).MustRegister(
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
)
Expand Down