1package 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 or 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 = IPv4TagName("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 166// StringTagName is a common tag name to be set to a string value 167type StringTagName string 168 169// Set adds a string tag to the `span` 170func (tag StringTagName) Set(span opentracing.Span, value string) { 171 span.SetTag(string(tag), value) 172} 173 174// --- 175 176// Uint32TagName is a common tag name to be set to a uint32 value 177type Uint32TagName string 178 179// Set adds a uint32 tag to the `span` 180func (tag Uint32TagName) Set(span opentracing.Span, value uint32) { 181 span.SetTag(string(tag), value) 182} 183 184// --- 185 186// Uint16TagName is a common tag name to be set to a uint16 value 187type Uint16TagName string 188 189// Set adds a uint16 tag to the `span` 190func (tag Uint16TagName) Set(span opentracing.Span, value uint16) { 191 span.SetTag(string(tag), value) 192} 193 194// --- 195 196// BoolTagName is a common tag name to be set to a bool value 197type BoolTagName string 198 199// Set adds a bool tag to the `span` 200func (tag BoolTagName) Set(span opentracing.Span, value bool) { 201 span.SetTag(string(tag), value) 202} 203 204// IPv4TagName is a common tag name to be set to an ipv4 value 205type IPv4TagName string 206 207// Set adds IP v4 host address of the peer as an uint32 value to the `span`, keep this for backward and zipkin compatibility 208func (tag IPv4TagName) Set(span opentracing.Span, value uint32) { 209 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" 213func (tag IPv4TagName) SetString(span opentracing.Span, value string) { 214 span.SetTag(string(tag), value) 215} 216