Skip to content
58 changes: 58 additions & 0 deletions quickwit/quickwit-metrics/examples/http_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,47 @@ static HTTP_ACTIVE_CONNECTIONS_BY_REGION: LazyGauge = lazy_gauge!(
"region" => "us-east-1",
);

// ─── Custom system prefix ───
//
// Override the default system prefix ("quickwit") with a custom value.
// This produces metric names like "myapp_db_queries_total" instead of
// "quickwit_db_queries_total".

static DB_QUERIES_TOTAL: LazyCounter = lazy_counter!(
name: "queries_total",
description: "Total number of database queries",
system: "myapp",
subsystem: "db",
);

static DB_QUERY_DURATION: LazyHistogram = lazy_histogram!(
name: "query_duration_seconds",
description: "Time spent executing database queries",
system: "myapp",
subsystem: "db",
buckets: vec![0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1.0],
);

static DB_CONNECTIONS: LazyGauge = lazy_gauge!(
name: "connections",
description: "Number of active database connections",
system: "myapp",
subsystem: "db",
);

// ─── Custom separator ───
//
// Override the default "_" separator with ".".
// This produces metric names like "myapp.http.requests_total".

static HTTP_REQUESTS_DOTTED: LazyCounter = lazy_counter!(
name: "requests_total",
description: "Total HTTP requests with dotted metric name",
system: "myapp",
subsystem: "http",
separator: ".",
);

// ─── LabelNames<N> examples ───

