Skip to content

Allow empty namespace, namespace chain for nested structs#34

Open
burik666 wants to merge 1 commit into
cabify:masterfrom
burik666:fix-empty-namespace
Open

Allow empty namespace, namespace chain for nested structs#34
burik666 wants to merge 1 commit into
cabify:masterfrom
burik666:fix-empty-namespace

Conversation

@burik666

@burik666 burik666 commented Feb 14, 2025

Copy link
Copy Markdown

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
Copy Markdown
Author

@carrascoacd @juananrey, review it please.

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

@carrascoacd carrascoacd left a comment

Copy link
Copy Markdown
Contributor

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!

@yaroslavcev yaroslavcev left a comment

Copy link
Copy Markdown

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

Comment thread init.go
@@ -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")

Copy link
Copy Markdown

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 thread init.go
Comment on lines +85 to +91
var namespaces []string

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

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

Copy link
Copy Markdown

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
Copy Markdown
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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you agree, I'll add ...string argument.

Yes, this is fine by me

@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