blob: 1037ca0e861db7dc8fb6ab2bea0dda795caf5620 [file] [log] [blame]
Prince Pereirac1c21d62021-04-22 08:38:15 +00001// 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
15package jaeger
16
17import (
18 "github.com/opentracing/opentracing-go/log"
19
20 "github.com/uber/jaeger-client-go/internal/baggage"
21)
22
23// baggageSetter is an actor that can set a baggage value on a Span given certain
24// restrictions (eg. maxValueLength).
25type baggageSetter struct {
26 restrictionManager baggage.RestrictionManager
27 metrics *Metrics
28}
29
30func newBaggageSetter(restrictionManager baggage.RestrictionManager, metrics *Metrics) *baggageSetter {
31 return &baggageSetter{
32 restrictionManager: restrictionManager,
33 metrics: metrics,
34 }
35}
36
37// (NB) span should hold the lock before making this call
38func (s *baggageSetter) setBaggage(span *Span, key, value string) {
39 var truncated bool
40 var prevItem string
41 restriction := s.restrictionManager.GetRestriction(span.serviceName(), 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
58func (s *baggageSetter) logFields(span *Span, key, value, prevItem string, truncated, valid bool) {
59 if !span.context.IsSampled() {
60 return
61 }
62 fields := []log.Field{
63 log.String("event", "baggage"),
64 log.String("key", key),
65 log.String("value", value),
66 }
67 if prevItem != "" {
68 fields = append(fields, log.String("override", "true"))
69 }
70 if truncated {
71 fields = append(fields, log.String("truncated", "true"))
72 }
73 if !valid {
74 fields = append(fields, log.String("invalid", "true"))
75 }
76 span.logFieldsNoLocking(fields...)
77}