span_options.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "time"
  17. "github.com/openzipkin/zipkin-go/model"
  18. )
  19. // SpanOption allows for functional options to adjust behavior and payload of
  20. // the Span to be created with tracer.StartSpan().
  21. type SpanOption func(t *Tracer, s *spanImpl)
  22. // Kind sets the kind of the span being created.
  23. func Kind(kind model.Kind) SpanOption {
  24. return func(t *Tracer, s *spanImpl) {
  25. s.Kind = kind
  26. }
  27. }
  28. // Parent will use provided SpanContext as parent to the span being created.
  29. func Parent(sc model.SpanContext) SpanOption {
  30. return func(t *Tracer, s *spanImpl) {
  31. if sc.Err != nil {
  32. // encountered an extraction error
  33. switch t.extractFailurePolicy {
  34. case ExtractFailurePolicyRestart:
  35. case ExtractFailurePolicyError:
  36. panic(s.SpanContext.Err)
  37. case ExtractFailurePolicyTagAndRestart:
  38. s.Tags["error.extract"] = sc.Err.Error()
  39. default:
  40. panic(ErrInvalidExtractFailurePolicy)
  41. }
  42. /* don't use provided SpanContext, but restart trace */
  43. return
  44. }
  45. s.SpanContext = sc
  46. }
  47. }
  48. // StartTime uses a given start time for the span being created.
  49. func StartTime(start time.Time) SpanOption {
  50. return func(t *Tracer, s *spanImpl) {
  51. s.Timestamp = start
  52. }
  53. }
  54. // RemoteEndpoint sets the remote endpoint of the span being created.
  55. func RemoteEndpoint(e *model.Endpoint) SpanOption {
  56. return func(t *Tracer, s *spanImpl) {
  57. s.RemoteEndpoint = e
  58. }
  59. }
  60. // Tags sets initial tags for the span being created. If default tracer tags
  61. // are present they will be overwritten on key collisions.
  62. func Tags(tags map[string]string) SpanOption {
  63. return func(t *Tracer, s *spanImpl) {
  64. for k, v := range tags {
  65. s.Tags[k] = v
  66. }
  67. }
  68. }
  69. // FlushOnFinish when set to false will disable span.Finish() to send the Span
  70. // to the Reporter automatically (which is the default behavior). If set to
  71. // false, having the Span be reported becomes the responsibility of the user.
  72. // This is available if late tag data is expected to be only available after the
  73. // required finish time of the Span.
  74. func FlushOnFinish(b bool) SpanOption {
  75. return func(t *Tracer, s *spanImpl) {
  76. s.flushOnFinish = b
  77. }
  78. }