Skip to content

Commit

Permalink
[mdatagen] Remove WithAttributes from the telemetry builder constructor
Browse files Browse the repository at this point in the history
Attribute sets for async instruments now can be set as options to callback setters and async instruments initializers. This allows each async instrument to have its own attribute set.
  • Loading branch information
dmitryax committed Jul 16, 2024
1 parent bf6bd9f commit 72c916e
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 134 deletions.
22 changes: 22 additions & 0 deletions .chloggen/mdatagen-remove-with-attributes-helper.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: mdatagen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Remove WithAttributes option from the telemetry builder constructor.

subtext: |
Attribute sets for async instruments now can be set as options to callback setters and async instruments initializers.
This allows each async instrument to have its own attribute set.
# One or more tracking issues or pull requests related to the change
issues: [10608]

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
2 changes: 1 addition & 1 deletion cmd/mdatagen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
go.opentelemetry.io/collector/pdata v1.12.0
go.opentelemetry.io/collector/receiver v0.105.0
go.opentelemetry.io/collector/semconv v0.105.0
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/metric v1.28.0
go.opentelemetry.io/otel/sdk/metric v1.28.0
go.opentelemetry.io/otel/trace v1.28.0
Expand Down Expand Up @@ -49,6 +48,7 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
go.opentelemetry.io/collector/featuregate v1.12.0 // indirect
go.opentelemetry.io/collector/internal/globalgates v0.105.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.50.0 // indirect
go.opentelemetry.io/otel/sdk v1.28.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,6 @@ func templatize(tmplFile string, md metadata) *template.Template {
}
return result
},
"hasAsync": func(t telemetry) bool {
for _, m := range t.Metrics {
if m.Data().IsAsync() {
return true
}
}
return false
},
"inc": func(i int) int { return i + 1 },
"distroURL": func(name string) string {
return distros[name]
Expand Down
29 changes: 9 additions & 20 deletions cmd/mdatagen/templates/telemetry.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
{{- end }}

{{ if hasAsync .Telemetry }}"go.opentelemetry.io/otel/attribute"{{- end }}
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/trace"
Expand All @@ -33,11 +32,10 @@ type TelemetryBuilder struct {
{{- range $name, $metric := .Telemetry.Metrics }}
{{ $name.Render }} metric.{{ $metric.Data.Instrument }}
{{- if and ($metric.Data.Async) (not $metric.Optional) }}
observe{{ $name.Render }} func() {{ $metric.Data.BasicType }}
observe{{ $name.Render }} func(context.Context, metric.Observer) error
{{- end }}
{{- end }}
level configtelemetry.Level
{{ if hasAsync .Telemetry }}attributeSet attribute.Set{{- end }}
}

// telemetryBuilderOption applies changes to default builder.
Expand All @@ -50,19 +48,10 @@ func WithLevel(lvl configtelemetry.Level) telemetryBuilderOption {
}
}

{{- if hasAsync .Telemetry }}
// WithAttributeSet applies a set of attributes for asynchronous instruments.
func WithAttributeSet(set attribute.Set) telemetryBuilderOption {
return func(builder *TelemetryBuilder) {
builder.attributeSet = set
}
}
{{- end }}

