Skip to content

Commit 572a18d

Browse files
chore: improve const maintainability
1 parent a4d38b5 commit 572a18d

File tree

5 files changed

+41
-22
lines changed

5 files changed

+41
-22
lines changed

agent-control/src/agent_control/defaults.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ pub(crate) fn get_custom_capabilities(agent_type_id: &AgentTypeID) -> Option<Cus
8787
pub const AGENT_TYPE_NAME_INFRA_AGENT: &str = "com.newrelic.infrastructure";
8888
pub const AGENT_TYPE_NAME_NRDOT: &str = "com.newrelic.opentelemetry.collector";
8989

90+
// Fleet Control auto generated agent id
91+
pub const AGENT_ID_INFRA_AGENT: &str = "nr-infra";
92+
pub const AGENT_ID_NRDOT: &str = "nrdot";
93+
9094
#[cfg(test)]
9195
pub(crate) mod tests {
9296
use super::*;

agent-control/src/cli/on_host/host_monitoring_gen/infra_config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::cli::error::CliError;
22
use crate::cli::on_host::config_gen::region::Region;
3+
use crate::cli::on_host::systemd_gen::NEW_RELIC_LICENSE_CONFIG_KEY;
34
use std::collections::HashMap;
45

56
const INFRA_AGENT_TYPE_FIELD: &str = "config_agent";
@@ -17,7 +18,7 @@ impl Default for InfraConfig {
1718
values: HashMap::from([
1819
(
1920
"license_key".to_string(),
20-
serde_yaml::Value::String("{{NEW_RELIC_LICENSE_KEY}}".to_string()),
21+
serde_yaml::Value::String(format!("{{{{{NEW_RELIC_LICENSE_CONFIG_KEY}}}}}")),
2122
),
2223
(
2324
"enable_process_metrics".to_string(),

agent-control/src/cli/on_host/host_monitoring_gen/otel_config_gen.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use crate::agent_control::defaults::{
2+
AGENT_CONTROL_LOCAL_DATA_DIR, AGENT_ID_NRDOT, SUB_AGENT_DIR, VALUES_DIR, VALUES_FILENAME,
3+
};
14
use crate::cli::error::CliError;
25
use std::path::PathBuf;
36
use tracing::info;
@@ -11,12 +14,12 @@ pub struct OtelConfigGen {
1114
impl Default for OtelConfigGen {
1215
fn default() -> Self {
1316
Self {
14-
otel_agent_values_path: PathBuf::from(
15-
"/etc/newrelic-agent-control/fleet/agents.d/nrdot/values",
16-
),
17-
otel_config_source_path: PathBuf::from(
18-
"/etc/newrelic-agent-control/examples/values-nr-otel-collector-agent-linux.yaml",
19-
),
17+
otel_agent_values_path: PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR)
18+
.join(SUB_AGENT_DIR)
19+
.join(AGENT_ID_NRDOT)
20+
.join(VALUES_DIR),
21+
otel_config_source_path: PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR)
22+
.join("examples/values-nr-otel-collector-agent-linux.yaml"),
2023
}
2124
}
2225
}
@@ -42,7 +45,7 @@ impl OtelConfigGen {
4245

4346
fn modify_values_yaml(&self) -> Result<(), CliError> {
4447
let source_path = self.otel_config_source_path.clone();
45-
let file_path = self.otel_agent_values_path.join("values.yaml");
48+
let file_path = self.otel_agent_values_path.join(VALUES_FILENAME);
4649
let content = std::fs::read_to_string(source_path)
4750
.map_err(|err| CliError::Command(format!("error reading otel values file: {err}")))?;
4851

agent-control/src/cli/on_host/systemd_gen.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//! Implementation of the generate-config command for the on-host cli.
22
3+
use crate::agent_control::defaults::AGENT_CONTROL_LOCAL_DATA_DIR;
34
use crate::cli::error::CliError;
45
use crate::cli::on_host::config_gen::region::{Region, region_parser};
56
use std::fs::OpenOptions;
67
use std::io::Write;
8+
use std::path::{Path, PathBuf};
79
use tracing::info;
810

9-
const CONFIG_PATH: &str = "/etc/newrelic-agent-control/newrelic-agent-control.conf";
10-
const NEW_RELIC_LICENSE_CONFIG_KEY: &str = "NEW_RELIC_LICENSE_KEY";
11+
const SERVICE_CONFIG_FILE: &str = "newrelic-agent-control.conf";
12+
pub const NEW_RELIC_LICENSE_CONFIG_KEY: &str = "NEW_RELIC_LICENSE_KEY";
1113
const OTEL_EXPORTER_OTLP_ENDPOINT_CONFIG_KEY: &str = "OTEL_EXPORTER_OTLP_ENDPOINT";
1214

1315
/// Generates the Agent Control configuration for host environments.
@@ -26,13 +28,23 @@ pub struct Args {
2628
pub fn generate_systemd_config(args: Args) -> Result<(), CliError> {
2729
info!("Adding required values to newrelic-agent-control.conf ");
2830

29-
update_config(CONFIG_PATH, &args.newrelic_license_key, args.region)?;
31+
let config_path = PathBuf::from(AGENT_CONTROL_LOCAL_DATA_DIR).join(SERVICE_CONFIG_FILE);
32+
33+
update_config(
34+
config_path.as_path(),
35+
&args.newrelic_license_key,
36+
args.region,
37+
)?;
3038

3139
info!("Host monitoring values generated successfully");
3240
Ok(())
3341
}
3442

35-
fn update_config(config_path: &str, new_license_key: &str, region: Region) -> Result<(), CliError> {
43+
fn update_config(
44+
config_path: &Path,
45+
new_license_key: &str,
46+
region: Region,
47+
) -> Result<(), CliError> {
3648
// Read the content from the configuration file
3749
let content = std::fs::read_to_string(config_path)
3850
.map_err(|err| CliError::Command(format!("error reading agent control .conf file: {err}")))?
@@ -90,7 +102,7 @@ mod tests {
90102
let new_license_key = "new_key";
91103
let region = Region::EU; // Assuming Region::EU is a valid variant
92104

93-
let result = update_config(file_path.to_str().unwrap(), new_license_key, region);
105+
let result = update_config(file_path.as_path(), new_license_key, region);
94106
assert!(result.is_ok());
95107

96108
let updated_content = std::fs::read_to_string(&file_path).unwrap();

test/onhost-e2e/ansible/infra_agent.yaml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@
1919
monitoring_source: "infra-agent"
2020
fleet_enabled: "false"
2121

22+
- name: Set test identifier
23+
set_fact:
24+
test_id: "onhost-e2e-infra-agent_{{ lookup('pipe', 'date +%Y-%m-%d_%H-%M-%S') }}"
25+
2226
- name: Setup AC config
2327
include_role:
2428
name: edit_yaml_config
2529
vars:
2630
config_path: "/etc/newrelic-agent-control/config.yaml"
31+
# Setting a custom host id makes the test assertion faster than custom attributes
32+
# as skip the attribute decoration on the BE
33+
host_id: "{{ test_id }}"
2734
update_config:
2835
log:
2936
format:
3037
formatter: pretty
3138
target: true
3239
level: debug
3340

34-
- name: Set test identifier
35-
set_fact:
36-
test_id: "onhost-e2e-infra-agent_{{ lookup('pipe', 'date +%Y-%m-%d_%H-%M-%S') }}"
37-
38-
- name: Add infra agent test_id attribute
39-
shell: echo 'NRIA_CUSTOM_ATTRIBUTES='\''{"label.test_id":"{{ test_id }}"}'\''' >> {{ agent_control_service_conf }}
40-
4141
- name: Restart Agent Control
4242
include_role:
4343
name: caos.ansible_roles.service_status
@@ -54,9 +54,8 @@
5454
nrql_query: >-
5555
SELECT *
5656
FROM SystemSample
57-
WHERE `label.test_id` = '{{ test_id }}'
57+
WHERE `host.id` = '{{ test_id }}'
5858
LIMIT 1
59-
# This was taking about 5m when the test was created
6059
retries: 120
6160
delay: 10
6261

0 commit comments

Comments
 (0)