pub struct Collector<'a, W: Write> {
charts: HashMap<String, CollectedChartInfo>,
writer: &'a mut W,
}
Expand description
A High-Level interface to run the data collecting BEGIN
-SET
-END
loop
and setup Chart and Dimension info in an efficient manner.
It’s used roughly like this:
Example:
use std::error;
use netdata_plugin::{Chart, Dimension};
use netdata_plugin::collector::Collector;
fn main() -> Result<(), Box<dyn error::Error>> {
// prepare collector
let mut writer = std::io::stdout();
let mut c = Collector::new(&mut writer);
// add charts and their associated dimensions
c.add_chart(&mut Chart { type_id: "mytype.something" /*...*/, ..Default::default()})?;
// writes CHART ...
c.add_dimension( "mytype.something", &Dimension {id: "d1", ..Default::default()})?;
c.add_dimension( "mytype.something", &Dimension {id: "d2", ..Default::default()})?;
// writes DIMENSION ...
// data collecting loop
loop {
c.prepare_value("mytype.something", "d1", 4242)?;
c.prepare_value("mytype.something", "d2", 4242)?;
c.commit_chart("mytype.something").unwrap();
// The BEGIN ... - SET ... - END block will be written to stdout
std::thread::sleep(std::time::Duration::from_secs(1));
break; // stop the demonstration ;)
}
Ok(())
}
Fields§
§charts: HashMap<String, CollectedChartInfo>
§writer: &'a mut W
Implementations§
Source§impl<'a, W: Write> Collector<'a, W>
impl<'a, W: Write> Collector<'a, W>
Sourcepub fn new(writer: &'a mut W) -> Self
pub fn new(writer: &'a mut W) -> Self
Initilaize the data store to manage the list of active Charts.
In most cases you can use a mutable stdout
reference as writer.
Sourcepub fn add_chart(&mut self, chart: &Chart<'_>) -> Result<(), CollectorError>
pub fn add_chart(&mut self, chart: &Chart<'_>) -> Result<(), CollectorError>
Registers a new Chart in this Collector and write
a CHART...
-command to the plugins stdout
to add the Chart properties
on the host side, too.
The type_id
of the Chart entry will be checkt for
formal validity in
advance and may raise ValidationErrors.
Sourcepub fn add_dimension(
&mut self,
chart_id: &str,
dimension: &Dimension<'_>,
) -> Result<(), CollectorError>
pub fn add_dimension( &mut self, chart_id: &str, dimension: &Dimension<'_>, ) -> Result<(), CollectorError>
Registers a new Dimension for a Chart of a given type.id
in this
Collector and write a DIMENSION...
-command to the plugins stdout
to add it
on the host side as well.
The id
field of the Dimension entry will be checkt for
formal validity in
advance and may raise ValidationErrors.
Sourcepub fn prepare_value(
&mut self,
chart_id: &str,
dimension_id: &str,
value: i64,
) -> Result<(), CollectorError>
pub fn prepare_value( &mut self, chart_id: &str, dimension_id: &str, value: i64, ) -> Result<(), CollectorError>
Update the value for one Dimension to be later commited with all other updated values of this Chart
If the reffered Dimension or Chart isn’t already registered in the Collector, it will will raise UnkownChart or UnkownDimension. This may be used to dynamically setup the needed categories on demand.
Sourcepub fn commit_chart(&mut self, chart_id: &str) -> Result<(), CollectorError>
pub fn commit_chart(&mut self, chart_id: &str) -> Result<(), CollectorError>
Send a block containing all updated values to stdout
.
In multi threaded scenarios, this could require additional lock preventions.