blob: a414b5951f03fc5761cd37f559ef255af698c979 [file] [log] [blame]
khenaidooc6c7bda2020-06-17 17:20:18 -04001package 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.
khenaidood948f772021-08-11 17:49:24 -040050 Component = StringTagName("component")
khenaidooc6c7bda2020-06-17 17:20:18 -040051
52 //////////////////////////////////////////////////////////////////////
53 // Sampling hint
54 //////////////////////////////////////////////////////////////////////
55
56 // SamplingPriority determines the priority of sampling this Span.
khenaidood948f772021-08-11 17:49:24 -040057 SamplingPriority = Uint16TagName("sampling.priority")
khenaidooc6c7bda2020-06-17 17:20:18 -040058
59 //////////////////////////////////////////////////////////////////////
khenaidood948f772021-08-11 17:49:24 -040060 // Peer tags. These tags can be emitted by either client-side or
khenaidooc6c7bda2020-06-17 17:20:18 -040061 // 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.
khenaidood948f772021-08-11 17:49:24 -040066 PeerService = StringTagName("peer.service")
khenaidooc6c7bda2020-06-17 17:20:18 -040067
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"
khenaidood948f772021-08-11 17:49:24 -040071 PeerAddress = StringTagName("peer.address")
khenaidooc6c7bda2020-06-17 17:20:18 -040072
73 // PeerHostname records the host name of the peer
khenaidood948f772021-08-11 17:49:24 -040074 PeerHostname = StringTagName("peer.hostname")
khenaidooc6c7bda2020-06-17 17:20:18 -040075
76 // PeerHostIPv4 records IP v4 host address of the peer
khenaidood948f772021-08-11 17:49:24 -040077 PeerHostIPv4 = IPv4TagName("peer.ipv4")
khenaidooc6c7bda2020-06-17 17:20:18 -040078
79 // PeerHostIPv6 records IP v6 host address of the peer
khenaidood948f772021-08-11 17:49:24 -040080 PeerHostIPv6 = StringTagName("peer.ipv6")
khenaidooc6c7bda2020-06-17 17:20:18 -040081
82 // PeerPort records port number of the peer
khenaidood948f772021-08-11 17:49:24 -040083 PeerPort = Uint16TagName("peer.port")
khenaidooc6c7bda2020-06-17 17:20:18 -040084
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.
khenaidood948f772021-08-11 17:49:24 -040091 HTTPUrl = StringTagName("http.url")
khenaidooc6c7bda2020-06-17 17:20:18 -040092
93 // HTTPMethod is the HTTP method of the request, and is case-insensitive.
khenaidood948f772021-08-11 17:49:24 -040094 HTTPMethod = StringTagName("http.method")
khenaidooc6c7bda2020-06-17 17:20:18 -040095
96 // HTTPStatusCode is the numeric HTTP status code (200, 404, etc) of the
97 // HTTP response.
khenaidood948f772021-08-11 17:49:24 -040098 HTTPStatusCode = Uint16TagName("http.status_code")
khenaidooc6c7bda2020-06-17 17:20:18 -040099
100 //////////////////////////////////////////////////////////////////////
101 // DB Tags
102 //////////////////////////////////////////////////////////////////////
103
104 // DBInstance is database instance name.
khenaidood948f772021-08-11 17:49:24 -0400105 DBInstance = StringTagName("db.instance")
khenaidooc6c7bda2020-06-17 17:20:18 -0400106
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).
khenaidood948f772021-08-11 17:49:24 -0400109 DBStatement = StringTagName("db.statement")
khenaidooc6c7bda2020-06-17 17:20:18 -0400110
111 // DBType is a database type. For any SQL database, "sql".
112 // For others, the lower-case database category, e.g. "redis"
khenaidood948f772021-08-11 17:49:24 -0400113 DBType = StringTagName("db.type")
khenaidooc6c7bda2020-06-17 17:20:18 -0400114
115 // DBUser is a username for accessing database.
khenaidood948f772021-08-11 17:49:24 -0400116 DBUser = StringTagName("db.user")
khenaidooc6c7bda2020-06-17 17:20:18 -0400117
118 //////////////////////////////////////////////////////////////////////
119 // Message Bus Tag
120 //////////////////////////////////////////////////////////////////////
121
122 // MessageBusDestination is an address at which messages can be exchanged
khenaidood948f772021-08-11 17:49:24 -0400123 MessageBusDestination = StringTagName("message_bus.destination")
khenaidooc6c7bda2020-06-17 17:20:18 -0400124
125 //////////////////////////////////////////////////////////////////////
126 // Error Tag
127 //////////////////////////////////////////////////////////////////////
128
129 // Error indicates that operation represented by the span resulted in an error.
khenaidood948f772021-08-11 17:49:24 -0400130 Error = BoolTagName("error")
khenaidooc6c7bda2020-06-17 17:20:18 -0400131)
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
khenaidood948f772021-08-11 17:49:24 -0400166// StringTagName is a common tag name to be set to a string value
167type StringTagName string
khenaidooc6c7bda2020-06-17 17:20:18 -0400168
169// Set adds a string tag to the `span`
khenaidood948f772021-08-11 17:49:24 -0400170func (tag StringTagName) Set(span opentracing.Span, value string) {
khenaidooc6c7bda2020-06-17 17:20:18 -0400171 span.SetTag(string(tag), value)
172}
173
174// ---
175
khenaidood948f772021-08-11 17:49:24 -0400176// Uint32TagName is a common tag name to be set to a uint32 value
177type Uint32TagName string
khenaidooc6c7bda2020-06-17 17:20:18 -0400178
179// Set adds a uint32 tag to the `span`
khenaidood948f772021-08-11 17:49:24 -0400180func (tag Uint32TagName) Set(span opentracing.Span, value uint32) {
khenaidooc6c7bda2020-06-17 17:20:18 -0400181 span.SetTag(string(tag), value)
182}
183
184// ---
185
khenaidood948f772021-08-11 17:49:24 -0400186// Uint16TagName is a common tag name to be set to a uint16 value
187type Uint16TagName string
khenaidooc6c7bda2020-06-17 17:20:18 -0400188
189// Set adds a uint16 tag to the `span`
khenaidood948f772021-08-11 17:49:24 -0400190func (tag Uint16TagName) Set(span opentracing.Span, value uint16) {
khenaidooc6c7bda2020-06-17 17:20:18 -0400191 span.SetTag(string(tag), value)
192}
193
194// ---
195
khenaidood948f772021-08-11 17:49:24 -0400196// BoolTagName is a common tag name to be set to a bool value
197type BoolTagName string
khenaidooc6c7bda2020-06-17 17:20:18 -0400198
khenaidood948f772021-08-11 17:49:24 -0400199// Set adds a bool tag to the `span`
200func (tag BoolTagName) Set(span opentracing.Span, value bool) {
khenaidooc6c7bda2020-06-17 17:20:18 -0400201 span.SetTag(string(tag), value)
202}
203
khenaidood948f772021-08-11 17:49:24 -0400204// IPv4TagName is a common tag name to be set to an ipv4 value
205type IPv4TagName string
khenaidooc6c7bda2020-06-17 17:20:18 -0400206
207// Set adds IP v4 host address of the peer as an uint32 value to the `span`, keep this for backward and zipkin compatibility
khenaidood948f772021-08-11 17:49:24 -0400208func (tag IPv4TagName) Set(span opentracing.Span, value uint32) {
khenaidooc6c7bda2020-06-17 17:20:18 -0400209 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"
khenaidood948f772021-08-11 17:49:24 -0400213func (tag IPv4TagName) SetString(span opentracing.Span, value string) {
khenaidooc6c7bda2020-06-17 17:20:18 -0400214 span.SetTag(string(tag), value)
215}