zipkin.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2017 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package jaeger
  21. import (
  22. "github.com/opentracing/opentracing-go"
  23. )
  24. // ZipkinSpanFormat is an OpenTracing carrier format constant
  25. const ZipkinSpanFormat = "zipkin-span-format"
  26. // ExtractableZipkinSpan is a type of Carrier used for integration with Zipkin-aware
  27. // RPC frameworks (like TChannel). It does not support baggage, only trace IDs.
  28. type ExtractableZipkinSpan interface {
  29. TraceID() uint64
  30. SpanID() uint64
  31. ParentID() uint64
  32. Flags() byte
  33. }
  34. // InjectableZipkinSpan is a type of Carrier used for integration with Zipkin-aware
  35. // RPC frameworks (like TChannel). It does not support baggage, only trace IDs.
  36. type InjectableZipkinSpan interface {
  37. SetTraceID(traceID uint64)
  38. SetSpanID(spanID uint64)
  39. SetParentID(parentID uint64)
  40. SetFlags(flags byte)
  41. }
  42. type zipkinPropagator struct {
  43. tracer *Tracer
  44. }
  45. func (p *zipkinPropagator) Inject(
  46. ctx SpanContext,
  47. abstractCarrier interface{},
  48. ) error {
  49. carrier, ok := abstractCarrier.(InjectableZipkinSpan)
  50. if !ok {
  51. return opentracing.ErrInvalidCarrier
  52. }
  53. carrier.SetTraceID(ctx.TraceID().Low) // TODO this cannot work with 128bit IDs
  54. carrier.SetSpanID(uint64(ctx.SpanID()))
  55. carrier.SetParentID(uint64(ctx.ParentID()))
  56. carrier.SetFlags(ctx.flags)
  57. return nil
  58. }
  59. func (p *zipkinPropagator) Extract(abstractCarrier interface{}) (SpanContext, error) {
  60. carrier, ok := abstractCarrier.(ExtractableZipkinSpan)
  61. if !ok {
  62. return emptyContext, opentracing.ErrInvalidCarrier
  63. }
  64. if carrier.TraceID() == 0 {
  65. return emptyContext, opentracing.ErrSpanContextNotFound
  66. }
  67. var ctx SpanContext
  68. ctx.traceID.Low = carrier.TraceID()
  69. ctx.spanID = SpanID(carrier.SpanID())
  70. ctx.parentID = SpanID(carrier.ParentID())
  71. ctx.flags = carrier.Flags()
  72. return ctx, nil
  73. }