Allow empty namespace, namespace chain for nested structs#34
Allow empty namespace, namespace chain for nested structs#34burik666 wants to merge 1 commit intocabify:masterfrom
Conversation
|
@carrascoacd @juananrey, review it please. |
carrascoacd
left a comment
There was a problem hiding this comment.
It looks good to me. Thanks for the contribution!
yaroslavcev
left a comment
There was a problem hiding this comment.
The approach LGTM, thought I'd keep behavior of public Init function as it is. Please also update the CHANGELOG.md
| @@ -100,8 +106,12 @@ func (in initializer) initMetrics(group reflect.Value, namespaces ...string) err | |||
| } | |||
| } else if fieldType.Type.Kind() == reflect.Struct { | |||
| namespace, ok := fieldType.Tag.Lookup("namespace") | |||
There was a problem hiding this comment.
WDYT if we write whole struct branch like this:
newNamespaces := namespaces
namespaceTag, ok := fieldType.Tag.Lookup("namespace")
if ok && namespaceTag != "" {
newNamespaces = append(newNamespaces, namespaceTag)
}
if err := in.initMetrics(field, newNamespaces...); err != nil {
return err
}
| var namespaces []string | ||
|
|
||
| if namespace != "" { | ||
| namespaces = append(namespaces, namespace) | ||
| } | ||
|
|
||
| return in.initMetrics(metricsPtr.Elem(), namespaces...) |
There was a problem hiding this comment.
These changes are not necessary to make namespace optional for nested structs, right?
Also. This is kind of breaking change. Before the change call to Init with empty namespace was producing _blah_blah metric (note the starting underscore). Even though it was not intentional, I'd keep this behaviour
There was a problem hiding this comment.
Underscores are added only for nested structures.
For example:
type metrics struct {
AAA func() prometheus.Counter `name:"aaa" help:""`
Base struct {
CCC func() prometheus.Counter `name:"ccc" help:""`
}
Sub struct {
BBB func() prometheus.Counter `name:"bbb" help:""`
} `namespace:"sub"`
}This will produce aaa, ccc, and _sub_bbb metrics. This looks a bit strange.
How about replacing the namespace argument with namespaces ...string in the Init function?
This would allow metrics to be created without a prefix at all while preserving this behavior.
There was a problem hiding this comment.
Hi! Sorry, for the late reply.
How about replacing the namespace argument with namespaces ...string in the Init function?
Oh, so you would like to initialze a metric without any namespace. What is your use case? I feel that having common prefix for related metrics is useful when querying. What you suggest is fine by me
There was a problem hiding this comment.
What is your use case?
To be honest, I don’t remember why I needed this. xD
The official client allows this. I would like to keep this option and not impose any restrictions when using this package.
If you agree, I'll add ...string argument.
There was a problem hiding this comment.
If you agree, I'll add ...string argument.
Yes, this is fine by me
e6b62bd to
274ab3a
Compare
The namespace is optional.
Nested structs without a namespace are useful for embedded structs.