blob: a414b5951f03fc5761cd37f559ef255af698c979 [file] [log] [blame]
Girish Gowdra5d7d6442020-09-08 17:03:11 -07001package 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.
Girish Gowdra390f12f2021-07-01 15:53:49 -070050 Component = StringTagName("component")
Girish Gowdra5d7d6442020-09-08 17:03:11 -070051
52 //////////////////////////////////////////////////////////////////////
53 // Sampling hint
54 //////////////////////////////////////////////////////////////////////
55
56 // SamplingPriority determines the priority of sampling this Span.
Girish Gowdra390f12f2021-07-01 15:53:49 -070057 SamplingPriority = Uint16TagName("sampling.priority")
Girish Gowdra5d7d6442020-09-08 17:03:11 -070058
59 //////////////////////////////////////////////////////////////////////
Girish Gowdra390f12f2021-07-01 15:53:49 -070060 // Peer tags. These tags can be emitted by either client-side or
Girish Gowdra5d7d6442020-09-08 17:03:11 -070061 // 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.
Girish Gowdra390f12f2021-07-01 15:53:49 -070066 PeerService = StringTagName("peer.service")
Girish Gowdra5d7d6442020-09-08 17:03:11 -070067
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"
Girish Gowdra390f12f2021-07-01 15:53:49 -070071 PeerAddress = StringTagName("peer.address")
Girish Gowdra5d7d6442020-09-08 17:03:11 -070072
73 // PeerHostname records the host name of the peer
Girish Gowdra390f12f2021-07-01 15:53:49 -070074 PeerHostname = StringTagName("peer.hostname")
Girish Gowdra5d7d6442020-09-08 17:03:11 -070075
76 // PeerHostIPv4 records IP v4 host address of the peer
Girish Gowdra390f12f2021-07-01 15:53:49 -070077 PeerHostIPv4 = IPv4TagName("peer.ipv4")
Girish Gowdra5d7d6442020-09-08 17:03:11 -070078
79 // PeerHostIPv6 records IP v6 host address of the peer
Girish Gowdra390f12f2021-07-01 15:53:49 -070080 PeerHostIPv6 = StringTagName("peer.ipv6")
Girish Gowdra5d7d6442020-09-08 17:03:11 -070081
82 // PeerPort records port number of the peer
Girish Gowdra390f12f2021-07-01 15:53:49 -070083 PeerPort = Uint16TagName("peer.port")
Girish Gowdra5d7d6442020-09-08 17:03:11 -070084
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.
Girish Gowdra390f12f2021-07-01 15:53:49 -070091 HTTPUrl = StringTagName("http.url")
Girish Gowdra5d7d6442020-09-08 17:03:11 -070092
93 // HTTPMethod is the HTTP method of the request, and is case-insensitive.
Girish Gowdra390f12f2021-07-01 15:53:49 -070094 HTTPMethod = StringTagName("http.method")
Girish Gowdra5d7d6442020-09-08 17:03:11 -070095
96 // HTTPStatusCode is the numeric HTTP status code (200, 404, etc) of the
97 // HTTP response.
Girish Gowdra390f12f2021-07-01 15:53:49 -070098 HTTPStatusCode = Uint16TagName("http.status_code")
Girish Gowdra5d7d6442020-09-08 17:03:11 -070099
100 //////////////////////////////////////////////////////////////////////
101 // DB Tags
102 //////////////////////////////////////////////////////////////////////
103
104 // DBInstance is database instance name.
Girish Gowdra390f12f2021-07-01 15:53:49 -0700105 DBInstance = StringTagName("db.instance")
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700106
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).
Girish Gowdra390f12f2021-07-01 15:53:49 -0700109 DBStatement = StringTagName("db.statement")
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700110
111 // DBType is a database type. For any SQL database, "sql".
112 // For others, the lower-case database category, e.g. "redis"
Girish Gowdra390f12f2021-07-01 15:53:49 -0700113 DBType = StringTagName("db.type")
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700114
115 // DBUser is a username for accessing database.
Girish Gowdra390f12f2021-07-01 15:53:49 -0700116 DBUser = StringTagName("db.user")
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700117
118 //////////////////////////////////////////////////////////////////////
119 // Message Bus Tag
120 //////////////////////////////////////////////////////////////////////
121
122 // MessageBusDestination is an address at which messages can be exchanged
Girish Gowdra390f12f2021-07-01 15:53:49 -0700123 MessageBusDestination = StringTagName("message_bus.destination")
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700124
125 //////////////////////////////////////////////////////////////////////
126 // Error Tag
127 //////////////////////////////////////////////////////////////////////
128
129 // Error indicates that operation represented by the span resulted in an error.
Girish Gowdra390f12f2021-07-01 15:53:49 -0700130 Error = BoolTagName("error")
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700131)
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
Girish Gowdra390f12f2021-07-01 15:53:49 -0700166// StringTagName is a common tag name to be set to a string value
167type StringTagName string
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700168
169// Set adds a string tag to the `span`
Girish Gowdra390f12f2021-07-01 15:53:49 -0700170func (tag StringTagName) Set(span opentracing.Span, value string) {
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700171 span.SetTag(string(tag), value)
172}
173
174// ---
175
Girish Gowdra390f12f2021-07-01 15:53:49 -0700176// Uint32TagName is a common tag name to be set to a uint32 value
177type Uint32TagName string
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700178
179// Set adds a uint32 tag to the `span`
Girish Gowdra390f12f2021-07-01 15:53:49 -0700180func (tag Uint32TagName) Set(span opentracing.Span, value uint32) {
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700181 span.SetTag(string(tag), value)
182}
183
184// ---
185
Girish Gowdra390f12f2021-07-01 15:53:49 -0700186// Uint16TagName is a common tag name to be set to a uint16 value
187type Uint16TagName string
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700188
189// Set adds a uint16 tag to the `span`
Girish Gowdra390f12f2021-07-01 15:53:49 -0700190func (tag Uint16TagName) Set(span opentracing.Span, value uint16) {
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700191 span.SetTag(string(tag), value)
192}
193
194// ---
195
Girish Gowdra390f12f2021-07-01 15:53:49 -0700196// BoolTagName is a common tag name to be set to a bool value
197type BoolTagName string
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700198
Girish Gowdra390f12f2021-07-01 15:53:49 -0700199// Set adds a bool tag to the `span`
200func (tag BoolTagName) Set(span opentracing.Span, value bool) {
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700201 span.SetTag(string(tag), value)
202}
203
Girish Gowdra390f12f2021-07-01 15:53:49 -0700204// IPv4TagName is a common tag name to be set to an ipv4 value
205type IPv4TagName string
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700206
207// Set adds IP v4 host address of the peer as an uint32 value to the `span`, keep this for backward and zipkin compatibility
Girish Gowdra390f12f2021-07-01 15:53:49 -0700208func (tag IPv4TagName) Set(span opentracing.Span, value uint32) {
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700209 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"
Girish Gowdra390f12f2021-07-01 15:53:49 -0700213func (tag IPv4TagName) SetString(span opentracing.Span, value string) {
Girish Gowdra5d7d6442020-09-08 17:03:11 -0700214 span.SetTag(string(tag), value)
215}