@@ -55,19 +55,18 @@ ConfigUpdate parse_dynamic_config(const nlohmann::json& j) {
5555 ConfigUpdate config_update;
5656
5757 if (auto sampling_rate_it = j.find (" tracing_sampling_rate" );
58- sampling_rate_it != j.cend ()) {
58+ sampling_rate_it != j.cend () && sampling_rate_it-> is_number () ) {
5959 config_update.trace_sampling_rate = *sampling_rate_it;
6060 }
6161
62- if (auto tags_it = j.find (" tracing_tags" ); tags_it != j.cend ()) {
62+ if (auto tags_it = j.find (" tracing_tags" );
63+ tags_it != j.cend () && tags_it->is_array ()) {
6364 config_update.tags = *tags_it;
6465 }
6566
6667 if (auto tracing_enabled_it = j.find (" tracing_enabled" );
67- tracing_enabled_it != j.cend ()) {
68- if (tracing_enabled_it->is_boolean ()) {
69- config_update.report_traces = tracing_enabled_it->get <bool >();
70- }
68+ tracing_enabled_it != j.cend () && tracing_enabled_it->is_boolean ()) {
69+ config_update.report_traces = tracing_enabled_it->get <bool >();
7170 }
7271
7372 return config_update;
@@ -120,26 +119,35 @@ nlohmann::json RemoteConfigurationManager::make_request_payload() {
120119 if (!applied_config_.empty ()) {
121120 auto config_states = nlohmann::json::array ();
122121 for (const auto & [_, config] : applied_config_) {
123- config_states.emplace_back (nlohmann::json{{" id" , config.id },
124- {" version" , config.version },
125- {" product" , k_apm_product}});
122+ nlohmann::json config_state = {
123+ {" id" , config.id },
124+ {" version" , config.version },
125+ {" product" , k_apm_product},
126+ {" apply_state" , config.state },
127+ };
128+
129+ if (config.error_message ) {
130+ config_state[" apply_error" ] = *config.error_message ;
131+ }
132+
133+ config_states.emplace_back (std::move (config_state));
126134 }
127135
128- j[" config_states" ] = config_states;
136+ j[" client " ][ " state " ][ " config_states" ] = config_states;
129137 }
130138
131139 if (state_.error_message ) {
132- j[" has_error" ] = true ;
133- j[" error" ] = *state_.error_message ;
140+ j[" client " ][ " state " ][ " has_error" ] = true ;
141+ j[" client " ][ " state " ][ " error" ] = *state_.error_message ;
134142 }
135143
136144 return j;
137145}
138146
139147std::vector<ConfigMetadata> RemoteConfigurationManager::process_response (
140148 const nlohmann::json& json) {
141- std::vector<ConfigMetadata> config_update ;
142- config_update .reserve (8 );
149+ std::vector<ConfigMetadata> config_updates ;
150+ config_updates .reserve (8 );
143151
144152 state_.error_message = nullopt ;
145153
@@ -158,12 +166,14 @@ std::vector<ConfigMetadata> RemoteConfigurationManager::process_response(
158166 if (client_configs_it == json.cend ()) {
159167 if (!applied_config_.empty ()) {
160168 std::for_each (applied_config_.cbegin (), applied_config_.cend (),
161- [this , &config_update](const auto it) {
162- config_update = revert_config (it.second );
169+ [this , &config_updates](const auto it) {
170+ auto updated = revert_config (it.second );
171+ config_updates.insert (config_updates.end (),
172+ updated.begin (), updated.end ());
163173 });
164174 applied_config_.clear ();
165175 }
166- return config_update ;
176+ return config_updates ;
167177 }
168178
169179 // Keep track of config path received to know which ones to revert.
@@ -194,34 +204,42 @@ std::vector<ConfigMetadata> RemoteConfigurationManager::process_response(
194204 " target file having path \" " ;
195205 append (*state_.error_message , config_path);
196206 *state_.error_message += ' \" ' ;
197- return config_update ;
207+ return config_updates ;
198208 }
199209
200210 const auto config_json = nlohmann::json::parse (
201211 base64_decode (target_it.value ().at (" raw" ).get <StringView>()));
202212
213+ Configuration new_config;
214+ new_config.id = config_json.at (" id" );
215+ new_config.hash = config_metadata.at (" /hashes/sha256" _json_pointer);
216+ new_config.version = config_json.at (" revision" );
217+
203218 const auto & targeted_service = config_json.at (" service_target" );
204219 if (targeted_service.at (" service" ).get <StringView>() !=
205220 tracer_signature_.default_service ||
206221 targeted_service.at (" env" ).get <StringView>() !=
207222 tracer_signature_.default_environment ) {
208- continue ;
209- }
223+ new_config.state = Configuration::State::error;
224+ new_config.error_message = " Wrong service targeted" ;
225+ } else {
226+ new_config.state = Configuration::State::acknowledged;
227+ new_config.content = parse_dynamic_config (config_json.at (" lib_config" ));
210228
211- Configuration new_config;
212- new_config.hash = config_metadata.at (" /hashes/sha256" _json_pointer);
213- new_config.id = config_json.at (" id" );
214- new_config.version = config_json.at (" revision" );
215- new_config.content = parse_dynamic_config (config_json.at (" lib_config" ));
229+ auto updated = apply_config (new_config);
230+ config_updates.insert (config_updates.end (), updated.begin (),
231+ updated.end ());
232+ }
216233
217- config_update = apply_config (new_config);
218234 applied_config_[std::string{config_path}] = new_config;
219235 }
220236
221237 // Applied configuration not present must be reverted.
222238 for (auto it = applied_config_.cbegin (); it != applied_config_.cend ();) {
223239 if (!visited_config.count (it->first )) {
224- config_update = revert_config (it->second );
240+ auto updated = revert_config (it->second );
241+ config_updates.insert (config_updates.end (), updated.begin (),
242+ updated.end ());
225243 it = applied_config_.erase (it);
226244 } else {
227245 it++;
@@ -232,10 +250,10 @@ std::vector<ConfigMetadata> RemoteConfigurationManager::process_response(
232250 error_message += e.what ();
233251
234252 state_.error_message = std::move (error_message);
235- return config_update ;
253+ return config_updates ;
236254 }
237255
238- return config_update ;
256+ return config_updates ;
239257}
240258
241259std::vector<ConfigMetadata> RemoteConfigurationManager::apply_config (
0 commit comments