Skip to content

Commit c2f1599

Browse files
authored
Generate assert function for each metric in mdatagen (#12179)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Completion of #12167 , cc @bogdandrutu Updated one test to verify the change, will update the rest ones after this PR merges. <!-- Issue number if applicable --> #### Link to tracking issue n/a <!--Describe what testing was performed and which tests were added.--> #### Testing Added <!--Describe the documentation added.--> #### Documentation Added <!--Please delete paragraphs that you did not use before submitting.-->
1 parent df99547 commit c2f1599

File tree

20 files changed

+954
-55
lines changed

20 files changed

+954
-55
lines changed
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: Generate assert function for each metric in mdatagen
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12179]
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: [api]

cmd/mdatagen/internal/samplereceiver/internal/metadatatest/generated_telemetrytest.go

+66-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/samplereceiver/internal/metadatatest/generated_telemetrytest_test.go

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/templates/telemetrytest.go.tmpl

+33-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func SetupTelemetry(opts ...componenttest.TelemetryOption) Telemetry {
2828
return Telemetry{ Telemetry: componenttest.NewTelemetry(opts...) }
2929
}
3030

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

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

53-
func getMetric(name string, got metricdata.ResourceMetrics) metricdata.Metrics {
53+
{{ range $name, $metric := .Telemetry.Metrics }}
54+
{{ if not $metric.Optional }}
55+
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) {
56+
want := metricdata.Metrics{
57+
Name: "otelcol_{{ $name }}",
58+
Description: "{{ $metric.Description }}{{ $metric.Stability }}",
59+
Unit: "{{ $metric.Unit }}",
60+
Data: metricdata.{{ $metric.Data.Type }}[{{ $metric.Data.BasicType }}]{
61+
{{- if $metric.Data.HasAggregated }}
62+
Temporality: metricdata.CumulativeTemporality,
63+
{{- end }}
64+
{{- if $metric.Data.HasMonotonic }}
65+
IsMonotonic: {{ $metric.Data.Monotonic }},
66+
{{- end }}
67+
DataPoints: dps,
68+
},
69+
}
70+
got := getMetric(t, tt, "otelcol_{{ $name }}")
71+
metricdatatest.AssertEqual(t, want, got, opts...)
72+
}
73+
74+
{{- end }}
75+
{{- end }}
76+
77+
func getMetric(t *testing.T, tt componenttest.Telemetry, name string) metricdata.Metrics {
78+
var md metricdata.ResourceMetrics
79+
require.NoError(t, tt.Reader.Collect(context.Background(), &md))
80+
return getMetricFromResource(name, md)
81+
}
82+
83+
func getMetricFromResource(name string, got metricdata.ResourceMetrics) metricdata.Metrics {
5484
for _, sm := range got.ScopeMetrics {
5585
for _, m := range sm.Metrics {
5686
if m.Name == name {
@@ -70,4 +100,3 @@ func lenMetrics(got metricdata.ResourceMetrics) int {
70100

71101
return metricsCount
72102
}
73-

cmd/mdatagen/internal/templates/telemetrytest_test.go.tmpl

+15-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ func TestSetupTelemetry(t *testing.T) {
6767
{{- end }}
6868
{{- end }}
6969
}, metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue())
70+
71+
{{- range $name, $metric := .Telemetry.Metrics }}
72+
{{ if not $metric.Optional }}
73+
AssertEqual{{ $name.Render }}(t, testTel.Telemetry,
74+
{{ if eq $metric.Data.Type "Gauge" -}}
75+
[]metricdata.DataPoint[{{ $metric.Gauge.MetricValueType.BasicType }}]{{"{{}}"}},
76+
{{- else if eq $metric.Data.Type "Sum" -}}
77+
[]metricdata.DataPoint[{{ $metric.Sum.MetricValueType.BasicType }}]{{"{{}}"}},
78+
{{- else if eq $metric.Data.Type "Histogram" -}}
79+
[]metricdata.HistogramDataPoint[{{ $metric.Histogram.MetricValueType.BasicType }}]{{"{{}}"}},
80+
{{- end }}
81+
metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue())
82+
{{- end }}
83+
{{- end }}
84+
7085
require.NoError(t, testTel.Shutdown(context.Background()))
7186
}
72-

exporter/exporterhelper/internal/metadatatest/generated_telemetrytest.go

+144-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)