Skip to content

Commit 6f5a535

Browse files
authored
chore(logs): improve log messages [NR-457551] (#1613)
* chore(logs): trace agent-control event-processor * Instrument agent-control event-processor * Remove redundant ids * Add missing target_id for instance-id logs * chore: add clarifying comment * chore: improve naming
1 parent 1c5803c commit 6f5a535

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

agent-control/src/agent_control.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod run;
1212
pub mod uptime_report;
1313
pub mod version_updater;
1414

15+
use crate::agent_control::defaults::AGENT_CONTROL_ID;
1516
use crate::event::AgentControlInternalEvent;
1617
use crate::event::channel::EventPublisher;
1718
use crate::event::{
@@ -39,7 +40,7 @@ use opamp_client::StartedClient;
3940
use resource_cleaner::ResourceCleaner;
4041
use std::sync::Arc;
4142
use std::time::SystemTime;
42-
use tracing::{debug, error, info, instrument, trace, warn};
43+
use tracing::{debug, error, info, info_span, instrument, trace, warn};
4344
use uptime_report::UptimeReporter;
4445
use version_updater::updater::VersionUpdater;
4546

@@ -121,6 +122,8 @@ where
121122
}
122123

123124
pub fn run(self) -> Result<(), AgentError> {
125+
let ac_startup_span = info_span!("start_agent_control", id = AGENT_CONTROL_ID);
126+
let _ac_startup_span_guard = ac_startup_span.enter();
124127
info!("Starting the agents supervisor runtime");
125128
// This is a first-time run and we already read the config earlier, the `initial_config` contains
126129
// the result as read by the `AgentControlConfigLoader`.
@@ -178,6 +181,8 @@ where
178181
.inspect_err(|err| error!("Error executing Agent Control updater: {err}"));
179182

180183
info!("Agents supervisor runtime successfully started");
184+
drop(_ac_startup_span_guard); // The span representing agent start finishes before entering in the `process_events` loop. Otherwise the span would be open while Agent Control runs.
185+
181186
self.process_events(running_sub_agents);
182187

183188
if let Some(opamp_client) = self.opamp_client {
@@ -287,6 +292,8 @@ where
287292
loop {
288293
select! {
289294
recv(&opamp_receiver.as_ref()) -> opamp_event_res => {
295+
let span = info_span!("process_fleet_event", id=AGENT_CONTROL_ID);
296+
let _span_guard = span.enter();
290297
match opamp_event_res {
291298
Err(_) => {
292299
debug!("channel closed");
@@ -316,6 +323,8 @@ where
316323
}
317324
},
318325
recv(&self.agent_control_internal_consumer.as_ref()) -> internal_event_res => {
326+
let span = info_span!("process_event", id=AGENT_CONTROL_ID);
327+
let _span_guard = span.enter();
319328
match internal_event_res {
320329
Err(err) => {
321330
debug!("Error receiving Agent Control internal event {err}");
@@ -337,6 +346,8 @@ where
337346
}
338347
}
339348
recv(self.application_event_consumer.as_ref()) -> _agent_control_event => {
349+
let span = info_span!("process_application_event", id=AGENT_CONTROL_ID);
350+
let _span_guard = span.enter();
340351
debug!("stopping Agent Control event processor");
341352
self.agent_control_publisher.broadcast(AgentControlEvent::AgentControlStopped);
342353
break sub_agents.stop();
@@ -402,7 +413,6 @@ where
402413
}
403414
}
404415

405-
#[instrument(skip_all)]
406416
pub(super) fn validate_apply_store_remote_config(
407417
&self,
408418
opamp_remote_config: &OpampRemoteConfig,
@@ -488,7 +498,7 @@ where
488498

489499
let apply_result = match current_dynamic_config.agents.get(agent_id) {
490500
Some(old_sub_agent_config) if old_sub_agent_config == agent_config => {
491-
debug!(%agent_id, "Retaining the existing running SubAgent as its configuration remains unchanged");
501+
debug!(%agent_id, "Retaining the existing running SubAgent as its type remains unchanged");
492502
Ok(())
493503
}
494504
Some(old_sub_agent_config) => {

agent-control/src/opamp/callbacks.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ where
184184
}
185185

186186
fn on_connect_failed(&self, err: ConnectionError) {
187-
log_connection_error(&err, self.agent_id.clone());
187+
log_connection_error(&err);
188188
self.publish_on_connect_failed(&err);
189189
}
190190

@@ -219,44 +219,38 @@ where
219219
}
220220

221221
fn get_effective_config(&self) -> Result<EffectiveConfig, Self::Error> {
222-
debug!(
223-
agent_id = %self.agent_id,
224-
"OpAMP get effective config");
222+
debug!("OpAMP get effective config");
225223

226224
let effective_config = self
227225
.effective_config_loader
228226
.load()
229227
.map_err(EffectiveConfigError::Loader)?;
230228

231229
// Not printing the effective config in case it contains sensitive info
232-
debug!(
233-
agent_id = %self.agent_id,
234-
"OpAMP effective config loaded"
235-
);
230+
debug!("OpAMP effective config loaded");
236231

237232
Ok(effective_config.into())
238233
}
239234
}
240235

241-
fn log_connection_error(err: &ConnectionError, agent_id: AgentID) {
236+
fn log_connection_error(err: &ConnectionError) {
242237
// Check if the error comes from receiving an undesired HTTP status code
243238
if let HTTPClientError(UnsuccessfulResponse(http_code, http_reason)) = &err {
244239
let reason = match http_code {
245-
400 => "The request was malformed",
246-
401 => "Check for missing or invalid license key",
247-
403 => "The account provided is not allowed to use this resource",
248-
404 => "The requested resource was not found",
240+
400 => "the request was malformed",
241+
401 => "check for missing or invalid license key",
242+
403 => "the account provided is not allowed to use this resource",
243+
404 => "the requested resource was not found",
249244
415 => "Content-Type or Content-Encoding for the HTTP request was wrong",
250-
500 => "Server-side problem",
251-
_ => "Reasons unknown",
245+
500 => "server-side problem",
246+
_ => "unknown",
252247
};
253-
error!(%agent_id, http_code, http_reason, reason,"OpAMP HTTP connection error");
254-
} else {
255248
error!(
256-
%agent_id,
257-
reason = err.to_string(),
258-
"OpAMP HTTP connection error"
259-
)
249+
http_code,
250+
http_reason, "OpAMP HTTP connection error: {reason}"
251+
);
252+
} else {
253+
error!("OpAMP HTTP connection error: {err}")
260254
}
261255
}
262256

agent-control/src/opamp/instance_id/getter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ where
6363
{
6464
fn get(&self, agent_id: &AgentID) -> Result<InstanceID, GetterError> {
6565
let storer = self.storer.lock().expect("failed to acquire the lock");
66-
debug!("retrieving instance id");
66+
debug!(target_agent_id = %agent_id, "retrieving instance id");
6767
let data = storer.get(agent_id)?;
6868

6969
match data {
@@ -72,8 +72,8 @@ where
7272
}
7373
Some(d) if d.identifiers == self.identifiers => return Ok(d.instance_id),
7474
Some(d) => debug!(
75-
"stored data had different identifiers {:?}!={:?}",
76-
d.identifiers, self.identifiers
75+
target_agent_id = %agent_id,
76+
"stored data had different identifiers {:?}!={:?}", d.identifiers, self.identifiers
7777
),
7878
}
7979

@@ -82,7 +82,7 @@ where
8282
identifiers: self.identifiers.clone(),
8383
};
8484

85-
debug!("persisting instance id {}", new_data.instance_id);
85+
debug!(target_agent_id = %agent_id, "persisting instance id {}", new_data.instance_id);
8686
storer.set(agent_id, &new_data)?;
8787

8888
Ok(new_data.instance_id)

0 commit comments

Comments
 (0)