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

metrics: assign histogram metric type on histogram construction #108597

Conversation

ericharmeling
Copy link
Contributor

@ericharmeling ericharmeling commented Aug 11, 2023

This commit assigns prometheusgo.MetricType_HISTOGRAM to the
Metadata.MetricType on histogram construction.

Before this change, GetMetadata() was returning the
Metadata.MetricType zero value (prometheusgo.MetricType_COUNTER)
for all histograms that did not explicitly specify the
prometheusgo.MetricType_HISTOGRAM for Metadata.MetricType in
their Metadata definitions. This prevented checks on histogram
Metadata.MetricType from properly evaluating the metrics as
histograms.

Fixes #106448.
Fixes #107701.

Releaes note: None

@ericharmeling ericharmeling requested review from a team and abarganier and removed request for a team August 11, 2023 15:13
@blathers-crl
Copy link

blathers-crl bot commented Aug 11, 2023

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@ericharmeling ericharmeling added the backport-23.1.x Flags PRs that need to be backported to 23.1 label Aug 11, 2023
@ericharmeling
Copy link
Contributor Author

For more details, see #107701 (comment)

Copy link
Collaborator

@dhartunian dhartunian left a comment

Choose a reason for hiding this comment

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

Reviewed all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @abarganier and @ericharmeling)


pkg/util/metric/metric.go line 404 at r1 (raw file):

func (h *Histogram) GetMetadata() Metadata {
	baseMetadata := h.Metadata
	baseMetadata.MetricType = prometheusgo.MetricType_HISTOGRAM

why isn't MetricType already set to prometheusgo.MetricType_HISTOGRAM upon metadata or histogram construction?

Copy link
Contributor Author

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @abarganier and @dhartunian)


pkg/util/metric/metric.go line 404 at r1 (raw file):

Previously, dhartunian (David Hartunian) wrote…

why isn't MetricType already set to prometheusgo.MetricType_HISTOGRAM upon metadata or histogram construction?

That's a good question.

We are only explicitly setting MetricType for a subset of metrics at metadata construction (only 3 histograms actually, in ttljob_metrics - see #107701 (comment)). For these metrics (most histograms), we are only ever returning the metric-specific MetricType via some getter function (either GetMetadata or GetType).

I have some guesses as to why this was designed this way, but I'm not 100% sure. So I opted to leave things the way they are, and just fix the metadata getter implementation. Do you think it's worth looking into and refactoring?

Copy link
Contributor

@abarganier abarganier left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dhartunian)


pkg/util/metric/metric.go line 404 at r1 (raw file):

Previously, ericharmeling (Eric Harmeling) wrote…

That's a good question.

We are only explicitly setting MetricType for a subset of metrics at metadata construction (only 3 histograms actually, in ttljob_metrics - see #107701 (comment)). For these metrics (most histograms), we are only ever returning the metric-specific MetricType via some getter function (either GetMetadata or GetType).

I have some guesses as to why this was designed this way, but I'm not 100% sure. So I opted to leave things the way they are, and just fix the metadata getter implementation. Do you think it's worth looking into and refactoring?

I sort of agree with David that it feels a bit off to just set and return this in the GetMetadata() function, instead of setting it on construction.

We don't necessarily have to update the metric.Metadata for every histogram to do this. Can't we just add something to NewHistogram [1] to set it there?

The concern here is inconsistent state. When you call GetMetadata() on a Histogram, it tells you that the MetricType is HISTOGRAM. But other code paths that do any sort of introspection on a Histogram won't see this same state for MetricType. That can lead to nasty bugs in the future because people wouldn't expect that it's only artificially set when GetMetadata() is called, but it's not actually reflected in the underlying state.

I think unless there's a good reason to avoid setting it in NewHistogram, we should just do it there for consistency.

@ericharmeling ericharmeling force-pushed the resolve-metadata-metric-type-histograms branch from 221e294 to d0f11b7 Compare August 21, 2023 15:28
Copy link
Contributor Author

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dhartunian)


