Skip to content

Commit 71e98d4

Browse files
committed
refactor(metrics): split server metrics crate
1 parent 6e295e4 commit 71e98d4

30 files changed

Lines changed: 931 additions & 790 deletions

File tree

Cargo.lock

Lines changed: 13 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ members = [
2929
"engine/packages/guard-core",
3030
"engine/packages/logs",
3131
"engine/packages/metrics",
32+
"engine/packages/metrics-server",
3233
"engine/packages/namespace",
3334
"engine/packages/pegboard",
3435
"engine/packages/pegboard-envoy",
@@ -452,6 +453,9 @@ members = [
452453
[workspace.dependencies.rivet-metrics]
453454
path = "engine/packages/metrics"
454455

456+
[workspace.dependencies.rivet-metrics-server]
457+
path = "engine/packages/metrics-server"
458+
455459
[workspace.dependencies.namespace]
456460
path = "engine/packages/namespace"
457461

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "rivet-metrics-server"
3+
version.workspace = true
4+
authors.workspace = true
5+
license.workspace = true
6+
edition.workspace = true
7+
8+
[dependencies]
9+
anyhow.workspace = true
10+
console-subscriber.workspace = true
11+
hyper.workspace = true
12+
opentelemetry.workspace = true
13+
opentelemetry-otlp.workspace = true
14+
opentelemetry-semantic-conventions.workspace = true
15+
opentelemetry_sdk.workspace = true
16+
rivet-config.workspace = true
17+
rivet-env.workspace = true
18+
rivet-metrics.workspace = true
19+
tokio.workspace = true
20+
tracing.workspace = true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod providers;
2+
mod server;
3+
4+
pub use providers::{OtelProviderGuard, init_otel_providers, set_sampler_ratio};
5+
pub use server::run_standalone;

engine/packages/metrics/src/providers.rs renamed to engine/packages/metrics-server/src/providers.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Based off of https://github.com/tokio-rs/tracing-opentelemetry/blob/v0.1.x/examples/opentelemetry-otlp.rs
2-
// Based off of https://github.com/tokio-rs/tracing-opentelemetry/blob/v0.1.x/examples/opentelemetry-otlp.rs
3-
41
use opentelemetry::KeyValue;
52
use opentelemetry::trace::{SamplingResult, SpanKind};
63
use opentelemetry_otlp::WithExportConfig;
@@ -11,7 +8,7 @@ use opentelemetry_sdk::{
118
use opentelemetry_semantic_conventions::{SCHEMA_URL, attribute::SERVICE_VERSION};
129
use std::sync::{Arc, OnceLock, RwLock};
1310

14-
/// Dynamic sampler that can be updated at runtime
11+
/// Dynamic sampler that can be updated at runtime.
1512
#[derive(Clone, Debug)]
1613
struct DynamicSampler {
1714
ratio: Arc<RwLock<f64>>,
@@ -43,7 +40,6 @@ impl opentelemetry_sdk::trace::ShouldSample for DynamicSampler {
4340
) -> SamplingResult {
4441
let ratio = self.ratio.read().ok().map(|r| *r).unwrap_or(0.001);
4542

46-
// Use TraceIdRatioBased sampling logic
4743
let sampler = Sampler::TraceIdRatioBased(ratio);
4844
sampler.should_sample(
4945
parent_context,
@@ -58,7 +54,7 @@ impl opentelemetry_sdk::trace::ShouldSample for DynamicSampler {
5854

5955
static SAMPLER: OnceLock<DynamicSampler> = OnceLock::new();
6056

61-
/// Update the sampler ratio at runtime
57+
/// Update the sampler ratio at runtime.
6258
pub fn set_sampler_ratio(ratio: f64) -> anyhow::Result<()> {
6359
let sampler = SAMPLER
6460
.get()
@@ -94,21 +90,17 @@ fn init_tracer_provider() -> SdkTracerProvider {
9490
.build()
9591
.unwrap();
9692

97-
// Create dynamic sampler with initial ratio from env
9893
let initial_ratio = std::env::var("RIVET_OTEL_SAMPLER_RATIO")
9994
.ok()
10095
.and_then(|s| s.parse::<f64>().ok())
10196
.unwrap_or(0.001);
10297

10398
let dynamic_sampler = DynamicSampler::new(initial_ratio);
10499

105-
// Store sampler globally for later updates
106100
let _ = SAMPLER.set(dynamic_sampler.clone());
107101

108102
SdkTracerProvider::builder()
109-
// Customize sampling strategy with parent-based sampling using our dynamic sampler
110103
.with_sampler(Sampler::ParentBased(Box::new(dynamic_sampler)))
111-
// If export trace to AWS X-Ray, you can use XrayIdGenerator
112104
.with_id_generator(RandomIdGenerator::default())
113105
.with_resource(resource())
114106
.with_batch_exporter(exporter)
@@ -117,7 +109,6 @@ fn init_tracer_provider() -> SdkTracerProvider {
117109

118110
/// Initialize OtelProviderGuard for opentelemetry-related termination processing.
119111
pub fn init_otel_providers() -> Option<OtelProviderGuard> {
120-
// Check if otel is enabled
121112
let enable_otel = std::env::var("RIVET_OTEL_ENABLED").map_or(false, |x| x == "1");
122113

123114
if enable_otel {
@@ -137,7 +128,7 @@ pub struct OtelProviderGuard {
137128
impl Drop for OtelProviderGuard {
138129
fn drop(&mut self) {
139130
if let Err(err) = self.tracer_provider.shutdown() {
140-
eprintln!("{err:?}");
131+
tracing::error!(?err, "failed to shut down otel tracer provider");
141132
}
142133
}
143134
}

engine/packages/metrics/src/server.rs renamed to engine/packages/metrics-server/src/server.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use hyper::{
66
header::CONTENT_TYPE,
77
service::{make_service_fn, service_fn},
88
};
9-
use prometheus::{Encoder, TextEncoder};
10-
11-
// TODO: Record extra labels
9+
use rivet_metrics::prometheus::{Encoder, TextEncoder};
1210

1311
#[tracing::instrument(skip_all)]
1412
pub async fn run_standalone(config: rivet_config::Config) -> Result<()> {
@@ -21,8 +19,7 @@ pub async fn run_standalone(config: rivet_config::Config) -> Result<()> {
2119
Err(err) => {
2220
tracing::error!(?host, ?port, ?err, "failed to bind metrics server");
2321

24-
// TODO: Find cleaner way of crashing entire program
25-
// Hard crash program since a server failing to bind is critical
22+
// Hard crash the program since a server failing to bind is critical.
2623
std::process::exit(1);
2724
}
2825
};
@@ -41,7 +38,7 @@ pub async fn run_standalone(config: rivet_config::Config) -> Result<()> {
4138
async fn serve_req(_req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
4239
let encoder = TextEncoder::new();
4340

44-
let metric_families = crate::registry::REGISTRY.gather();
41+
let metric_families = rivet_metrics::REGISTRY.gather();
4542
let mut buffer = Vec::new();
4643
encoder
4744
.encode(&metric_families, &mut buffer)

engine/packages/metrics/Cargo.toml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,5 @@ license.workspace = true
66
edition.workspace = true
77

88
[dependencies]
9-
anyhow.workspace = true
10-
console-subscriber.workspace = true
11-
hyper.workspace = true
129
lazy_static.workspace = true
13-
opentelemetry.workspace = true
14-
opentelemetry-otlp.workspace = true
15-
opentelemetry-semantic-conventions.workspace = true
16-
opentelemetry_sdk.workspace = true
1710
prometheus.workspace = true
18-
rivet-config.workspace = true
19-
rivet-env.workspace = true
20-
tokio.workspace = true
21-
tracing.workspace = true

engine/packages/metrics/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
mod providers;
2-
31
mod buckets;
42
mod registry;
5-
mod server;
63

74
pub use buckets::{BUCKETS, MICRO_BUCKETS, TASK_POLL_BUCKETS};
85
pub use prometheus;
9-
pub use providers::{OtelProviderGuard, init_otel_providers, set_sampler_ratio};
106
pub use registry::REGISTRY;
11-
pub use server::run_standalone;

engine/packages/runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ opentelemetry.workspace = true
1717
reqwest.workspace = true
1818
rivet-env.workspace = true
1919
rivet-metrics.workspace = true
20+
rivet-metrics-server.workspace = true
2021
sentry.workspace = true
2122
serde_json.workspace = true
2223
serde.workspace = true

engine/packages/runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{env, future::Future, sync::Arc, time::Duration};
22

3-
use rivet_metrics::init_otel_providers;
3+
use rivet_metrics_server::init_otel_providers;
44
use tokio::sync::{Notify, OnceCell};
55
mod metrics;
66
mod traces;

0 commit comments

Comments
 (0)