baggage_setter.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/log"
  23. "github.com/uber/jaeger-client-go/internal/baggage"
  24. )
  25. // baggageSetter is an actor that can set a baggage value on a Span given certain
  26. // restrictions (eg. maxValueLength).
  27. type baggageSetter struct {
  28. restrictionManager baggage.RestrictionManager
  29. metrics *Metrics
  30. }
  31. func newBaggageSetter(restrictionManager baggage.RestrictionManager, metrics *Metrics) *baggageSetter {
  32. return &baggageSetter{
  33. restrictionManager: restrictionManager,
  34. metrics: metrics,
  35. }
  36. }
  37. // (NB) span should hold the lock before making this call
  38. func (s *baggageSetter) setBaggage(span *Span, key, value string) {
  39. var truncated bool
  40. var prevItem string
  41. restriction := s.restrictionManager.GetRestriction(key)
  42. if !restriction.KeyAllowed() {
  43. s.logFields(span, key, value, prevItem, truncated, restriction.KeyAllowed())
  44. s.metrics.BaggageUpdateFailure.Inc(1)
  45. return
  46. }
  47. if len(value) > restriction.MaxValueLength() {
  48. truncated = true
  49. value = value[:restriction.MaxValueLength()]
  50. s.metrics.BaggageTruncate.Inc(1)
  51. }
  52. prevItem = span.context.baggage[key]
  53. s.logFields(span, key, value, prevItem, truncated, restriction.KeyAllowed())
  54. span.context = span.context.WithBaggageItem(key, value)
  55. s.metrics.BaggageUpdateSuccess.Inc(1)
  56. }
  57. func (s *baggageSetter) logFields(span *Span, key, value, prevItem string, truncated, valid bool) {
  58. if !span.context.IsSampled() {
  59. return
  60. }
  61. fields := []log.Field{
  62. log.String("event", "baggage"),
  63. log.String("key", key),
  64. log.String("value", value),
  65. }
  66. if prevItem != "" {
  67. fields = append(fields, log.String("override", "true"))
  68. }
  69. if truncated {
  70. fields = append(fields, log.String("truncated", "true"))
  71. }
  72. if !valid {
  73. fields = append(fields, log.String("invalid", "true"))
  74. }
  75. span.logFieldsNoLocking(fields...)
  76. }