Skip to content

Commit bbe1e0b

Browse files
committed
receiver/prometheus/internal: add createNodeAndResourcePdata for Prometheus->OTLP Pdata
Starts the progressive effort to convert directly from Prometheus->OTLPPdata directly instead of Prometheus->OpenCensusProto->OTLPPdata by adding a converter for node+resource -> pdata.Resource. Updates #3137
1 parent 51281a7 commit bbe1e0b

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package internal
16+
17+
import (
18+
"net"
19+
20+
"go.opentelemetry.io/collector/consumer/pdata"
21+
"go.opentelemetry.io/collector/translator/conventions"
22+
)
23+
24+
func createNodeAndResourcePdata(job, instance, scheme string) pdata.Resource {
25+
host, port, err := net.SplitHostPort(instance)
26+
if err != nil {
27+
host = instance
28+
}
29+
resource := pdata.NewResource()
30+
attrs := resource.Attributes()
31+
attrs.UpsertString(conventions.AttributeServiceName, job)
32+
attrs.UpsertString(conventions.AttributeHostName, host)
33+
attrs.UpsertString(jobAttr, job)
34+
attrs.UpsertString(instanceAttr, instance)
35+
attrs.UpsertString(portAttr, port)
36+
attrs.UpsertString(schemeAttr, scheme)
37+
38+
39+
return resource
40+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package internal
16+
17+
import (
18+
"sort"
19+
"testing"
20+
"unsafe"
21+
22+
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
23+
"github.com/google/go-cmp/cmp"
24+
"google.golang.org/protobuf/testing/protocmp"
25+
26+
"go.opentelemetry.io/collector/consumer/pdata"
27+
otlpresource "go.opentelemetry.io/collector/internal/data/protogen/resource/v1"
28+
"go.opentelemetry.io/collector/translator/internaldata"
29+
)
30+
31+
func TestCreateNodeAndResourceConversion(t *testing.T) {
32+
job, instance, scheme := "converter", "ocmetrics", "http"
33+
ocNode, ocResource := createNodeAndResource(job, instance, scheme)
34+
mdFromOC := internaldata.OCToMetrics(internaldata.MetricsData{
35+
Node: ocNode,
36+
Resource: ocResource,
37+
// We need to pass in a dummy set of metrics
38+
// just to populate and allow for full conversion.
39+
Metrics: []*metricspb.Metric{
40+
{
41+
MetricDescriptor: &metricspb.MetricDescriptor{
42+
Name: "m1",
43+
Description: "d1",
44+
Unit: "By",
45+
},
46+
},
47+
},
48+
})
49+
50+
fromOCResource := protoResource(mdFromOC.ResourceMetrics().At(0).Resource())
51+
byDirectOTLPResource := protoResource(createNodeAndResourcePdata(job, instance, scheme))
52+
53+
if diff := cmp.Diff(byDirectOTLPResource, fromOCResource, protocmp.Transform()); diff != "" {
54+
t.Fatalf("Resource mismatch: got: - want: +\n%s", diff)
55+
}
56+
}
57+
58+
// Unfortunately pdata doesn't expose a way for us to retrieve the underlying resource,
59+
// yet we need
60+
func protoResource(presource pdata.Resource) *otlpresource.Resource {
61+
type extract struct {
62+
orig *otlpresource.Resource
63+
}
64+
65+
extracted := (*extract)(unsafe.Pointer(&presource))
66+
if extracted == nil {
67+
return nil
68+
}
69+
// Ensure that the attributes are sorted.
70+
resource := extracted.orig
71+
sort.Slice(resource.Attributes, func(i, j int) bool {
72+
return resource.Attributes[i].Key < resource.Attributes[j].Key
73+
})
74+
return resource
75+
}

0 commit comments

Comments
 (0)