-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jsm tracing propagation fixed release-1.3.x (#365)
* jsm tracing propagation fixed * refactoring, not functional changes: renamed arguments, inlined func call into if-err block * refactored tracing.go to omit returning nil added tests for tracing.go * changed way how http header obtained in tests * added nil check for msg.Data as well * added nil check into dispatcher * code style fix: ineffectual assignment to span * tracing full test coverage * improved testing, refactored jsm dispatcher according to review comments
- Loading branch information
1 parent
2017237
commit 6a38645
Showing
5 changed files
with
248 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package tracing | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/cloudevents/sdk-go/v2/binding" | ||
"go.opencensus.io/trace" | ||
|
||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
"github.com/nats-io/nats.go" | ||
"github.com/nats-io/stan.go" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const data = `{ | ||
"specversion":"1.0", | ||
"type":"type", | ||
"source":"source", | ||
"id":"1234-1234-1234", | ||
"traceparent": "00-8abe1a4854a9864ffa63046ef07b5dbe-8829876d85d5a76d-01", | ||
"tracestate": "rojo=00f067aa0ba902b7", | ||
"data":{"firstName":"John"} }` | ||
const traceId = "8abe1a4854a9864ffa63046ef07b5dbe" | ||
const tp = "00-" + traceId + "-8829876d85d5a76d-01" | ||
const ts = "rojo=00f067aa0ba902b7" | ||
|
||
func TestConvertEventToHttpHeader(t *testing.T) { | ||
event := cloudevents.NewEvent() | ||
event.SetExtension(traceParentHeader, tp) | ||
event.SetExtension(traceStateHeader, ts) | ||
|
||
headers := ConvertEventToHttpHeader(&event) | ||
if headers.Get(traceParentHeader) != tp { | ||
t.Fatalf("%s header mismatch", traceParentHeader) | ||
} | ||
if headers.Get(traceStateHeader) != ts { | ||
t.Fatalf("%s header mismatch", traceStateHeader) | ||
} | ||
} | ||
|
||
func TestConvertEventToHttpHeaderEmptyEvent(t *testing.T) { | ||
event := cloudevents.NewEvent() | ||
headers := ConvertEventToHttpHeader(&event) | ||
if headers.Get(traceParentHeader) != "" { | ||
t.Fatalf("%s header must be empty", traceParentHeader) | ||
} | ||
if headers.Get(traceStateHeader) != "" { | ||
t.Fatalf("%s header must be empty", traceStateHeader) | ||
} | ||
} | ||
|
||
func TestConvertNatsMsgToEventIsNotNullableIfNil(t *testing.T) { | ||
message := ConvertNatsMsgToEvent(zap.NewNop(), nil) | ||
if message == nil { | ||
t.Fatalf("Message must be non-nil") | ||
} | ||
} | ||
|
||
func TestConvertNatsMsgToEventIsNotNullableEmptyData(t *testing.T) { | ||
msg := nats.NewMsg("subject") | ||
msg.Data = []byte("{}") | ||
message := ConvertNatsMsgToEvent(zap.NewNop(), msg) | ||
if message == nil { | ||
t.Fatalf("Message must be non-nil") | ||
} | ||
} | ||
|
||
func TestConvertNatsMsgToEventIsNotNullableData(t *testing.T) { | ||
msg := nats.Msg{} | ||
msg.Data = []byte(data) | ||
message := ConvertNatsMsgToEvent(zap.NewNop(), &msg) | ||
if message == nil { | ||
t.Fatalf("Message must be non-nil") | ||
} | ||
} | ||
|
||
func TestConvertNatssMsgToEventIsNotNullableIfNil(t *testing.T) { | ||
message := ConvertNatssMsgToEvent(zap.NewNop(), nil) | ||
if message == nil { | ||
t.Fatalf("Message must be non-nil") | ||
} | ||
} | ||
|
||
func TestConvertNatssMsgToEventIsNotNullableEmptyData(t *testing.T) { | ||
msg := stan.Msg{} | ||
msg.Data = []byte("{}") | ||
message := ConvertNatssMsgToEvent(zap.NewNop(), &msg) | ||
if message == nil { | ||
t.Fatalf("Message must be non-nil") | ||
} | ||
} | ||
|
||
func TestConvertNatssMsgToEventIsNotNullableData(t *testing.T) { | ||
msg := stan.Msg{} | ||
msg.Data = []byte(data) | ||
message := ConvertNatssMsgToEvent(zap.NewNop(), &msg) | ||
if message == nil { | ||
t.Fatalf("Message must be non-nil") | ||
} | ||
} | ||
|
||
func TestStartTraceFromMessage(t *testing.T) { | ||
natMsg := nats.Msg{} | ||
natMsg.Data = []byte(data) | ||
msg := ConvertNatsMsgToEvent(zap.NewNop(), &natMsg) | ||
ctx, span := StartTraceFromMessage(zap.NewNop(), context.Background(), msg, "span-name") | ||
tc := trace.FromContext(ctx) | ||
if traceId != tc.SpanContext().TraceID.String() { | ||
t.Fatalf("TraceId is incorrect, expected: %v, actual: %v", traceId, tc.SpanContext().TraceID) | ||
} | ||
if span == nil { | ||
t.Fatalf("Span must be non-nil") | ||
} | ||
} | ||
|
||
func TestStartTraceFromMessageIsNil(t *testing.T) { | ||
ctx, span := StartTraceFromMessage(zap.NewNop(), context.Background(), nil, "span-name") | ||
tc := trace.FromContext(ctx) | ||
if traceId == tc.SpanContext().TraceID.String() { | ||
t.Fatalf("TraceId must be new") | ||
} | ||
if span == nil { | ||
t.Fatalf("Span must be non-nil") | ||
} | ||
} | ||
|
||
func TestStartTraceFromMessageTraceParentIsNil(t *testing.T) { | ||
msg := cloudevents.NewEvent() | ||
ctx, span := StartTraceFromMessage(zap.NewNop(), context.Background(), &msg, "span-name") | ||
tc := trace.FromContext(ctx) | ||
if traceId == tc.SpanContext().TraceID.String() { | ||
t.Fatalf("TraceId must be new") | ||
} | ||
if span == nil { | ||
t.Fatalf("Span must be non-nil") | ||
} | ||
} | ||
|
||
func TestStartTraceFromMessageTraceStateIsNil(t *testing.T) { | ||
msg := cloudevents.NewEvent() | ||
msg.SetExtension(traceParentHeader, tp) | ||
ctx, span := StartTraceFromMessage(zap.NewNop(), context.Background(), &msg, "span-name") | ||
tc := trace.FromContext(ctx) | ||
if traceId != tc.SpanContext().TraceID.String() { | ||
t.Fatalf("TraceId is incorrect, expected: %v, actual: %v", traceId, tc.SpanContext().TraceID) | ||
} | ||
if span == nil { | ||
t.Fatalf("Span must be non-nil") | ||
} | ||
} | ||
|
||
func TestSerializeTraceTransformers(t *testing.T) { | ||
msg := cloudevents.NewEvent() | ||
msg.SetExtension(traceParentHeader, tp) | ||
msg.SetExtension(traceStateHeader, ts) | ||
sc, _ := format.SpanContextFromHeaders(tp, ts) | ||
transformers := SerializeTraceTransformers(sc) | ||
message := binding.ToMessage(&msg) | ||
event, _ := binding.ToEvent(context.Background(), message, transformers...) | ||
if tp != event.Extensions()[traceParentHeader] { | ||
t.Fatalf("Traceparent is incorrect, expected: %v, actual: %v", tp, event.Extensions()[traceParentHeader]) | ||
} | ||
if ts != event.Extensions()[traceStateHeader] { | ||
t.Fatalf("Tracestate is incorrect, expected: %v, actual: %v", tp, event.Extensions()[traceStateHeader]) | ||
} | ||
} |