pub struct TelemetryClient { /* private fields */ }
Expand description
Application Insights telemetry client provides an interface to track telemetry items.
Implementations§
Source§impl TelemetryClient
impl TelemetryClient
Sourcepub fn new(i_key: String) -> Self
pub fn new(i_key: String) -> Self
Creates a new telemetry client that submits telemetry with specified instrumentation key.
Sourcepub fn from_config(config: TelemetryConfig) -> Self
pub fn from_config(config: TelemetryConfig) -> Self
Creates a new telemetry client configured with specified configuration.
Sourcepub fn is_enabled(&self) -> bool
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());
Sourcepub fn enabled(&mut self, enabled: bool)
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);
Sourcepub fn context(&self) -> &TelemetryContext
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"));
Sourcepub fn context_mut(&mut self) -> &mut TelemetryContext
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()));
Sourcepub fn track_event(&self, name: impl Into<String>)
pub fn track_event(&self, name: impl Into<String>)
Sourcepub fn track_trace(&self, message: impl Into<String>, severity: SeverityLevel)
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);
Sourcepub fn track_metric(&self, name: impl Into<String>, value: f64)
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);
Sourcepub fn track_request(
&self,
method: Method,
uri: Uri,
duration: Duration,
response_code: impl Into<String>,
)
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");
Sourcepub fn track_remote_dependency(
&self,
name: impl Into<String>,
dependency_type: impl Into<String>,
target: impl Into<String>,
success: bool,
)
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
);
Sourcepub fn track_availability(
&self,
name: impl Into<String>,
duration: Duration,
success: bool,
)
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
);
Sourcepub fn track<E>(&self, event: E)
pub fn track<E>(&self, event: E)
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);
Sourcepub fn flush_channel(&self)
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;
}
}
Sourcepub async fn close_channel(self)
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());
Sourcepub async fn terminate(self)
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());