Struct segment::Batcher

source ·
pub struct Batcher { /* private fields */ }
Expand description

A batcher can accept messages into an internal buffer, and report when messages must be flushed.

The recommended usage pattern looks something like this:

use segment::{Batcher, Client, HttpClient};
use segment::message::{BatchMessage, Track, User};
use serde_json::json;

let mut batcher = Batcher::new(None);
let client = HttpClient::default();

for i in 0..100 {
    let msg = Track {
        user: User::UserId { user_id: format!("user-{}", i) },
        event: "Example".to_owned(),
        properties: json!({ "foo": "bar" }),
        ..Default::default()
    };

    // Batcher returns back ownership of a message if the internal buffer
    // would overflow.
    //
    // When this occurs, we flush the batcher, create a new batcher, and add
    // the message into the new batcher.
    if let Some(msg) = batcher.push(msg).unwrap() {
        client.send("your_write_key".to_string(), batcher.into_message());
        batcher = Batcher::new(None);
        batcher.push(msg).unwrap();
    }
}

Batcher will attempt to fit messages into maximally-sized batches, thus reducing the number of round trips required with Segment’s tracking API. However, if you produce messages infrequently, this may significantly delay the sending of messages to Segment.

If this delay is a concern, it is recommended that you periodically flush the batcher on your own by calling into_message.

By default if the message you push in the batcher does not contains any timestamp, the timestamp at the time of the push will be automatically added to your message. You can disable this behaviour with the [without_auto_timestamp] method though.

Implementations§

source§

impl Batcher

source

pub fn new(context: Option<Value>) -> Self

Construct a new, empty batcher.

Optionally, you may specify a context that should be set on every batch returned by into_message.

source

pub fn without_auto_timestamp(&mut self)

source

pub fn is_empty(&self) -> bool

source

pub fn push( &mut self, msg: impl Into<BatchMessage> ) -> Result<Option<BatchMessage>>

Push a message into the batcher.

Returns Ok(None) if the message was accepted and is now owned by the batcher.

Returns Ok(Some(msg)) if the message was rejected because the current batch would be oversized if this message were accepted. The given message is returned back, and it is recommended that you flush the current batch before attempting to push msg in again.

Returns an error if the message is too large to be sent to Segment’s API.

source

pub fn into_message(self) -> Message

Consumes this batcher and converts it into a message that can be sent to Segment.

Trait Implementations§

source§

impl Clone for Batcher

source§

fn clone(&self) -> Batcher

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Batcher

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more