Struct TelemetryClient

Source
pub struct TelemetryClient { /* private fields */ }
Expand description

Application Insights telemetry client provides an interface to track telemetry items.

Implementations§

Source§

impl TelemetryClient

Source

pub fn new(i_key: String) -> Self

Creates a new telemetry client that submits telemetry with specified instrumentation key.

Source

pub fn from_config(config: TelemetryConfig) -> Self

Creates a new telemetry client configured with specified configuration.

Source

pub fn is_enabled(&self) -> bool

Determines whether this client is enabled and will accept telemetry.

§Examples
let client = TelemetryClient::new("<instrumentation key>".to_string());
assert!(client.is_enabled());
Source

pub fn enabled(&mut self, enabled: bool)

Enables or disables telemetry client. When disabled, telemetry is silently swallowed by the client. Defaults to enabled.

§Examples
let mut client = TelemetryClient::new("<instrumentation key>".to_string());
assert!(client.is_enabled());

client.enabled(false);
assert_eq!(client.is_enabled(), false);
Source

pub fn context(&self) -> &TelemetryContext

Returns an immutable reference to a collection of tag data to attach to the telemetry item.

§Examples
let mut client = TelemetryClient::new("<instrumentation key>".to_string());
client.context_mut().tags_mut().cloud_mut().set_role("rust_server".to_string());

assert_eq!(client.context().tags().cloud().role(), Some("rust_server"));
Source

pub fn context_mut(&mut self) -> &mut TelemetryContext

Returns a mutable reference to a collection of tag data to attach to the telemetry item.

§Examples
let mut client = TelemetryClient::new("<instrumentation key>".to_string());
client.context_mut().tags_mut().insert("app_version".into(), "v0.1.1".to_string());
client.context_mut().properties_mut().insert("Resource Group".into(), "my-rg".to_string());

assert_eq!(client.context().tags().get("app_version"), Some(&"v0.1.1".to_string()));
assert_eq!(client.context().properties().get("Resource Group"), Some(&"my-rg".to_string()));
Source

pub fn track_event(&self, name: impl Into<String>)

Logs a user action with the specified name.

§Examples
client.track_event("app is running");
Source

pub fn track_trace(&self, message: impl Into<String>, severity: SeverityLevel)

Logs a trace message with a specified severity level.

§Examples
client.track_trace("Unable to connect to a gateway", SeverityLevel::Warning);
Source

pub fn track_metric(&self, name: impl Into<String>, value: f64)

Logs a numeric value that is not specified with a specific event. Typically used to send regular reports of performance indicators.

§Examples
client.track_metric("gateway_latency_ms", 113.0);
Source

pub fn track_request( &self, method: Method, uri: Uri, duration: Duration, response_code: impl Into<String>, )

Logs a HTTP request with the specified method, URL, duration and response code.

§Examples
use http::{Method, Uri};
use std::time::Duration;

let uri: Uri = "https://api.github.com/dmolokanov/appinsights-rs".parse().unwrap();
client.track_request(Method::GET, uri, Duration::from_millis(100), "200");
Source

pub fn track_remote_dependency( &self, name: impl Into<String>, dependency_type: impl Into<String>, target: impl Into<String>, success: bool, )

Logs a dependency with the specified name, type, target, and success status.

§Examples
client.track_remote_dependency(
    "GET https://api.github.com/dmolokanov/appinsights-rs",
    "HTTP",
    "api.github.com",
    true
);
Source

pub fn track_availability( &self, name: impl Into<String>, duration: Duration, success: bool, )

Logs an availability test result with the specified test name, duration, and success status.

§Examples
use std::time::Duration;

client.track_availability(
    "GET https://api.github.com/dmolokanov/appinsights-rs",
    Duration::from_millis(100),
    true
);
Source

pub fn track<E>(&self, event: E)
where E: Telemetry, (TelemetryContext, E): Into<Envelope>,

Submits a specific telemetry event.

§Examples
use appinsights::telemetry::AggregateMetricTelemetry;

let mut telemetry = AggregateMetricTelemetry::new("device_message_latency_per_min");
telemetry.stats_mut().add_data(&[113.0, 250.0, 316.0]);

client.track(telemetry);
Source

pub fn flush_channel(&self)

Forces all pending telemetry items to be submitted. The current task will not be blocked.

§Examples
let mut counter = 0;

// send heartbeats while application is running
let running = true;
while running {
    client.track_event("app is running");
    counter += 1;

    // if the rate is bigger than submission interval you can make sure that data is
    // triggered for submission (each 100 items)
    if counter == 100 {
        // trigger submission of all pending items
        client.flush_channel();
        counter = 0;
    }
}
Source

pub async fn close_channel(self)

Flushes and tears down the submission flow and closes internal channels. It blocks the current task until all pending telemetry items have been submitted and it is safe to shutdown without losing telemetry. This method consumes the value of client so it makes impossible to use a client with close channel.

§Examples
// send heartbeats while application is running
let running = true;
while running {
    client.track_event("app is running");
}

// wait until pending telemetry is sent at most once and tear down submission flow
client.close_channel().await;

// unable to sent any telemetry after client closes its channel
// client.track_event("app is stopped".to_string());
Source

pub async fn terminate(self)

Tears down the submission flow and closes internal channels. Any telemetry waiting to be sent is discarded. This is a more abrupt version of close_channel. This method consumes the value of client so it makes impossible to use a client with close channel.

This method should be used in cases when the client should be stopped. It is a separate function until async_drop is implemented in rust.

§Examples
// send heartbeats while application is running
let running = true;
while running {
    client.track_event("app is running");
}

// wait until pending telemetry is sent at most once and tear down submission flow
client.terminate().await;

// unable to sent any telemetry after client closes its channel
// client.track_event("app is stopped".to_string());

Trait Implementations§

Source§

impl From<(TelemetryConfig, TelemetryContext)> for TelemetryClient

Source§

fn from((config, context): (TelemetryConfig, TelemetryContext)) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> Instrument for T

Source§

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

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

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 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> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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

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

Source§

impl<T> MaybeSendSync for T