Macro vector_common::registered_event

source ·
macro_rules! registered_event {
    ($event:ident => $($tail:tt)*) => { ... };
    ($event:ident { $( $field:ident: $type:ty, )* } => $($tail:tt)*) => { ... };
    (
        => $event:ident {
            $( $field:ident: $type:ty = $value:expr, )*
        }

        fn emit(&$slf:ident, $data_name:ident: $data:ident)
            $emit_body:block

        $(fn register($fixed_name:ident: $fixed_tags:ty, $tags_name:ident: $tags:ty)
            $register_body:block)?
    ) => { ... };
}
Expand description

Macro to take care of some of the repetitive boilerplate in implementing a registered event. See the other events in this module for examples of how to use this.

§Usage

registered_event!(
    Event {
        event_field: &'static str,
    } => {
        handle_field: Counter = counter!("name", "tag" => self.event_field),
    }
    fn emit(&self, data: DataType) {
        self.handle_field.increment(data.0);
    }
);

let handle = register!(Event { event_field: "message" });

handle.emit(DataType(123));

In this example, the first set of fields describes the data required to register the event. This is what would be used by the register! macro. For example, register!(Event { event_field: "something" }). The second set of fields describes the data required to store the registered handle, namely the Counters and Gauges that record the handle from metrics as well as any associated data for emitting traces or debug messages, followed by an initialization assignment value. The emit function is the code required to update the metrics and generate any log messages.