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

Commit

Permalink
Add more semantic tags (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
stamm authored and yurishkuro committed Apr 5, 2017
1 parent e79bb85 commit eaa2524
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
44 changes: 42 additions & 2 deletions ext/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import opentracing "github.com/opentracing/opentracing-go"
//
var (
//////////////////////////////////////////////////////////////////////
// SpanKind (client/server)
// SpanKind (client/server or producer/consumer)
//////////////////////////////////////////////////////////////////////

// SpanKind hints at relationship between spans, e.g. client/server
Expand All @@ -31,6 +31,16 @@ var (
SpanKindRPCServerEnum = SpanKindEnum("server")
SpanKindRPCServer = opentracing.Tag{Key: string(SpanKind), Value: SpanKindRPCServerEnum}

// SpanKindProducer marks a span representing the producer-side of a
// message bus
SpanKindProducerEnum = SpanKindEnum("producer")
SpanKindProducer = opentracing.Tag{Key: string(SpanKind), Value: SpanKindProducerEnum}

// SpanKindConsumer marks a span representing the consumer-side of a
// message bus
SpanKindConsumerEnum = SpanKindEnum("consumer")
SpanKindConsumer = opentracing.Tag{Key: string(SpanKind), Value: SpanKindConsumerEnum}

//////////////////////////////////////////////////////////////////////
// Component name
//////////////////////////////////////////////////////////////////////
Expand All @@ -52,9 +62,14 @@ var (
// communications, like an RPC call.
//////////////////////////////////////////////////////////////////////

// PeerService records the service name of the peer
// PeerService records the service name of the peer.
PeerService = stringTagName("peer.service")

// PeerAddress records the address name of the peer. This may be a "ip:port",
// a bare "hostname", a FQDN or even a database DSN substring
// like "mysql://[email protected]:3306/dbname"
PeerAddress = stringTagName("peer.address")

// PeerHostname records the host name of the peer
PeerHostname = stringTagName("peer.hostname")

Expand Down Expand Up @@ -82,6 +97,31 @@ var (
// HTTP response.
HTTPStatusCode = uint16TagName("http.status_code")

//////////////////////////////////////////////////////////////////////
// DB Tags
//////////////////////////////////////////////////////////////////////

// DBInstance is database instance name.
DBInstance = stringTagName("db.instance")

// DBStatement is a database statement for the given database type.
// It can be a query or a prepared statement (i.e., before substitution).
DBStatement = stringTagName("db.statement")

// DBType is a database type. For any SQL database, "sql".
// For others, the lower-case database category, e.g. "redis"
DBType = stringTagName("db.type")

// DBUser is a username for accessing database.
DBUser = stringTagName("db.user")

//////////////////////////////////////////////////////////////////////
// Message Bus Tag
//////////////////////////////////////////////////////////////////////

// MessageBusDestination is an address at which messages can be exchanged
MessageBusDestination = stringTagName("message_bus.destination")

//////////////////////////////////////////////////////////////////////
// Error Tag
//////////////////////////////////////////////////////////////////////
Expand Down
47 changes: 47 additions & 0 deletions ext/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestPeerTags(t *testing.T) {
tracer := mocktracer.New()
span := tracer.StartSpan("my-trace")
ext.PeerService.Set(span, "my-service")
ext.PeerAddress.Set(span, "my-hostname:8080")
ext.PeerHostname.Set(span, "my-hostname")
ext.PeerHostIPv4.Set(span, uint32(127<<24|1))
ext.PeerHostIPv6.Set(span, "::")
Expand All @@ -29,6 +30,7 @@ func TestPeerTags(t *testing.T) {
rawSpan := tracer.FinishedSpans()[0]
assert.Equal(t, map[string]interface{}{
"peer.service": "my-service",
"peer.address": "my-hostname:8080",
"peer.hostname": "my-hostname",
"peer.ipv4": uint32(127<<24 | 1),
"peer.ipv6": "::",
Expand Down Expand Up @@ -57,6 +59,25 @@ func TestHTTPTags(t *testing.T) {
}, rawSpan.Tags())
}

func TestDBTags(t *testing.T) {
tracer := mocktracer.New()
span := tracer.StartSpan("my-trace", ext.SpanKindRPCClient)
ext.DBInstance.Set(span, "127.0.0.1:3306/customers")
ext.DBStatement.Set(span, "SELECT * FROM user_table")
ext.DBType.Set(span, "sql")
ext.DBUser.Set(span, "customer_user")
span.Finish()

rawSpan := tracer.FinishedSpans()[0]
assert.Equal(t, map[string]interface{}{
"db.instance": "127.0.0.1:3306/customers",
"db.statement": "SELECT * FROM user_table",
"db.type": "sql",
"db.user": "customer_user",
"span.kind": ext.SpanKindRPCClientEnum,
}, rawSpan.Tags())
}

func TestMiscTags(t *testing.T) {
tracer := mocktracer.New()
span := tracer.StartSpan("my-trace")
Expand Down Expand Up @@ -99,3 +120,29 @@ func TestRPCServerOption(t *testing.T) {
"bag": "gage",
}, rawSpan.Context().(mocktracer.MockSpanContext).Baggage)
}

func TestMessageBusProducerTags(t *testing.T) {
tracer := mocktracer.New()
span := tracer.StartSpan("my-trace", ext.SpanKindProducer)
ext.MessageBusDestination.Set(span, "topic name")
span.Finish()

rawSpan := tracer.FinishedSpans()[0]
assert.Equal(t, map[string]interface{}{
"message_bus.destination": "topic name",
"span.kind": ext.SpanKindProducerEnum,
}, rawSpan.Tags())
}

func TestMessageBusConsumerTags(t *testing.T) {
tracer := mocktracer.New()
span := tracer.StartSpan("my-trace", ext.SpanKindConsumer)
ext.MessageBusDestination.Set(span, "topic name")
span.Finish()

rawSpan := tracer.FinishedSpans()[0]
assert.Equal(t, map[string]interface{}{
"message_bus.destination": "topic name",
"span.kind": ext.SpanKindConsumerEnum,
}, rawSpan.Tags())
}

0 comments on commit eaa2524

Please sign in to comment.