Skip to content

Conversation

ArthurSens
Copy link
Member

@ArthurSens ArthurSens commented Jun 3, 2025

This PR adds the helper struct UnitNamer, which follows fundamentals similar to MetricNamer and LabelNamer.

When building metadata information, for example:

  • In OTel collector's Remote Write exporter[1]

The unit information is sent separately from the metric name, and that's when this API will be useful.

Which provides an API to translate units from OTel conventions to Prometheus conventions

Signed-off-by: Arthur Silva Sens <[email protected]>
@ArthurSens
Copy link
Member Author

Some extra information:

In the foreseeable future, I can also see a need for such an API once client_golang supports the exposure of Unit metadata (See old PR prometheus/client_golang#1392 that added such a feature but was rejected due to lack of use-cases). While it was rejected in the past, OTel-GO SDK and OTel Collector's Prometheus exporter will only be able to expose unit metadata once client_golang adds support for it.


Second extra information:

To make sure we have consistent APIs with MetricNamer and UnitNamer, I've updated our test to make sure that whatever is built from UnitNamer.Build is also present in MetricNamer.Build if withMetricSuffixes is true.

@aknuds1 aknuds1 requested a review from Copilot June 4, 2025 10:04
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces the UnitNamer API to assist in building compliant unit names separate from metric names when constructing metadata for Prometheus Remote Write exporters. Key changes include:

  • Adding a new helper struct (UnitNamer) with its Build method in unit_namer.go.
  • Refactoring unit mapping and cleanup functions out of metric_namer.go into unit_namer.go.
  • Expanding test coverage in metric_namer_test.go to verify the correlation between metric names and unit names when suffixes are enabled.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
unit_namer.go New file implementing UnitNamer and associated unit-processing functions.
metric_namer_test.go Updated tests to validate both MetricNamer and UnitNamer functionality.
metric_namer.go Removed redundant unit-processing functions, consolidating logic in UnitNamer.
Comments suppressed due to low confidence (1)

unit_namer.go:106

  • Ensure that the variable multipleUnderscoresRE is properly defined or imported within the package so that the cleanUpUnit function works as intended without runtime errors.
return strings.TrimPrefix(multipleUnderscoresRE.ReplaceAllString(

@ArthurSens
Copy link
Member Author

heads up @ywwg, this PR is blocking adoption in otel-collector. No need to rush here, but I'd like to proceed at some point 😇

Copy link
Member

@ywwg ywwg left a comment

Choose a reason for hiding this comment

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

looks great!

@@ -138,7 +138,8 @@ func TestMetricNamer_Build(t *testing.T) {
Unit: "",
Type: MetricTypeGauge,
},
expected: "test@namespace_metric", // TODO: should be "test_namespace_metric"
expectedMetricName: "test@namespace_metric", // TODO: should be "test_namespace_metric"
Copy link
Member

Choose a reason for hiding this comment

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

did we file an issue for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, I don't think we have!

Copy link
Member Author

Choose a reason for hiding this comment

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

#39

@ArthurSens ArthurSens merged commit a885254 into main Jun 11, 2025
8 checks passed
@ArthurSens ArthurSens deleted the unit-builder branch June 11, 2025 13:23
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.

2 participants