{{- range $name, $metric := .Telemetry.Metrics }}
{{- if $metric.Optional }}
// Init{{ $name.Render }} configures the {{ $name.Render }} metric.
func (builder *TelemetryBuilder) Init{{ $name.Render }}({{ if $metric.Data.Async -}}cb func() {{ $metric.Data.BasicType }}{{- end }}) error {
func (builder *TelemetryBuilder) Init{{ $name.Render }}({{ if $metric.Data.Async -}}cb func() {{ $metric.Data.BasicType }}{{- end }}, opts ...metric.ObserveOption) error {
var err error
builder.{{ $name.Render }}, err = builder.meter.{{ $metric.Data.Instrument }}(
"{{ $name }}",
Expand All @@ -77,7 +66,7 @@ func (builder *TelemetryBuilder) Init{{ $name.Render }}({{ if $metric.Data.Async
return err
}
_, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error {
o.Observe{{ casesTitle $metric.Data.BasicType }}(builder.{{ $name.Render }}, cb(), metric.WithAttributeSet(builder.attributeSet))
o.Observe{{ casesTitle $metric.Data.BasicType }}(builder.{{ $name.Render }}, cb(), opts...)
return nil
}, builder.{{ $name.Render }})
{{- end }}
Expand All @@ -87,9 +76,12 @@ func (builder *TelemetryBuilder) Init{{ $name.Render }}({{ if $metric.Data.Async
{{- else }}
{{ if $metric.Data.Async -}}
// With{{ $name.Render }}Callback sets callback for observable {{ $name.Render }} metric.
func With{{ $name.Render }}Callback(cb func() {{ $metric.Data.BasicType }}) telemetryBuilderOption {
func With{{ $name.Render }}Callback(cb func() {{ $metric.Data.BasicType }}, opts ...metric.ObserveOption) telemetryBuilderOption {
return func(builder *TelemetryBuilder) {
builder.observe{{ $name.Render }} = cb
builder.observe{{ $name.Render }} = func(_ context.Context, o metric.Observer) error {
o.Observe{{ casesTitle $metric.Data.BasicType }}(builder.{{ $name.Render }}, cb(), opts...)
return nil
}
}
}
{{- end }}
Expand Down Expand Up @@ -123,10 +115,7 @@ func NewTelemetryBuilder(settings component.TelemetrySettings, options ...teleme
)
errs = errors.Join(errs, err)
{{- if $metric.Data.Async }}
_, err = builder.meter.RegisterCallback(func(_ context.Context, o metric.Observer) error {
o.Observe{{ casesTitle $metric.Data.BasicType }}(builder.{{ $name.Render }}, builder.observe{{ $name.Render }}(), metric.WithAttributeSet(builder.attributeSet))
return nil
}, builder.{{ $name.Render }})
_, err = builder.meter.RegisterCallback(builder.observe{{ $name.Render }}, builder.{{ $name.Render }})
errs = errors.Join(errs, err)
{{- end }}
{{- end }}
Expand Down
17 changes: 4 additions & 13 deletions exporter/exporterhelper/internal/metadata/generated_telemetry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions exporter/exporterhelper/obsexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ func NewObsReport(cfg ObsReportSettings) (*ObsReport, error) {
}

func newExporter(cfg ObsReportSettings) (*ObsReport, error) {
telemetryBuilder, err := metadata.NewTelemetryBuilder(cfg.ExporterCreateSettings.TelemetrySettings,
metadata.WithAttributeSet(attribute.NewSet(attribute.String(obsmetrics.ExporterKey, cfg.ExporterID.String()))),
)
telemetryBuilder, err := metadata.NewTelemetryBuilder(cfg.ExporterCreateSettings.TelemetrySettings)
if err != nil {
return nil, err
}
Expand Down
8 changes: 6 additions & 2 deletions exporter/exporterhelper/queue_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"go.uber.org/multierr"
"go.uber.org/zap"
Expand Down Expand Up @@ -74,6 +75,7 @@ type queueSender struct {
consumers *queue.Consumers[Request]

telemetryBuilder *metadata.TelemetryBuilder
exporterID component.ID
}

func newQueueSender(q exporterqueue.Queue[Request], set exporter.Settings, numConsumers int,
Expand All @@ -83,6 +85,7 @@ func newQueueSender(q exporterqueue.Queue[Request], set exporter.Settings, numCo
numConsumers: numConsumers,
traceAttribute: attribute.String(obsmetrics.ExporterKey, set.ID.String()),
telemetryBuilder: telemetryBuilder,
exporterID: set.ID,
}
consumeFunc := func(ctx context.Context, req Request) error {
err := qs.nextSender.send(ctx, req)
Expand All @@ -102,9 +105,10 @@ func (qs *queueSender) Start(ctx context.Context, host component.Host) error {
return err
}

opts := metric.WithAttributeSet(attribute.NewSet(attribute.String(obsmetrics.ExporterKey, qs.exporterID.String())))
return multierr.Append(
qs.telemetryBuilder.InitExporterQueueSize(func() int64 { return int64(qs.queue.Size()) }),
qs.telemetryBuilder.InitExporterQueueCapacity(func() int64 { return int64(qs.queue.Capacity()) }),
qs.telemetryBuilder.InitExporterQueueSize(func() int64 { return int64(qs.queue.Size()) }, opts),
qs.telemetryBuilder.InitExporterQueueCapacity(func() int64 { return int64(qs.queue.Capacity()) }, opts),
)
}

Expand Down
23 changes: 7 additions & 16 deletions processor/batchprocessor/internal/metadata/generated_telemetry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions processor/batchprocessor/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func newBatchProcessorTelemetry(set processor.Settings, currentMetadataCardinali

telemetryBuilder, err := metadata.NewTelemetryBuilder(set.TelemetrySettings,
metadata.WithLevel(set.MetricsLevel),
metadata.WithProcessorBatchMetadataCardinalityCallback(func() int64 { return int64(currentMetadataCardinality()) }),
metadata.WithAttributeSet(attrs),
metadata.WithProcessorBatchMetadataCardinalityCallback(func() int64 {
return int64(currentMetadataCardinality())
}, metric.WithAttributeSet(attrs)),
)

if err != nil {
Expand Down
Loading

0 comments on commit 72c916e

Please sign in to comment.