-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from opentracing/bg/protobuf
Add protocol buffers for binary injection/joining
- Loading branch information
Showing
6 changed files
with
632 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package wire | ||
|
||
// ProtobufCarrier is a DelegatingCarrier that uses protocol buffers as the | ||
// the underlying datastructure. The reason for implementing DelagatingCarrier | ||
// is to allow for end users to serialize the underlying protocol buffers using | ||
// jsonpb or any other serialization forms they want. | ||
type ProtobufCarrier TracerState | ||
|
||
// SetState set's the tracer state. | ||
func (p *ProtobufCarrier) SetState(traceID, spanID int64, sampled bool) { | ||
p.TraceId = traceID | ||
p.SpanId = spanID | ||
p.Sampled = sampled | ||
} | ||
|
||
// State returns the tracer state. | ||
func (p *ProtobufCarrier) State() (traceID, spanID int64, sampled bool) { | ||
traceID = p.TraceId | ||
spanID = p.SpanId | ||
sampled = p.Sampled | ||
return traceID, spanID, sampled | ||
} | ||
|
||
// SetBaggageItem sets a baggage item. | ||
func (p *ProtobufCarrier) SetBaggageItem(key, value string) { | ||
if p.BaggageItems == nil { | ||
p.BaggageItems = map[string]string{key: value} | ||
return | ||
} | ||
|
||
p.BaggageItems[key] = value | ||
} | ||
|
||
// GetBaggage iterates over each baggage item and executes the callback with | ||
// the key:value pair. | ||
func (p *ProtobufCarrier) GetBaggage(f func(k, v string)) { | ||
for k, v := range p.BaggageItems { | ||
f(k, v) | ||
} | ||
} |
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,38 @@ | ||
package wire_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/opentracing/basictracer-go" | ||
"github.com/opentracing/basictracer-go/wire" | ||
) | ||
|
||
func TestProtobufCarrier(t *testing.T) { | ||
var carrier basictracer.DelegatingCarrier = &wire.ProtobufCarrier{} | ||
|
||
var traceID, spanID int64 = 1, 2 | ||
sampled := true | ||
baggageKey, expVal := "key1", "val1" | ||
|
||
carrier.SetState(traceID, spanID, sampled) | ||
carrier.SetBaggageItem(baggageKey, expVal) | ||
gotTraceID, gotSpanID, gotSampled := carrier.State() | ||
if traceID != gotTraceID || spanID != gotSpanID || sampled != gotSampled { | ||
t.Errorf("Wanted state %d %d %t, got %d %d %t", spanID, traceID, sampled, | ||
gotTraceID, gotSpanID, gotSampled) | ||
} | ||
|
||
gotBaggage := map[string]string{} | ||
f := func(k, v string) { | ||
gotBaggage[k] = v | ||
} | ||
|
||
carrier.GetBaggage(f) | ||
value, ok := gotBaggage[baggageKey] | ||
if !ok { | ||
t.Errorf("Expected baggage item %s to exist", baggageKey) | ||
} | ||
if value != expVal { | ||
t.Errorf("Expected key %s to be %s, got %s", baggageKey, expVal, value) | ||
} | ||
} |
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,6 @@ | ||
package wire | ||
|
||
//go:generate protoc --gogofaster_out=$GOPATH/src/github.com/opentracing/basictracer-go/wire wire.proto | ||
|
||
// Run `go get github.com/gogo/protobuf/protoc-gen-gogofaster` to install the | ||
// gogofaster generator binary. |
Oops, something went wrong.