tracer_options.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2022 The OpenZipkin 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 zipkin
  15. import (
  16. "errors"
  17. "github.com/openzipkin/zipkin-go/idgenerator"
  18. "github.com/openzipkin/zipkin-go/model"
  19. )
  20. // Tracer Option Errors
  21. var (
  22. ErrInvalidEndpoint = errors.New("requires valid local endpoint")
  23. ErrInvalidExtractFailurePolicy = errors.New("invalid extract failure policy provided")
  24. )
  25. // ExtractFailurePolicy deals with Extraction errors
  26. type ExtractFailurePolicy int
  27. // ExtractFailurePolicyOptions
  28. const (
  29. ExtractFailurePolicyRestart ExtractFailurePolicy = iota
  30. ExtractFailurePolicyError
  31. ExtractFailurePolicyTagAndRestart
  32. )
  33. // TracerOption allows for functional options to adjust behavior of the Tracer
  34. // to be created with NewTracer().
  35. type TracerOption func(o *Tracer) error
  36. // WithLocalEndpoint sets the local endpoint of the tracer.
  37. func WithLocalEndpoint(e *model.Endpoint) TracerOption {
  38. return func(o *Tracer) error {
  39. if e == nil {
  40. o.localEndpoint = nil
  41. return nil
  42. }
  43. ep := *e
  44. o.localEndpoint = &ep
  45. return nil
  46. }
  47. }
  48. // WithExtractFailurePolicy allows one to set the ExtractFailurePolicy.
  49. func WithExtractFailurePolicy(p ExtractFailurePolicy) TracerOption {
  50. return func(o *Tracer) error {
  51. if p < 0 || p > ExtractFailurePolicyTagAndRestart {
  52. return ErrInvalidExtractFailurePolicy
  53. }
  54. o.extractFailurePolicy = p
  55. return nil
  56. }
  57. }
  58. // WithNoopSpan if set to true will switch to a NoopSpan implementation
  59. // if the trace is not sampled.
  60. func WithNoopSpan(unsampledNoop bool) TracerOption {
  61. return func(o *Tracer) error {
  62. o.unsampledNoop = unsampledNoop
  63. return nil
  64. }
  65. }
  66. // WithSharedSpans allows to place client-side and server-side annotations
  67. // for a RPC call in the same span (Zipkin V1 behavior) or different spans
  68. // (more in line with other tracing solutions). By default this Tracer
  69. // uses shared host spans (so client-side and server-side in the same span).
  70. func WithSharedSpans(val bool) TracerOption {
  71. return func(o *Tracer) error {
  72. o.sharedSpans = val
  73. return nil
  74. }
  75. }
  76. // WithSampler allows one to set a Sampler function
  77. func WithSampler(sampler Sampler) TracerOption {
  78. return func(o *Tracer) error {
  79. o.sampler = sampler
  80. return nil
  81. }
  82. }
  83. // WithTraceID128Bit if set to true will instruct the Tracer to start traces
  84. // with 128 bit TraceID's. If set to false the Tracer will start traces with
  85. // 64 bits.
  86. func WithTraceID128Bit(val bool) TracerOption {
  87. return func(o *Tracer) error {
  88. if val {
  89. o.generate = idgenerator.NewRandom128()
  90. } else {
  91. o.generate = idgenerator.NewRandom64()
  92. }
  93. return nil
  94. }
  95. }
  96. // WithIDGenerator allows one to set a custom ID Generator
  97. func WithIDGenerator(generator idgenerator.IDGenerator) TracerOption {
  98. return func(o *Tracer) error {
  99. o.generate = generator
  100. return nil
  101. }
  102. }
  103. // WithTags allows one to set default tags to be added to each created span
  104. func WithTags(tags map[string]string) TracerOption {
  105. return func(o *Tracer) error {
  106. for k, v := range tags {
  107. o.defaultTags[k] = v
  108. }
  109. return nil
  110. }
  111. }
  112. // WithNoopTracer allows one to start the Tracer as Noop implementation.
  113. func WithNoopTracer(tracerNoop bool) TracerOption {
  114. return func(o *Tracer) error {
  115. if tracerNoop {
  116. o.noop = 1
  117. } else {
  118. o.noop = 0
  119. }
  120. return nil
  121. }
  122. }