|
| 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