mirror of
https://github.com/hanzoai/codex.git
synced 2026-10-30 14:59:26 +00:00
Upstream crates read Dev's words and defaults from crates in hanzoai/dev, by a path out of this tree. What they read is data and an interface, so it moves here, into codex-rs/hanzo, which absorbs hanzo-version: - the brand strings and the TUI's defaults (hanzo-tui's constants and input_background), read by codex-tui; - API_BASE, DEFAULT_MODEL, is_hanzo and the model catalog (from hanzo-config), read by models-manager and model-provider; - `model::pick`, the `/model <name>` match, pure over the catalog; - the controller interface the agent loop consults (hanzo-loop: Controller, Handle, Verdict, Card, Request, Shortlist), read by codex-core; - the version hook, now `hanzo::version`. Dev's crates depend on this one from now on, never the reverse. Kai and the Hanzo account are behaviour, and still named from here until the next two commits turn them into hooks a binary fills.
80 lines
2.5 KiB
Rust
80 lines
2.5 KiB
Rust
use codex_otel::MetricsClient;
|
|
use codex_otel::MetricsConfig;
|
|
use codex_otel::Result;
|
|
use opentelemetry::KeyValue;
|
|
use opentelemetry_sdk::metrics::InMemoryMetricExporter;
|
|
use opentelemetry_sdk::metrics::data::AggregatedMetrics;
|
|
use opentelemetry_sdk::metrics::data::Metric;
|
|
use opentelemetry_sdk::metrics::data::MetricData;
|
|
use opentelemetry_sdk::metrics::data::ResourceMetrics;
|
|
use std::collections::BTreeMap;
|
|
|
|
pub(crate) fn build_metrics_with_defaults(
|
|
default_tags: &[(&str, &str)],
|
|
) -> Result<(MetricsClient, InMemoryMetricExporter)> {
|
|
let exporter = InMemoryMetricExporter::default();
|
|
let mut config = MetricsConfig::in_memory(
|
|
"test",
|
|
"codex-cli",
|
|
hanzo::version::get(),
|
|
exporter.clone(),
|
|
);
|
|
for (key, value) in default_tags {
|
|
config = config.with_tag(*key, *value)?;
|
|
}
|
|
let metrics = MetricsClient::new(config)?;
|
|
Ok((metrics, exporter))
|
|
}
|
|
|
|
pub(crate) fn latest_metrics(exporter: &InMemoryMetricExporter) -> ResourceMetrics {
|
|
exporter
|
|
.get_finished_metrics()
|
|
.expect("finished metrics should be available")
|
|
.into_iter()
|
|
.last()
|
|
.expect("metrics export should exist")
|
|
}
|
|
|
|
pub(crate) fn find_metric<'a>(
|
|
resource_metrics: &'a ResourceMetrics,
|
|
name: &str,
|
|
) -> Option<&'a Metric> {
|
|
for scope_metrics in resource_metrics.scope_metrics() {
|
|
for metric in scope_metrics.metrics() {
|
|
if metric.name() == name {
|
|
return Some(metric);
|
|
}
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
pub(crate) fn attributes_to_map<'a>(
|
|
attributes: impl Iterator<Item = &'a KeyValue>,
|
|
) -> BTreeMap<String, String> {
|
|
attributes
|
|
.map(|kv| (kv.key.as_str().to_string(), kv.value.as_str().to_string()))
|
|
.collect()
|
|
}
|
|
|
|
pub(crate) fn histogram_data(
|
|
resource_metrics: &ResourceMetrics,
|
|
name: &str,
|
|
) -> (Vec<f64>, Vec<u64>, f64, u64) {
|
|
let metric = find_metric(resource_metrics, name).expect("metric should exist");
|
|
match metric.data() {
|
|
AggregatedMetrics::F64(data) => match data {
|
|
MetricData::Histogram(histogram) => {
|
|
let points: Vec<_> = histogram.data_points().collect();
|
|
assert_eq!(points.len(), 1);
|
|
let point = points[0];
|
|
let bounds = point.bounds().collect();
|
|
let bucket_counts = point.bucket_counts().collect();
|
|
(bounds, bucket_counts, point.sum(), point.count())
|
|
}
|
|
_ => panic!("unexpected histogram aggregation"),
|
|
},
|
|
_ => panic!("unexpected metric data type"),
|
|
}
|
|
}
|