Skip to content

Commit c7fc1d1

Browse files
committed
tidy up a bit
1 parent 7726d70 commit c7fc1d1

File tree

10 files changed

+186
-192
lines changed

10 files changed

+186
-192
lines changed

runtimes/core/src/api/auth/local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl AuthHandler for LocalAuthHandler {
171171
};
172172
self.tracer.request_span_end(&model_resp, false);
173173
self.requests_total
174-
.with([("code", &e.code.to_string())])
174+
.with([("code", e.code.to_string())])
175175
.increment();
176176
Err(e)
177177
}

runtimes/core/src/api/endpoint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ impl EndpointHandler {
687687
}),
688688
};
689689
self.shared.tracer.request_span_end(&model_resp, sensitive);
690-
self.requests_total.with([("code", &code)]).increment();
690+
self.requests_total.with([("code", code)]).increment();
691691
}
692692

693693
if let Ok(val) = HeaderValue::from_str(request.span.0.serialize_encore().as_str()) {

runtimes/core/src/metrics/counter.rs

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ where
7979
pub fn with<L, K, V>(&self, dynamic_labels: L) -> Counter<T>
8080
where
8181
L: IntoIterator<Item = (K, V)>,
82-
K: AsRef<str>,
83-
V: AsRef<str>,
82+
K: Into<String>,
83+
V: Into<String>,
8484
{
8585
// Convert dynamic_labels to HashMap first
8686
let dynamic_labels_map: HashMap<String, String> = dynamic_labels
8787
.into_iter()
88-
.map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
88+
.map(|(k, v)| (k.into(), v.into()))
8989
.collect();
9090

9191
// Validate required keys are present
@@ -138,3 +138,77 @@ where
138138
)
139139
}
140140
}
141+
142+
/// Builder for creating counter schemas with static labels and required dynamic keys
143+
pub struct CounterSchemaBuilder<T> {
144+
name: String,
145+
static_labels: Vec<(String, String)>,
146+
required_dynamic_keys: HashSet<String>,
147+
registry: Arc<metrics::Registry>,
148+
_phantom: std::marker::PhantomData<T>,
149+
}
150+
151+
impl<T> CounterSchemaBuilder<T>
152+
where
153+
Arc<AtomicU64>: CounterOps<T>,
154+
T: One + Send + Sync + 'static,
155+
{
156+
pub(crate) fn new(name: String, registry: Arc<metrics::Registry>) -> Self {
157+
Self {
158+
name,
159+
static_labels: Vec::new(),
160+
required_dynamic_keys: HashSet::new(),
161+
registry,
162+
_phantom: std::marker::PhantomData,
163+
}
164+
}
165+
166+
/// Add static labels that are set once when the schema is created
167+
pub fn static_labels<I, K, V>(mut self, labels: I) -> Self
168+
where
169+
I: IntoIterator<Item = (K, V)>,
170+
K: AsRef<str>,
171+
V: AsRef<str>,
172+
{
173+
for (key, value) in labels {
174+
self.static_labels
175+
.push((key.as_ref().to_string(), value.as_ref().to_string()));
176+
}
177+
self
178+
}
179+
180+
/// Add a single static label
181+
pub fn static_label(mut self, key: &str, value: &str) -> Self {
182+
self.static_labels
183+
.push((key.to_string(), value.to_string()));
184+
self
185+
}
186+
187+
/// Specify required dynamic label keys that must be provided at increment time
188+
pub fn require_dynamic_keys<I, K>(mut self, keys: I) -> Self
189+
where
190+
I: IntoIterator<Item = K>,
191+
K: AsRef<str>,
192+
{
193+
for key in keys {
194+
self.required_dynamic_keys.insert(key.as_ref().to_string());
195+
}
196+
self
197+
}
198+
199+
/// Add a single required dynamic key
200+
pub fn require_dynamic_key(mut self, key: &str) -> Self {
201+
self.required_dynamic_keys.insert(key.to_string());
202+
self
203+
}
204+
205+
/// Build the counter schema
206+
pub fn build(self) -> Schema<T> {
207+
Schema::new(
208+
self.name,
209+
self.static_labels,
210+
self.required_dynamic_keys,
211+
self.registry,
212+
)
213+
}
214+
}

runtimes/core/src/metrics/exporter/gcp.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,12 @@ impl Gcp {
8282
self.project_id
8383
);
8484

85-
// Convert our metrics to Google Cloud TimeSeries format
8685
let time_series = self.get_metric_data(metrics);
8786

