本文整理汇总了Golang中github.com/opentracing/opentracing-go/mocktracer.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestTraceServer
func TestTraceServer(t *testing.T) {
tracer := mocktracer.New()
// Initialize the ctx with a nameless Span.
contextSpan := tracer.StartSpan("").(*mocktracer.MockSpan)
ctx := opentracing.ContextWithSpan(context.Background(), contextSpan)
var innerEndpoint endpoint.Endpoint
innerEndpoint = func(context.Context, interface{}) (interface{}, error) {
return struct{}{}, nil
}
tracedEndpoint := kitot.TraceServer(tracer, "testOp")(innerEndpoint)
if _, err := tracedEndpoint(ctx, struct{}{}); err != nil {
t.Fatal(err)
}
if want, have := 1, len(tracer.FinishedSpans); want != have {
t.Fatalf("Want %v span(s), found %v", want, have)
}
endpointSpan := tracer.FinishedSpans[0]
// Test that the op name is updated
if want, have := "testOp", endpointSpan.OperationName; want != have {
t.Fatalf("Want %q, have %q", want, have)
}
// ... and that the ID is unmodified.
if want, have := contextSpan.SpanID, endpointSpan.SpanID; want != have {
t.Errorf("Want SpanID %q, have %q", want, have)
}
}
示例2: TestRPCServerOption
func TestRPCServerOption(t *testing.T) {
tracer := mocktracer.New()
parent := tracer.StartSpan("my-trace")
parent.SetBaggageItem("bag", "gage")
carrier := opentracing.HTTPHeadersCarrier{}
err := tracer.Inject(parent.Context(), opentracing.HTTPHeaders, carrier)
if err != nil {
t.Fatal(err)
}
parCtx, err := tracer.Extract(opentracing.HTTPHeaders, carrier)
if err != nil {
t.Fatal(err)
}
tracer.StartSpan("my-child", ext.RPCServerOption(parCtx)).Finish()
rawSpan := tracer.FinishedSpans()[0]
assert.Equal(t, map[string]interface{}{
"span.kind": ext.SpanKindRPCServerEnum,
}, rawSpan.Tags())
assert.Equal(t, map[string]string{
"bag": "gage",
}, rawSpan.Context().(mocktracer.MockSpanContext).Baggage)
}
示例3: TestTraceServer
func TestTraceServer(t *testing.T) {
tracer := mocktracer.New()
// Initialize the ctx with a nameless Span.
contextSpan := tracer.StartSpan("").(*mocktracer.MockSpan)
ctx := opentracing.ContextWithSpan(context.Background(), contextSpan)
tracedEndpoint := kitot.TraceServer(tracer, "testOp")(endpoint.Nop)
if _, err := tracedEndpoint(ctx, struct{}{}); err != nil {
t.Fatal(err)
}
finishedSpans := tracer.FinishedSpans()
if want, have := 1, len(finishedSpans); want != have {
t.Fatalf("Want %v span(s), found %v", want, have)
}
// Test that the op name is updated
endpointSpan := finishedSpans[0]
if want, have := "testOp", endpointSpan.OperationName; want != have {
t.Fatalf("Want %q, have %q", want, have)
}
contextContext := contextSpan.Context().(mocktracer.MockSpanContext)
endpointContext := endpointSpan.Context().(mocktracer.MockSpanContext)
// ...and that the ID is unmodified.
if want, have := contextContext.SpanID, endpointContext.SpanID; want != have {
t.Errorf("Want SpanID %q, have %q", want, have)
}
}
示例4: TestPeerTags
func TestPeerTags(t *testing.T) {
if ext.PeerService != "peer.service" {
t.Fatalf("Invalid PeerService %v", ext.PeerService)
}
tracer := mocktracer.New()
span := tracer.StartSpan("my-trace")
ext.PeerService.Set(span, "my-service")
ext.PeerHostname.Set(span, "my-hostname")
ext.PeerHostIPv4.Set(span, uint32(127<<24|1))
ext.PeerHostIPv6.Set(span, "::")
ext.PeerPort.Set(span, uint16(8080))
ext.SamplingPriority.Set(span, uint16(1))
ext.SpanKind.Set(span, ext.SpanKindRPCServerEnum)
ext.SpanKindRPCClient.Set(span)
span.Finish()
rawSpan := tracer.FinishedSpans()[0]
assert.Equal(t, map[string]interface{}{
"peer.service": "my-service",
"peer.hostname": "my-hostname",
"peer.ipv4": uint32(127<<24 | 1),
"peer.ipv6": "::",
"peer.port": uint16(8080),
"span.kind": ext.SpanKindRPCClientEnum,
}, rawSpan.Tags())
assert.True(t, span.Context().(mocktracer.MockSpanContext).Sampled)
ext.SamplingPriority.Set(span, uint16(0))
assert.False(t, span.Context().(mocktracer.MockSpanContext).Sampled)
}
示例5: TestTraceClient
func TestTraceClient(t *testing.T) {
tracer := mocktracer.New()
// Initialize the ctx with a parent Span.
parentSpan := tracer.StartSpan("parent").(*mocktracer.MockSpan)
defer parentSpan.Finish()
ctx := opentracing.ContextWithSpan(context.Background(), parentSpan)
tracedEndpoint := kitot.TraceClient(tracer, "testOp")(endpoint.Nop)
if _, err := tracedEndpoint(ctx, struct{}{}); err != nil {
t.Fatal(err)
}
// tracedEndpoint created a new Span.
finishedSpans := tracer.FinishedSpans()
if want, have := 1, len(finishedSpans); want != have {
t.Fatalf("Want %v span(s), found %v", want, have)
}
endpointSpan := finishedSpans[0]
if want, have := "testOp", endpointSpan.OperationName; want != have {
t.Fatalf("Want %q, have %q", want, have)
}
parentContext := parentSpan.Context().(mocktracer.MockSpanContext)
endpointContext := parentSpan.Context().(mocktracer.MockSpanContext)
// ... and that the parent ID is set appropriately.
if want, have := parentContext.SpanID, endpointContext.SpanID; want != have {
t.Errorf("Want ParentID %q, have %q", want, have)
}
}
示例6: TestExtractInboundSpanWithZipkinTracer
func TestExtractInboundSpanWithZipkinTracer(t *testing.T) {
tracer := mocktracer.New()
callReq := new(callReq)
callReq.Tracing = Span{traceID: 1, spanID: 2, flags: 1}
callReq.Headers = transportHeaders{
ArgScheme: string(JSON),
CallerName: "caller",
}
c := Connection{
channelConnectionCommon: channelConnectionCommon{tracer: tracer},
remotePeerInfo: PeerInfo{HostPort: "host:123"},
}
c.parseRemotePeerAddress()
// fail to extract with zipkin format, as MockTracer does not support it
assert.Nil(t, c.extractInboundSpan(callReq), "zipkin format not available")
// add zipkin format extractor and try again
tracer.RegisterExtractor(zipkinSpanFormat, new(zipkinExtractor))
span := c.extractInboundSpan(callReq)
require.NotNil(t, span, "zipkin format available")
// validate the extracted span was correctly populated
s1, ok := span.(*mocktracer.MockSpan)
require.True(t, ok)
assert.Equal(t, 1, s1.SpanContext.TraceID)
assert.Equal(t, 2, s1.ParentID)
assert.True(t, s1.SpanContext.Sampled)
assert.Equal(t, "", s1.OperationName, "operation name unknown initially")
assert.Equal(t, string(JSON), s1.Tag("as"))
assert.Equal(t, "caller", s1.Tag(string(ext.PeerService)))
assert.Equal(t, "host", s1.Tag(string(ext.PeerHostname)))
assert.Equal(t, uint16(123), s1.Tag(string(ext.PeerPort)))
// start a temporary span so that we can populate headers with baggage
tempSpan := tracer.StartSpan("test")
tempSpan.SetBaggageItem("x", "y")
headers := make(map[string]string)
carrier := tracingHeadersCarrier(headers)
err := tracer.Inject(tempSpan.Context(), opentracing.TextMap, carrier)
assert.NoError(t, err)
// run the public ExtractInboundSpan method with application headers
inCall := &InboundCall{
response: &InboundCallResponse{
span: span,
},
}
ctx := context.Background()
ctx2 := ExtractInboundSpan(ctx, inCall, headers, tracer)
span = opentracing.SpanFromContext(ctx2)
s2, ok := span.(*mocktracer.MockSpan)
require.True(t, ok)
assert.Equal(t, s1, s2, "should be the same span started previously")
assert.Equal(t, "y", s2.BaggageItem("x"), "baggage should've been added")
}
示例7: TestServer
func TestServer(t *testing.T) {
tracer := mocktracer.New()
s := &Server{HostPort: "127.0.0.1:0", Tracer: tracer}
err := s.Start()
require.NoError(t, err)
defer s.Close()
assert.Equal(t, tracer, s.Ch.Tracer())
port, err := strconv.Atoi(s.Port())
assert.NoError(t, err)
assert.True(t, port > 0)
}
示例8: runWithMockTracer
func (s *PropagationTestSuite) runWithMockTracer(t *testing.T) {
mockTracer := mocktracer.New()
s.runWithTracer(t, tracerChoice{
tracerType: Mock,
tracer: mockTracer,
spansRecorded: func() int {
return len(MockTracerSampledSpans(mockTracer))
},
resetSpans: func() {
mockTracer.Reset()
},
})
}
示例9: TestTraceHTTPRequestRoundtrip
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.ToHTTPRequest(tracer, logger)
req, _ := http.NewRequest("GET", "http://test.biz/url", nil)
// Call the RequestFunc.
afterCtx := toHTTPFunc(beforeCtx, req)
// The Span should not have changed.
afterSpan := opentracing.SpanFromContext(afterCtx)
if beforeSpan != afterSpan {
t.Errorf("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 FromHTTPRequest to verify that we can join with the trace given a req.
fromHTTPFunc := kitot.FromHTTPRequest(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)
}
}
示例10: TestMiscTags
func TestMiscTags(t *testing.T) {
tracer := mocktracer.New()
span := tracer.StartSpan("my-trace")
ext.Component.Set(span, "my-awesome-library")
ext.SamplingPriority.Set(span, 1)
ext.Error.Set(span, true)
span.Finish()
rawSpan := tracer.FinishedSpans()[0]
assert.Equal(t, map[string]interface{}{
"component": "my-awesome-library",
"error": true,
}, rawSpan.Tags())
}
示例11: TestHTTPTags
func TestHTTPTags(t *testing.T) {
tracer := mocktracer.New()
span := tracer.StartSpan("my-trace", ext.SpanKindRPCServer)
ext.HTTPUrl.Set(span, "test.biz/uri?protocol=false")
ext.HTTPMethod.Set(span, "GET")
ext.HTTPStatusCode.Set(span, 301)
span.Finish()
rawSpan := tracer.FinishedSpans()[0]
assert.Equal(t, map[string]interface{}{
"http.url": "test.biz/uri?protocol=false",
"http.method": "GET",
"http.status_code": uint16(301),
"span.kind": ext.SpanKindRPCServerEnum,
}, rawSpan.Tags())
}
示例12: TestCurrentSpan
func TestCurrentSpan(t *testing.T) {
ctx := context.Background()
span := CurrentSpan(ctx)
require.NotNil(t, span, "CurrentSpan() should always return something")
tracer := mocktracer.New()
sp := tracer.StartSpan("test")
ctx = opentracing.ContextWithSpan(ctx, sp)
span = CurrentSpan(ctx)
require.NotNil(t, span, "CurrentSpan() should always return something")
assert.EqualValues(t, 0, span.TraceID(), "mock tracer is not Zipkin-compatible")
tracer.RegisterInjector(zipkinSpanFormat, new(zipkinInjector))
span = CurrentSpan(ctx)
require.NotNil(t, span, "CurrentSpan() should always return something")
assert.NotEqual(t, uint64(0), span.TraceID(), "mock tracer is now Zipkin-compatible")
}
示例13: TestTraceGRPCRequestRoundtrip
func TestTraceGRPCRequestRoundtrip(t *testing.T) {
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)
var toGRPCFunc grpc.RequestFunc = kitot.ToGRPCRequest(tracer, nil)
md := metadata.Pairs()
// Call the RequestFunc.
afterCtx := toGRPCFunc(beforeCtx, &md)
// The Span should not have changed.
afterSpan := opentracing.SpanFromContext(afterCtx)
if beforeSpan != afterSpan {
t.Errorf("Should not swap in a new span")
}
// No spans should have finished yet.
if want, have := 0, len(tracer.FinishedSpans); want != have {
t.Errorf("Want %v span(s), found %v", want, have)
}
// Use FromGRPCRequest to verify that we can join with the trace given MD.
var fromGRPCFunc grpc.RequestFunc = kitot.FromGRPCRequest(tracer, "joined", nil)
joinCtx := fromGRPCFunc(afterCtx, &md)
joinedSpan := opentracing.SpanFromContext(joinCtx).(*mocktracer.MockSpan)
if joinedSpan.SpanID == beforeSpan.SpanID {
t.Error("SpanID should have changed", joinedSpan.SpanID, beforeSpan.SpanID)
}
// Check that the parent/child relationship is as expected for the joined span.
if want, have := beforeSpan.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)
}
}
示例14: TestTracingInjectorExtractor
func TestTracingInjectorExtractor(t *testing.T) {
tracer := mocktracer.New()
tracer.RegisterInjector(zipkinSpanFormat, new(zipkinInjector))
tracer.RegisterExtractor(zipkinSpanFormat, new(zipkinExtractor))
sp := tracer.StartSpan("x")
var injectable injectableSpan
err := tracer.Inject(sp.Context(), zipkinSpanFormat, &injectable)
require.NoError(t, err)
tsp := Span(injectable)
assert.NotEqual(t, uint64(0), tsp.TraceID())
assert.NotEqual(t, uint64(0), tsp.SpanID())
sp2, err := tracer.Extract(zipkinSpanFormat, &tsp)
require.NoError(t, err)
require.NotNil(t, sp2)
}
示例15: TestTChannelTracerDepth2
func TestTChannelTracerDepth2(t *testing.T) {
tracer := mocktracer.New()
dispatcher := createTChannelDispatcher(tracer, t)
client := json.New(dispatcher.Channel("yarpc-test"))
handler := handler{client: client, t: t}
handler.register(dispatcher)
require.NoError(t, dispatcher.Start())
defer dispatcher.Stop()
ctx, cancel := handler.createContextWithBaggage(tracer)
defer cancel()
err := handler.echoEcho(ctx)
assert.NoError(t, err)
AssertDepth2Spans(t, tracer)
}