]>
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 | #include "jaegertracing/baggage/RemoteRestrictionManager.h" | |
18 | ||
19 | #include <sstream> | |
20 | ||
21 | #include "jaegertracing/baggage/RemoteRestrictionJSON.h" | |
22 | #include "jaegertracing/net/http/Response.h" | |
23 | #include "jaegertracing/utils/ErrorUtil.h" | |
24 | ||
25 | namespace jaegertracing { | |
26 | namespace baggage { | |
27 | namespace { | |
28 | ||
29 | constexpr auto kDefaultHostPort = "127.0.0.1:5778"; | |
30 | ||
31 | } // anonymous namespace | |
32 | ||
33 | RemoteRestrictionManager::RemoteRestrictionManager( | |
34 | const std::string& serviceName, | |
35 | const std::string& hostPort, | |
36 | bool denyBaggageOnInitializationFailure, | |
37 | const Clock::duration& refreshInterval, | |
38 | logging::Logger& logger, | |
39 | metrics::Metrics& metrics) | |
40 | : _serviceName(serviceName) | |
41 | , _serverAddress( | |
42 | net::IPAddress::v4(hostPort.empty() ? kDefaultHostPort : hostPort)) | |
43 | , _denyBaggageOnInitializationFailure(denyBaggageOnInitializationFailure) | |
44 | , _refreshInterval(refreshInterval == Clock::duration() | |
45 | ? defaultRefreshInterval() | |
46 | : refreshInterval) | |
47 | , _logger(logger) | |
48 | , _metrics(metrics) | |
49 | , _running(true) | |
50 | , _initialized(false) | |
51 | , _thread([this]() { poll(); }) | |
52 | { | |
53 | } | |
54 | ||
55 | Restriction | |
56 | RemoteRestrictionManager::getRestriction(const std::string& /* service */, | |
57 | const std::string& key) | |
58 | { | |
59 | std::lock_guard<std::mutex> lock(_mutex); | |
60 | ||
61 | if (!_initialized) { | |
62 | if (_denyBaggageOnInitializationFailure) { | |
63 | return Restriction(false, 0); | |
64 | } | |
65 | return Restriction(true, kDefaultMaxValueLength); | |
66 | } | |
67 | ||
68 | auto itr = _restrictions.find(key); | |
69 | if (itr != std::end(_restrictions)) { | |
70 | return itr->second; | |
71 | } | |
72 | return Restriction(false, 0); | |
73 | } | |
74 | ||
75 | void RemoteRestrictionManager::close() noexcept | |
76 | { | |
77 | std::unique_lock<std::mutex> lock(_mutex); | |
78 | if (!_running) { | |
79 | return; | |
80 | } | |
81 | _running = false; | |
82 | lock.unlock(); | |
83 | _cv.notify_one(); | |
84 | _thread.join(); | |
85 | } | |
86 | ||
87 | void RemoteRestrictionManager::poll() noexcept | |
88 | { | |
89 | net::URI remoteURI; | |
90 | try { | |
91 | std::ostringstream oss; | |
92 | oss << "http://" << _serverAddress.authority() | |
93 | << "/baggageRestrictions?service=" | |
94 | << net::URI::queryEscape(_serviceName); | |
95 | remoteURI = net::URI::parse(oss.str()); | |
96 | updateRestrictions(remoteURI); | |
97 | } catch (...) { | |
98 | auto logger = logging::consoleLogger(); | |
99 | utils::ErrorUtil::logError(*logger, | |
100 | "Failed in RemoteRestrictionManager::poll"); | |
101 | return; | |
102 | } | |
103 | ||
104 | Clock::time_point lastUpdateTime = Clock::now(); | |
105 | while (true) { | |
106 | { | |
107 | std::unique_lock<std::mutex> lock(_mutex); | |
108 | _cv.wait_until(lock, lastUpdateTime + _refreshInterval, [this]() { | |
109 | return !_running; | |
110 | }); | |
111 | if (!_running) { | |
112 | return; | |
113 | } | |
114 | } | |
115 | ||
116 | if ((Clock::now() - lastUpdateTime) >= _refreshInterval) { | |
117 | updateRestrictions(remoteURI); | |
118 | lastUpdateTime = Clock::now(); | |
119 | } | |
120 | } | |
121 | } | |
122 | ||
123 | void RemoteRestrictionManager::updateRestrictions( | |
124 | const net::URI& remoteURI) noexcept | |
125 | { | |
126 | try { | |
127 | const auto responseHTTP = net::http::get(remoteURI); | |
128 | if (responseHTTP.statusCode() != 200) { | |
129 | std::ostringstream oss; | |
130 | oss << "Received HTTP error response" | |
131 | << ", uri=" << remoteURI | |
132 | << ", statusCode=" << responseHTTP.statusCode() | |
133 | << ", reason=" << responseHTTP.reason(); | |
134 | _logger.error(oss.str()); | |
135 | return; | |
136 | } | |
137 | ||
138 | thrift::BaggageRestrictionManager_getBaggageRestrictions_result | |
139 | response = nlohmann::json::parse(responseHTTP.body()); | |
140 | if (response.__isset.success) { | |
141 | KeyRestrictionMap restrictions; | |
142 | restrictions.reserve(response.success.size()); | |
143 | std::transform( | |
144 | std::begin(response.success), | |
145 | std::end(response.success), | |
146 | std::inserter(restrictions, std::end(restrictions)), | |
147 | [](const thrift::BaggageRestriction restriction) { | |
148 | return std::make_pair( | |
149 | restriction.baggageKey, | |
150 | Restriction(true, restriction.maxValueLength)); | |
151 | }); | |
152 | { | |
153 | std::lock_guard<std::mutex> lock(_mutex); | |
154 | _restrictions = std::move(restrictions); | |
155 | if (!_initialized) { | |
156 | _initialized = true; | |
157 | } | |
158 | } | |
159 | _metrics.baggageRestrictionsUpdateSuccess().inc(1); | |
160 | } | |
161 | else { | |
162 | std::ostringstream oss; | |
163 | oss << "Failed to update baggage restrictions" | |
164 | ", response=" | |
165 | << responseHTTP.body(); | |
166 | _logger.error(oss.str()); | |
167 | _metrics.baggageRestrictionsUpdateFailure().inc(1); | |
168 | } | |
169 | } catch (...) { | |
170 | utils::ErrorUtil::logError(_logger, | |
171 | "Failed to update baggage restrictions"); | |
172 | _metrics.baggageRestrictionsUpdateFailure().inc(1); | |
173 | } | |
174 | } | |
175 | ||
176 | } // namespace baggage | |
177 | } // namespace jaegertracing |