tracer.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package trace
  2. import (
  3. "io"
  4. )
  5. var (
  6. // global tracer
  7. _tracer Tracer = nooptracer{}
  8. )
  9. // SetGlobalTracer SetGlobalTracer
  10. func SetGlobalTracer(tracer Tracer) {
  11. _tracer = tracer
  12. }
  13. // Tracer is a simple, thin interface for Trace creation and propagation.
  14. type Tracer interface {
  15. // New trace instance with given title.
  16. New(operationName string, opts ...Option) Trace
  17. // Inject takes the Trace instance and injects it for
  18. // propagation within `carrier`. The actual type of `carrier` depends on
  19. // the value of `format`.
  20. Inject(t Trace, format interface{}, carrier interface{}) error
  21. // Extract returns a Trace instance given `format` and `carrier`.
  22. // return `ErrTraceNotFound` if trace not found.
  23. Extract(format interface{}, carrier interface{}) (Trace, error)
  24. }
  25. // New trace instance with given operationName.
  26. func New(operationName string, opts ...Option) Trace {
  27. return _tracer.New(operationName, opts...)
  28. }
  29. // Inject takes the Trace instance and injects it for
  30. // propagation within `carrier`. The actual type of `carrier` depends on
  31. // the value of `format`.
  32. func Inject(t Trace, format interface{}, carrier interface{}) error {
  33. return _tracer.Inject(t, format, carrier)
  34. }
  35. // Extract returns a Trace instance given `format` and `carrier`.
  36. // return `ErrTraceNotFound` if trace not found.
  37. func Extract(format interface{}, carrier interface{}) (Trace, error) {
  38. return _tracer.Extract(format, carrier)
  39. }
  40. // Close trace flush data.
  41. func Close() error {
  42. if closer, ok := _tracer.(io.Closer); ok {
  43. return closer.Close()
  44. }
  45. return nil
  46. }
  47. // Trace trace common interface.
  48. type Trace interface {
  49. // Fork fork a trace with client trace.
  50. Fork(serviceName, operationName string) Trace
  51. // Follow
  52. Follow(serviceName, operationName string) Trace
  53. // Finish when trace finish call it.
  54. Finish(err *error)
  55. // Scan scan trace into info.
  56. // Deprecated: method Scan is deprecated, use Inject instead of Scan
  57. // Scan(ti *Info)
  58. // Adds a tag to the trace.
  59. //
  60. // If there is a pre-existing tag set for `key`, it is overwritten.
  61. //
  62. // Tag values can be numeric types, strings, or bools. The behavior of
  63. // other tag value types is undefined at the OpenTracing level. If a
  64. // tracing system does not know how to handle a particular value type, it
  65. // may ignore the tag, but shall not panic.
  66. // NOTE current only support legacy tag: TagAnnotation TagAddress TagComment
  67. // other will be ignore
  68. SetTag(tags ...Tag) Trace
  69. // LogFields is an efficient and type-checked way to record key:value
  70. // NOTE current unsupport
  71. SetLog(logs ...LogField) Trace
  72. // Visit visits the k-v pair in trace, calling fn for each.
  73. Visit(fn func(k, v string))
  74. // SetTitle reset trace title
  75. SetTitle(title string)
  76. }