blob: a414b5951f03fc5761cd37f559ef255af698c979 [file] [log] [blame]
Rohan Agrawalc32d9932020-06-15 11:01:47 +00001package ext
2
3import "github.com/opentracing/opentracing-go"
4
5// These constants define common tag names recommended for better portability across
6// tracing systems and languages/platforms.
7//
8// The tag names are defined as typed strings, so that in addition to the usual use
9//
10// span.setTag(TagName, value)
11//
12// they also support value type validation via this additional syntax:
13//
14// TagName.Set(span, value)
15//
16var (
17 //////////////////////////////////////////////////////////////////////
18 // SpanKind (client/server or producer/consumer)
19 //////////////////////////////////////////////////////////////////////
20
21 // SpanKind hints at relationship between spans, e.g. client/server
22 SpanKind = spanKindTagName("span.kind")
23
24 // SpanKindRPCClient marks a span representing the client-side of an RPC
25 // or other remote call
26 SpanKindRPCClientEnum = SpanKindEnum("client")
27 SpanKindRPCClient = opentracing.Tag{Key: string(SpanKind), Value: SpanKindRPCClientEnum}
28
29 // SpanKindRPCServer marks a span representing the server-side of an RPC
30 // or other remote call
31 SpanKindRPCServerEnum = SpanKindEnum("server")
32 SpanKindRPCServer = opentracing.Tag{Key: string(SpanKind), Value: SpanKindRPCServerEnum}
33
34 // SpanKindProducer marks a span representing the producer-side of a
35 // message bus
36 SpanKindProducerEnum = SpanKindEnum("producer")
37 SpanKindProducer = opentracing.Tag{Key: string(SpanKind), Value: SpanKindProducerEnum}
38
39 // SpanKindConsumer marks a span representing the consumer-side of a
40 // message bus
41 SpanKindConsumerEnum = SpanKindEnum("consumer")
42 SpanKindConsumer = opentracing.Tag{Key: string(SpanKind), Value: SpanKindConsumerEnum}
43
44 //////////////////////////////////////////////////////////////////////
45 // Component name
46 //////////////////////////////////////////////////////////////////////
47
48 // Component is a low-cardinality identifier of the module, library,
49 // or package that is generating a span.
Maninder12b909f2020-10-23 14:23:36 +053050 Component = StringTagName("component")
Rohan Agrawalc32d9932020-06-15 11:01:47 +000051
52 //////////////////////////////////////////////////////////////////////
53 // Sampling hint
54 //////////////////////////////////////////////////////////////////////
55
56 // SamplingPriority determines the priority of sampling this Span.
Maninder12b909f2020-10-23 14:23:36 +053057 SamplingPriority = Uint16TagName("sampling.priority")
Rohan Agrawalc32d9932020-06-15 11:01:47 +000058
59 //////////////////////////////////////////////////////////////////////
Maninder12b909f2020-10-23 14:23:36 +053060 // Peer tags. These tags can be emitted by either client-side or
Rohan Agrawalc32d9932020-06-15 11:01:47 +000061 // server-side to describe the other side/service in a peer-to-peer
62 // communications, like an RPC call.
63 //////////////////////////////////////////////////////////////////////
64
65 // PeerService records the service name of the peer.
Maninder12b909f2020-10-23 14:23:36 +053066 PeerService = StringTagName("peer.service")
Rohan Agrawalc32d9932020-06-15 11:01:47 +000067
68 // PeerAddress records the address name of the peer. This may be a "ip:port",
69 // a bare "hostname", a FQDN or even a database DSN substring
70 // like "mysql://username@127.0.0.1:3306/dbname"
Maninder12b909f2020-10-23 14:23:36 +053071 PeerAddress = StringTagName("peer.address")
Rohan Agrawalc32d9932020-06-15 11:01:47 +000072
73 // PeerHostname records the host name of the peer
Maninder12b909f2020-10-23 14:23:36 +053074 PeerHostname = StringTagName("peer.hostname")
Rohan Agrawalc32d9932020-06-15 11:01:47 +000075
76 // PeerHostIPv4 records IP v4 host address of the peer
Maninder12b909f2020-10-23 14:23:36 +053077 PeerHostIPv4 = IPv4TagName("peer.ipv4")
Rohan Agrawalc32d9932020-06-15 11:01:47 +000078
79 // PeerHostIPv6 records IP v6 host address of the peer
Maninder12b909f2020-10-23 14:23:36 +053080 PeerHostIPv6 = StringTagName("peer.ipv6")
Rohan Agrawalc32d9932020-06-15 11:01:47 +000081
82 // PeerPort records port number of the peer
Maninder12b909f2020-10-23 14:23:36 +053083 PeerPort = Uint16TagName("peer.port")
Rohan Agrawalc32d9932020-06-15 11:01:47 +000084
85 //////////////////////////////////////////////////////////////////////
86 // HTTP Tags
87 //////////////////////////////////////////////////////////////////////
88
89 // HTTPUrl should be the URL of the request being handled in this segment
90 // of the trace, in standard URI format. The protocol is optional.
Maninder12b909f2020-10-23 14:23:36 +053091 HTTPUrl = StringTagName("http.url")
Rohan Agrawalc32d9932020-06-15 11:01:47 +000092
93 // HTTPMethod is the HTTP method of the request, and is case-insensitive.
Maninder12b909f2020-10-23 14:23:36 +053094 HTTPMethod = StringTagName("http.method")
Rohan Agrawalc32d9932020-06-15 11:01:47 +000095
96 // HTTPStatusCode is the numeric HTTP status code (200, 404, etc) of the
97 // HTTP response.
Maninder12b909f2020-10-23 14:23:36 +053098 HTTPStatusCode = Uint16TagName("http.status_code")
Rohan Agrawalc32d9932020-06-15 11:01:47 +000099
100 //////////////////////////////////////////////////////////////////////
101 // DB Tags
102 //////////////////////////////////////////////////////////////////////
103
104 // DBInstance is database instance name.
Maninder12b909f2020-10-23 14:23:36 +0530105 DBInstance = StringTagName("db.instance")
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000106
107 // DBStatement is a database statement for the given database type.
108 // It can be a query or a prepared statement (i.e., before substitution).
Maninder12b909f2020-10-23 14:23:36 +0530109 DBStatement = StringTagName("db.statement")
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000110
111 // DBType is a database type. For any SQL database, "sql".
112 // For others, the lower-case database category, e.g. "redis"
Maninder12b909f2020-10-23 14:23:36 +0530113 DBType = StringTagName("db.type")
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000114
115 // DBUser is a username for accessing database.
Maninder12b909f2020-10-23 14:23:36 +0530116 DBUser = StringTagName("db.user")
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000117
118 //////////////////////////////////////////////////////////////////////
119 // Message Bus Tag
120 //////////////////////////////////////////////////////////////////////
121
122 // MessageBusDestination is an address at which messages can be exchanged
Maninder12b909f2020-10-23 14:23:36 +0530123 MessageBusDestination = StringTagName("message_bus.destination")
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000124
125 //////////////////////////////////////////////////////////////////////
126 // Error Tag
127 //////////////////////////////////////////////////////////////////////
128
129 // Error indicates that operation represented by the span resulted in an error.
Maninder12b909f2020-10-23 14:23:36 +0530130 Error = BoolTagName("error")
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000131)
132
133// ---
134
135// SpanKindEnum represents common span types
136type SpanKindEnum string
137
138type spanKindTagName string
139
140// Set adds a string tag to the `span`
141func (tag spanKindTagName) Set(span opentracing.Span, value SpanKindEnum) {
142 span.SetTag(string(tag), value)
143}
144
145type rpcServerOption struct {
146 clientContext opentracing.SpanContext
147}
148
149func (r rpcServerOption) Apply(o *opentracing.StartSpanOptions) {
150 if r.clientContext != nil {
151 opentracing.ChildOf(r.clientContext).Apply(o)
152 }
153 SpanKindRPCServer.Apply(o)
154}
155
156// RPCServerOption returns a StartSpanOption appropriate for an RPC server span
157// with `client` representing the metadata for the remote peer Span if available.
158// In case client == nil, due to the client not being instrumented, this RPC
159// server span will be a root span.
160func RPCServerOption(client opentracing.SpanContext) opentracing.StartSpanOption {
161 return rpcServerOption{client}
162}
163
164// ---
165
Maninder12b909f2020-10-23 14:23:36 +0530166// StringTagName is a common tag name to be set to a string value
167type StringTagName string
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000168
169// Set adds a string tag to the `span`
Maninder12b909f2020-10-23 14:23:36 +0530170func (tag StringTagName) Set(span opentracing.Span, value string) {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000171 span.SetTag(string(tag), value)
172}
173
174// ---
175
Maninder12b909f2020-10-23 14:23:36 +0530176// Uint32TagName is a common tag name to be set to a uint32 value
177type Uint32TagName string
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000178
179// Set adds a uint32 tag to the `span`
Maninder12b909f2020-10-23 14:23:36 +0530180func (tag Uint32TagName) Set(span opentracing.Span, value uint32) {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000181 span.SetTag(string(tag), value)
182}
183
184// ---
185
Maninder12b909f2020-10-23 14:23:36 +0530186// Uint16TagName is a common tag name to be set to a uint16 value
187type Uint16TagName string
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000188
189// Set adds a uint16 tag to the `span`
Maninder12b909f2020-10-23 14:23:36 +0530190func (tag Uint16TagName) Set(span opentracing.Span, value uint16) {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000191 span.SetTag(string(tag), value)
192}
193
194// ---
195
Maninder12b909f2020-10-23 14:23:36 +0530196// BoolTagName is a common tag name to be set to a bool value
197type BoolTagName string
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000198
Maninder12b909f2020-10-23 14:23:36 +0530199// Set adds a bool tag to the `span`
200func (tag BoolTagName) Set(span opentracing.Span, value bool) {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000201 span.SetTag(string(tag), value)
202}
203
Maninder12b909f2020-10-23 14:23:36 +0530204// IPv4TagName is a common tag name to be set to an ipv4 value
205type IPv4TagName string
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000206
207// Set adds IP v4 host address of the peer as an uint32 value to the `span`, keep this for backward and zipkin compatibility
Maninder12b909f2020-10-23 14:23:36 +0530208func (tag IPv4TagName) Set(span opentracing.Span, value uint32) {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000209 span.SetTag(string(tag), value)
210}
211
212// SetString records IP v4 host address of the peer as a .-separated tuple to the `span`. E.g., "127.0.0.1"
Maninder12b909f2020-10-23 14:23:36 +0530213func (tag IPv4TagName) SetString(span opentracing.Span, value string) {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000214 span.SetTag(string(tag), value)
215}