Skip to content

[mdatagen] Fix invalid test generation for conditionally_required enum attributes#15029

Open
Krishnachaitanyakc wants to merge 3 commits intoopen-telemetry:mainfrom
Krishnachaitanyakc:fix/issue-14196
Open

[mdatagen] Fix invalid test generation for conditionally_required enum attributes#15029
Krishnachaitanyakc wants to merge 3 commits intoopen-telemetry:mainfrom
Krishnachaitanyakc:fix/issue-14196

Conversation

@Krishnachaitanyakc
Copy link
Copy Markdown

@Krishnachaitanyakc Krishnachaitanyakc commented Mar 30, 2026

Description

When an attribute has requirement_level: conditionally_required and an enum type, mdatagen generated invalid test code. The generated metrics_test.go called With{Attr}MetricAttribute(AttributeEnumAttrRed) — passing the enum constant (an integer) directly — but With{Attr}MetricAttribute expects a string. This caused a compile error in generated test code.

Root cause: The getAttributeValue template helper returns AttributeEnumAttrRed (the typed integer constant), which is correct for required attributes passed as positional arguments. But for conditionally_required attributes, the With{X}MetricAttribute(string) option function expects a string, so .String() must be appended.

Fix:

  • Added a getConditionalAttributeValue template helper in helper.tmpl that appends .String() to enum constants, producing the correct string type.
  • Updated metrics_test.go.tmpl to use getConditionalAttributeValue in the two places that generate With{Attr}MetricAttribute(...) calls for conditionally_required attributes.
  • Fixed config.go.tmpl to only import "slices" when there are actually required attributes in reaggregation metrics (not just any reaggregation metrics), avoiding spurious unused-import compile errors.
  • Updated the sampleconnector metadata.yaml to mark enum_attr as conditionally_required to cover this case in generated sample/test output.

Resolves #14196

…d enum attributes

When an attribute has requirement_level: conditionally_required and an enum
type, the generated metrics_test.go was passing the enum constant directly
(an integer) to With{Attr}MetricAttribute(), which expects a string. This
caused compile errors in generated test code.

Fix by introducing a getConditionalAttributeValue template helper that
appends .String() to enum attribute constants, producing the correct string
type. Also fix the config.go.tmpl to only import "slices" when there are
actually required attributes in reaggregation metrics, avoiding unused import
compile errors.

Resolves open-telemetry#14196

Assisted-by: Claude Sonnet 4.6
Restore stdlib/third-party/local import group separation to satisfy
impi linter rules.
@Krishnachaitanyakc Krishnachaitanyakc marked this pull request as ready for review April 2, 2026 15:27
@Krishnachaitanyakc Krishnachaitanyakc requested review from a team and dmitryax as code owners April 2, 2026 15:27
@Krishnachaitanyakc
Copy link
Copy Markdown
Author

@dmitryax @dmathieu can you please review these changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[mdatagen] Invalid generated tests for conditionally_required enum attribute

1 participant