pkg/util/metric/metric.go line 404 at r1 (raw file):

Previously, abarganier (Alex Barganier) wrote…

I sort of agree with David that it feels a bit off to just set and return this in the GetMetadata() function, instead of setting it on construction.

We don't necessarily have to update the metric.Metadata for every histogram to do this. Can't we just add something to NewHistogram [1] to set it there?

The concern here is inconsistent state. When you call GetMetadata() on a Histogram, it tells you that the MetricType is HISTOGRAM. But other code paths that do any sort of introspection on a Histogram won't see this same state for MetricType. That can lead to nasty bugs in the future because people wouldn't expect that it's only artificially set when GetMetadata() is called, but it's not actually reflected in the underlying state.

I think unless there's a good reason to avoid setting it in NewHistogram, we should just do it there for consistency.

That makes a lot of sense! Updated this PR to assign the right metric type on histogram construction.

@ericharmeling ericharmeling changed the title metrics: assign histogram metric type before returning metadata metrics: assign histogram metric type on histogram construction Aug 21, 2023
Copy link
Contributor

@abarganier abarganier left a comment

Choose a reason for hiding this comment

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

Changes looking good!

Should we also do this in the runtimeHistogram?

func newRuntimeHistogram(metadata metric.Metadata, buckets []float64) *runtimeHistogram {

Seems like it's off in its own world, but it uses the same Metadata type 🤷

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dhartunian)

This commit assigns prometheusgo.MetricType_HISTOGRAM to the
Metadata.MetricType on histogram construction.

Before this change, GetMetadata() was returning the
Metadata.MetricType zero value (prometheusgo.MetricType_COUNTER)
for all histograms that did not explicitly specify the
prometheusgo.MetricType_HISTOGRAM for Metadata.MetricType in
their Metadata definitions. This prevented checks on histogram
Metadata.MetricType from properly evaluating the metrics as
histograms.

Fixes cockroachdb#106448.
Fixes cockroachdb#107701.

Releaes note: None
@ericharmeling ericharmeling force-pushed the resolve-metadata-metric-type-histograms branch from d0f11b7 to fb8f99b Compare August 22, 2023 20:44
@ericharmeling ericharmeling requested a review from a team as a code owner August 22, 2023 20:44
Copy link
Contributor Author

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

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

TFTR!

Should we also need to do this in the runtimeHistogram?

Good catch - I think we should do it while were doing this. 🤷‍♂️

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dhartunian)

Copy link
Collaborator

@dhartunian dhartunian left a comment

Choose a reason for hiding this comment

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

:lgtm: maybe later on if we touch this code again it might be worth creating separate Metadata structs for different types. There's no reason to mix up counter Metadata with a histogram Metadata.

Reviewed 1 of 1 files at r2, 1 of 1 files at r3, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @abarganier)

Copy link
Contributor

@abarganier abarganier left a comment

Choose a reason for hiding this comment

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

:lgtm: - nice work Eric!

Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @ericharmeling)

Copy link
Contributor

@abarganier abarganier left a comment

Choose a reason for hiding this comment

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

:lgtm: - nice work Eric!

Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @ericharmeling)

Copy link
Contributor Author

@ericharmeling ericharmeling left a comment

Choose a reason for hiding this comment

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

TFTRs!

maybe later on if we touch this code again it might be worth creating separate Metadata structs for different types. There's no reason to mix up counter Metadata with a histogram Metadata.

Right on. Should I make an issue for this work?

Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @ericharmeling)

@ericharmeling
Copy link
Contributor Author

bors r+

@craig craig bot merged commit ee7cdcf into cockroachdb:master Aug 25, 2023
@craig
Copy link
Contributor

craig bot commented Aug 25, 2023

Build succeeded:

@blathers-crl
Copy link

blathers-crl bot commented Aug 25, 2023

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from fb8f99b to blathers/backport-release-22.2-108597: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.2.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.1.x Flags PRs that need to be backported to 23.1 db-cy-23
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tsdump: histograms are missing tsdump is missing percentile metrics
4 participants