Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Support ForeachBaggageItem (#85)
Browse files Browse the repository at this point in the history
* Support ForeachBaggageItem
* Return bool from handler
  • Loading branch information
yurishkuro committed Jun 30, 2016
1 parent b228439 commit d5b9be1
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 51 deletions.
8 changes: 4 additions & 4 deletions Makefile
Expand Up @@ -6,6 +6,10 @@
test:
go test -v -cover ./...

.PHONY: fmt
fmt:
go fmt ./...

.PHONY: lint
lint:
golint ./...
Expand All @@ -16,7 +20,3 @@ lint:
vet:
go vet ./...

.PHONY: example
example:
go build -o build/dapperish-example ./examples/dapperish.go
./build/dapperish-example
2 changes: 1 addition & 1 deletion ext/tags.go
Expand Up @@ -33,7 +33,7 @@ var (
// Component name
//////////////////////////////////////////////////////////////////////

// Component is a low-cardinality identifier of the module, library,
// Component is a low-cardinality identifier of the module, library,
// or package that is generating a span.
Component = stringTag("component")

Expand Down
52 changes: 7 additions & 45 deletions ext/tags_test.go
Expand Up @@ -4,8 +4,8 @@ import (
"reflect"
"testing"

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

func assertEqual(t *testing.T, expected, actual interface{}) {
Expand All @@ -19,7 +19,7 @@ func TestPeerTags(t *testing.T) {
if ext.PeerService != "peer.service" {
t.Fatalf("Invalid PeerService %v", ext.PeerService)
}
tracer := noopTracer{}
tracer := mocktracer.New()
span := tracer.StartSpan("my-trace")
ext.PeerService.Set(span, "my-service")
ext.PeerHostname.Set(span, "my-hostname")
Expand All @@ -31,7 +31,7 @@ func TestPeerTags(t *testing.T) {
ext.SpanKind.Set(span, ext.SpanKindRPCClient)
span.Finish()

rawSpan := span.(*noopSpan)
rawSpan := span.(*mocktracer.MockSpan)
assertEqual(t, "my-service", rawSpan.Tags["peer.service"])
assertEqual(t, "my-hostname", rawSpan.Tags["peer.hostname"])
assertEqual(t, uint32(127<<24|1), rawSpan.Tags["peer.ipv4"])
Expand All @@ -40,68 +40,30 @@ func TestPeerTags(t *testing.T) {
}

func TestHTTPTags(t *testing.T) {
tracer := noopTracer{}
tracer := mocktracer.New()
span := tracer.StartSpan("my-trace")
ext.HTTPUrl.Set(span, "test.biz/uri?protocol=false")
ext.HTTPMethod.Set(span, "GET")
ext.HTTPStatusCode.Set(span, 301)
span.Finish()

rawSpan := span.(*noopSpan)
rawSpan := span.(*mocktracer.MockSpan)
assertEqual(t, "test.biz/uri?protocol=false", rawSpan.Tags["http.url"])
assertEqual(t, "GET", rawSpan.Tags["http.method"])
assertEqual(t, uint16(301), rawSpan.Tags["http.status_code"])
}

func TestMiscTags(t *testing.T) {
tracer := noopTracer{}
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 := span.(*noopSpan)
rawSpan := span.(*mocktracer.MockSpan)
assertEqual(t, "my-awesome-library", rawSpan.Tags["component"])
assertEqual(t, uint16(1), rawSpan.Tags["sampling.priority"])
assertEqual(t, true, rawSpan.Tags["error"])
}

// noopTracer and noopSpan with span tags implemented
type noopTracer struct{}

type noopSpan struct {
Tags opentracing.Tags
}

func (n noopSpan) SetTag(key string, value interface{}) opentracing.Span {
n.Tags[key] = value
return n
}

func (n noopSpan) Finish() {}
func (n noopSpan) FinishWithOptions(opts opentracing.FinishOptions) {}
func (n noopSpan) SetBaggageItem(key, val string) opentracing.Span { return n }
func (n noopSpan) BaggageItem(key string) string { return "" }
func (n noopSpan) LogEvent(event string) {}
func (n noopSpan) LogEventWithPayload(event string, payload interface{}) {}
func (n noopSpan) Log(data opentracing.LogData) {}
func (n noopSpan) SetOperationName(operationName string) opentracing.Span { return n }
func (n noopSpan) Tracer() opentracing.Tracer { return nil }

func (n noopTracer) StartSpan(operationName string) opentracing.Span {
return &noopSpan{Tags: make(opentracing.Tags)}
}

func (n noopTracer) StartSpanWithOptions(opts opentracing.StartSpanOptions) opentracing.Span {
return noopSpan{Tags: make(opentracing.Tags)}
}

func (n noopTracer) Inject(sp opentracing.Span, format interface{}, carrier interface{}) error {
panic("not implemented")
}

func (n noopTracer) Join(operationName string, format interface{}, carrier interface{}) (opentracing.Span, error) {
panic("not implemented")
}
9 changes: 9 additions & 0 deletions mocktracer/mocktracer.go
Expand Up @@ -170,6 +170,15 @@ func (s *MockSpan) BaggageItem(key string) string {
return s.Baggage[key]
}

// ForeachBaggageItem belongs to the Span interface
func (s *MockSpan) ForeachBaggageItem(handler func(k, v string) bool) {
for k, v := range s.Baggage {
if !handler(k, v) {
break
}
}
}

// LogEvent belongs to the Span interface
func (s *MockSpan) LogEvent(event string) {
s.Log(opentracing.LogData{
Expand Down
1 change: 1 addition & 0 deletions noop.go
Expand Up @@ -21,6 +21,7 @@ func (n noopSpan) Finish() {}
func (n noopSpan) FinishWithOptions(opts FinishOptions) {}
func (n noopSpan) SetBaggageItem(key, val string) Span { return n }
func (n noopSpan) BaggageItem(key string) string { return emptyString }
func (n noopSpan) ForeachBaggageItem(handler func(k, v string) bool) {}
func (n noopSpan) LogEvent(event string) {}
func (n noopSpan) LogEventWithPayload(event string, payload interface{}) {}
func (n noopSpan) Log(data LogData) {}
Expand Down
1 change: 0 additions & 1 deletion propagation.go
Expand Up @@ -131,7 +131,6 @@ func (c TextMapCarrier) Set(key, val string) {
c[key] = val
}


// HTTPHeaderTextMapCarrier satisfies both TextMapWriter and TextMapReader.
type HTTPHeaderTextMapCarrier http.Header

Expand Down
8 changes: 8 additions & 0 deletions span.go
Expand Up @@ -87,6 +87,14 @@ type Span interface {
// See the `SetBaggageItem` notes about `restrictedKey`.
BaggageItem(restrictedKey string) string

// ForeachBaggageItem allows reading all baggage items stored in the span.
// The handler function will be called for each baggage key/value pair.
// The bool return value indicates if the handler wants to continue iterating
// through the rest of the baggage items; for example if the handler is trying to
// find some baggage item by pattern matching the name, it can return false
// as soon as the item is found to stop further iterations.
ForeachBaggageItem(handler func(k, v string) bool)

// Provides access to the Tracer that created this Span.
Tracer() Tracer
}
Expand Down
1 change: 1 addition & 0 deletions testtracer_test.go
Expand Up @@ -30,6 +30,7 @@ func (n testSpan) Finish() {}
func (n testSpan) FinishWithOptions(opts FinishOptions) {}
func (n testSpan) SetBaggageItem(key, val string) Span { return n }
func (n testSpan) BaggageItem(key string) string { return "" }
func (n testSpan) ForeachBaggageItem(handler func(k, v string) bool) {}
func (n testSpan) LogEvent(event string) {}
func (n testSpan) LogEventWithPayload(event string, payload interface{}) {}
func (n testSpan) Log(data LogData) {}
Expand Down

0 comments on commit d5b9be1

Please sign in to comment.