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

Allow empty namespace, namespace chain for nested structs #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

burik666
Copy link

@burik666 burik666 commented Feb 14, 2025

The namespace is optional.

Nested structs without a namespace are useful for embedded structs.

var metrics struct {
	BaseMetrics
	Sub         SubMetrics `namespace:"sub"`
	SomeCounter func(...) prometheus.Counter
}

@burik666
Copy link
Author

@carrascoacd @juananrey, review it please.

@carrascoacd carrascoacd self-requested a review March 3, 2025 11:47
Copy link
Contributor

@carrascoacd carrascoacd left a comment

Choose a reason for hiding this comment

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

It looks good to me. Thanks for the contribution!

Copy link

@yaroslavcev yaroslavcev left a comment

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

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
			}

Comment on lines +85 to +91
var namespaces []string

if namespace != "" {
namespaces = append(namespaces, namespace)
}

return in.initMetrics(metricsPtr.Elem(), namespaces...)

Choose a reason for hiding this comment

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

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

Copy link
Author

Choose a reason for hiding this comment

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

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.

@burik666 burik666 force-pushed the fix-empty-namespace branch from e6b62bd to 274ab3a Compare March 6, 2025 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants