Struct Collector

Source
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>

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

§

impl<'a, W> Freeze for Collector<'a, W>

§

impl<'a, W> RefUnwindSafe for Collector<'a, W>
where W: RefUnwindSafe,

§

impl<'a, W> Send for Collector<'a, W>
where W: Send,

§

impl<'a, W> Sync for Collector<'a, W>
where W: Sync,

§

impl<'a, W> Unpin for Collector<'a, W>

§

impl<'a, W> !UnwindSafe for Collector<'a, W>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,