add configurable line width

This commit is contained in:
Chris Beck
2025-12-04 18:44:21 -07:00
parent 5cca5d226c
commit 7cb0da2337
2 changed files with 14 additions and 4 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
#![deny(missing_docs)] #![deny(missing_docs)]
//! Minimal API for getting time series data from prometheus //! Minimal API for getting time series data from prometheus, and making small plots.
//! //!
//! To use it, instantiate one of the request objects, //! To use it, instantiate one of the request objects,
//! e.g. QueryRequest or QueryRangeRequest. When it's helpful a builder is provided. //! e.g. QueryRequest or QueryRangeRequest. When it's helpful a builder is provided.
@@ -16,7 +16,7 @@
//! `serde::Deserialize` in the `PromData` that results from the call. //! `serde::Deserialize` in the `PromData` that results from the call.
//! //!
//! When `plot` feature is active, the plot module can be used to plot the timeseries data. //! When `plot` feature is active, the plot module can be used to plot the timeseries data.
//! This is mostly intended to be used as previews in alert messages. //! This was designed to be used to generate previews in alert messages.
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Serialize, de::DeserializeOwned}; use serde::{Serialize, de::DeserializeOwned};
+12 -2
View File
@@ -39,6 +39,8 @@ pub struct PlotStyle {
pub utc_offset_hours: i32, pub utc_offset_hours: i32,
/// Optional title override - used when prometheus aggregations remove __name__ /// Optional title override - used when prometheus aggregations remove __name__
pub title: Option<String>, pub title: Option<String>,
/// The stroke width for data lines (default: 1)
pub line_width: u32,
} }
impl Default for PlotStyle { impl Default for PlotStyle {
@@ -69,6 +71,7 @@ impl Default for PlotStyle {
skip_labels: vec!["job".into(), "instance".into()], skip_labels: vec!["job".into(), "instance".into()],
utc_offset_hours: 0, utc_offset_hours: 0,
title: None, title: None,
line_width: 1,
} }
} }
} }
@@ -91,6 +94,12 @@ impl PlotStyle {
self.utc_offset_hours = offset; self.utc_offset_hours = offset;
self self
} }
/// Set the stroke width for data lines
pub fn with_line_width(mut self, width: u32) -> Self {
self.line_width = width;
self
}
} }
/// A shaded region appearing on the plot to indicate values that would trigger an alert /// A shaded region appearing on the plot to indicate values that would trigger an alert
@@ -214,15 +223,16 @@ impl PlotStyle {
for (idx, (mut metric, vals)) in specific_labels.into_iter().zip(ts.into_iter()).enumerate() for (idx, (mut metric, vals)) in specific_labels.into_iter().zip(ts.into_iter()).enumerate()
{ {
let color = &self.data_colors[idx % self.data_colors.len()]; let color = &self.data_colors[idx % self.data_colors.len()];
let style = ShapeStyle::from(color).stroke_width(self.line_width);
let name = metric.remove("__name__").unwrap_or_default(); let name = metric.remove("__name__").unwrap_or_default();
let label = format!("{name} {metric:?}"); let label = format!("{name} {metric:?}");
let series = ctx.draw_series(LineSeries::new(vals, color))?; let series = ctx.draw_series(LineSeries::new(vals, style))?;
if show_legend { if show_legend {
series series
.label(label) .label(label)
.legend(move |(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], color)); .legend(move |(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], style));
} }
} }