Skip to content

Commit

Permalink
use expected traces with ptracetest.CompareTraces
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Oct 12, 2023
1 parent f9eb606 commit 259b290
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 47 deletions.
178 changes: 131 additions & 47 deletions functional_tests/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/docker/docker/api/types"
docker "github.com/docker/docker/client"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/ptracetest"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/signalfxreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/splunkhecreceiver"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -276,15 +277,138 @@ func testNodeJSTraces(t *testing.T) {
waitForTraces(t, 3, tracesConsumer)

latestTrace := tracesConsumer.AllTraces()[len(tracesConsumer.AllTraces())-1]
actualSpan := latestTrace.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0)

expectedSpan := expectedTraces.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0)
expectedSpan.Attributes().Range(func(k string, _ pcommon.Value) bool {
_, ok := actualSpan.Attributes().Get(k)
assert.True(t, ok)
return true
})
ignoreSpanAttribute("net.peer.port", expectedTraces)
ignoreSpanAttribute("net.peer.port", latestTrace)
ignoreSpanAttribute("http.user_agent", expectedTraces)
ignoreSpanAttribute("http.user_agent", latestTrace)
ignoreTraceID(expectedTraces)
ignoreSpanID(expectedTraces)
ignoreTraceID(latestTrace)
ignoreSpanID(latestTrace)
ignoreStartTimestamp(latestTrace)
ignoreEndTimestamp(latestTrace)
ignoreStartTimestamp(expectedTraces)
ignoreEndTimestamp(expectedTraces)

require.NoError(t, ptracetest.CompareTraces(expectedTraces, latestTrace,
ptracetest.IgnoreResourceAttributeValue("container.id"),
ptracetest.IgnoreResourceAttributeValue("k8s.deployment.name"),
ptracetest.IgnoreResourceAttributeValue("k8s.pod.ip"),
ptracetest.IgnoreResourceAttributeValue("k8s.pod.name"),
ptracetest.IgnoreResourceAttributeValue("k8s.pod.uid"),
ptracetest.IgnoreResourceAttributeValue("k8s.replicaset.name"),
ptracetest.IgnoreResourceSpansOrder(),
ptracetest.IgnoreScopeSpansOrder(),
))
}

func ignoreStartTimestamp(traces ptrace.Traces) {
for i := 0; i < traces.ResourceSpans().Len(); i++ {
rs := traces.ResourceSpans().At(i)
for j := 0; j < rs.ScopeSpans().Len(); j++ {
ss := rs.ScopeSpans().At(j)
for k := 0; k < ss.Spans().Len(); k++ {
span := ss.Spans().At(k)
span.SetStartTimestamp(0)
}
}
}
}

func ignoreEndTimestamp(traces ptrace.Traces) {
for i := 0; i < traces.ResourceSpans().Len(); i++ {
rs := traces.ResourceSpans().At(i)
for j := 0; j < rs.ScopeSpans().Len(); j++ {
ss := rs.ScopeSpans().At(j)
for k := 0; k < ss.Spans().Len(); k++ {
span := ss.Spans().At(k)
span.SetEndTimestamp(0)
}
}
}
}

func ignoreSpanAttribute(attributeName string, traces ptrace.Traces) {
for i := 0; i < traces.ResourceSpans().Len(); i++ {
rs := traces.ResourceSpans().At(i)
for j := 0; j < rs.ScopeSpans().Len(); j++ {
ss := rs.ScopeSpans().At(j)
for k := 0; k < ss.Spans().Len(); k++ {
span := ss.Spans().At(k)
if _, ok := span.Attributes().Get(attributeName); ok {
span.Attributes().PutStr(attributeName, "*")
}
}
}
}
}

func ignoreTraceID(traces ptrace.Traces) {
for i := 0; i < traces.ResourceSpans().Len(); i++ {
rs := traces.ResourceSpans().At(i)
for j := 0; j < rs.ScopeSpans().Len(); j++ {
ss := rs.ScopeSpans().At(j)
for k := 0; k < ss.Spans().Len(); k++ {
span := ss.Spans().At(k)
span.SetTraceID(pcommon.NewTraceIDEmpty())
}
}
}
}

func ignoreSpanID(traces ptrace.Traces) {
for i := 0; i < traces.ResourceSpans().Len(); i++ {
rs := traces.ResourceSpans().At(i)
for j := 0; j < rs.ScopeSpans().Len(); j++ {
ss := rs.ScopeSpans().At(j)
for k := 0; k < ss.Spans().Len(); k++ {
span := ss.Spans().At(k)
span.SetSpanID(pcommon.NewSpanIDEmpty())
}
}
}
}

