Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use semconv in integration tests #660

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions test/integration/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import (
"github.com/mariomac/guara/pkg/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"

"github.com/grafana/beyla/test/integration/components/jaeger"
grpcclient "github.com/grafana/beyla/test/integration/components/testserver/grpc/client"
"github.com/grafana/beyla/test/tools"
)

func testHTTPTracesNoTraceID(t *testing.T) {
Expand Down Expand Up @@ -55,7 +58,7 @@ func testHTTPTracesCommon(t *testing.T, doTraceID bool, httpCode int) {
require.Equal(t, http.StatusOK, resp.StatusCode)
var tq jaeger.TracesQuery
require.NoError(t, json.NewDecoder(resp.Body).Decode(&tq))
traces := tq.FindBySpan(jaeger.Tag{Key: "url.path", Type: "string", Value: "/" + slug})
traces := tq.FindBySpan(tools.KeyValueToJaegerTag(semconv.URLPath("/" + slug)))
require.Len(t, traces, 1)
trace = traces[0]
require.Len(t, trace.Spans, 3) // parent - in queue - processing
Expand All @@ -76,20 +79,20 @@ func testHTTPTracesCommon(t *testing.T, doTraceID bool, httpCode int) {
// check duration is at least 10ms
assert.Less(t, (10 * time.Millisecond).Microseconds(), parent.Duration)
// check span attributes
sd := parent.Diff(
jaeger.Tag{Key: "http.request.method", Type: "string", Value: "GET"},
jaeger.Tag{Key: "http.response.status_code", Type: "int64", Value: float64(httpCode)},
jaeger.Tag{Key: "url.path", Type: "string", Value: "/" + slug},
jaeger.Tag{Key: "server.port", Type: "int64", Value: float64(8080)},
jaeger.Tag{Key: "http.route", Type: "string", Value: "/" + slug},
tags := append(
tools.KeyValuesToJaegerTags([]attribute.KeyValue{
semconv.HTTPRequestMethodGet,
semconv.HTTPResponseStatusCode(httpCode),
semconv.ServerPort(8080),
semconv.HTTPRoute("/" + slug),
}),
jaeger.Tag{Key: "span.kind", Type: "string", Value: "server"},
)
sd := parent.Diff(tags...)
assert.Empty(t, sd, sd.String())

if httpCode >= 500 {
sd := parent.Diff(
jaeger.Tag{Key: "otel.status_code", Type: "string", Value: "ERROR"},
)
sd := parent.Diff(tools.KeyValueToJaegerTag(semconv.OTelStatusCodeError))
assert.Empty(t, sd, sd.String())
}

Expand All @@ -107,7 +110,7 @@ func testHTTPTracesCommon(t *testing.T, doTraceID bool, httpCode int) {
assert.LessOrEqual(t,
queue.StartTime+queue.Duration,
parent.StartTime+parent.Duration+1) // adding 1 to tolerate inaccuracies from rounding from ns to ms
// check span attributes

// check span attributes
sd = queue.Diff(
jaeger.Tag{Key: "span.kind", Type: "string", Value: "internal"},
Expand Down Expand Up @@ -145,10 +148,10 @@ func testHTTPTracesCommon(t *testing.T, doTraceID bool, httpCode int) {
assert.Regexp(t, `^beyla-\d+$`, serviceInstance.Value)

jaeger.Diff([]jaeger.Tag{
{Key: "otel.library.name", Type: "string", Value: "github.com/grafana/beyla"},
{Key: "telemetry.sdk.language", Type: "string", Value: "go"},
{Key: "telemetry.sdk.name", Type: "string", Value: "beyla"},
{Key: "service.namespace", Type: "string", Value: "integration-test"},
tools.KeyValueToJaegerTag(semconv.OTelScopeName("github.com/grafana/beyla")),
tools.KeyValueToJaegerTag(semconv.TelemetrySDKLanguageGo),
tools.KeyValueToJaegerTag(semconv.TelemetrySDKName("beyla")),
tools.KeyValueToJaegerTag(semconv.ServiceNamespace("integration-test")),
serviceInstance,
}, process.Tags)
assert.Empty(t, sd, sd.String())
Expand All @@ -162,7 +165,7 @@ func testHTTPTracesCommon(t *testing.T, doTraceID bool, httpCode int) {
require.Equal(t, http.StatusOK, resp.StatusCode)
var tq jaeger.TracesQuery
require.NoError(t, json.NewDecoder(resp.Body).Decode(&tq))
traces := tq.FindBySpan(jaeger.Tag{Key: "url.path", Type: "string", Value: "/metrics"})
traces := tq.FindBySpan(tools.KeyValueToJaegerTag(semconv.URLPath("/metrics")))
require.Len(t, traces, 0)
}

Expand All @@ -180,7 +183,7 @@ func testGRPCTracesForServiceName(t *testing.T, svcName string) {
require.Equal(t, http.StatusOK, resp.StatusCode)
var tq jaeger.TracesQuery
require.NoError(t, json.NewDecoder(resp.Body).Decode(&tq))
traces := tq.FindBySpan(jaeger.Tag{Key: "rpc.method", Type: "string", Value: "/routeguide.RouteGuide/Debug"})
traces := tq.FindBySpan(tools.KeyValueToJaegerTag(semconv.RPCMethod("/routeguide.RouteGuide/Debug")))
require.Len(t, traces, 1)
trace = traces[0]
require.Len(t, trace.Spans, 3) // parent - in queue - processing
Expand All @@ -195,13 +198,15 @@ func testGRPCTracesForServiceName(t *testing.T, svcName string) {
// check duration is at least 10ms (10,000 microseconds)
assert.Less(t, (10 * time.Millisecond).Microseconds(), parent.Duration)
// check span attributes
sd := parent.Diff(
jaeger.Tag{Key: "server.port", Type: "int64", Value: float64(5051)},
jaeger.Tag{Key: "rpc.grpc.status_code", Type: "int64", Value: float64(2)},
jaeger.Tag{Key: "rpc.method", Type: "string", Value: "/routeguide.RouteGuide/Debug"},
jaeger.Tag{Key: "rpc.system", Type: "string", Value: "grpc"},
tags := append(
tools.KeyValuesToJaegerTags([]attribute.KeyValue{
semconv.ServerPort(5051),
semconv.RPCGRPCStatusCodeUnknown,
semconv.RPCMethod("/routeguide.RouteGuide/Debug"),
semconv.RPCSystemGRPC}),
jaeger.Tag{Key: "span.kind", Type: "string", Value: "server"},
)
sd := parent.Diff(tags...)
assert.Empty(t, sd, sd.String())

// Check the information of the "in queue" span
Expand Down
22 changes: 22 additions & 0 deletions test/tools/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tools

import (
"github.com/grafana/beyla/test/integration/components/jaeger"
"go.opentelemetry.io/otel/attribute"
)

func KeyValueToJaegerTag(kv attribute.KeyValue) jaeger.Tag {
return jaeger.Tag{
Key: string(kv.Key),
Type: kv.Value.Type().String(),
Value: kv.Value.AsInterface(),
}
}

func KeyValuesToJaegerTags(kvs []attribute.KeyValue) []jaeger.Tag {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To minimize the use of generic helper files, I'd consider moving these functions to the test/integration/jaeger/jaeger.go files and rename them to something like jaeger.FromOTEL(kv attribute.KeyValue) jaeger.Tag

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the functions and renamed them. if you prefer "FromOTEL" vs "FromOtel" let me know and I can make that change as well 👍.

tags := make([]jaeger.Tag, len(kvs))
for i, kv := range kvs {
tags[i] = KeyValueToJaegerTag(kv)
}
return tags
}
Loading