mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-26 10:27:32 +00:00
110 lines
3.2 KiB
Rust
110 lines
3.2 KiB
Rust
use conduwuit_core::{
|
|
Result,
|
|
config::Config,
|
|
err,
|
|
metrics::{Metrics, duration_buckets},
|
|
};
|
|
use opentelemetry::{
|
|
InstrumentationScope, KeyValue, global,
|
|
metrics::{MeterProvider, noop::NoopMeterProvider},
|
|
};
|
|
use opentelemetry_sdk::{
|
|
Resource,
|
|
metrics::{Aggregation, Instrument, SdkMeterProvider, Stream},
|
|
resource::{EnvResourceDetector, TelemetryResourceDetector},
|
|
};
|
|
use opentelemetry_semantic_conventions::{SCHEMA_URL, attribute, metric};
|
|
use tokio::runtime;
|
|
|
|
/// The returned provider must be shut down before the runtime stops so pending
|
|
/// OTLP exports are flushed.
|
|
pub(crate) fn init(
|
|
config: &Config,
|
|
runtime: Option<&runtime::Handle>,
|
|
) -> Result<(Metrics, SdkMeterProvider)> {
|
|
let resource = resource(config);
|
|
|
|
let mut builder = SdkMeterProvider::builder()
|
|
.with_resource(resource)
|
|
.with_view(|instrument: &Instrument| match instrument.name() {
|
|
| metric::HTTP_SERVER_REQUEST_DURATION => Stream::builder()
|
|
.with_aggregation(Aggregation::ExplicitBucketHistogram {
|
|
boundaries: duration_buckets(),
|
|
record_min_max: true,
|
|
})
|
|
.build()
|
|
.ok(),
|
|
| _ => None,
|
|
});
|
|
|
|
let registry = config
|
|
.allow_prometheus_metrics
|
|
.then(prometheus::Registry::new);
|
|
let mut readers = registry.is_some();
|
|
if let Some(registry) = ®istry {
|
|
let exporter = opentelemetry_prometheus::exporter()
|
|
.with_registry(registry.clone())
|
|
.scope_info_enabled(false)
|
|
.build()
|
|
.map_err(|e| err!(Config("allow_prometheus_metrics", "{e}")))?;
|
|
|
|
builder = builder.with_reader(exporter);
|
|
}
|
|
|
|
#[cfg(feature = "otlp_telemetry")]
|
|
if config.allow_otlp_metrics {
|
|
use opentelemetry_otlp::MetricExporter;
|
|
|
|
let exporter = MetricExporter::builder()
|
|
.build()
|
|
.map_err(|e| err!(Config("allow_otlp_metrics", "{e}")))?;
|
|
|
|
builder = builder.with_periodic_exporter(exporter);
|
|
readers = true;
|
|
}
|
|
|
|
#[cfg(not(feature = "otlp_telemetry"))]
|
|
if config.allow_otlp_metrics {
|
|
conduwuit_core::warn!(
|
|
"allow_otlp_metrics is set but this build does not include the otlp_telemetry \
|
|
feature"
|
|
);
|
|
}
|
|
|
|
let provider = builder.build();
|
|
let scope = InstrumentationScope::builder(conduwuit_core::BRANDING)
|
|
.with_version(conduwuit_core::version())
|
|
.with_schema_url(SCHEMA_URL)
|
|
.build();
|
|
|
|
// Without a reader the SDK rejects every instrument and logs an error for
|
|
// each, so nothing is recorded.
|
|
let meter = if readers {
|
|
global::set_meter_provider(provider.clone());
|
|
provider.meter_with_scope(scope)
|
|
} else {
|
|
NoopMeterProvider::new().meter_with_scope(scope)
|
|
};
|
|
|
|
Ok((Metrics::new(runtime.cloned(), meter, registry), provider))
|
|
}
|
|
|
|
pub(crate) fn resource(config: &Config) -> Resource {
|
|
// Defaults first so OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME win.
|
|
let mut resource = Resource::builder_empty()
|
|
.with_detector(Box::new(TelemetryResourceDetector))
|
|
.with_service_name(conduwuit_core::BRANDING)
|
|
.with_attributes([
|
|
KeyValue::new(attribute::SERVICE_VERSION, conduwuit_core::version()),
|
|
KeyValue::new(attribute::SERVICE_INSTANCE_ID, config.server_name.to_string()),
|
|
])
|
|
.with_detector(Box::new(EnvResourceDetector::new()));
|
|
if let Some(name) = std::env::var("OTEL_SERVICE_NAME")
|
|
.ok()
|
|
.filter(|name| !name.is_empty())
|
|
{
|
|
resource = resource.with_service_name(name);
|
|
}
|
|
resource.build()
|
|
}
|