func shortenNames(value string) string {
if strings.HasPrefix(value, "kube-proxy") {
return "kube-proxy"
}
if strings.HasPrefix(value, "local-path-provisioner") {
return "local-path-provisioner"
}
if strings.HasPrefix(value, "kindnet") {
return "kindnet"
}
if strings.HasPrefix(value, "coredns") {
return "coredns"
}
if strings.HasPrefix(value, "otelcol") {
return "otelcol"
}
if strings.HasPrefix(value, "sock-splunk-otel-collector-agent") {
return "sock-splunk-otel-collector-agent"
}
if strings.HasPrefix(value, "sock-splunk-otel-collector-k8s-cluster-receiver") {
return "sock-splunk-otel-collector-k8s-cluster-receiver"
}
if strings.HasPrefix(value, "cert-manager-cainjector") {
return "cert-manager-cainjector"
}
if strings.HasPrefix(value, "sock-operator") {
return "sock-operator"
}
if strings.HasPrefix(value, "nodejs-test") {
return "nodejs-test"
}
if strings.HasPrefix(value, "cert-manager-webhook") {
return "cert-manager-webhook"
}
if strings.HasPrefix(value, "cert-manager") {
return "cert-manager"
}

return value
}

func testK8sClusterReceiverMetrics(t *testing.T) {
Expand All @@ -294,46 +418,6 @@ func testK8sClusterReceiverMetrics(t *testing.T) {
require.NoError(t, err)

replaceWithStar := func(string) string { return "*" }
shortenNames := func(value string) string {
if strings.HasPrefix(value, "kube-proxy") {
return "kube-proxy"
}
if strings.HasPrefix(value, "local-path-provisioner") {
return "local-path-provisioner"
}
if strings.HasPrefix(value, "kindnet") {
return "kindnet"
}
if strings.HasPrefix(value, "coredns") {
return "coredns"
}
if strings.HasPrefix(value, "otelcol") {
return "otelcol"
}
if strings.HasPrefix(value, "sock-splunk-otel-collector-agent") {
return "sock-splunk-otel-collector-agent"
}
if strings.HasPrefix(value, "sock-splunk-otel-collector-k8s-cluster-receiver") {
return "sock-splunk-otel-collector-k8s-cluster-receiver"
}
if strings.HasPrefix(value, "cert-manager-cainjector") {
return "cert-manager-cainjector"
}
if strings.HasPrefix(value, "sock-operator") {
return "sock-operator"
}
if strings.HasPrefix(value, "nodejs-test") {
return "nodejs-test"
}
if strings.HasPrefix(value, "cert-manager-webhook") {
return "cert-manager-webhook"
}
if strings.HasPrefix(value, "cert-manager") {
return "cert-manager"
}

return value
}
containerImageShorten := func(value string) string {
return value[(strings.LastIndex(value, "/") + 1):]
}
Expand Down
39 changes: 39 additions & 0 deletions functional_tests/internal/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -93,3 +94,41 @@ func writeLogs(filePath string, logs plog.Logs) error {
}
return os.WriteFile(filePath, b, 0600)
}

// WriteTraces writes a ptrace.Traces to the specified file in YAML format.
func WriteTraces(t *testing.T, filePath string, traces ptrace.Traces) error {
if err := writeTraces(filePath, traces); err != nil {
return err
}
t.Logf("Golden file successfully written to %s.", filePath)
t.Log("NOTE: The WriteLogs call must be removed in order to pass the test.")
t.Fail()
return nil
}

func marshalTraces(traces ptrace.Traces) ([]byte, error) {
marshaler := &ptrace.JSONMarshaler{}
fileBytes, err := marshaler.MarshalTraces(traces)
if err != nil {
return nil, err
}
var jsonVal map[string]interface{}
if err = json.Unmarshal(fileBytes, &jsonVal); err != nil {
return nil, err
}
b := &bytes.Buffer{}
enc := yaml.NewEncoder(b)
enc.SetIndent(2)
if err := enc.Encode(jsonVal); err != nil {
return nil, err
}
return b.Bytes(), nil
}

func writeTraces(filePath string, traces ptrace.Traces) error {
b, err := marshalTraces(traces)
if err != nil {
return err
}
return os.WriteFile(filePath, b, 0600)
}

0 comments on commit 259b290

Please sign in to comment.