You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

370 lines
9.6 KiB

  1. // Copyright 2018, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package jaeger contains an OpenCensus tracing exporter for Jaeger.
  15. package jaeger // import "go.opencensus.io/exporter/jaeger"
  16. import (
  17. "bytes"
  18. "encoding/binary"
  19. "errors"
  20. "fmt"
  21. "io"
  22. "io/ioutil"
  23. "log"
  24. "net/http"
  25. "github.com/apache/thrift/lib/go/thrift"
  26. gen "go.opencensus.io/exporter/jaeger/internal/gen-go/jaeger"
  27. "go.opencensus.io/trace"
  28. "google.golang.org/api/support/bundler"
  29. )
  30. const defaultServiceName = "OpenCensus"
  31. // Options are the options to be used when initializing a Jaeger exporter.
  32. type Options struct {
  33. // Endpoint is the Jaeger HTTP Thrift endpoint.
  34. // For example, http://localhost:14268.
  35. //
  36. // Deprecated: Use CollectorEndpoint instead.
  37. Endpoint string
  38. // CollectorEndpoint is the full url to the Jaeger HTTP Thrift collector.
  39. // For example, http://localhost:14268/api/traces
  40. CollectorEndpoint string
  41. // AgentEndpoint instructs exporter to send spans to jaeger-agent at this address.
  42. // For example, localhost:6831.
  43. AgentEndpoint string
  44. // OnError is the hook to be called when there is
  45. // an error occurred when uploading the stats data.
  46. // If no custom hook is set, errors are logged.
  47. // Optional.
  48. OnError func(err error)
  49. // Username to be used if basic auth is required.
  50. // Optional.
  51. Username string
  52. // Password to be used if basic auth is required.
  53. // Optional.
  54. Password string
  55. // ServiceName is the Jaeger service name.
  56. // Deprecated: Specify Process instead.
  57. ServiceName string
  58. // Process contains the information about the exporting process.
  59. Process Process
  60. //BufferMaxCount defines the total number of traces that can be buffered in memory
  61. BufferMaxCount int
  62. }
  63. // NewExporter returns a trace.Exporter implementation that exports
  64. // the collected spans to Jaeger.
  65. func NewExporter(o Options) (*Exporter, error) {
  66. if o.Endpoint == "" && o.CollectorEndpoint == "" && o.AgentEndpoint == "" {
  67. return nil, errors.New("missing endpoint for Jaeger exporter")
  68. }
  69. var endpoint string
  70. var client *agentClientUDP
  71. var err error
  72. if o.Endpoint != "" {
  73. endpoint = o.Endpoint + "/api/traces?format=jaeger.thrift"
  74. log.Printf("Endpoint has been deprecated. Please use CollectorEndpoint instead.")
  75. } else if o.CollectorEndpoint != "" {
  76. endpoint = o.CollectorEndpoint
  77. } else {
  78. client, err = newAgentClientUDP(o.AgentEndpoint, udpPacketMaxLength)
  79. if err != nil {
  80. return nil, err
  81. }
  82. }
  83. onError := func(err error) {
  84. if o.OnError != nil {
  85. o.OnError(err)
  86. return
  87. }
  88. log.Printf("Error when uploading spans to Jaeger: %v", err)
  89. }
  90. service := o.Process.ServiceName
  91. if service == "" && o.ServiceName != "" {
  92. // fallback to old service name if specified
  93. service = o.ServiceName
  94. } else if service == "" {
  95. service = defaultServiceName
  96. }
  97. tags := make([]*gen.Tag, len(o.Process.Tags))
  98. for i, tag := range o.Process.Tags {
  99. tags[i] = attributeToTag(tag.key, tag.value)
  100. }
  101. e := &Exporter{
  102. endpoint: endpoint,
  103. agentEndpoint: o.AgentEndpoint,
  104. client: client,
  105. username: o.Username,
  106. password: o.Password,
  107. process: &gen.Process{
  108. ServiceName: service,
  109. Tags: tags,
  110. },
  111. }
  112. bundler := bundler.NewBundler((*gen.Span)(nil), func(bundle interface{}) {
  113. if err := e.upload(bundle.([]*gen.Span)); err != nil {
  114. onError(err)
  115. }
  116. })
  117. // Set BufferedByteLimit with the total number of spans that are permissible to be held in memory.
  118. // This needs to be done since the size of messages is always set to 1. Failing to set this would allow
  119. // 1G messages to be held in memory since that is the default value of BufferedByteLimit.
  120. if o.BufferMaxCount != 0 {
  121. bundler.BufferedByteLimit = o.BufferMaxCount
  122. }
  123. e.bundler = bundler
  124. return e, nil
  125. }
  126. // Process contains the information exported to jaeger about the source
  127. // of the trace data.
  128. type Process struct {
  129. // ServiceName is the Jaeger service name.
  130. ServiceName string
  131. // Tags are added to Jaeger Process exports
  132. Tags []Tag
  133. }
  134. // Tag defines a key-value pair
  135. // It is limited to the possible conversions to *jaeger.Tag by attributeToTag
  136. type Tag struct {
  137. key string
  138. value interface{}
  139. }
  140. // BoolTag creates a new tag of type bool, exported as jaeger.TagType_BOOL
  141. func BoolTag(key string, value bool) Tag {
  142. return Tag{key, value}
  143. }
  144. // StringTag creates a new tag of type string, exported as jaeger.TagType_STRING
  145. func StringTag(key string, value string) Tag {
  146. return Tag{key, value}
  147. }
  148. // Int64Tag creates a new tag of type int64, exported as jaeger.TagType_LONG
  149. func Int64Tag(key string, value int64) Tag {
  150. return Tag{key, value}
  151. }
  152. // Exporter is an implementation of trace.Exporter that uploads spans to Jaeger.
  153. type Exporter struct {
  154. endpoint string
  155. agentEndpoint string
  156. process *gen.Process
  157. bundler *bundler.Bundler
  158. client *agentClientUDP
  159. username, password string
  160. }
  161. var _ trace.Exporter = (*Exporter)(nil)
  162. // ExportSpan exports a SpanData to Jaeger.
  163. func (e *Exporter) ExportSpan(data *trace.SpanData) {
  164. e.bundler.Add(spanDataToThrift(data), 1)
  165. // TODO(jbd): Handle oversized bundlers.
  166. }
  167. // As per the OpenCensus Status code mapping in
  168. // https://opencensus.io/tracing/span/status/
  169. // the status is OK if the code is 0.
  170. const opencensusStatusCodeOK = 0
  171. func spanDataToThrift(data *trace.SpanData) *gen.Span {
  172. tags := make([]*gen.Tag, 0, len(data.Attributes))
  173. for k, v := range data.Attributes {
  174. tag := attributeToTag(k, v)
  175. if tag != nil {
  176. tags = append(tags, tag)
  177. }
  178. }
  179. tags = append(tags,
  180. attributeToTag("status.code", data.Status.Code),
  181. attributeToTag("status.message", data.Status.Message),
  182. )
  183. // Ensure that if Status.Code is not OK, that we set the "error" tag on the Jaeger span.
  184. // See Issue https://github.com/census-instrumentation/opencensus-go/issues/1041
  185. if data.Status.Code != opencensusStatusCodeOK {
  186. tags = append(tags, attributeToTag("error", true))
  187. }
  188. var logs []*gen.Log
  189. for _, a := range data.Annotations {
  190. fields := make([]*gen.Tag, 0, len(a.Attributes))
  191. for k, v := range a.Attributes {
  192. tag := attributeToTag(k, v)
  193. if tag != nil {
  194. fields = append(fields, tag)
  195. }
  196. }
  197. fields = append(fields, attributeToTag("message", a.Message))
  198. logs = append(logs, &gen.Log{
  199. Timestamp: a.Time.UnixNano() / 1000,
  200. Fields: fields,
  201. })
  202. }
  203. var refs []*gen.SpanRef
  204. for _, link := range data.Links {
  205. refs = append(refs, &gen.SpanRef{
  206. TraceIdHigh: bytesToInt64(link.TraceID[0:8]),
  207. TraceIdLow: bytesToInt64(link.TraceID[8:16]),
  208. SpanId: bytesToInt64(link.SpanID[:]),
  209. })
  210. }
  211. return &gen.Span{
  212. TraceIdHigh: bytesToInt64(data.TraceID[0:8]),
  213. TraceIdLow: bytesToInt64(data.TraceID[8:16]),
  214. SpanId: bytesToInt64(data.SpanID[:]),
  215. ParentSpanId: bytesToInt64(data.ParentSpanID[:]),
  216. OperationName: name(data),
  217. Flags: int32(data.TraceOptions),
  218. StartTime: data.StartTime.UnixNano() / 1000,
  219. Duration: data.EndTime.Sub(data.StartTime).Nanoseconds() / 1000,
  220. Tags: tags,
  221. Logs: logs,
  222. References: refs,
  223. }
  224. }
  225. func name(sd *trace.SpanData) string {
  226. n := sd.Name
  227. switch sd.SpanKind {
  228. case trace.SpanKindClient:
  229. n = "Sent." + n
  230. case trace.SpanKindServer:
  231. n = "Recv." + n
  232. }
  233. return n
  234. }
  235. func attributeToTag(key string, a interface{}) *gen.Tag {
  236. var tag *gen.Tag
  237. switch value := a.(type) {
  238. case bool:
  239. tag = &gen.Tag{
  240. Key: key,
  241. VBool: &value,
  242. VType: gen.TagType_BOOL,
  243. }
  244. case string:
  245. tag = &gen.Tag{
  246. Key: key,
  247. VStr: &value,
  248. VType: gen.TagType_STRING,
  249. }
  250. case int64:
  251. tag = &gen.Tag{
  252. Key: key,
  253. VLong: &value,
  254. VType: gen.TagType_LONG,
  255. }
  256. case int32:
  257. v := int64(value)
  258. tag = &gen.Tag{
  259. Key: key,
  260. VLong: &v,
  261. VType: gen.TagType_LONG,
  262. }
  263. case float64:
  264. v := float64(value)
  265. tag = &gen.Tag{
  266. Key: key,
  267. VDouble: &v,
  268. VType: gen.TagType_DOUBLE,
  269. }
  270. }
  271. return tag
  272. }
  273. // Flush waits for exported trace spans to be uploaded.
  274. //
  275. // This is useful if your program is ending and you do not want to lose recent spans.
  276. func (e *Exporter) Flush() {
  277. e.bundler.Flush()
  278. }
  279. func (e *Exporter) upload(spans []*gen.Span) error {
  280. batch := &gen.Batch{
  281. Spans: spans,
  282. Process: e.process,
  283. }
  284. if e.endpoint != "" {
  285. return e.uploadCollector(batch)
  286. }
  287. return e.uploadAgent(batch)
  288. }
  289. func (e *Exporter) uploadAgent(batch *gen.Batch) error {
  290. return e.client.EmitBatch(batch)
  291. }
  292. func (e *Exporter) uploadCollector(batch *gen.Batch) error {
  293. body, err := serialize(batch)
  294. if err != nil {
  295. return err
  296. }
  297. req, err := http.NewRequest("POST", e.endpoint, body)
  298. if err != nil {
  299. return err
  300. }
  301. if e.username != "" && e.password != "" {
  302. req.SetBasicAuth(e.username, e.password)
  303. }
  304. req.Header.Set("Content-Type", "application/x-thrift")
  305. resp, err := http.DefaultClient.Do(req)
  306. if err != nil {
  307. return err
  308. }
  309. io.Copy(ioutil.Discard, resp.Body)
  310. resp.Body.Close()
  311. if resp.StatusCode < 200 || resp.StatusCode >= 300 {
  312. return fmt.Errorf("failed to upload traces; HTTP status code: %d", resp.StatusCode)
  313. }
  314. return nil
  315. }
  316. func serialize(obj thrift.TStruct) (*bytes.Buffer, error) {
  317. buf := thrift.NewTMemoryBuffer()
  318. if err := obj.Write(thrift.NewTBinaryProtocolTransport(buf)); err != nil {
  319. return nil, err
  320. }
  321. return buf.Buffer, nil
  322. }
  323. func bytesToInt64(buf []byte) int64 {
  324. u := binary.BigEndian.Uint64(buf)
  325. return int64(u)
  326. }