Skip to content

Commit

Permalink
mdatagen: support setting telemetry level in telemetry builder (open-…
Browse files Browse the repository at this point in the history
…telemetry#10234)

This allows components to set the minimum level needed for them to
produce telemetry. By default, this is set to
`configtelemetryLevelBasic`. If the telemetry level is below that
minimum level, then the noop meter is used for metrics.

---------

Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
codeboten authored and steves-canva committed Jun 13, 2024
1 parent ead39cc commit 1013bc4
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 26 deletions.
25 changes: 25 additions & 0 deletions .chloggen/codeboten_mdatagen-telemetrylevel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# 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: support using telemetry level in telemetry builder

# One or more tracking issues or pull requests related to the change
issues: [10234]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: This allows components to set the minimum level needed for them to produce telemetry. By default, this is set to configtelemetry.LevelBasic. If the telemetry level is below that minimum level, then the noop meter is used for metrics.

# 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: []
25 changes: 25 additions & 0 deletions .chloggen/codeboten_mdatagen-telemetrylevel2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: respect telemetry level for all metrics

# One or more tracking issues or pull requests related to the change
issues: [10234]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# 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: []
2 changes: 1 addition & 1 deletion cmd/mdatagen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/component v0.101.0
go.opentelemetry.io/collector/config/configtelemetry v0.101.0
go.opentelemetry.io/collector/confmap v0.101.0
go.opentelemetry.io/collector/confmap/provider/fileprovider v0.101.0
go.opentelemetry.io/collector/consumer v0.101.0
Expand Down Expand Up @@ -43,7 +44,6 @@ require (
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.53.0 // indirect
github.com/prometheus/procfs v0.15.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.101.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.49.0 // indirect
go.opentelemetry.io/otel/sdk v1.27.0 // indirect
Expand Down

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

2 changes: 2 additions & 0 deletions cmd/mdatagen/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"strings"

"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/confmap/provider/fileprovider"
Expand Down Expand Up @@ -227,6 +228,7 @@ type tests struct {
}

type telemetry struct {
Level configtelemetry.Level `mapstructure:"level"`
Metrics map[metricName]metric `mapstructure:"metrics"`
}

Expand Down
10 changes: 8 additions & 2 deletions cmd/mdatagen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,12 @@ func TestGenerateTelemetryMetadata(t *testing.T) {
package metadata
import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
)
func Meter(settings component.TelemetrySettings) metric.Meter {
Expand Down Expand Up @@ -569,9 +572,12 @@ func Tracer(settings component.TelemetrySettings) trace.Tracer {
package metadata
import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
)
func Meter(settings component.TelemetrySettings) metric.Meter {
Expand Down
2 changes: 2 additions & 0 deletions cmd/mdatagen/metadata-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ tests:
# Optional: map of metric names with the key being the metric name and value
# being described below.
telemetry:
# Optional: level allows components to specify the minimum telemetry level for metrics to be produced. defaults to basic if not set.
level: string
metrics:
<metric.name>:
# Required: whether the metric is collected by default.
Expand Down
27 changes: 23 additions & 4 deletions cmd/mdatagen/templates/telemetry.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"errors"
{{- end }}

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
)

func Meter(settings component.TelemetrySettings) metric.Meter {
Expand All @@ -31,11 +34,19 @@ type TelemetryBuilder struct {
observe{{ $name.Render }} func() {{ $metric.Data.BasicType }}
{{- end }}
{{- end }}
level configtelemetry.Level
}

// telemetryBuilderOption applies changes to default builder.
type telemetryBuilderOption func(*TelemetryBuilder)

// WithLevel sets the current telemetry level for the component.
func WithLevel(lvl configtelemetry.Level) telemetryBuilderOption {
return func(builder *TelemetryBuilder) {
builder.level = lvl
}
}

{{- range $name, $metric := .Telemetry.Metrics }}
{{ if $metric.Data.Async -}}
// With{{ $name.Render }}Callback sets callback for observable {{ $name.Render }} metric.
Expand All @@ -50,12 +61,20 @@ func With{{ $name.Render }}Callback(cb func() {{ $metric.Data.BasicType }}) tele
// NewTelemetryBuilder provides a struct with methods to update all internal telemetry
// for a component
func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) {
builder := TelemetryBuilder{}
builder := TelemetryBuilder{level: configtelemetry.LevelBasic}
for _, op := range options {
op(&builder)
}
var err, errs error
meter := Meter(settings)
var (
err, errs error
meter metric.Meter
)
if builder.level >= configtelemetry.Level{{ casesTitle .Telemetry.Level.String }} {
meter = Meter(settings)
} else {
meter = noop.Meter{}
}

{{- range $name, $metric := .Telemetry.Metrics }}
builder.{{ $name.Render }}, err = meter.{{ $metric.Data.Instrument }}(
"{{ $name }}",
Expand Down
23 changes: 20 additions & 3 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.

23 changes: 20 additions & 3 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.

1 change: 1 addition & 0 deletions processor/batchprocessor/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ status:
tests:

telemetry:
level: normal
metrics:
processor_batch_batch_size_trigger_send:
enabled: true
Expand Down
2 changes: 1 addition & 1 deletion processor/batchprocessor/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (bpt *batchProcessorTelemetry) createOtelMetrics(set component.TelemetrySet
meter = noopmetric.Meter{}
}

bpt.telemetryBuilder, err = metadata.NewTelemetryBuilder(set)
bpt.telemetryBuilder, err = metadata.NewTelemetryBuilder(set, metadata.WithLevel(bpt.level))
errors = multierr.Append(errors, err)

bpt.batchMetadataCardinality, err = meter.Int64ObservableUpDownCounter(
Expand Down
Loading

0 comments on commit 1013bc4

Please sign in to comment.