Skip to content

Commit 6ea4b9e

Browse files
committed
Add createNodeAndResource unit test
1 parent 3977f60 commit 6ea4b9e

File tree

1 file changed

+75
-5
lines changed

1 file changed

+75
-5
lines changed

receiver/prometheusreceiver/internal/prom_to_otlp_test.go

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@ import (
2020
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
2121
"github.com/stretchr/testify/require"
2222

23+
"go.opentelemetry.io/collector/consumer/pdata"
2324
"go.opentelemetry.io/collector/translator/internaldata"
2425
)
2526

2627
// Parity test to ensure that createNodeAndResource produces identical results to createNodeAndResourcePdata.
2728
func TestCreateNodeAndResourceEquivalence(t *testing.T) {
2829
job, instance, scheme := "converter", "ocmetrics", "http"
2930
ocNode, ocResource := createNodeAndResource(job, instance, scheme)
30-
mdFromOC := internaldata.OCToMetrics(internaldata.MetricsData{
31-
Node: ocNode,
32-
Resource: ocResource,
31+
mdFromOC := internaldata.OCToMetrics(ocNode, ocResource,
3332
// We need to pass in a dummy set of metrics
3433
// just to populate and allow for full conversion.
35-
Metrics: []*metricspb.Metric{
34+
[]*metricspb.Metric{
3635
{
3736
MetricDescriptor: &metricspb.MetricDescriptor{
3837
Name: "m1",
@@ -41,10 +40,81 @@ func TestCreateNodeAndResourceEquivalence(t *testing.T) {
4140
},
4241
},
4342
},
44-
})
43+
)
4544

4645
fromOCResource := mdFromOC.ResourceMetrics().At(0).Resource().Attributes().Sort()
4746
byDirectOTLPResource := createNodeAndResourcePdata(job, instance, scheme).Attributes().Sort()
4847

4948
require.Equal(t, byDirectOTLPResource, fromOCResource)
5049
}
50+
51+
type jobInstanceDefinition struct {
52+
job, instance, host, scheme, port string
53+
}
54+
55+
func makeResourceWithJobInstanceScheme(def *jobInstanceDefinition) pdata.Resource {
56+
resource := pdata.NewResource()
57+
attrs := resource.Attributes()
58+
// Using hardcoded values to assert on outward expectations so that
59+
// when variables change, these tests will fail and we'll have reports.
60+
attrs.UpsertString("service.name", def.job)
61+
attrs.UpsertString("host.name", def.host)
62+
attrs.UpsertString("job", def.job)
63+
attrs.UpsertString("instance", def.instance)
64+
attrs.UpsertString("port", def.port)
65+
attrs.UpsertString("scheme", def.scheme)
66+
return resource
67+
}
68+
69+
func TestCreateNodeAndResourcePromToOTLP(t *testing.T) {
70+
tests := []struct {
71+
name, job string
72+
instance string
73+
scheme string
74+
want pdata.Resource
75+
}{
76+
{
77+
name: "all attributes proper",
78+
job: "job", instance: "localhost:8888", scheme: "http",
79+
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{
80+
"job", "localhost:8888", "localhost", "http", "8888",
81+
}),
82+
},
83+
{
84+
name: "missing port",
85+
job: "job", instance: "myinstance", scheme: "https",
86+
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{
87+
"job", "myinstance", "myinstance", "https", "",
88+
}),
89+
},
90+
{
91+
name: "blank scheme",
92+
job: "job", instance: "myinstance:443", scheme: "",
93+
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{
94+
"job", "myinstance:443", "myinstance", "", "443",
95+
}),
96+
},
97+
{
98+
name: "blank instance, blank scheme",
99+
job: "job", instance: "", scheme: "",
100+
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{
101+
"job", "", "", "", "",
102+
}),
103+
},
104+
{
105+
name: "blank instance, non-blank scheme",
106+
job: "job", instance: "", scheme: "http",
107+
want: makeResourceWithJobInstanceScheme(&jobInstanceDefinition{
108+
"job", "", "", "http", "",
109+
}),
110+
},
111+
}
112+
113+
for _, tt := range tests {
114+
tt := tt
115+
t.Run(tt.name, func(t *testing.T) {
116+
got := createNodeAndResourcePdata(tt.job, tt.instance, tt.scheme)
117+
require.Equal(t, got, tt.want)
118+
})
119+
}
120+
}

0 commit comments

Comments
 (0)