This PR addresses issue #7987 (issue (1)): generated semconv metric
helpers should not risk mutating caller-provided `attrs` backing arrays
when required attributes are appended.
Update the generator logic to merge required-attribute using capacity
clamping before append:
```
append(attrs[:len(attrs):len(attrs)], requiredAttrs...)
```
This guarantees append allocates a new backing array for the merged
slice instead of writing into caller memory when there are attributes
appended. It makes not allocations in the case attributes are not
appended.
This is related to issue #7938.
### The Bug
Semconv code generator Jinja2 template (instrument.j2) had an early
return optimization that skipped required attributes when no extra
optional attributes were passed.
The root cause seemed to be two macros in
semconv/templates/registry/go/instrument.j2:
- add_method_with_optional (for counters/updowncounters)
- record_method_with_optional (for histograms/gauges)
Both had `if len(attrs) == 0 { m.Inst.Add(ctx, incr); return }`, which
bypassed required attributes entirely.
### The Fix
I added a conditional check to verify when required attributes exist
`(req_attr | length > 0)`, the early return now passes them via
metric.WithAttributes(...). When there are no required attributes, the
original fast path is preserved.
I also regenerated the semconv `v1.39.0`, but if thats not desired due
to existing clients, let me know. It is my first time contributing here
and any feedback is appreciated!
---------
Co-authored-by: Robert Pająk <pellared@hotmail.com>