Skip to content

Commit

Permalink
add test for mdatagen telemetry metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
diurnalist committed Dec 1, 2024
1 parent d09823b commit 1c1d6f8
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
91 changes: 91 additions & 0 deletions cmd/mdatagen/internal/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pmetric"
)

func TestNewCommand(t *testing.T) {
Expand Down Expand Up @@ -644,6 +645,96 @@ func Meter(settings component.TelemetrySettings) metric.Meter {
func Tracer(settings component.TelemetrySettings) trace.Tracer {
return settings.TracerProvider.Tracer("")
}
`,
},
{
name: "foo component with telemetry metrics",
md: Metadata{
Type: "foo",
Status: &Status{
Stability: map[component.StabilityLevel][]string{
component.StabilityLevelAlpha: {"metrics"},
},
Distributions: []string{"contrib"},
Class: "receiver",
},
Telemetry: Telemetry{
Metrics: map[MetricName]Metric{
"foo_count": {
Sum: &Sum{
MetricValueType: MetricValueType{
ValueType: pmetric.NumberDataPointValueTypeInt,
},
},
},
},
},
},
expected: `// Code generated by mdatagen. DO NOT EDIT.
package metadata
import (
"errors"
"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 {
return settings.MeterProvider.Meter("")
}
func Tracer(settings component.TelemetrySettings) trace.Tracer {
return settings.TracerProvider.Tracer("")
}
// TelemetryBuilder provides an interface for components to report telemetry
// as defined in metadata and user config.
type TelemetryBuilder struct {
meter metric.Meter
FooCount metric.Int64UpDownCounter
}
// TelemetryBuilderOption applies changes to default builder.
type TelemetryBuilderOption interface {
apply(*TelemetryBuilder)
}
type telemetryBuilderOptionFunc func(mb *TelemetryBuilder)
func (tbof telemetryBuilderOptionFunc) apply(mb *TelemetryBuilder) {
tbof(mb)
}
// 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{}
for _, op := range options {
op.apply(&builder)
}
builder.meter = Meter(settings)
var err, errs error
builder.FooCount, err = getLeveledMeter(builder.meter, configtelemetry.LevelBasic, settings.MetricsLevel).Int64UpDownCounter(
"otelcol_foo_count",
metric.WithDescription(""),
metric.WithUnit("<nil>"),
)
errs = errors.Join(errs, err)
return &builder, errs
}
func getLeveledMeter(meter metric.Meter, cfgLevel, srvLevel configtelemetry.Level) metric.Meter {
if cfgLevel <= srvLevel {
return meter
}
return noop.Meter{}
}
`,
},
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/mdatagen/internal/templates/telemetry.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
{{- end }}

"go.opentelemetry.io/otel/metric"
{{- if .Telemetry.Metrics }}
"go.opentelemetry.io/otel/metric/noop"
{{- end }}
"go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/collector/component"
Expand Down

0 comments on commit 1c1d6f8

Please sign in to comment.