Skip to content

[cmd/mdatagen] only import "slices" when a required reagg attribute exists#15145

Open
alliasgher wants to merge 1 commit intoopen-telemetry:mainfrom
alliasgher:fix/mdatagen-slices-import
Open

[cmd/mdatagen] only import "slices" when a required reagg attribute exists#15145
alliasgher wants to merge 1 commit intoopen-telemetry:mainfrom
alliasgher:fix/mdatagen-slices-import

Conversation

@alliasgher
Copy link
Copy Markdown

Description

config.go.tmpl unconditionally imports "slices" whenever reaggregation_enabled: true and there are aggregatable (recommended/opt_in) attributes ($hasReagMetrics = true). However, slices.Contains is only emitted for attributes with requirement_level: required. When a receiver has reaggregation enabled but carries no required attributes, the generated file fails to compile:

```
imported and not used: "slices"
```

This was discovered while enabling re-aggregation in the tlscheck receiver (open-telemetry/opentelemetry-collector-contrib), which has only recommended/opt_in metric attributes (the endpoint is already a resource attribute so no metric attribute needs to be required).

Fix: add a second template variable $hasRequiredReagAttr and gate the "slices" import on it, so the import is only emitted when slices.Contains will actually be called.

Testing

  • The fixed mdatagen generates compilable output for receivers with no required reagg attributes.
  • Existing receivers with required attributes (e.g. tcpcheck) still compile correctly with the fix applied.

…xists

The config.go.tmpl unconditionally imports "slices" whenever
reaggregation is enabled and there are aggregatable (recommended/opt_in)
attributes. However, slices.Contains is only emitted for *required*
attributes. When a receiver has reaggregation enabled but carries no
required attributes, the generated file fails to compile with
"slices imported and not used".

Add a second template variable $hasRequiredReagAttr and gate the
"slices" import on it.

Signed-off-by: Ali <alliasgher123@gmail.com>
@alliasgher alliasgher requested review from a team and dmitryax as code owners April 17, 2026 23:15
nicknikolakakis added a commit to nicknikolakakis/opentelemetry-collector-contrib that referenced this pull request Apr 18, 2026
Set reaggregation_enabled: true in metadata.yaml and mark all attributes
(class, direction, disk_state, packet.type) as recommended. Removed unused
slices import from generated code (upstream mdatagen template bug
open-telemetry/opentelemetry-collector#15145).

Fixes open-telemetry#46369
Part of open-telemetry#45396

Signed-off-by: Nick Nikolakakis <nonicked@protonmail.com>
atoulme pushed a commit to open-telemetry/opentelemetry-collector-contrib that referenced this pull request Apr 23, 2026
Part of #45396.

## Summary

Sets `reaggregation_enabled: true` in `metadata.yaml` and assigns
requirement levels to the three x.509 certificate attributes:

| Attribute | Level | Rationale |
|-----------|-------|-----------|
| `tlscheck.x509.cn` | `recommended` | Enabled by default; identifies
the cert subject |
| `tlscheck.x509.issuer` | `recommended` | Enabled by default;
identifies the cert authority |
| `tlscheck.x509.san` | `opt_in` | Disabled by default; SANs can be
many/verbose |

The `tlscheck.target` is already a resource attribute so no metric
attribute needs `required` level.

Generated metadata files with mdatagen.

## Note

When running `mdatagen` with `recommended`/`opt_in` attributes but no
`required` attribute, the generated `generated_config.go` imports
`"slices"` unconditionally but never calls any `slices.*` function,
causing a compile error. A fix has been submitted to the upstream
mdatagen template: open-telemetry/opentelemetry-collector#15145. The
files in this PR were generated with that fix applied.

## Testing

All existing `receiver/tlscheckreceiver` tests pass with the changes
applied:

\`\`\`
ok
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tlscheckreceiver
ok
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tlscheckreceiver/internal/metadata
\`\`\`

---------

Signed-off-by: Ali <alliasgher123@gmail.com>
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.

1 participant