-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
base: main
Are you sure you want to change the base?
Conversation
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]>
b99524c
to
582e0a3
Compare
@@ -33,7 +33,7 @@ func main() { | |||
|
|||
// Create a new registry. | |||
reg := prometheus.NewRegistry() | |||
prometheus.WrapRegistererWith(prometheus.Labels{"serviceName": "my-service-name"}, reg).MustRegister( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. 😄
There was a problem hiding this 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( |
There was a problem hiding this comment.
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?
Signed-off-by: Lili Cosic <[email protected]>
3702c2a
to
fd683b8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM ❤️
@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 😬 |
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.