Skip to content

Optionally add OM unit #1392

Merged
bwplotka merged 53 commits intoprometheus:mainfrom
vesari:add-unit
Apr 2, 2026
Merged

Optionally add OM unit #1392
bwplotka merged 53 commits intoprometheus:mainfrom
vesari:add-unit

Conversation

@vesari
Copy link
Copy Markdown
Contributor

@vesari vesari commented Nov 26, 2023

This PR adds support for unit for Open Metrics. It is ready for review, but not ready to merge (see my long comment below). Fixes #684.

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
vesari added 11 commits January 19, 2024 14:22
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
@ArthurSens
Copy link
Copy Markdown
Member

Hey @vesari , just passing by to ask if there is anything we could do to unblock you 👋

@vesari
Copy link
Copy Markdown
Contributor Author

vesari commented Mar 15, 2024

Hey @vesari , just passing by to ask if there is anything we could do to unblock you 👋

@ArthurSens hello! I think I'm just using a Go version which is too new, I plan on fixing that later today. Thanks a lot for checking on me, much appreciated! :)

vesari added 5 commits March 15, 2024 13:33
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
@vesari
Copy link
Copy Markdown
Contributor Author

vesari commented Dec 14, 2025

This is amazing. Solid work, thanks for finding way to not break compatibility.

Added a few suggestions and ideas, hope those help! Overall this goes in a good direction 👍🏽

Thank you very much, I'm working on addressing your requests for changes and your observations. I'll get back to you once I'm done :)

vesari added 5 commits January 4, 2026 15:53
…t is given

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
…c argument in V2 NewDesc

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
@vesari vesari requested a review from bwplotka January 11, 2026 10:14
@vesari
Copy link
Copy Markdown
Contributor Author

vesari commented Jan 11, 2026

This is now ready for review :)

Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Comment thread prometheus/desc.go Outdated
Copy link
Copy Markdown
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.

Early review, looks good!

Comment thread prometheus/desc.go
Comment thread prometheus/desc.go Outdated
for _, opt := range opts {
opt(d)
}
unit := d.unit
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd inline this

Comment thread prometheus/desc.go Outdated
}
unitStr := ""
if d.unit != "" {
unitStr = fmt.Sprintf(", unit: %q", d.unit)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd add this inline and show unit is empty? We expanded the data structure so we can expand the error too

Comment thread prometheus/example_metricvec_test.go Outdated

func NewInfoVec(name, help string, labelNames []string) *InfoVec {
desc := prometheus.NewDesc(name, help, labelNames, nil)
func NewInfoVec(name, help string, labelNames []string, unit ...string) *InfoVec {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Info likely does not have unit?

Comment thread prometheus/go_collector_latest.go Outdated
sampleMap[d.Name] = &sampleBuf[len(sampleBuf)-1]

// Extract unit from the runtime/metrics name (e.g., "/gc/heap/allocs:bytes" -> "bytes")
unit := d.Name[strings.IndexRune(d.Name, ':')+1:]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As found in https://gist.github.com/bwplotka/a38c33a74fad88a46c4623e924fa58c8#file-prometheusclient_golang1392-md, I think indeed we should handle the case of no unit here gracefully (-1 index response):

> Unsafe Substring Slice during Runtime Metric Unit Extraction

File with exact line number: prometheus/go_collector_latest.go:L213
Link to a file line on GitHub PR: https://github.com/prometheus/client_golang/pull/1392/files#diff-8360f0c0ae2f0d98fb9ecdf37119d6d333dc17ec23f0547055c1ddfdf4ed0759R213
Problem: The unit is extracted using d.Name[strings.IndexRune(d.Name, ':')+1:]. If a runtime metric is introduced without a :, strings.IndexRune evaluates to -1 resulting in -1+1 = 0. The unit silently turns into the entire metric name, bypassing obvious fault detection. (Note: this extraction assumption existed before the PR for histograms, but was generalized here).
Suggestion: Consider checking if strings.ContainsRune(d.Name, ':') before slicing to future-proof the collector against misconfigured runtime metrics.

vesari and others added 6 commits March 19, 2026 11:04
Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>
Signed-off-by: Arianna Vespri <36129782+vesari@users.noreply.github.com>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
@vesari vesari requested a review from bwplotka April 2, 2026 07:19
@vesari
Copy link
Copy Markdown
Contributor Author

vesari commented Apr 2, 2026

Ready for review.

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

It looks generally good, but some comments are not address. If you want to skip them, that's ok, just let us know what's the rationales

Thanks!

Comment on lines +229 to +236
var (
enc expfmt.Encoder
encOpts []expfmt.EncoderOption
)
if opts.EnableOpenMetricsTextCreatedSamples {
enc = expfmt.NewEncoder(w, contentType, expfmt.WithCreatedLines())
} else {
enc = expfmt.NewEncoder(w, contentType)
encOpts = append(encOpts, expfmt.WithCreatedLines())
}
enc = expfmt.NewEncoder(w, contentType, encOpts...)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unrelated change, but if we want this we could simplify further:

Suggested change
var (
enc expfmt.Encoder
encOpts []expfmt.EncoderOption
)
if opts.EnableOpenMetricsTextCreatedSamples {
enc = expfmt.NewEncoder(w, contentType, expfmt.WithCreatedLines())
} else {
enc = expfmt.NewEncoder(w, contentType)
encOpts = append(encOpts, expfmt.WithCreatedLines())
}
enc = expfmt.NewEncoder(w, contentType, encOpts...)
var encOpts []expfmt.EncoderOption
if opts.EnableOpenMetricsTextCreatedSamples {
encOpts = append(encOpts, expfmt.WithCreatedLines())
}
enc := expfmt.NewEncoder(w, contentType, encOpts...)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's do in separate PR

Comment thread prometheus/promhttp/http_test.go
Comment thread prometheus/promhttp/http_test.go
Copy link
Copy Markdown
Contributor Author

@vesari vesari left a comment

Choose a reason for hiding this comment

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

I could not comment if not in the form a review as the GitHub UI is being whimsical for some reason and also doesn't allow me to accept your code suggestion

@vesari
Copy link
Copy Markdown
Contributor Author

vesari commented Apr 2, 2026

@bwplotka About your comment: "do you plan to address this?"
I had originally addressed your Dec 2 comment asking to skip printing the unit when it's an empty string. But then in your March 9 review on desc.go:232, you asked to show the unit inline even when empty ("I'd add this inline and show unit is empty? We expanded the data structure so we can expand the error too"). The current unit: "" in these test expectations follows that guidance. Maybe I misunderstood something?

@vesari
Copy link
Copy Markdown
Contributor Author

vesari commented Apr 2, 2026

@bwplotka about your "ping" comment:
I think I already addressed this? TestHandlerWithEmptyUnit (line 586), TestHandlerWithLongUnit (line 615),and TestHandlerWithEmojiUnit (line 648) are all there. Are they not what you expected?

Copy link
Copy Markdown
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 for catching my misunderstanding (error vs String). All good except one tiny nit!

@bwplotka bwplotka merged commit f23aad5 into prometheus:main Apr 2, 2026
8 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.

OpenMetrics unit support (in v1)

4 participants