const ROUTE_LABEL_NAMES: LabelNames<2> = label_names!("method", "path");
Expand Down Expand Up @@ -183,6 +224,23 @@ fn main() {
println!(" set quickwit_http_requests_total absolute = 1,000,000");
println!();

println!("=== Custom system prefix ===");
DB_QUERIES_TOTAL.inc();
println!(" myapp_db_queries_total = {}", DB_QUERIES_TOTAL.get());
DB_QUERY_DURATION.observe(0.042);
println!(" myapp_db_query_duration_seconds observed 0.042");
DB_CONNECTIONS.set(5.0);
println!(" myapp_db_connections = {}", DB_CONNECTIONS.get());
println!();

println!("=== Custom separator ===");
HTTP_REQUESTS_DOTTED.inc_by(3);
println!(
" myapp.http.requests_total = {}",
HTTP_REQUESTS_DOTTED.get()
);
println!();

println!("Prometheus scrape endpoint: http://127.0.0.1:9000/metrics");
println!("Press Ctrl+C to stop.");
std::thread::park();
Expand Down
62 changes: 59 additions & 3 deletions quickwit/quickwit-metrics/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,22 @@ impl CounterFn for Counter {
/// ```
#[macro_export]
macro_rules! counter {
// Base declaration: all-static name, labels, and key — zero allocations.
// Base declaration with explicit separator, system, and subsystem prefix - zero allocations.
(
name: $name:literal,
description: $description:literal,
subsystem: $subsystem:tt
system: $system:expr,
subsystem: $subsystem:expr,
separator: $separator:expr
$(, $label:literal => $value:literal)* $(,)?
) => {{
$crate::__key_info_metadata!(
kind: $crate::MetricKind::Counter,
name: $name,
description: $description,
subsystem: $subsystem
system: $system,
subsystem: $subsystem,
separator: $separator
$(, $label => $value)*
);
$crate::__metric_declaration!(
Expand All @@ -282,6 +286,58 @@ macro_rules! counter {
)
}};

// Base declaration with explicit system and subsystem prefix - zero allocations.
(
name: $name:literal,
description: $description:literal,
system: $system:expr,
subsystem: $subsystem:expr
$(, $label:literal => $value:literal)* $(,)?
) => {{
$crate::counter!(
name: $name,
description: $description,
system: $system,
subsystem: $subsystem,
separator: $crate::SEPARATOR
$(, $label => $value)*
)
}};

// Base declaration with subsystem only — system defaults to SYSTEM.
(
name: $name:literal,
description: $description:literal,
subsystem: $subsystem:expr,
separator: $separator:expr
$(, $label:literal => $value:literal)* $(,)?
) => {{
$crate::counter!(
name: $name,
description: $description,
system: $crate::SYSTEM,
subsystem: $subsystem,
separator: $separator
$(, $label => $value)*
)
}};

// Base declaration with subsystem only — system defaults to SYSTEM.
(
name: $name:literal,
description: $description:literal,
subsystem: $subsystem:expr
$(, $label:literal => $value:literal)* $(,)?
Comment thread
Mallets marked this conversation as resolved.
) => {{
$crate::counter!(
name: $name,
description: $description,
system: $crate::SYSTEM,
subsystem: $subsystem
$(, $label => $value)*
)
}};

// Parent extension with inline dynamic labels.
// Derives a child counter by inheriting the parent's name and labels,
// appending new (possibly dynamic) key => value pairs.
Expand Down
62 changes: 59 additions & 3 deletions quickwit/quickwit-metrics/src/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,18 +331,22 @@ impl Drop for GaugeGuard {
/// ```
#[macro_export]
macro_rules! gauge {
// Base declaration: all-static name, labels, and key — zero allocations.
// Base declaration with explicit separator, system, and subsystem prefix - zero allocations.
(
name: $name:literal,
description: $description:literal,
subsystem: $subsystem:tt
system: $system:expr,
subsystem: $subsystem:expr,
separator: $separator:expr
$(, $label:literal => $value:literal)* $(,)?
) => {{
$crate::__key_info_metadata!(
kind: $crate::MetricKind::Gauge,
name: $name,
description: $description,
subsystem: $subsystem
system: $system,
subsystem: $subsystem,
separator: $separator
$(, $label => $value)*
);
$crate::__metric_declaration!(
Expand All @@ -353,6 +357,58 @@ macro_rules! gauge {
)
}};

// Base declaration with explicit system and subsystem prefix - zero allocations.
(
name: $name:literal,
description: $description:literal,
system: $system:expr,
subsystem: $subsystem:expr
$(, $label:literal => $value:literal)* $(,)?
) => {{
$crate::gauge!(
name: $name,
description: $description,
system: $system,
subsystem: $subsystem,
separator: $crate::SEPARATOR
$(, $label => $value)*
)
}};

// Base declaration with subsystem only — system defaults to SYSTEM.
(
name: $name:literal,
description: $description:literal,
subsystem: $subsystem:expr,
separator: $separator:expr
$(, $label:literal => $value:literal)* $(,)?
) => {{
$crate::gauge!(
name: $name,
description: $description,
system: $crate::SYSTEM,
subsystem: $subsystem,
separator: $separator
$(, $label => $value)*
)
}};

// Base declaration with subsystem only — system defaults to SYSTEM.
(
name: $name:literal,
description: $description:literal,
subsystem: $subsystem:expr
$(, $label:literal => $value:literal)* $(,)?
) => {{
$crate::gauge!(
name: $name,
description: $description,
system: $crate::SYSTEM,
subsystem: $subsystem
$(, $label => $value)*
)
}};

// Parent extension with inline dynamic labels.
// Derives a child gauge by inheriting the parent's name and labels,
// appending new (possibly dynamic) key => value pairs.
Expand Down
72 changes: 65 additions & 7 deletions quickwit/quickwit-metrics/src/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,30 +261,30 @@ impl Drop for HistogramTimer {
/// ```
#[macro_export]
macro_rules! histogram {
// Base declaration: all-static name, labels, and key — zero allocations.
// Base declaration with explicit separator, system, and subsystem prefix - zero allocations.
(
name: $name:literal,
description: $description:literal,
subsystem: $subsystem:tt,
system: $system:expr,
subsystem: $subsystem:expr,
separator: $separator:expr,
buckets: $buckets:expr
$(, $label:literal => $value:literal)* $(,)?
) => {{
// Expand compile-time statics: KEY_NAME, INFO, KEY, LABELS, METADATA.
$crate::__key_info_metadata!(
kind: $crate::MetricKind::Histogram,
name: $name,
description: $description,
subsystem: $subsystem
system: $system,
subsystem: $subsystem,
separator: $separator
$(, $label => $value)*
);
// Link histogram bucket configuration to the metric info and register it
// with the inventory so the recorder can configure bucket boundaries.
static HISTOGRAM_CONFIG: $crate::HistogramConfig = $crate::HistogramConfig {
info: &INFO,
buckets_fn: || $buckets,
};
$crate::__inventory::submit!(HISTOGRAM_CONFIG);
// Thread-local cache + global DashMap registration.
$crate::__metric_declaration!(
metric_type: $crate::Histogram,
register_fn: $crate::__histogram_get_or_register,
Expand All @@ -293,6 +293,64 @@ macro_rules! histogram {
)
}};

// Base declaration with explicit system and subsystem prefix - zero allocations.
(
name: $name:literal,
description: $description:literal,
system: $system:expr,
subsystem: $subsystem:expr,
buckets: $buckets:expr
$(, $label:literal => $value:literal)* $(,)?
) => {{
$crate::histogram!(
name: $name,
description: $description,
system: $system,
subsystem: $subsystem,
separator: $crate::SEPARATOR,
buckets: $buckets
$(, $label => $value)*
)
}};

// Base declaration with subsystem only — system defaults to SYSTEM.
(
name: $name:literal,
description: $description:literal,
subsystem: $subsystem:expr,
separator: $separator:expr,
buckets: $buckets:expr
$(, $label:literal => $value:literal)* $(,)?
) => {{
$crate::histogram!(
name: $name,
description: $description,
system: $crate::SYSTEM,
subsystem: $subsystem,
separator: $separator,
buckets: $buckets
$(, $label => $value)*
)
}};

// Base declaration with subsystem only — system defaults to SYSTEM.
(
name: $name:literal,
description: $description:literal,
subsystem: $subsystem:expr,
buckets: $buckets:expr
$(, $label:literal => $value:literal)* $(,)?
) => {{
$crate::histogram!(
name: $name,
description: $description,
system: $crate::SYSTEM,
subsystem: $subsystem,
buckets: $buckets
$(, $label => $value)*
)
}};

// Parent extension with inline dynamic labels.
// Derives a child histogram by inheriting the parent's name and labels,
// appending new (possibly dynamic) key => value pairs.
Expand Down
Loading
Loading