Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rsafonseca authored and felipejfc committed Jul 29, 2024
1 parent b1a821a commit bc5ecf7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
5 changes: 5 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
)

var (
Expand Down Expand Up @@ -540,6 +541,10 @@ func (a *agentImpl) writeToConnection(ctx context.Context, data []byte) error {
}

func createConnectionSpan(ctx context.Context, conn net.Conn, op string) trace.Span {
if ctx == nil {
_, span := noop.NewTracerProvider().Tracer("noop").Start(context.TODO(), op)
return span
}
remoteAddress := ""
if conn.RemoteAddr() != nil {
remoteAddress = conn.RemoteAddr().String()
Expand Down
2 changes: 1 addition & 1 deletion agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ func TestAgentHandle(t *testing.T) {
})

// ag.Close on method exit
mockConn.EXPECT().RemoteAddr().MaxTimes(1)
mockConn.EXPECT().RemoteAddr().MaxTimes(2)
mockConn.EXPECT().Close().MaxTimes(1)

ag.chSend <- pendingWrite{ctx: nil, data: expectedBytes, err: nil}
Expand Down
4 changes: 4 additions & 0 deletions service/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,12 +701,16 @@ func TestRemoteServiceRPC(t *testing.T) {

expected = &test.SomeStruct{}
b, err := proto.Marshal(expected)

assert.NoError(t, err)
mockRPCClient.EXPECT().Call(ctx, protos.RPCType_User, rt, gomock.Any(), expectedMsg, gomock.Any()).Return(&protos.Response{Data: b}, table.err)
}

err := svc.RPC(ctx, table.serverID, rt, table.reply, table.arg)
assert.Equal(t, table.err, err)
if table.reply != nil {
// We should consider dropping XXX_NoUnkeyedLiteral, XXX_unrecognized and XXX_sizecache from generated protobufs as this is unuseful overhead
expected.XXX_sizecache = 0
assert.Equal(t, table.reply, expected)
}
})
Expand Down
14 changes: 8 additions & 6 deletions tracing/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,22 @@ func TestFinishSpan(t *testing.T) {

FinishSpan(ctx, testErr)

spanStatus := span.(interface{ Status() codes.Code }).Status()
spanStatus := span.(sdktrace.ReadOnlySpan).Status().Code
assert.Equal(t, codes.Error, spanStatus)

spanEvents := span.(interface{ Events() []sdktrace.Event }).Events()
assert.Len(t, spanEvents, 1)
assert.Equal(t, "exception", spanEvents[0].Name)
assert.Equal(t, testErr.Error(), spanEvents[0].Attributes[0].Value.AsString())
spanError := span.(sdktrace.ReadOnlySpan).Status().Description
assert.Equal(t, testErr.Error(), spanError)
})

t.Run("without error", func(t *testing.T) {
ctx, span := StartSpan(context.Background(), "test-operation")

FinishSpan(ctx, nil)

spanStatus := span.(interface{ Status() codes.Code }).Status()
spanStatus := span.(sdktrace.ReadOnlySpan).Status().Code
assert.Equal(t, codes.Unset, spanStatus)

spanEvents := span.(interface{ Events() []sdktrace.Event }).Events()
Expand All @@ -185,20 +186,21 @@ func TestFinishSpanWithErr(t *testing.T) {
ctx, span := StartSpan(context.Background(), "my-op", attribute.String("hi", "hello"))
assert.NotPanics(t, func() { FinishSpan(ctx, errors.New("hello")) })

spanStatus := span.(interface{ Status() codes.Code }).Status()
spanStatus := span.(sdktrace.ReadOnlySpan).Status().Code
assert.Equal(t, codes.Error, spanStatus)

spanEvents := span.(interface{ Events() []sdktrace.Event }).Events()
assert.Len(t, spanEvents, 1)
assert.Equal(t, "exception", spanEvents[0].Name)
assert.Equal(t, "hello", spanEvents[0].Attributes[0].Value.AsString())
spanError := span.(sdktrace.ReadOnlySpan).Status().Description
assert.Equal(t, "hello", spanError)
}

func TestFinishSpanWithoutErr(t *testing.T) {
ctx, span := StartSpan(context.Background(), "my-op", attribute.String("hi", "hello"))
assert.NotPanics(t, func() { FinishSpan(ctx, nil) })

spanStatus := span.(interface{ Status() codes.Code }).Status()
spanStatus := span.(sdktrace.ReadOnlySpan).Status().Code
assert.Equal(t, codes.Unset, spanStatus)

spanEvents := span.(interface{ Events() []sdktrace.Event }).Events()
Expand Down
16 changes: 13 additions & 3 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,20 @@ func StartSpanFromRequest(
attributes := []attribute.KeyValue{
attribute.String("local.id", serverID),
attribute.String("span.kind", "server"),
attribute.String("peer.id", pcontext.GetFromPropagateCtx(ctx, constants.PeerIDKey).(string)),
attribute.String("peer.service", pcontext.GetFromPropagateCtx(ctx, constants.PeerServiceKey).(string)),
attribute.String("request.id", pcontext.GetFromPropagateCtx(ctx, constants.RequestIDKey).(string)),
}
peerId, ok := pcontext.GetFromPropagateCtx(ctx, constants.PeerIDKey).(string)
if ok {
attributes = append(attributes, attribute.String("peer.id", peerId))
}
peerService, ok := pcontext.GetFromPropagateCtx(ctx, constants.PeerServiceKey).(string)
if ok {
attributes = append(attributes, attribute.String("peer.service", peerService))
}
requestId, ok := pcontext.GetFromPropagateCtx(ctx, constants.RequestIDKey).(string)
if ok {
attributes = append(attributes, attribute.String("request.id", requestId))
}

ctx, _ = tracing.StartSpan(ctx, route, attributes...)

return ctx
Expand Down
8 changes: 5 additions & 3 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
pcontext "github.com/topfreegames/pitaya/v2/context"
"github.com/topfreegames/pitaya/v2/protos"
"github.com/topfreegames/pitaya/v2/serialize/mocks"
"github.com/topfreegames/pitaya/v2/tracing"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/trace"
Expand Down Expand Up @@ -249,6 +250,8 @@ func TestStartSpanFromRequest(t *testing.T) {
// Assert
assert.NotNil(t, newCtx)

tracing.FinishSpan(newCtx, nil)

spans := exporter.GetSpans()
assert.Len(t, spans, 1)

Expand Down Expand Up @@ -285,6 +288,8 @@ func TestStartSpanFromRequest(t *testing.T) {
// Assert
assert.NotNil(t, newCtx)

tracing.FinishSpan(newCtx, nil)

spans := exporter.GetSpans()
assert.Len(t, spans, 1)

Expand All @@ -294,9 +299,6 @@ func TestStartSpanFromRequest(t *testing.T) {
expectedAttrs := []attribute.KeyValue{
attribute.String("local.id", serverID),
attribute.String("span.kind", "server"),
attribute.String("peer.id", ""),
attribute.String("peer.service", ""),
attribute.String("request.id", ""),
}

for _, attr := range expectedAttrs {
Expand Down

0 comments on commit bc5ecf7

Please sign in to comment.