1package ext 2 3import opentracing "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) 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 ////////////////////////////////////////////////////////////////////// 35 // Component name 36 ////////////////////////////////////////////////////////////////////// 37 38 // Component is a low-cardinality identifier of the module, library, 39 // or package that is generating a span. 40 Component = stringTagName("component") 41 42 ////////////////////////////////////////////////////////////////////// 43 // Sampling hint 44 ////////////////////////////////////////////////////////////////////// 45 46 // SamplingPriority determines the priority of sampling this Span. 47 SamplingPriority = uint16TagName("sampling.priority") 48 49 ////////////////////////////////////////////////////////////////////// 50 // Peer tags. These tags can be emitted by either client-side of 51 // server-side to describe the other side/service in a peer-to-peer 52 // communications, like an RPC call. 53 ////////////////////////////////////////////////////////////////////// 54 55 // PeerService records the service name of the peer 56 PeerService = stringTagName("peer.service") 57 58 // PeerHostname records the host name of the peer 59 PeerHostname = stringTagName("peer.hostname") 60 61 // PeerHostIPv4 records IP v4 host address of the peer 62 PeerHostIPv4 = uint32TagName("peer.ipv4") 63 64 // PeerHostIPv6 records IP v6 host address of the peer 65 PeerHostIPv6 = stringTagName("peer.ipv6") 66 67 // PeerPort records port number of the peer 68 PeerPort = uint16TagName("peer.port") 69 70 ////////////////////////////////////////////////////////////////////// 71 // HTTP Tags 72 ////////////////////////////////////////////////////////////////////// 73 74 // HTTPUrl should be the URL of the request being handled in this segment 75 // of the trace, in standard URI format. The protocol is optional. 76 HTTPUrl = stringTagName("http.url") 77 78 // HTTPMethod is the HTTP method of the request, and is case-insensitive. 79 HTTPMethod = stringTagName("http.method") 80 81 // HTTPStatusCode is the numeric HTTP status code (200, 404, etc) of the 82 // HTTP response. 83 HTTPStatusCode = uint16TagName("http.status_code") 84 85 ////////////////////////////////////////////////////////////////////// 86 // Error Tag 87 ////////////////////////////////////////////////////////////////////// 88 89 // Error indicates that operation represented by the span resulted in an error. 90 Error = boolTagName("error") 91) 92 93// --- 94 95// SpanKindEnum represents common span types 96type SpanKindEnum string 97 98type spanKindTagName string 99 100// Set adds a string tag to the `span` 101func (tag spanKindTagName) Set(span opentracing.Span, value SpanKindEnum) { 102 span.SetTag(string(tag), value) 103} 104 105type rpcServerOption struct { 106 clientContext opentracing.SpanContext 107} 108 109func (r rpcServerOption) Apply(o *opentracing.StartSpanOptions) { 110 if r.clientContext != nil { 111 opentracing.ChildOf(r.clientContext).Apply(o) 112 } 113 SpanKindRPCServer.Apply(o) 114} 115 116// RPCServerOption returns a StartSpanOption appropriate for an RPC server span 117// with `client` representing the metadata for the remote peer Span if available. 118// In case client == nil, due to the client not being instrumented, this RPC 119// server span will be a root span. 120func RPCServerOption(client opentracing.SpanContext) opentracing.StartSpanOption { 121 return rpcServerOption{client} 122} 123 124// --- 125 126type stringTagName string 127 128// Set adds a string tag to the `span` 129func (tag stringTagName) Set(span opentracing.Span, value string) { 130 span.SetTag(string(tag), value) 131} 132 133// --- 134 135type uint32TagName string 136 137// Set adds a uint32 tag to the `span` 138func (tag uint32TagName) Set(span opentracing.Span, value uint32) { 139 span.SetTag(string(tag), value) 140} 141 142// --- 143 144type uint16TagName string 145 146// Set adds a uint16 tag to the `span` 147func (tag uint16TagName) Set(span opentracing.Span, value uint16) { 148 span.SetTag(string(tag), value) 149} 150 151// --- 152 153type boolTagName string 154 155// Add adds a bool tag to the `span` 156func (tag boolTagName) Set(span opentracing.Span, value bool) { 157 span.SetTag(string(tag), value) 158} 159