zipkin.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2017 Uber Technologies, Inc.
  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
  15. import (
  16. "github.com/opentracing/opentracing-go"
  17. )
  18. // ZipkinSpanFormat is an OpenTracing carrier format constant
  19. const ZipkinSpanFormat = "zipkin-span-format"
  20. // ExtractableZipkinSpan is a type of Carrier used for integration with Zipkin-aware
  21. // RPC frameworks (like TChannel). It does not support baggage, only trace IDs.
  22. type ExtractableZipkinSpan interface {
  23. TraceID() uint64
  24. SpanID() uint64
  25. ParentID() uint64
  26. Flags() byte
  27. }
  28. // InjectableZipkinSpan is a type of Carrier used for integration with Zipkin-aware
  29. // RPC frameworks (like TChannel). It does not support baggage, only trace IDs.
  30. type InjectableZipkinSpan interface {
  31. SetTraceID(traceID uint64)
  32. SetSpanID(spanID uint64)
  33. SetParentID(parentID uint64)
  34. SetFlags(flags byte)
  35. }
  36. type zipkinPropagator struct {
  37. tracer *Tracer
  38. }
  39. func (p *zipkinPropagator) Inject(
  40. ctx SpanContext,
  41. abstractCarrier interface{},
  42. ) error {
  43. carrier, ok := abstractCarrier.(InjectableZipkinSpan)
  44. if !ok {
  45. return opentracing.ErrInvalidCarrier
  46. }
  47. carrier.SetTraceID(ctx.TraceID().Low) // TODO this cannot work with 128bit IDs
  48. carrier.SetSpanID(uint64(ctx.SpanID()))
  49. carrier.SetParentID(uint64(ctx.ParentID()))
  50. carrier.SetFlags(ctx.samplingState.flags())
  51. return nil
  52. }
  53. func (p *zipkinPropagator) Extract(abstractCarrier interface{}) (SpanContext, error) {
  54. carrier, ok := abstractCarrier.(ExtractableZipkinSpan)
  55. if !ok {
  56. return emptyContext, opentracing.ErrInvalidCarrier
  57. }
  58. if carrier.TraceID() == 0 {
  59. return emptyContext, opentracing.ErrSpanContextNotFound
  60. }
  61. var ctx SpanContext
  62. ctx.traceID.Low = carrier.TraceID()
  63. ctx.spanID = SpanID(carrier.SpanID())
  64. ctx.parentID = SpanID(carrier.ParentID())
  65. ctx.samplingState = &samplingState{}
  66. ctx.samplingState.setFlags(carrier.Flags())
  67. return ctx, nil
  68. }