]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Copyright (c) 2017 Uber Technologies, Inc. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #ifndef JAEGERTRACING_BAGGAGE_BAGGAGESETTER_H | |
18 | #define JAEGERTRACING_BAGGAGE_BAGGAGESETTER_H | |
19 | ||
20 | #include "jaegertracing/LogRecord.h" | |
21 | #include "jaegertracing/Span.h" | |
22 | #include "jaegertracing/baggage/RestrictionManager.h" | |
23 | #include "jaegertracing/metrics/Metrics.h" | |
24 | ||
25 | namespace jaegertracing { | |
26 | namespace baggage { | |
27 | ||
28 | class BaggageSetter { | |
29 | public: | |
30 | BaggageSetter(RestrictionManager& restrictionManager, | |
31 | metrics::Metrics& metrics) | |
32 | : _restrictionManager(restrictionManager) | |
33 | , _metrics(metrics) | |
34 | { | |
35 | } | |
36 | ||
37 | template <typename LoggingFunction> | |
38 | void setBaggage(Span& span, | |
39 | SpanContext::StrMap& baggage, | |
40 | const std::string& key, | |
41 | std::string value, | |
42 | LoggingFunction logFn) const | |
43 | { | |
44 | auto truncated = false; | |
45 | const auto restriction = | |
46 | _restrictionManager.getRestriction(span.serviceNameNoLock(), key); | |
47 | if (!restriction.keyAllowed()) { | |
48 | logFields(span, | |
49 | key, | |
50 | value, | |
51 | std::string(), | |
52 | truncated, | |
53 | restriction.keyAllowed(), | |
54 | logFn); | |
55 | _metrics.baggageUpdateFailure().inc(1); | |
56 | return; | |
57 | } | |
58 | ||
59 | if (static_cast<int>(value.size()) > restriction.maxValueLength()) { | |
60 | truncated = true; | |
61 | value = value.substr(0, restriction.maxValueLength()); | |
62 | _metrics.baggageTruncate().inc(1); | |
63 | } | |
64 | ||
65 | auto itr = baggage.find(key); | |
66 | const auto prevItem = | |
67 | (itr == std::end(baggage) ? std::string() : itr->second); | |
68 | if (itr == std::end(baggage)) { | |
69 | baggage[key] = value; | |
70 | } | |
71 | else { | |
72 | itr->second = value; | |
73 | } | |
74 | logFields(span, | |
75 | key, | |
76 | value, | |
77 | prevItem, | |
78 | truncated, | |
79 | restriction.keyAllowed(), | |
80 | logFn); | |
81 | _metrics.baggageUpdateSuccess().inc(1); | |
82 | } | |
83 | ||
84 | private: | |
85 | template <typename LoggingFunction> | |
86 | void logFields(const Span& span, | |
87 | const std::string& key, | |
88 | const std::string& value, | |
89 | const std::string& prevItem, | |
90 | bool truncated, | |
91 | bool valid, | |
92 | LoggingFunction logFn) const | |
93 | { | |
94 | if (!span.contextNoLock().isSampled()) { | |
95 | return; | |
96 | } | |
97 | ||
98 | std::vector<Tag> fields( | |
99 | { { "event", "baggage" }, { "key", key }, { "value", value } }); | |
100 | if (!prevItem.empty()) { | |
101 | fields.push_back(Tag("override", "true")); | |
102 | } | |
103 | if (truncated) { | |
104 | fields.push_back(Tag("truncated", "true")); | |
105 | } | |
106 | if (!valid) { | |
107 | fields.push_back(Tag("invalid", "true")); | |
108 | } | |
109 | ||
110 | logFn(std::begin(fields), std::end(fields)); | |
111 | } | |
112 | ||
113 | RestrictionManager& _restrictionManager; | |
114 | metrics::Metrics& _metrics; | |
115 | }; | |
116 | ||
117 | } // namespace baggage | |
118 | } // namespace jaegertracing | |
119 | ||
120 | #endif // JAEGERTRACING_BAGGAGE_BAGGAGESETTER_H |