Skip to content

Commit a8346ed

Browse files
feat: add new host_monitoring cli creating infra-agent config (#1806)
* feat: add new host_monitoring cli creating infra-agent config * feat: move new to from infra-config to the test block * feat: move infra_gent config to a constant. * feat: simplify the host_monitoring tests. * feat: improve message of why we avoid some host monitoring generate tests on windows * feat: use agent_set instead of host_monitoring enum * Update agent-control/src/cli/on_host/host_monitoring_gen/infra_config_gen.rs Co-authored-by: danielorihuela <[email protected]> * feat: Fix formatting * feat: otel monitoring cli (#1817) * feat: add systemd cli and otel host_monitoring cli creating otel config * feat: rollback wrong command name change for generateConfig * feat: move otel_endpoint to be part of the region * feat: merge otel endpoint and license_key modification in systemd endpoint to one action * feat: Add error handling to generate_host_monitoring_config --------- Co-authored-by: danielorihuela <[email protected]>
1 parent 1a939cd commit a8346ed

File tree

15 files changed

+1140
-95
lines changed

15 files changed

+1140
-95
lines changed

agent-control/src/bin/main_agent_control_onhost_cli.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::process::ExitCode;
22

33
use clap::{CommandFactory, Parser, error::ErrorKind};
4+
use newrelic_agent_control::cli::on_host::{host_monitoring_gen, systemd_gen};
45
use newrelic_agent_control::cli::{logs, on_host::config_gen};
56
use tracing::{Level, error};
67

@@ -15,10 +16,15 @@ struct Cli {
1516
}
1617

1718
/// Commands supported by the cli
19+
#[allow(clippy::large_enum_variant)]
1820
#[derive(Debug, clap::Subcommand)]
1921
enum Commands {
2022
// Generate Agent Control configuration according to the provided configuration data.
2123
GenerateConfig(config_gen::Args),
24+
// Generate Host Monitoring configuration according to the provided configuration data.
25+
HostMonitoring(host_monitoring_gen::Args),
26+
// Generate systemd configuration according to the provided configuration data.
27+
SystemdConfig(systemd_gen::Args),
2228
}
2329

2430
fn main() -> ExitCode {
@@ -39,6 +45,10 @@ fn main() -> ExitCode {
3945
}
4046
config_gen::generate_config(args)
4147
}
48+
Commands::HostMonitoring(args) => {
49+
host_monitoring_gen::generate_host_monitoring_config(args)
50+
}
51+
Commands::SystemdConfig(args) => systemd_gen::generate_systemd_config(args),
4252
};
4353

4454
if let Err(err) = result {

agent-control/src/bin/main_config_migrate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ fn main() -> Result<(), Box<dyn Error>> {
4040
MappingType::Dir(dir_path) => {
4141
legacy_config_renamer.rename_path(dir_path.dir_path.as_path())?
4242
}
43-
MappingType::File(file_path) => {
44-
legacy_config_renamer.rename_path(file_path.as_path())?
43+
MappingType::File(file_info) => {
44+
legacy_config_renamer.rename_path(file_info.file_path.as_path())?
4545
}
4646
}
4747
}

agent-control/src/cli/on_host.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
pub mod config_gen;
2+
3+
pub mod host_monitoring_gen;
4+
pub mod systemd_gen;

agent-control/src/cli/on_host/config_gen/region.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ const PUBLIC_KEY_ENDPOINT_EU: &str =
1616
const PUBLIC_KEY_ENDPOINT_STAGING: &str =
1717
"https://staging-publickeys.newrelic.com/r/blob-management/global/agentconfiguration/jwks.json";
1818

19+
const OTLP_URL_STAGING: &str = "staging.otlp.nr-data.net";
20+
const OTLP_URL_EU: &str = "otlp.eu01.nr-data.net";
21+
const OTLP_URL_US: &str = "otlp.nr-data.net";
22+
1923
/// Represents the supported region and defines related fields. It cannot wrap the [Environments] enum
2024
/// due to clap limitations. Re-defining the enum is simpler than extending and using some mapping
2125
/// tool such as [clap::builder::TypedValueParser::map].
@@ -74,6 +78,16 @@ impl Region {
7478
.expect("known uris should be valid")
7579
}
7680

81+
pub fn otel_endpoint(&self) -> Uri {
82+
match &self {
83+
Self::US => OTLP_URL_US,
84+
Self::EU => OTLP_URL_EU,
85+
Self::STAGING => OTLP_URL_STAGING,
86+
}
87+
.parse()
88+
.expect("known uris should be valid")
89+
}
90+
7791
// Helper to obtain the token renewal endpoint that is also needed in Agent Control configuration
7892
pub fn token_renewal_endpoint(&self) -> Uri {
7993
NewRelicEnvironment::from(self.to_owned()).token_renewal_endpoint()
@@ -144,6 +158,17 @@ mod tests {
144158
);
145159
}
146160

161+
#[rstest]
162+
#[case(Region::US, "otlp.nr-data.net")]
163+
#[case(Region::EU, "otlp.eu01.nr-data.net")]
164+
#[case(Region::STAGING, "staging.otlp.nr-data.net")]
165+
fn test_otel_endpoint(#[case] region: Region, #[case] expected_endpoint: &str) {
166+
assert_eq!(
167+
region.otel_endpoint().to_string(),
168+
expected_endpoint.to_string()
169+
);
170+
}
171+
147172
#[rstest]
148173
#[case("us", Region::US)]
149174
#[case("US", Region::US)]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//! Implementation of the generate-config command for the on-host cli.
2+
3+
use crate::cli::error::CliError;
4+
use crate::cli::on_host::config_gen::config::AgentSet;
5+
use crate::cli::on_host::config_gen::region::{Region, region_parser};
6+
use crate::cli::on_host::host_monitoring_gen::infra_config_gen::InfraConfigGenerator;
7+
use crate::cli::on_host::host_monitoring_gen::otel_config_gen::OtelConfigGen;
8+
use tracing::info;
9+
10+
pub mod infra_config;
11+
pub mod infra_config_gen;
12+
pub mod otel_config_gen;
13+
14+
/// Generates the Agent Control configuration for host environments.
15+
#[derive(Debug, clap::Parser)]
16+
pub struct Args {
17+
/// Agent to be used for host monitoring.
18+
#[arg(long, required = true)]
19+
agent_set: AgentSet,
20+
21+
/// Custom Attributes
22+
#[arg(long)]
23+
custom_attributes: Option<String>,
24+
25+
/// Proxy configuration
26+
#[arg(long)]
27+
proxy: Option<String>,
28+
29+
/// New Relic region
30+
#[arg(long, value_parser = region_parser(), required = true)]
31+
region: Region,
32+
}
33+
34+
/// Generates the Host monitoring values either infra-agent or otel.
35+
pub fn generate_host_monitoring_config(args: Args) -> Result<(), CliError> {
36+
info!("Generating Host monitoring values");
37+
38+
match args.agent_set {
39+
AgentSet::InfraAgent => {
40+
let infra_config_generator = InfraConfigGenerator::default();
41+
42+
infra_config_generator
43+
.generate_infra_config(args.region, args.custom_attributes, args.proxy)
44+
.map_err(|err| {
45+
CliError::Command(format!("failed generating infra config: {err}"))
46+
})?;
47+
}
48+
AgentSet::Otel => {
49+
let otel_config_generator = OtelConfigGen::default();
50+
otel_config_generator
51+
.generate_otel_config()
52+
.map_err(|err| {
53+
CliError::Command(format!("failed generating otel config: {err}"))
54+
})?;
55+
}
56+
_ => {}
57+
}
58+
59+
info!("Host monitoring values generated successfully");
60+
Ok(())
61+
}

0 commit comments

Comments
 (0)