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

Add CollectAndFormat to testutil, allowing caller to assert as they want to on the exported metric #1503

Merged
merged 1 commit into from May 9, 2024

Conversation

jcass8695
Copy link
Contributor

CollectAndFormat is a helper function that returns the formatted metrics to the caller, allowing them to use it how they want. This is different to CollectAndCompare where the comparison is done strictly on behalf of the caller. Often it is more convenient to perform a simple substring match on the formatted metric rather than the full export. This is especially true testing a duration-like gauge metric, where the exact value of the metric might not be easily determined up front.

@jcass8695
Copy link
Contributor Author

@ArthurSens @bwplotka @kakkoyun if you have time would you mind taking a look at this please? Do you you think it's something that would be useful?

@jcass8695
Copy link
Contributor Author

@ArthurSens @bwplotka @kakkoyun bumping this PR. Happy to let it fall into your backlog of reviews, but a 👍🏻 or something to show you've seen it would be much appreciated, thank you 🙏🏻 .

@bwplotka
Copy link
Member

bwplotka commented May 9, 2024

Checking now, sorry for lag!

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.

Nice work, thanks! Some suggestions, otherwise LGTM!

Comment on lines 224 to 226
// CollectAndFormat registers the provided Collector with a newly created pedantic Registry. It then calls Gather with
// that Registry and filters by the provided metricNames. It writes the gathered metrics to a bugger formatted according
// to the provided format argument
Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. I will be controversial here and suggest we stop doing so verbose comments. This comment explains implementation. I know you copied that for consistency, but we have to start somewhere. What about focusing on contract and usability. Implementation is trivial and self-described by the code. E.g.:

Suggested change
// CollectAndFormat registers the provided Collector with a newly created pedantic Registry. It then calls Gather with
// that Registry and filters by the provided metricNames. It writes the gathered metrics to a bugger formatted according
// to the provided format argument
// CollectAndFormat collects the given `metricNames` metrics using pedantic registry and returns in the given format.

Do you mind fixing GatherAndCompare in the same manner, if that's ok with you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I'm OK with that

// CollectAndFormat registers the provided Collector with a newly created pedantic Registry. It then calls Gather with
// that Registry and filters by the provided metricNames. It writes the gathered metrics to a bugger formatted according
// to the provided format argument
func CollectAndFormat(c prometheus.Collector, format expfmt.FormatType, metricNames ...string) (*bytes.Buffer, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Any reason to NOT return bytes or string instead? I would vote for string, it does not matter much. Buffer is weird, usually when you want things to be in buffer, we would need to pass io.Writer in args or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, no reason I don't think. Although I don't totally understand this:

we would need to pass io.Writer in args or something

Why is this the case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps it should be bytes, as the requested format can be protobuf?

CollectAndFormat is a helper function that returns the formatted metrics
to the caller, allowing them to use it how they want. This is different
to CollectAndCompare where the comparison is done strictly on behalf of
the caller. Often it is more convenient to perform a simple substring
match on the formatted metric.

Signed-off-by: Jack Cassidy <j.cassidy45@gmail.com>
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.

Thanks!

@bwplotka bwplotka enabled auto-merge (squash) May 9, 2024 15:04
@bwplotka bwplotka merged commit 36b9f46 into prometheus:main May 9, 2024
9 checks passed
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.

None yet

2 participants