File: http_test.go

package info (click to toggle)
golang-github-go-kit-kit 0.13.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,784 kB
  • sloc: sh: 22; makefile: 11
file content (109 lines) | stat: -rw-r--r-- 3,765 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package opentracing_test

import (
	"context"
	"net/http"
	"reflect"
	"testing"

	opentracing "github.com/opentracing/opentracing-go"
	"github.com/opentracing/opentracing-go/ext"
	"github.com/opentracing/opentracing-go/mocktracer"

	kitot "github.com/go-kit/kit/tracing/opentracing"
	"github.com/go-kit/log"
)

func TestTraceHTTPRequestRoundtrip(t *testing.T) {
	logger := log.NewNopLogger()
	tracer := mocktracer.New()

	// Initialize the ctx with a Span to inject.
	beforeSpan := tracer.StartSpan("to_inject").(*mocktracer.MockSpan)
	defer beforeSpan.Finish()
	beforeSpan.SetBaggageItem("baggage", "check")
	beforeCtx := opentracing.ContextWithSpan(context.Background(), beforeSpan)

	toHTTPFunc := kitot.ContextToHTTP(tracer, logger)
	req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
	// Call the RequestFunc.
	afterCtx := toHTTPFunc(beforeCtx, req)

	// The Span should not have changed.
	afterSpan := opentracing.SpanFromContext(afterCtx)
	if beforeSpan != afterSpan {
		t.Error("Should not swap in a new span")
	}

	// No spans should have finished yet.
	finishedSpans := tracer.FinishedSpans()
	if want, have := 0, len(finishedSpans); want != have {
		t.Errorf("Want %v span(s), found %v", want, have)
	}

	// Use HTTPToContext to verify that we can join with the trace given a req.
	fromHTTPFunc := kitot.HTTPToContext(tracer, "joined", logger)
	joinCtx := fromHTTPFunc(afterCtx, req)
	joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)

	joinedContext := joinedSpan.Context().(mocktracer.MockSpanContext)
	beforeContext := beforeSpan.Context().(mocktracer.MockSpanContext)

	if joinedContext.SpanID == beforeContext.SpanID {
		t.Error("SpanID should have changed", joinedContext.SpanID, beforeContext.SpanID)
	}

	// Check that the parent/child relationship is as expected for the joined span.
	if want, have := beforeContext.SpanID, joinedSpan.ParentID; want != have {
		t.Errorf("Want ParentID %q, have %q", want, have)
	}
	if want, have := "joined", joinedSpan.OperationName; want != have {
		t.Errorf("Want %q, have %q", want, have)
	}
	if want, have := "check", joinedSpan.BaggageItem("baggage"); want != have {
		t.Errorf("Want %q, have %q", want, have)
	}
}

func TestContextToHTTPTags(t *testing.T) {
	tracer := mocktracer.New()
	span := tracer.StartSpan("to_inject").(*mocktracer.MockSpan)
	defer span.Finish()
	ctx := opentracing.ContextWithSpan(context.Background(), span)
	req, _ := http.NewRequest("GET", "http://test.biz/path", nil)

	kitot.ContextToHTTP(tracer, log.NewNopLogger())(ctx, req)

	expectedTags := map[string]interface{}{
		string(ext.HTTPMethod):   "GET",
		string(ext.HTTPUrl):      "http://test.biz/path",
		string(ext.PeerHostname): "test.biz",
	}
	if !reflect.DeepEqual(expectedTags, span.Tags()) {
		t.Errorf("Want %q, have %q", expectedTags, span.Tags())
	}
}

func TestHTTPToContextTags(t *testing.T) {
	tracer := mocktracer.New()
	parentSpan := tracer.StartSpan("to_extract").(*mocktracer.MockSpan)
	defer parentSpan.Finish()
	req, _ := http.NewRequest("GET", "http://test.biz/path", nil)
	tracer.Inject(parentSpan.Context(), opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))

	ctx := kitot.HTTPToContext(tracer, "op", log.NewNopLogger())(context.Background(), req)
	opentracing.SpanFromContext(ctx).Finish()

	childSpan := tracer.FinishedSpans()[0]
	expectedTags := map[string]interface{}{
		string(ext.HTTPMethod): "GET",
		string(ext.HTTPUrl):    "http://test.biz/path",
		string(ext.SpanKind):   ext.SpanKindRPCServerEnum,
	}
	if !reflect.DeepEqual(expectedTags, childSpan.Tags()) {
		t.Errorf("Want %q, have %q", expectedTags, childSpan.Tags())
	}
	if want, have := "op", childSpan.OperationName; want != have {
		t.Errorf("Want %q, have %q", want, have)
	}
}