Skip to content

Generate helper metrics equal functions to simplify asserts #12167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/mdatagen/internal/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (d *Histogram) HasMonotonic() bool {
}

func (d *Histogram) HasAggregated() bool {
return false
return true
}

func (d *Histogram) Instrument() string {
Expand Down
2 changes: 1 addition & 1 deletion cmd/mdatagen/internal/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMetricData(t *testing.T) {
{&Sum{Async: true}, "Sum", true, true, "ObservableUpDownCounter", true},
{&Sum{MetricValueType: MetricValueType{pmetric.NumberDataPointValueTypeInt}, Async: true}, "Sum", true, true, "Int64ObservableUpDownCounter", true},
{&Sum{MetricValueType: MetricValueType{pmetric.NumberDataPointValueTypeDouble}, Async: true}, "Sum", true, true, "Float64ObservableUpDownCounter", true},
{&Histogram{}, "Histogram", false, false, "Histogram", false},
{&Histogram{}, "Histogram", true, false, "Histogram", false},
} {
assert.Equal(t, arg.wantType, arg.metricData.Type())
assert.Equal(t, arg.wantHasAggregated, arg.metricData.HasAggregated())
Expand Down

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

37 changes: 32 additions & 5 deletions cmd/mdatagen/internal/templates/telemetrytest.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func SetupTelemetry() Telemetry {
return Telemetry{ Telemetry: componenttest.NewTelemetry() }
}

{{- if or isConnector isExporter isExtension isProcessor isReceiver isScraper }}
{{if or isConnector isExporter isExtension isProcessor isReceiver isScraper }}
func (tt *Telemetry) NewSettings() {{ .Status.Class }}.Settings {
set := {{ .Status.Class }}test.NewNopSettings()
set.ID = component.NewID(component.MustNewType("{{ .Type }}"))
Expand All @@ -42,15 +42,43 @@ func (tt *Telemetry) AssertMetrics(t *testing.T, expected []metricdata.Metrics,
require.NoError(t, tt.Reader.Collect(context.Background(), &md))
// ensure all required metrics are present
for _, want := range expected {
got := getMetric(want.Name, md)
got := getMetricFromResource(want.Name, md)
metricdatatest.AssertEqual(t, want, got, opts...)
}

// ensure no additional metrics are emitted
require.Equal(t, len(expected), lenMetrics(md))
}

func getMetric(name string, got metricdata.ResourceMetrics) metricdata.Metrics {
{{range $name, $metric := .Telemetry.Metrics }}
func AssertEqual{{ $name.Render }}(t *testing.T, tt componenttest.Telemetry, dps []metricdata.{{- if eq $metric.Data.Type "Histogram" }} {{$metric.Data.Type}} {{- end }}DataPoint[{{ $metric.Data.BasicType }}], opts ...metricdatatest.Option) {
want := metricdata.Metrics{
Name: "otelcol_{{ $name }}",
Description: "{{ $metric.Description }}",
Unit: "{{ $metric.Unit }}",
Data: metricdata.{{ $metric.Data.Type }}[{{ $metric.Data.BasicType }}]{
{{- if $metric.Data.HasAggregated }}
Temporality: metricdata.CumulativeTemporality,
{{- end }}
{{- if $metric.Data.HasMonotonic }}
IsMonotonic: {{ $metric.Data.Monotonic }},
{{- end }}
DataPoints: dps,
},
}
got := getMetric(t, tt, "otelcol_{{ $name }}")
metricdatatest.AssertEqual(t, want, got, opts...)
}

{{end }}

func getMetric(t *testing.T, tt componenttest.Telemetry, name string) metricdata.Metrics {
var md metricdata.ResourceMetrics
require.NoError(t, tt.Reader.Collect(context.Background(), &md))
return getMetricFromResource(name, md)
}

func getMetricFromResource(name string, got metricdata.ResourceMetrics) metricdata.Metrics {
for _, sm := range got.ScopeMetrics {
for _, m := range sm.Metrics {
if m.Name == name {
Expand All @@ -69,5 +97,4 @@ func lenMetrics(got metricdata.ResourceMetrics) int {
}

return metricsCount
}

}

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

Loading
Loading