8887
// Send metrics in batches (Google Cloud allows up to 200 time series per request)
8988
for batch in time_series.chunks(200) {
9089
if let Err(e) = self.send_time_series_batch(client, batch.to_vec()).await {
9190
log::error!("Failed to export metrics batch: {}", e);
92-
// Continue with remaining batches even if one fails
9391
}
9492
}
9593

runtimes/core/src/metrics/gauge.rs

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ where
9090
pub fn with<L, K, V>(&self, dynamic_labels: L) -> Gauge<T>
9191
where
9292
L: IntoIterator<Item = (K, V)>,
93-
K: AsRef<str>,
94-
V: AsRef<str>,
93+
K: Into<String>,
94+
V: Into<String>,
9595
{
9696
// Convert dynamic_labels to HashMap first
9797
let dynamic_labels_map: HashMap<String, String> = dynamic_labels
9898
.into_iter()
99-
.map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
99+
.map(|(k, v)| (k.into(), v.into()))
100100
.collect();
101101

102102
// Validate required keys are present
@@ -133,3 +133,77 @@ where
133133
)
134134
}
135135
}
136+
137+
/// Builder for creating gauge schemas with static labels and required dynamic keys
138+
pub struct GaugeSchemaBuilder<T> {
139+
name: String,
140+
static_labels: Vec<(String, String)>,
141+
required_dynamic_keys: HashSet<String>,
142+
registry: Arc<metrics::Registry>,
143+
_phantom: std::marker::PhantomData<T>,
144+
}
145+
146+
impl<T> GaugeSchemaBuilder<T>
147+
where
148+
Arc<AtomicU64>: GaugeOps<T>,
149+
T: Send + Sync + 'static,
150+
{
151+
pub(crate) fn new(name: String, registry: Arc<metrics::Registry>) -> Self {
152+
Self {
153+
name,
154+
static_labels: Vec::new(),
155+
required_dynamic_keys: HashSet::new(),
156+
registry,
157+
_phantom: std::marker::PhantomData,
158+
}
159+
}
160+
161+
/// Add static labels that are set once when the schema is created
162+
pub fn static_labels<I, K, V>(mut self, labels: I) -> Self
163+
where
164+
I: IntoIterator<Item = (K, V)>,
165+
K: AsRef<str>,
166+
V: AsRef<str>,
167+
{
168+
for (key, value) in labels {
169+
self.static_labels
170+
.push((key.as_ref().to_string(), value.as_ref().to_string()));
171+
}
172+
self
173+
}
174+
175+
/// Add a single static label
176+
pub fn static_label(mut self, key: &str, value: &str) -> Self {
177+
self.static_labels
178+
.push((key.to_string(), value.to_string()));
179+
self
180+
}
181+
182+
/// Specify required dynamic label keys that must be provided at set/add/sub time
183+
pub fn require_dynamic_keys<I, K>(mut self, keys: I) -> Self
184+
where
185+
I: IntoIterator<Item = K>,
186+
K: AsRef<str>,
187+
{
188+
for key in keys {
189+
self.required_dynamic_keys.insert(key.as_ref().to_string());
190+
}
191+
self
192+
}
193+
194+
/// Add a single required dynamic key
195+
pub fn require_dynamic_key(mut self, key: &str) -> Self {
196+
self.required_dynamic_keys.insert(key.to_string());
197+
self
198+
}
199+
200+
/// Build the gauge schema
201+
pub fn build(self) -> Schema<T> {
202+
Schema::new(
203+
self.name,
204+
self.static_labels,
205+
self.required_dynamic_keys,
206+
self.registry,
207+
)
208+
}
209+
}

runtimes/core/src/metrics/manager.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ impl Manager {
9696
}
9797
}
9898

99-
/// Get direct access to the registry
10099
pub fn registry(&self) -> &Registry {
101100
&self.registry
102101
}

runtimes/core/src/metrics/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use manager::Manager;
1616
pub use registry::{CollectedMetric, MetricValue, Registry};
1717
pub use system::SystemMetricsCollector;
1818

19-
/// Create a requests counter schema with service and endpoint static labels, requiring code as dynamic label
19+
/// Create a requests counter schema
2020
pub fn requests_total_counter(
2121
registry: &Registry,
2222
service: &str,
@@ -29,7 +29,7 @@ pub fn requests_total_counter(
2929
.build()
3030
}
3131

32-
/// Create a memory usage gauge schema requiring type label as dynamic
32+
/// Create a memory usage gauge schema
3333
pub fn memory_usage_gauge_schema(registry: &Registry) -> gauge::Schema<u64> {
3434
registry
3535
.gauge_schema::<u64>("e_sys_memory_used_bytes")

0 commit comments

Comments
 (0)