aKode fase 2c: eliminato il codice di invio dati (crate analytics, otel-trace-websocket, sentry, esportatori OTLP); /feedback salva solo in locale; chiavi legacy [analytics]/[otel] accettate e ignorate; test di analytics/OTLP/upload rimossi
Some checks failed
blocking-ci / rust-ci (push) Has been cancelled
blocking-ci / sdk (push) Has been cancelled
blocking-ci / CI required (push) Has been cancelled
blocking-ci / Bazel (push) Has been cancelled
blocking-ci / Blob size policy (push) Has been cancelled
blocking-ci / cargo-deny (push) Has been cancelled
blocking-ci / Codespell (push) Has been cancelled
blocking-ci / repo-checks (push) Has been cancelled
postmerge-ci / rust-ci-full (push) Has been cancelled
postmerge-ci / v8-canary (push) Has been cancelled
postmerge-ci / Postmerge CI results (push) Has been cancelled
Some checks failed
blocking-ci / rust-ci (push) Has been cancelled
blocking-ci / sdk (push) Has been cancelled
blocking-ci / CI required (push) Has been cancelled
blocking-ci / Bazel (push) Has been cancelled
blocking-ci / Blob size policy (push) Has been cancelled
blocking-ci / cargo-deny (push) Has been cancelled
blocking-ci / Codespell (push) Has been cancelled
blocking-ci / repo-checks (push) Has been cancelled
postmerge-ci / rust-ci-full (push) Has been cancelled
postmerge-ci / v8-canary (push) Has been cancelled
postmerge-ci / Postmerge CI results (push) Has been cancelled
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
use crate::config::OtelExporter;
|
||||
use crate::config::OtelHttpProtocol;
|
||||
use crate::metrics::MetricsError;
|
||||
use crate::metrics::Result;
|
||||
use crate::metrics::config::MetricsConfig;
|
||||
use crate::metrics::config::MetricsExporter;
|
||||
use crate::metrics::names::TURN_CRITICAL_PATH_COMPONENT_METRIC;
|
||||
use crate::metrics::names::TURN_CRITICAL_PATH_DURATION_METRIC;
|
||||
use crate::metrics::timer::Timer;
|
||||
@@ -18,13 +15,6 @@ use opentelemetry::metrics::Gauge;
|
||||
use opentelemetry::metrics::Histogram;
|
||||
use opentelemetry::metrics::Meter;
|
||||
use opentelemetry::metrics::MeterProvider as _;
|
||||
use opentelemetry_otlp::OTEL_EXPORTER_OTLP_METRICS_TIMEOUT;
|
||||
use opentelemetry_otlp::Protocol;
|
||||
use opentelemetry_otlp::WithExportConfig;
|
||||
use opentelemetry_otlp::WithHttpConfig;
|
||||
use opentelemetry_otlp::WithTonicConfig;
|
||||
use opentelemetry_otlp::tonic_types::metadata::MetadataMap;
|
||||
use opentelemetry_otlp::tonic_types::transport::ClientTlsConfig;
|
||||
use opentelemetry_sdk::Resource;
|
||||
use opentelemetry_sdk::metrics::InstrumentKind;
|
||||
use opentelemetry_sdk::metrics::ManualReader;
|
||||
@@ -45,10 +35,6 @@ use std::sync::Weak;
|
||||
use std::time::Duration;
|
||||
use tracing::debug;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "client_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
const ENV_ATTRIBUTE: &str = "env";
|
||||
const METER_NAME: &str = "codex";
|
||||
const MILLISECOND_DURATION_UNIT: &str = "ms";
|
||||
@@ -121,7 +107,6 @@ impl MetricReader for SharedManualReader {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct MetricsClientInner {
|
||||
pub(super) network_policy: codex_http_client::NetworkPolicy,
|
||||
meter_provider: SdkMeterProvider,
|
||||
meter: Meter,
|
||||
counters: Mutex<HashMap<InstrumentKey, Counter<u64>>>,
|
||||
@@ -129,8 +114,6 @@ pub(super) struct MetricsClientInner {
|
||||
histograms: Mutex<HashMap<String, Histogram<f64>>>,
|
||||
duration_histograms: Mutex<HashMap<InstrumentKey, Histogram<f64>>>,
|
||||
runtime_reader: Option<Arc<ManualReader>>,
|
||||
requires_analytics_consent: bool,
|
||||
statsig_disabled_metrics: &'static [&'static str],
|
||||
default_tags: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
@@ -151,10 +134,6 @@ impl MetricsClientInner {
|
||||
}
|
||||
let attributes = self.attributes(tags)?;
|
||||
|
||||
if self.statsig_disabled_metrics.contains(&name) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut counters = self
|
||||
.counters
|
||||
.lock()
|
||||
@@ -185,10 +164,6 @@ impl MetricsClientInner {
|
||||
validate_metric_name(name)?;
|
||||
let attributes = self.attributes(tags)?;
|
||||
|
||||
if self.statsig_disabled_metrics.contains(&name) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut histograms = self
|
||||
.histograms
|
||||
.lock()
|
||||
@@ -214,10 +189,6 @@ impl MetricsClientInner {
|
||||
validate_metric_name(name)?;
|
||||
let attributes = self.attributes(tags)?;
|
||||
|
||||
if self.statsig_disabled_metrics.contains(&name) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut gauges = self
|
||||
.gauges
|
||||
.lock()
|
||||
@@ -248,10 +219,6 @@ impl MetricsClientInner {
|
||||
validate_metric_name(name)?;
|
||||
let attributes = self.attributes(tags)?;
|
||||
|
||||
if self.statsig_disabled_metrics.contains(&name) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let _gauge = self
|
||||
.meter
|
||||
.i64_observable_gauge(name.to_string())
|
||||
@@ -273,10 +240,6 @@ impl MetricsClientInner {
|
||||
validate_metric_name(name)?;
|
||||
let attributes = self.attributes(tags)?;
|
||||
|
||||
if self.statsig_disabled_metrics.contains(&name) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut histograms = self
|
||||
.duration_histograms
|
||||
.lock()
|
||||
@@ -332,10 +295,10 @@ impl MetricsClientInner {
|
||||
}
|
||||
}
|
||||
|
||||
/// OpenTelemetry metrics client used by Codex.
|
||||
/// Local, in-process OpenTelemetry metrics client. It never exports over the network.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct MetricsClient {
|
||||
// Keep the original provider so its owner only shuts down its own exporter.
|
||||
// Keep the original provider so its owner only shuts down its own reader.
|
||||
pub(super) inner: Arc<MetricsClientInner>,
|
||||
// Installed clients share this slot, so existing clones follow account changes.
|
||||
pub(super) active: Option<Arc<RwLock<Arc<MetricsClientInner>>>>,
|
||||
@@ -345,14 +308,12 @@ impl MetricsClient {
|
||||
/// Build a metrics client from configuration and validate defaults.
|
||||
pub fn new(config: MetricsConfig) -> Result<Self> {
|
||||
let MetricsConfig {
|
||||
http_client_factory,
|
||||
environment,
|
||||
service_name,
|
||||
service_version,
|
||||
exporter,
|
||||
export_interval,
|
||||
runtime_reader,
|
||||
statsig_disabled_metrics,
|
||||
default_tags,
|
||||
} = config;
|
||||
|
||||
@@ -379,34 +340,11 @@ impl MetricsClient {
|
||||
)
|
||||
});
|
||||
|
||||
let requires_analytics_consent =
|
||||
matches!(exporter, MetricsExporter::Otlp(OtelExporter::Statsig));
|
||||
let (meter_provider, meter) = match exporter {
|
||||
MetricsExporter::InMemory(exporter) => {
|
||||
build_provider(resource, exporter, export_interval, runtime_reader.clone())
|
||||
}
|
||||
MetricsExporter::Otlp(exporter) => {
|
||||
let temporality = otlp_metrics_temporality(
|
||||
&exporter,
|
||||
std::env::var("OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE")
|
||||
.ok()
|
||||
.as_deref(),
|
||||
);
|
||||
let exporter = crate::network_policy::PolicyExporter {
|
||||
exporter: build_otlp_metric_exporter(
|
||||
exporter,
|
||||
temporality,
|
||||
&http_client_factory,
|
||||
)?,
|
||||
policy: http_client_factory.network_policy().clone(),
|
||||
};
|
||||
build_provider(resource, exporter, export_interval, runtime_reader.clone())
|
||||
}
|
||||
};
|
||||
let (meter_provider, meter) =
|
||||
build_provider(resource, exporter, export_interval, runtime_reader.clone());
|
||||
|
||||
Ok(Self {
|
||||
inner: Arc::new(MetricsClientInner {
|
||||
network_policy: http_client_factory.network_policy().clone(),
|
||||
meter_provider,
|
||||
meter,
|
||||
counters: Mutex::new(HashMap::new()),
|
||||
@@ -414,8 +352,6 @@ impl MetricsClient {
|
||||
histograms: Mutex::new(HashMap::new()),
|
||||
duration_histograms: Mutex::new(HashMap::new()),
|
||||
runtime_reader,
|
||||
requires_analytics_consent,
|
||||
statsig_disabled_metrics,
|
||||
default_tags,
|
||||
}),
|
||||
active: None,
|
||||
@@ -438,22 +374,6 @@ impl MetricsClient {
|
||||
.counter(name, /*description*/ None, inc, tags)
|
||||
}
|
||||
|
||||
/// Record a counter while applying analytics consent only to the built-in exporter.
|
||||
/// The consent check and recording use the same exporter even during provider replacement.
|
||||
pub fn counter_with_analytics_consent(
|
||||
&self,
|
||||
name: &str,
|
||||
inc: i64,
|
||||
tags: &[(&str, &str)],
|
||||
analytics_enabled: bool,
|
||||
) -> Result<()> {
|
||||
let inner = self.active_inner();
|
||||
if inner.requires_analytics_consent && !analytics_enabled {
|
||||
return Ok(());
|
||||
}
|
||||
inner.counter(name, /*description*/ None, inc, tags)
|
||||
}
|
||||
|
||||
/// Send a single counter increment with an instrument description.
|
||||
pub fn counter_with_description(
|
||||
&self,
|
||||
@@ -639,104 +559,3 @@ where
|
||||
let meter = provider.meter(METER_NAME);
|
||||
(provider, meter)
|
||||
}
|
||||
|
||||
fn otlp_metrics_temporality(exporter: &OtelExporter, preference: Option<&str>) -> Temporality {
|
||||
// Select before resolving Statsig to OTLP so its built-in route stays Delta.
|
||||
match exporter {
|
||||
OtelExporter::None | OtelExporter::Statsig => Temporality::Delta,
|
||||
OtelExporter::OtlpGrpc { .. } | OtelExporter::OtlpHttp { .. } => match preference {
|
||||
Some(value) if value.eq_ignore_ascii_case("cumulative") => Temporality::Cumulative,
|
||||
Some(value) if value.eq_ignore_ascii_case("lowmemory") => Temporality::LowMemory,
|
||||
// Preserve Codex's existing default for unset, empty, or unknown values.
|
||||
_ => Temporality::Delta,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn build_otlp_metric_exporter(
|
||||
exporter: OtelExporter,
|
||||
temporality: Temporality,
|
||||
factory: &codex_http_client::HttpClientFactory,
|
||||
) -> Result<opentelemetry_otlp::MetricExporter> {
|
||||
match exporter {
|
||||
OtelExporter::None => Err(MetricsError::ExporterDisabled),
|
||||
OtelExporter::Statsig => build_otlp_metric_exporter(
|
||||
crate::config::resolve_exporter(&OtelExporter::Statsig),
|
||||
temporality,
|
||||
factory,
|
||||
),
|
||||
OtelExporter::OtlpGrpc {
|
||||
endpoint,
|
||||
headers,
|
||||
tls,
|
||||
} => {
|
||||
debug!("Using OTLP Grpc exporter for metrics: {endpoint}");
|
||||
|
||||
let header_map = crate::otlp::build_header_map(&headers);
|
||||
|
||||
let base_tls_config = ClientTlsConfig::new()
|
||||
.with_enabled_roots()
|
||||
.assume_http2(true);
|
||||
|
||||
let tls_config = match tls.as_ref() {
|
||||
Some(tls) => crate::otlp::build_grpc_tls_config(&endpoint, base_tls_config, tls)
|
||||
.map_err(|err| MetricsError::InvalidConfig {
|
||||
message: err.to_string(),
|
||||
})?,
|
||||
None => base_tls_config,
|
||||
};
|
||||
|
||||
opentelemetry_otlp::MetricExporter::builder()
|
||||
.with_tonic()
|
||||
.with_endpoint(endpoint)
|
||||
.with_temporality(temporality)
|
||||
.with_metadata(MetadataMap::from_headers(header_map))
|
||||
.with_tls_config(tls_config)
|
||||
.build()
|
||||
.map_err(|source| MetricsError::ExporterBuild { source })
|
||||
}
|
||||
OtelExporter::OtlpHttp {
|
||||
endpoint,
|
||||
headers,
|
||||
protocol,
|
||||
tls,
|
||||
} => {
|
||||
debug!("Using OTLP Http exporter for metrics: {endpoint}");
|
||||
|
||||
let protocol = match protocol {
|
||||
OtelHttpProtocol::Binary => Protocol::HttpBinary,
|
||||
OtelHttpProtocol::Json => Protocol::HttpJson,
|
||||
};
|
||||
|
||||
let mut exporter_builder = opentelemetry_otlp::MetricExporter::builder()
|
||||
.with_http()
|
||||
.with_endpoint(endpoint)
|
||||
.with_temporality(temporality)
|
||||
.with_protocol(protocol)
|
||||
.with_headers(headers);
|
||||
|
||||
if factory.network_policy().is_managed() {
|
||||
let client = crate::otlp::build_async_http_client(
|
||||
factory,
|
||||
tls.as_ref(),
|
||||
OTEL_EXPORTER_OTLP_METRICS_TIMEOUT,
|
||||
)
|
||||
.map_err(|err| MetricsError::InvalidConfig {
|
||||
message: err.to_string(),
|
||||
})?;
|
||||
exporter_builder = exporter_builder.with_http_client(client);
|
||||
} else if let Some(tls) = tls.as_ref() {
|
||||
let client =
|
||||
crate::otlp::build_http_client(tls, OTEL_EXPORTER_OTLP_METRICS_TIMEOUT)
|
||||
.map_err(|err| MetricsError::InvalidConfig {
|
||||
message: err.to_string(),
|
||||
})?;
|
||||
exporter_builder = exporter_builder.with_http_client(client);
|
||||
}
|
||||
|
||||
exporter_builder
|
||||
.build()
|
||||
.map_err(|source| MetricsError::ExporterBuild { source })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user