use ordered_float::OrderedFloat;
pub use std::time::Duration;
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(transparent)]
pub struct OrderedF64(OrderedFloat<f64>);
impl OrderedF64 {
pub const fn new(value: f64) -> Self {
Self(OrderedFloat(value))
}
}
impl From<f64> for OrderedF64 {
fn from(value: f64) -> Self {
Self(OrderedFloat(value))
}
}
impl From<OrderedF64> for f64 {
fn from(value: OrderedF64) -> Self {
value.0.0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum AgentMetadata {
#[non_exhaustive]
Ec2AgentMetadata {
aws_account_id: String,
aws_region_id: String,
ec2_instance_id: String,
ec2_instance_type: String,
},
#[non_exhaustive]
FargateAgentMetadata {
aws_account_id: String,
aws_region_id: String,
ecs_task_arn: String,
ecs_cluster_arn: String,
cpu_limit: Option<OrderedF64>,
memory_limit: Option<u64>,
},
#[deprecated = "Use AgentMetadata::NoMetadata"]
Other,
NoMetadata,
}
impl AgentMetadata {
#[cfg_attr(
feature = "aws-metadata-no-defaults",
doc = "[aws::load_agent_metadata],"
)]
#[cfg_attr(
feature = "aws-metadata-no-defaults",
doc = "`aws::load_agent_metadata`,"
)]
pub fn ec2_agent_metadata(
aws_account_id: String,
aws_region_id: String,
ec2_instance_id: String,
) -> Ec2AgentMetadataBuilder {
Ec2AgentMetadataBuilder {
aws_account_id,
aws_region_id,
ec2_instance_id,
ec2_instance_type: None,
}
}
#[cfg_attr(
feature = "aws-metadata-no-defaults",
doc = "[aws::load_agent_metadata],"
)]
#[cfg_attr(
feature = "aws-metadata-no-defaults",
doc = "`aws::load_agent_metadata`,"
)]
pub fn fargate_agent_metadata(
aws_account_id: String,
aws_region_id: String,
ecs_task_arn: String,
ecs_cluster_arn: String,
) -> FargateAgentMetadataBuilder {
FargateAgentMetadataBuilder {
aws_account_id,
aws_region_id,
ecs_task_arn,
ecs_cluster_arn,
cpu_limit: None,
memory_limit: None,
}
}
}
#[derive(Debug, Clone)]
pub struct Ec2AgentMetadataBuilder {
aws_account_id: String,
aws_region_id: String,
ec2_instance_id: String,
ec2_instance_type: Option<String>,
}
impl Ec2AgentMetadataBuilder {
pub fn with_ec2_instance_type(mut self, ec2_instance_type: String) -> Self {
self.ec2_instance_type = Some(ec2_instance_type);
self
}
pub fn build(self) -> AgentMetadata {
AgentMetadata::Ec2AgentMetadata {
aws_account_id: self.aws_account_id,
aws_region_id: self.aws_region_id,
ec2_instance_id: self.ec2_instance_id,
ec2_instance_type: self.ec2_instance_type.unwrap_or_default(),
}
}
}
#[derive(Debug, Clone)]
pub struct FargateAgentMetadataBuilder {
aws_account_id: String,
aws_region_id: String,
ecs_task_arn: String,
ecs_cluster_arn: String,
cpu_limit: Option<f64>,
memory_limit: Option<u64>,
}
impl FargateAgentMetadataBuilder {
pub fn with_cpu_limit(mut self, cpu_limit: f64) -> Self {
self.cpu_limit = Some(cpu_limit);
self
}
pub fn with_memory_limit(mut self, memory_limit: u64) -> Self {
self.memory_limit = Some(memory_limit);
self
}
pub fn build(self) -> AgentMetadata {
AgentMetadata::FargateAgentMetadata {
aws_account_id: self.aws_account_id,
aws_region_id: self.aws_region_id,
ecs_task_arn: self.ecs_task_arn,
ecs_cluster_arn: self.ecs_cluster_arn,
cpu_limit: self.cpu_limit.map(Into::into),
memory_limit: self.memory_limit,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReportMetadata<'a> {
pub instance: &'a AgentMetadata,
pub start: Duration,
pub end: Duration,
pub reporting_interval: Duration,
}
#[cfg(feature = "aws-metadata-no-defaults")]
pub mod aws;
#[cfg(test)]
pub(crate) const DUMMY_METADATA: ReportMetadata<'static> = ReportMetadata {
instance: &AgentMetadata::NoMetadata,
start: Duration::from_secs(1),
end: Duration::from_secs(2),
reporting_interval: Duration::from_secs(1),
};