Skip to content

Commit

Permalink
Use .Attributes to skip unsafe extraction plus use require.Equal
Browse files Browse the repository at this point in the history
Addresses feedback pointed out by Bogdan, Tigran, Jaana
by using Resource.Attributes().Sort() instead and also avoid
using go-cmp/cmp and instead opt to using require.Equal.
  • Loading branch information
odeke-em committed May 12, 2021
1 parent 122cc14 commit 9e0df1b
Showing 1 changed file with 4 additions and 29 deletions.
33 changes: 4 additions & 29 deletions receiver/prometheusreceiver/internal/prom_to_otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
package internal

import (
"sort"
"testing"
"unsafe"

metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/testing/protocmp"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/consumer/pdata"
otlpresource "go.opentelemetry.io/collector/internal/data/protogen/resource/v1"
"go.opentelemetry.io/collector/translator/internaldata"
)

Expand All @@ -47,28 +42,8 @@ func TestCreateNodeAndResourceConversion(t *testing.T) {
},
})

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

if diff := cmp.Diff(byDirectOTLPResource, fromOCResource, protocmp.Transform()); diff != "" {
t.Fatalf("Resource mismatch: got: - want: +\n%s", diff)
}
}

// Unfortunately pdata doesn't expose a way for us to retrieve the underlying resource,
// yet we need to compare the resources which are hidden by an unexported value.
func protoResource(presource pdata.Resource) *otlpresource.Resource {
type extract struct {
orig *otlpresource.Resource
}
extracted := (*extract)(unsafe.Pointer(&presource))
if extracted == nil {
return nil
}
// Ensure that the attributes are sorted so that we can properly compare the raw values.
resource := extracted.orig
sort.Slice(resource.Attributes, func(i, j int) bool {
return resource.Attributes[i].Key < resource.Attributes[j].Key
})
return resource
require.Equal(t, byDirectOTLPResource, fromOCResource)
}

0 comments on commit 9e0df1b

Please sign in to comment.