opentelemetry-java icon indicating copy to clipboard operation
opentelemetry-java copied to clipboard

Fix serialization of array-valued scope and resource attributes in Prometheus exporter

Open thswlsqls opened this issue 5 months ago • 1 comments

Fixes #5987

Description

  • In Otel2PrometheusConverter.convertAttributes(), scope and resource attributes were serialized with raw Object.toString(), so array values became [a, b] (no quotes, no escaping) instead of valid JSON.
  • Both paths now use the existing toLabelValue(AttributeType, Object) helper, matching the point-attribute path which already JSON-encodes arrays as ["a","b"]. Scalar output (STRING/BOOLEAN/LONG/DOUBLE) is unchanged because toLabelValue returns toString() for them.
  • Completes the incomplete fix from #7291 (which aligned only the point-attribute path with the spec). Spec: non-string attribute values are encoded as JSON strings (OTel Prometheus compatibility, common attributes).

Testing done

  • Added Otel2PrometheusConverterTest#arrayValuedScopeAttributeSerializedAsJson (scope otel_scope_* label) and an array-valued resource attribute case in resourceAttributesAdditionArgs (resource label). Both fail before the fix, pass after.
  • ./gradlew :exporters:prometheus:check — 192 tests pass (Otel2PrometheusConverterTest green; one unrelated flaky PrometheusHttpServerTest.fetch_DuplicateMetrics log-assertion fails intermittently and passes on rerun/in isolation).
  • Private-method change only: no public API change, no docs/apidiffs diff.
  • Added a ## Unreleased entry in CHANGELOG.md.

thswlsqls avatar Jun 21 '26 00:06 thswlsqls

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 91.61%. Comparing base (0004c7e) to head (95c98a0).

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #8497   +/-   ##
=========================================
  Coverage     91.60%   91.61%           
  Complexity    10310    10310           
=========================================
  Files          1013     1013           
  Lines         27258    27260    +2     
  Branches       3201     3201           
=========================================
+ Hits          24971    24973    +2     
  Misses         1560     1560           
  Partials        727      727           

:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Jun 21 '26 00:06 codecov[bot]

Ready for review. Removed the placeholder CHANGELOG link that was failing markdown-link-check.

thswlsqls avatar Jul 05 '26 05:07 thswlsqls

Thank you for your contribution @thswlsqls! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

otelbot[bot] avatar Jul 10 '26 17:07 otelbot[bot]