This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ext/tags mechanism for adding common tags to the span
- Loading branch information
Yuri Shkuro
committed
Dec 28, 2015
1 parent
61e3c73
commit 79e44f7
Showing
13 changed files
with
272 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package dapperish | ||
|
||
import ( | ||
"bytes" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package dapperish | ||
|
||
import ( | ||
"math/rand" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package dapperish | ||
|
||
import ( | ||
"fmt" | ||
|
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,51 @@ | ||
package ext | ||
|
||
import ( | ||
"github.com/opentracing/api-golang/opentracing" | ||
) | ||
|
||
var ( | ||
// RPC tags as symmetrical, each can be emitted by either client-side of server-side | ||
|
||
// RPCService records the service name of the RPC peer | ||
RPCService = &stringTag{"rpc.service"} | ||
|
||
// RPCHostname records the host name of the RPC peer | ||
RPCHostname = &stringTag{"rpc.hostname"} | ||
|
||
// RPCHostIPv4 records IP v4 host address of the RPC peer | ||
RPCHostIPv4 = &uint32Tag{"rpc.ipv4"} | ||
|
||
// RPCHostIPv6 records IP v6 host address of the RPC peer | ||
RPCHostIPv6 = &stringTag{"rpc.ipv6"} | ||
|
||
// RPCPort records port number of the RPC peer | ||
RPCPort = &uint16Tag{"rpc.port"} | ||
) | ||
|
||
type stringTag struct { | ||
Key string | ||
} | ||
|
||
// Add adds a string tag to the `span` | ||
func (tag *stringTag) Add(span opentracing.Span, value string) { | ||
span.SetTag(tag.Key, value) | ||
} | ||
|
||
type uint32Tag struct { | ||
Key string | ||
} | ||
|
||
// Add adds a uint32 tag to the `span` | ||
func (tag *uint32Tag) Add(span opentracing.Span, value uint32) { | ||
span.SetTag(tag.Key, value) | ||
} | ||
|
||
type uint16Tag struct { | ||
Key string | ||
} | ||
|
||
// Add adds a uint16 tag to the `span` | ||
func (tag *uint16Tag) Add(span opentracing.Span, value uint16) { | ||
span.SetTag(tag.Key, 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,35 @@ | ||
package ext_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/opentracing/api-golang/examples/dapperish" | ||
"github.com/opentracing/api-golang/opentracing/ext" | ||
"github.com/opentracing/api-golang/opentracing/standardtracer" | ||
"github.com/opentracing/api-golang/testutils" | ||
"github.com/opentracing/api-golang/testutils/assert" | ||
) | ||
|
||
func TestRPCTags(t *testing.T) { | ||
if ext.RPCService.Key != "rpc.service" { | ||
t.Fatalf("Invalid RPCService.Key %v", ext.RPCService.Key) | ||
} | ||
recorder := testutils.NewInMemoryRecorder("test-process") | ||
tracer := standardtracer.New(recorder, dapperish.NewTraceContextSource()) | ||
span := tracer.StartTrace("my-trace") | ||
ext.RPCService.Add(span, "my-service") | ||
ext.RPCHostname.Add(span, "my-hostname") | ||
ext.RPCHostIPv4.Add(span, uint32(127<<24|1)) | ||
ext.RPCHostIPv6.Add(span, "::") | ||
ext.RPCPort.Add(span, uint16(8080)) | ||
span.Finish() | ||
if len(recorder.GetSpans()) != 1 { | ||
t.Fatal("Span not recorded") | ||
} | ||
rawSpan := recorder.GetSpans()[0] | ||
assert.EqualValues(t, "my-service", rawSpan.Tags[ext.RPCService.Key]) | ||
assert.EqualValues(t, "my-hostname", rawSpan.Tags[ext.RPCHostname.Key]) | ||
assert.EqualValues(t, uint32(127<<24|1), rawSpan.Tags[ext.RPCHostIPv4.Key]) | ||
assert.EqualValues(t, "::", rawSpan.Tags[ext.RPCHostIPv6.Key]) | ||
assert.EqualValues(t, 8080, rawSpan.Tags[ext.RPCPort.Key]) | ||
} |
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,57 @@ | ||
package assert | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
// NOTE: the functions in this module are borrowed from https://github.com/stretchr/testify | ||
// to avoid creating a dependency (until Go comes up with proper dependency manager) | ||
|
||
// EqualValues asserts that two objects are equal or convertable to the same types | ||
// and equal. | ||
// | ||
// assert.EqualValues(t, uint32(123), int32(123), "123 and 123 should be equal") | ||
// | ||
// If assertion is not successful, execution halts via t.Fatalf() call | ||
func EqualValues(t *testing.T, expected, actual interface{}) bool { | ||
if !ObjectsAreEqualValues(expected, actual) { | ||
t.Fatalf("Not equal: %#v (expected)\n"+ | ||
" != %#v (actual)", expected, actual) | ||
} | ||
return true | ||
} | ||
|
||
// ObjectsAreEqual determines if two objects are considered equal. | ||
func ObjectsAreEqual(expected, actual interface{}) bool { | ||
|
||
if expected == nil || actual == nil { | ||
return expected == actual | ||
} | ||
|
||
if reflect.DeepEqual(expected, actual) { | ||
return true | ||
} | ||
|
||
return false | ||
|
||
} | ||
|
||
// ObjectsAreEqualValues determines whether two objects are equal, | ||
// or if their values are equal. | ||
func ObjectsAreEqualValues(expected, actual interface{}) bool { | ||
if ObjectsAreEqual(expected, actual) { | ||
return true | ||
} | ||
|
||
actualType := reflect.TypeOf(actual) | ||
expectedValue := reflect.ValueOf(expected) | ||
if expectedValue.Type().ConvertibleTo(actualType) { | ||
// Attempt comparison after type conversion | ||
if reflect.DeepEqual(actual, expectedValue.Convert(actualType).Interface()) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
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,11 @@ | ||
package assert_test | ||
|
||
import ( | ||
"github.com/opentracing/api-golang/testutils/assert" | ||
"testing" | ||
) | ||
|
||
func TestAssert(t *testing.T) { | ||
assert.EqualValues(t, int16(123), int16(123)) | ||
assert.EqualValues(t, int16(123), int32(123)) | ||
} |
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,66 @@ | ||
package testutils | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/opentracing/api-golang/opentracing" | ||
) | ||
|
||
// InMemoryRecorder is a simple thread-safe implementation of opentracing.Recorder | ||
// that stores all reported spans in memory, accessible via reporter.GetSpans() | ||
type InMemoryRecorder struct { | ||
processName string | ||
spans []*opentracing.RawSpan | ||
tags opentracing.Tags | ||
lock sync.Mutex | ||
} | ||
|
||
// NewInMemoryRecorder instantiates a new InMemoryRecorder with the given `processName` | ||
func NewInMemoryRecorder(processName string) *InMemoryRecorder { | ||
return &InMemoryRecorder{ | ||
processName: processName, | ||
spans: make([]*opentracing.RawSpan, 0), | ||
tags: make(opentracing.Tags), | ||
} | ||
} | ||
|
||
// ProcessName implements ProcessName() of opentracing.Recorder | ||
func (recorder *InMemoryRecorder) ProcessName() string { | ||
return recorder.processName | ||
} | ||
|
||
// SetTag implements SetTag() of opentracing.Recorder. Tags can be retrieved via recorder.GetTags() | ||
func (recorder *InMemoryRecorder) SetTag(key string, val interface{}) opentracing.ProcessIdentifier { | ||
recorder.lock.Lock() | ||
defer recorder.lock.Unlock() | ||
recorder.tags[key] = val | ||
return recorder | ||
} | ||
|
||
// RecordSpan implements RecordSpan() of opentracing.Recorder. | ||
// The recorder spans can be retrieved via recorder.Spans slice. | ||
func (recorder *InMemoryRecorder) RecordSpan(span *opentracing.RawSpan) { | ||
recorder.lock.Lock() | ||
defer recorder.lock.Unlock() | ||
recorder.spans = append(recorder.spans, span) | ||
} | ||
|
||
// GetSpans returns a snapshot of spans recorded so far. | ||
func (recorder *InMemoryRecorder) GetSpans() []*opentracing.RawSpan { | ||
recorder.lock.Lock() | ||
defer recorder.lock.Unlock() | ||
spans := make([]*opentracing.RawSpan, len(recorder.spans)) | ||
copy(spans, recorder.spans) | ||
return spans | ||
} | ||
|
||
// GetTags returns a snapshot of tags. | ||
func (recorder *InMemoryRecorder) GetTags() opentracing.Tags { | ||
recorder.lock.Lock() | ||
defer recorder.lock.Unlock() | ||
tags := make(opentracing.Tags) | ||
for k, v := range recorder.tags { | ||
tags[k] = v | ||
} | ||
return tags | ||
} |
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,42 @@ | ||
package testutils_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/opentracing/api-golang/examples/dapperish" | ||
"github.com/opentracing/api-golang/opentracing" | ||
"github.com/opentracing/api-golang/testutils" | ||
) | ||
|
||
func TestInMemoryRecorderSpans(t *testing.T) { | ||
recorder := testutils.NewInMemoryRecorder("unit-test") | ||
var apiRecorder opentracing.Recorder = recorder | ||
if apiRecorder.ProcessName() != "unit-test" { | ||
t.Fatalf("Invalid process name") | ||
} | ||
span := &opentracing.RawSpan{ | ||
TraceContext: &dapperish.DapperishTraceContext{}, | ||
Operation: "test-span", | ||
Start: time.Now(), | ||
Duration: -1, | ||
} | ||
apiRecorder.RecordSpan(span) | ||
if len(recorder.GetSpans()) != 1 { | ||
t.Fatal("No spans recorded") | ||
} | ||
if recorder.GetSpans()[0] != span { | ||
t.Fatal("Span not recorded") | ||
} | ||
} | ||
|
||
func TestInMemoryRecorderTags(t *testing.T) { | ||
recorder := testutils.NewInMemoryRecorder("unit-test") | ||
recorder.SetTag("tag1", "hello") | ||
if len(recorder.GetTags()) != 1 { | ||
t.Fatal("Tag not stored") | ||
} | ||
if recorder.GetTags()["tag1"] != "hello" { | ||
t.Fatal("tag1 != hello") | ||
} | ||
} |