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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lilic
Copy link
Contributor

@lilic lilic commented Oct 15, 2024

This is a nitpick, so feel free to ignore, 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.

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]>
@@ -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. 😄

Copy link
Member

@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Yes! Thanks, great catch. All our examples would use some refresh 🤔 added #1652

@@ -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
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
Member

@kakkoyun kakkoyun left a comment

Choose a reason for hiding this comment

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

LGTM ❤️

@kakkoyun
Copy link
Member

@lilic I'm not sure if the tests' failure are related. I'm happy to merge it when the CI is green.

I'll check whether it's something that a maintainer should take care of soonish 😅 Sorry for the inconvenience if it's the case.

@ArthurSens
Copy link
Member

@lilic I'm not sure if the tests' failure are related. I'm happy to merge it when the CI is green.

I'll check whether it's something that a maintainer should take care of soonish 😅 Sorry for the inconvenience if it's the case.

They are not, it is failing in several PRs 😬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants