Skip to content

Commit 567a175

Browse files
authored
[batchprocessor] ensure attributes are set on metadata metric (#10245)
Fixing this bug required a change in mdatagen to ensure attributes could be passed in to the telemetry builder. Fixes #9674 --------- Signed-off-by: Alex Boten <[email protected]>
1 parent f355e44 commit 567a175

File tree

9 files changed

+103
-11
lines changed

9 files changed

+103
-11
lines changed

.chloggen/codeboten_fix-9674-2.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: mdatagen
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: support setting an AttributeSet for async instruments
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [9674]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

.chloggen/codeboten_fix-9674.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: batchprocessor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: ensure attributes are set on cardinality metadata metric
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [9674]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

cmd/mdatagen/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
go.opentelemetry.io/collector/pdata v1.8.0
1515
go.opentelemetry.io/collector/receiver v0.101.0
1616
go.opentelemetry.io/collector/semconv v0.101.0
17+
go.opentelemetry.io/otel v1.27.0
1718
go.opentelemetry.io/otel/metric v1.27.0
1819
go.opentelemetry.io/otel/sdk/metric v1.27.0
1920
go.opentelemetry.io/otel/trace v1.27.0
@@ -44,7 +45,6 @@ require (
4445
github.com/prometheus/client_model v0.6.1 // indirect
4546
github.com/prometheus/common v0.53.0 // indirect
4647
github.com/prometheus/procfs v0.15.0 // indirect
47-
go.opentelemetry.io/otel v1.27.0 // indirect
4848
go.opentelemetry.io/otel/exporters/prometheus v0.49.0 // indirect
4949
go.opentelemetry.io/otel/sdk v1.27.0 // indirect
5050
go.uber.org/multierr v1.11.0 // indirect

cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go

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

cmd/mdatagen/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ func templatize(tmplFile string, md metadata) *template.Template {
189189
}
190190
return result
191191
},
192+
"hasAsync": func(t telemetry) bool {
193+
for _, m := range t.Metrics {
194+
if m.Data().IsAsync() {
195+
return true
196+
}
197+
}
198+
return false
199+
},
192200
"inc": func(i int) int { return i + 1 },
193201
"distroURL": func(name string) string {
194202
return distros[name]

cmd/mdatagen/templates/telemetry.go.tmpl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
{{- end }}
1010

11+
{{ if hasAsync .Telemetry }}"go.opentelemetry.io/otel/attribute"{{- end }}
1112
"go.opentelemetry.io/otel/metric"
1213
"go.opentelemetry.io/otel/metric/noop"
1314
"go.opentelemetry.io/otel/trace"
@@ -35,6 +36,7 @@ type TelemetryBuilder struct {
3536
{{- end }}
3637
{{- end }}
3738
level configtelemetry.Level
39+
{{ if hasAsync .Telemetry }}attributeSet attribute.Set{{- end }}
3840
}
3941

4042
// telemetryBuilderOption applies changes to default builder.
@@ -47,6 +49,15 @@ func WithLevel(lvl configtelemetry.Level) telemetryBuilderOption {
4749
}
4850
}
4951

52+
{{- if hasAsync .Telemetry }}
53+
// WithAttributeSet applies a set of attributes for asynchronous instruments.
54+
func WithAttributeSet(set attribute.Set) telemetryBuilderOption {
55+
return func(builder *TelemetryBuilder) {
56+
builder.attributeSet = set
57+
}
58+
}
59+
{{- end }}
60+
5061
{{- range $name, $metric := .Telemetry.Metrics }}
5162
{{ if $metric.Data.Async -}}
5263
// With{{ $name.Render }}Callback sets callback for observable {{ $name.Render }} metric.
@@ -58,6 +69,7 @@ func With{{ $name.Render }}Callback(cb func() {{ $metric.Data.BasicType }}) tele
5869
{{- end }}
5970
{{- end }}
6071

72+
6173
// NewTelemetryBuilder provides a struct with methods to update all internal telemetry
6274
// for a component
6375
func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) {
@@ -85,7 +97,7 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme
8597
{{- end }}
8698
{{ if $metric.Data.Async -}}
8799
metric.With{{ casesTitle $metric.Data.BasicType }}Callback(func(_ context.Context, o metric.{{ casesTitle $metric.Data.BasicType }}Observer) error {
88-
o.Observe(builder.observe{{ $name.Render }}())
100+
o.Observe(builder.observe{{ $name.Render }}(), metric.WithAttributeSet(builder.attributeSet))
89101
return nil
90102
}),
91103
{{- end }}

processor/batchprocessor/batch_processor_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ func TestBatchProcessorSentBySize(t *testing.T) {
275275
IsMonotonic: false,
276276
DataPoints: []metricdata.DataPoint[int64]{
277277
{
278-
Value: 1,
278+
Value: 1,
279+
Attributes: attribute.NewSet(attribute.String("processor", "batch")),
279280
},
280281
},
281282
},

processor/batchprocessor/internal/metadata/generated_telemetry.go

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

processor/batchprocessor/metrics.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,41 @@ type batchProcessorTelemetry struct {
2828

2929
exportCtx context.Context
3030

31-
processorAttr []attribute.KeyValue
31+
processorAttr attribute.Set
3232
telemetryBuilder *metadata.TelemetryBuilder
3333
}
3434

3535
func newBatchProcessorTelemetry(set processor.CreateSettings, currentMetadataCardinality func() int) (*batchProcessorTelemetry, error) {
36+
attrs := attribute.NewSet(attribute.String(obsmetrics.ProcessorKey, set.ID.String()))
37+
3638
telemetryBuilder, err := metadata.NewTelemetryBuilder(set.TelemetrySettings,
3739
metadata.WithLevel(set.MetricsLevel),
3840
metadata.WithProcessorBatchMetadataCardinalityCallback(func() int64 { return int64(currentMetadataCardinality()) }),
41+
metadata.WithAttributeSet(attrs),
3942
)
4043

4144
if err != nil {
4245
return nil, err
4346
}
4447

4548
return &batchProcessorTelemetry{
46-
processorAttr: []attribute.KeyValue{attribute.String(obsmetrics.ProcessorKey, set.ID.String())},
4749
exportCtx: context.Background(),
4850
detailed: set.MetricsLevel == configtelemetry.LevelDetailed,
4951
telemetryBuilder: telemetryBuilder,
52+
processorAttr: attrs,
5053
}, nil
5154
}
5255

5356
func (bpt *batchProcessorTelemetry) record(trigger trigger, sent, bytes int64) {
5457
switch trigger {
5558
case triggerBatchSize:
56-
bpt.telemetryBuilder.ProcessorBatchBatchSizeTriggerSend.Add(bpt.exportCtx, 1, metric.WithAttributes(bpt.processorAttr...))
59+
bpt.telemetryBuilder.ProcessorBatchBatchSizeTriggerSend.Add(bpt.exportCtx, 1, metric.WithAttributeSet(bpt.processorAttr))
5760
case triggerTimeout:
58-
bpt.telemetryBuilder.ProcessorBatchTimeoutTriggerSend.Add(bpt.exportCtx, 1, metric.WithAttributes(bpt.processorAttr...))
61+
bpt.telemetryBuilder.ProcessorBatchTimeoutTriggerSend.Add(bpt.exportCtx, 1, metric.WithAttributeSet(bpt.processorAttr))
5962
}
6063

61-
bpt.telemetryBuilder.ProcessorBatchBatchSendSize.Record(bpt.exportCtx, sent, metric.WithAttributes(bpt.processorAttr...))
64+
bpt.telemetryBuilder.ProcessorBatchBatchSendSize.Record(bpt.exportCtx, sent, metric.WithAttributeSet(bpt.processorAttr))
6265
if bpt.detailed {
63-
bpt.telemetryBuilder.ProcessorBatchBatchSendSizeBytes.Record(bpt.exportCtx, bytes, metric.WithAttributes(bpt.processorAttr...))
66+
bpt.telemetryBuilder.ProcessorBatchBatchSendSizeBytes.Record(bpt.exportCtx, bytes, metric.WithAttributeSet(bpt.processorAttr))
6467
}
6568
}

0 commit comments

Comments
 (0)