Skip to content

Commit ebd3248

Browse files
authored
Return early in semconv generated packages if no attributes passed (#7222)
- Reduces unnecessary allocations for empty attribute - Fixes case where we were double recording for `Record` methods
1 parent e7fa5ba commit ebd3248

File tree

19 files changed

+671
-0
lines changed

19 files changed

+671
-0
lines changed

semconv/templates/registry/go/instrument.j2

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ func (m {{ name }}) Add(
115115
incr {{ value_type(metric) | lower }},
116116
{{ params(metric.attributes, pkg=pkg, prefix="\t") }}
117117
) {
118+
if len(attrs) == 0 {
119+
m.{{ inst }}.Add(ctx, incr)
120+
return
121+
}
122+
118123
o := addOptPool.Get().(*[]metric.AddOption)
119124
defer func() {
120125
*o = (*o)[:0]
@@ -171,6 +176,11 @@ func (m {{ name }}) Record(
171176
val {{ value_type(metric) | lower }},
172177
{{ params(metric.attributes, pkg=pkg, prefix="\t") }}
173178
) {
179+
if len(attrs) == 0 {
180+
m.{{ inst }}.Record(ctx, val)
181+
return
182+
}
183+
174184
o := recOptPool.Get().(*[]metric.RecordOption)
175185
defer func() {
176186
*o = (*o)[:0]
@@ -205,6 +215,7 @@ func (m {{ name }}) Record(
205215
func (m {{ name }}) Record(ctx context.Context, val {{ value_type(metric) | lower }}, attrs ...attribute.KeyValue) {
206216
if len(attrs) == 0 {
207217
m.{{ inst }}.Record(ctx, val)
218+
return
208219
}
209220

210221
o := recOptPool.Get().(*[]metric.RecordOption)

semconv/v1.36.0/azureconv/metric.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

semconv/v1.36.0/cicdconv/metric.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

semconv/v1.36.0/containerconv/metric.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

semconv/v1.36.0/dbconv/metric.go

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

semconv/v1.36.0/dnsconv/metric.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)