Skip to content

Commit

Permalink
fix npe while using nonRecording span (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
123liuziming authored Dec 9, 2024
1 parent e4f7186 commit d71d610
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (h *HttpServerAttrsExtractor[REQUEST, RESPONSE, GETTER1, GETTER2, GETTER3])
attributes, context = h.NetworkExtractor.OnEnd(attributes, context, request, response, err)
span := trace.SpanFromContext(context)
localRootSpan, ok := span.(sdktrace.ReadOnlySpan)
if ok {
if ok && span.IsRecording() {
route := h.Base.HttpGetter.GetHttpRoute(request)
if !strings.Contains(localRootSpan.Name(), route) {
route = localRootSpan.Name()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func TestHttpServerExtractorEnd(t *testing.T) {
}
attrs := make([]attribute.KeyValue, 0)
ctx := context.Background()
ctx = trace.ContextWithSpan(ctx, &testReadOnlySpan{})
ctx = trace.ContextWithSpan(ctx, &testReadOnlySpan{isRecording: true})
attrs, _ = httpServerExtractor.OnEnd(attrs, ctx, testRequest{}, testResponse{}, nil)
if attrs[0].Key != semconv.HTTPResponseStatusCodeKey || attrs[0].Value.AsInt64() != 200 {
t.Fatalf("status code should be 200")
Expand Down Expand Up @@ -392,3 +392,48 @@ func TestHttpClientExtractorWithFilter(t *testing.T) {
panic("attribute should be test")
}
}

func TestNonRecordingSpan(t *testing.T) {
httpServerExtractor := HttpServerAttrsExtractor[testRequest, testResponse, httpServerAttrsGetter, networkAttrsGetter, urlAttrsGetter]{
Base: HttpCommonAttrsExtractor[testRequest, testResponse, httpServerAttrsGetter, networkAttrsGetter]{},
NetworkExtractor: net.NetworkAttrsExtractor[testRequest, testResponse, networkAttrsGetter]{},
UrlExtractor: net.UrlAttrsExtractor[testRequest, testResponse, urlAttrsGetter]{},
}
attrs := make([]attribute.KeyValue, 0)
ctx := context.Background()
ctx = trace.ContextWithSpan(ctx, &testReadOnlySpan{isRecording: false})
attrs, _ = httpServerExtractor.OnEnd(attrs, ctx, testRequest{}, testResponse{}, nil)
if attrs[0].Key != semconv.HTTPResponseStatusCodeKey || attrs[0].Value.AsInt64() != 200 {
t.Fatalf("status code should be 200")
}
if attrs[1].Key != semconv.NetworkProtocolNameKey || attrs[1].Value.AsString() != "network-protocol-name" {
t.Fatalf("wrong network protocol name")
}
if attrs[2].Key != semconv.NetworkProtocolVersionKey || attrs[2].Value.AsString() != "network-protocol-version" {
t.Fatalf("wrong network protocol version")
}
if attrs[3].Key != semconv.NetworkTransportKey || attrs[3].Value.AsString() != "network-transport" {
t.Fatalf("wrong network transport")
}
if attrs[4].Key != semconv.NetworkTypeKey || attrs[4].Value.AsString() != "network-type" {
t.Fatalf("wrong network type")
}
if attrs[5].Key != semconv.NetworkProtocolNameKey || attrs[5].Value.AsString() != "network-protocol-name" {
t.Fatalf("wrong network protocol name")
}
if attrs[6].Key != semconv.NetworkProtocolVersionKey || attrs[6].Value.AsString() != "network-protocol-version" {
t.Fatalf("wrong network protocol version")
}
if attrs[7].Key != semconv.NetworkLocalAddressKey || attrs[7].Value.AsString() != "network-local-inet-address" {
t.Fatalf("wrong network protocol inet address")
}
if attrs[8].Key != semconv.NetworkPeerAddressKey || attrs[8].Value.AsString() != "network-peer-inet-address" {
t.Fatalf("wrong network peer address")
}
if attrs[9].Key != semconv.NetworkLocalPortKey || attrs[9].Value.AsInt64() != 8080 {
t.Fatalf("wrong network local port")
}
if attrs[10].Key != semconv.NetworkPeerPortKey || attrs[10].Value.AsInt64() != 8080 {
t.Fatalf("wrong network peer port")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ func (ts testSpan) SetStatus(status codes.Code, desc string) {

type testReadOnlySpan struct {
sdktrace.ReadWriteSpan
isRecording bool
}

func (t *testReadOnlySpan) Name() string {
return "http-route"
}

func (t *testReadOnlySpan) IsRecording() bool {
return t.isRecording
}

type customizedNetHttpAttrsGetter struct {
code int
}
Expand Down

0 comments on commit d71d610

Please sign in to comment.