Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions examples/lading-openmetrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
blackhole:
- http:
binding_addr: "127.0.0.1:9100"
concurrent_requests_max: 1000
response_delay_millis: 100
headers:
content-type: "text/plain; version=0.0.4"
body_variant:
openmetrics:
metric_name_prefix: "om_scale"
include_help: true
include_type: true
counters:
count: 240
gauges:
count: 180
histograms:
count: 20
buckets: ["0.005", "0.01", "0.025", "0.05", "0.1", "0.25", "0.5", "1", "2.5", "5"]
summaries:
count: 40
quantiles: ["0.5", "0.75", "0.9", "0.95", "0.99"]
labels:
services: ["checkout", "catalog", "payments", "fulfillment", "search"]
regions: ["us-east-1", "us-west-2", "eu-central-1", "ap-southeast-1"]
methods: ["GET", "POST", "PUT", "DELETE"]
status_classes: ["2xx", "3xx", "4xx", "5xx"]
consumers: ["consumer-00", "consumer-01", "consumer-02", "consumer-03"]
route_count: 60
41 changes: 41 additions & 0 deletions lading/src/blackhole/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use bytes::Bytes;
use http::{HeaderMap, header::InvalidHeaderValue, status::InvalidStatusCode};
use http_body_util::{BodyExt, combinators::BoxBody};
use hyper::{Request, Response, StatusCode, header};
use lading_payload::openmetrics;
use metrics::counter;
use serde::{Deserialize, Serialize};
use std::{net::SocketAddr, time::Duration};
Expand Down Expand Up @@ -39,6 +40,9 @@ pub enum Error {
/// Failed to deserialize the configuration.
#[error("Failed to deserialize the configuration: {0}")]
Serde(#[from] serde_json::Error),
/// `OpenMetrics` payload generation failed.
#[error("OpenMetrics payload error: {0}")]
OpenMetrics(#[from] lading_payload::openmetrics::Error),
/// Wrapper for [`crate::blackhole::common::Error`].
#[error(transparent)]
Common(#[from] crate::blackhole::common::Error),
Expand Down Expand Up @@ -75,6 +79,9 @@ pub enum BodyVariant {
RawBytes,
/// Respond with a hardcoded string value
Static(String),
/// Respond with a generated `OpenMetrics` text exposition body.
#[serde(rename = "openmetrics")]
OpenMetrics(Box<openmetrics::Config>),
}

fn default_body_variant() -> BodyVariant {
Expand Down Expand Up @@ -111,6 +118,7 @@ pub struct Config {
pub binding_addr: SocketAddr,
/// the body variant to respond with, default nothing
#[serde(default = "default_body_variant")]
#[serde(with = "serde_yaml::with::singleton_map_recursive")]
pub body_variant: BodyVariant,
/// Headers to include in the response; default is `Content-Type: application/json`
#[serde(with = "http_serde::header_map", default = "default_headers")]
Expand Down Expand Up @@ -233,6 +241,7 @@ impl Http {
BodyVariant::Nothing => vec![],
BodyVariant::RawBytes => config.raw_bytes.clone(),
BodyVariant::Static(val) => val.as_bytes().to_vec(),
BodyVariant::OpenMetrics(conf) => openmetrics::OpenMetrics::new(conf)?.into_bytes(),
};

Ok(Self {
Expand Down Expand Up @@ -339,4 +348,36 @@ raw_bytes: [0x01, 0x02, 0x10]
},
);
}

#[test]
fn config_deserializes_openmetrics() {
let contents = r#"
binding_addr: "127.0.0.1:1000"
body_variant:
openmetrics:
metric_name_prefix: "om_test"
counters:
count: 2
gauges:
count: 3
histograms:
count: 1
buckets: ["0.5", "1"]
summaries:
count: 1
quantiles: ["0.5", "0.99"]
"#;
let config: Config =
serde_yaml::from_str(contents).expect("Contents do not match the structure expected");
match config.body_variant {
BodyVariant::OpenMetrics(openmetrics) => {
assert_eq!(openmetrics.metric_name_prefix, "om_test");
assert_eq!(openmetrics.counters.count, 2);
assert_eq!(openmetrics.gauges.count, 3);
assert_eq!(openmetrics.histograms.count, 1);
assert_eq!(openmetrics.summaries.count, 1);
}
other => panic!("expected openmetrics body variant, got {other:?}"),
}
}
}
2 changes: 2 additions & 0 deletions lading_payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub use datadog_logs::DatadogLog;
pub use dogstatsd::DogStatsD;
pub use fluent::Fluent;
pub use json::Json;
pub use openmetrics::OpenMetrics;
pub use opentelemetry::log::OpentelemetryLogs;
pub use opentelemetry::metric::OpentelemetryMetrics;
pub use opentelemetry::trace::OpentelemetryTraces;
Expand All @@ -39,6 +40,7 @@ pub mod datadog_logs;
pub mod dogstatsd;
pub mod fluent;
pub mod json;
pub mod openmetrics;
pub mod opentelemetry;
pub mod procfs;
pub mod splunk_hec;
Expand Down
Loading
Loading