Skip to content

Commit f767c3b

Browse files
committed
aws: make ULIDs more readable in debug logs
1 parent 89f7f07 commit f767c3b

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

factory/aws/src/aws.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ use aws_sdk_ec2::types::{
1818
};
1919
use base64::Engine;
2020
use buildomat_client::types::*;
21-
use rusty_ulid::Ulid;
2221
use slog::{debug, error, info, o, warn, Logger};
2322

24-
use super::{config::ConfigFileAwsTarget, Central, ConfigFile};
23+
use super::{config::ConfigFileAwsTarget, types::*, Central, ConfigFile};
2524

2625
#[derive(Debug)]
2726
struct Instance {
2827
id: String,
2928
state: String,
3029
ip: Option<String>,
31-
worker_id: Option<Ulid>,
32-
lease_id: Option<Ulid>,
30+
worker_id: Option<WorkerId>,
31+
lease_id: Option<LeaseId>,
3332
launch_time: Option<aws_sdk_ec2::primitives::DateTime>,
3433
}
3534

@@ -45,13 +44,13 @@ impl From<(&aws_sdk_ec2::types::Instance, &str)> for Instance {
4544
.tags()
4645
.tag(&format!("{tag}-worker_id"))
4746
.as_ref()
48-
.and_then(|v| Ulid::from_str(v).ok());
47+
.and_then(|v| WorkerId::from_str(v).ok());
4948

5049
let lease_id = i
5150
.tags()
5251
.tag(&format!("{tag}-lease_id"))
5352
.as_ref()
54-
.and_then(|v| Ulid::from_str(v).ok());
53+
.and_then(|v| LeaseId::from_str(v).ok());
5554

5655
let ip = i.private_ip_address.clone();
5756

factory/aws/src/main.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,48 @@ mod aws;
1414
mod config;
1515
use config::ConfigFile;
1616

17+
mod types {
18+
use rusty_ulid::Ulid;
19+
use std::str::FromStr;
20+
21+
macro_rules! ulid_new_type {
22+
($name:ident, $prefix:literal) => {
23+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
24+
#[repr(transparent)]
25+
pub struct $name(Ulid);
26+
27+
impl FromStr for $name {
28+
type Err = anyhow::Error;
29+
30+
fn from_str(s: &str) -> Result<Self, Self::Err> {
31+
Ok($name(Ulid::from_str(s)?))
32+
}
33+
}
34+
35+
impl std::fmt::Display for $name {
36+
fn fmt(
37+
&self,
38+
f: &mut std::fmt::Formatter<'_>,
39+
) -> std::fmt::Result {
40+
self.0.fmt(f)
41+
}
42+
}
43+
44+
impl std::fmt::Debug for $name {
45+
fn fmt(
46+
&self,
47+
f: &mut std::fmt::Formatter<'_>,
48+
) -> std::fmt::Result {
49+
format_args!("{}:{self}", $prefix).fmt(f)
50+
}
51+
}
52+
};
53+
}
54+
55+
ulid_new_type!(LeaseId, "lease");
56+
ulid_new_type!(WorkerId, "worker");
57+
}
58+
1759
struct Central {
1860
log: Logger,
1961
config: config::ConfigFile,

0 commit comments

Comments
 (0)