blob: 52e889582a85947c2da6d7fbb1f2538574ae361d [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.
50 Component = stringTagName("component")
51
52 //////////////////////////////////////////////////////////////////////
53 // Sampling hint
54 //////////////////////////////////////////////////////////////////////
55
56 // SamplingPriority determines the priority of sampling this Span.
57 SamplingPriority = uint16TagName("sampling.priority")
58
59 //////////////////////////////////////////////////////////////////////
60 // Peer tags. These tags can be emitted by either client-side of
61 // 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.
66 PeerService = stringTagName("peer.service")
67
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"
71 PeerAddress = stringTagName("peer.address")
72
73 // PeerHostname records the host name of the peer
74 PeerHostname = stringTagName("peer.hostname")
75
76 // PeerHostIPv4 records IP v4 host address of the peer
77 PeerHostIPv4 = ipv4Tag("peer.ipv4")
78
79 // PeerHostIPv6 records IP v6 host address of the peer
80 PeerHostIPv6 = stringTagName("peer.ipv6")
81
82 // PeerPort records port number of the peer
83 PeerPort = uint16TagName("peer.port")
84
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.
91 HTTPUrl = stringTagName("http.url")
92
93 // HTTPMethod is the HTTP method of the request, and is case-insensitive.
94 HTTPMethod = stringTagName("http.method")
95
96 // HTTPStatusCode is the numeric HTTP status code (200, 404, etc) of the
97 // HTTP response.
98 HTTPStatusCode = uint16TagName("http.status_code")
99
100 //////////////////////////////////////////////////////////////////////
101 // DB Tags
102 //////////////////////////////////////////////////////////////////////
103
104 // DBInstance is database instance name.
105 DBInstance = stringTagName("db.instance")
106
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).
109 DBStatement = stringTagName("db.statement")
110
111 // DBType is a database type. For any SQL database, "sql".
112 // For others, the lower-case database category, e.g. "redis"
113 DBType = stringTagName("db.type")
114
115 // DBUser is a username for accessing database.
116 DBUser = stringTagName("db.user")
117
118 //////////////////////////////////////////////////////////////////////
119 // Message Bus Tag
120 //////////////////////////////////////////////////////////////////////
121
122 // MessageBusDestination is an address at which messages can be exchanged
123 MessageBusDestination = stringTagName("message_bus.destination")
124
125 //////////////////////////////////////////////////////////////////////
126 // Error Tag
127 //////////////////////////////////////////////////////////////////////
128
129 // Error indicates that operation represented by the span resulted in an error.
130 Error = boolTagName("error")
131)
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
166type stringTagName string
167
168// Set adds a string tag to the `span`
169func (tag stringTagName) Set(span opentracing.Span, value string) {
170 span.SetTag(string(tag), value)
171}
172
173// ---
174
175type uint32TagName string
176
177// Set adds a uint32 tag to the `span`
178func (tag uint32TagName) Set(span opentracing.Span, value uint32) {
179 span.SetTag(string(tag), value)
180}
181
182// ---
183
184type uint16TagName string
185
186// Set adds a uint16 tag to the `span`
187func (tag uint16TagName) Set(span opentracing.Span, value uint16) {
188 span.SetTag(string(tag), value)
189}
190
191// ---
192
193type boolTagName string
194
195// Add adds a bool tag to the `span`
196func (tag boolTagName) Set(span opentracing.Span, value bool) {
197 span.SetTag(string(tag), value)
198}
199
200type ipv4Tag string
201
202// Set adds IP v4 host address of the peer as an uint32 value to the `span`, keep this for backward and zipkin compatibility
203func (tag ipv4Tag) Set(span opentracing.Span, value uint32) {
204 span.SetTag(string(tag), value)
205}
206
207// SetString records IP v4 host address of the peer as a .-separated tuple to the `span`. E.g., "127.0.0.1"
208func (tag ipv4Tag) SetString(span opentracing.Span, value string) {
209 span.SetTag(string(tag), value)
210}