Rust

Installation

Add the following dependency to your Cargo.toml file:

[dependencies]
telemetry-sh = "1.0.0"
serde_json = "1.0"
chrono = "0.4"  # For handling timestamps (used in the log data)

Usage

Import Library

Add the necessary imports to your Rust file:

use telemetry_sh::Telemetry;
use serde_json::json;
use chrono::Utc;

Initialize Client

Initialize the Telemetry client with your API key:

fn main() {
    let mut telemetry = Telemetry::new();
    telemetry.init("YOUR_API_KEY".to_string());
}

Log Some Data

Telemetry automatically creates tables when data is logged. In the following example, we log some Uber ride data to a table called uber_rides. Telemetry will automatically create this table and its corresponding schema with columns: city, price, and timestamp.

fn log_data(telemetry: &Telemetry) -> Result<(), Box<dyn std::error::Error>> {
    let data = json!({
        "city": "paris",
        "price": 42,
        "timestamp": Utc::now().to_rfc3339()
    });

    let response = telemetry.log("uber_rides", &data)?;
    println!("Log response: {:?}", response);

    Ok(())
}

Query Some Data

You can query the data using SQL through the query API:

fn query_data(telemetry: &Telemetry) -> Result<(), Box<dyn std::error::Error>> {
    let query = "
        SELECT
            city,
            AVG(price) AS average_price
        FROM
            uber_rides
        GROUP BY
            city
    ";

    let query_response = telemetry.query(query)?;
    println!("Query response: {:?}", query_response);

    Ok(())
}

Example Main Function

Here is an example of how you can integrate logging and querying data in your main function:

fn main() {
    let mut telemetry = Telemetry::new();
    telemetry.init("YOUR_API_KEY".to_string());  // Replace with your actual API key

    if let Err(e) = log_data(&telemetry) {
        eprintln!("Error logging data: {}", e);
    }

    if let Err(e) = query_data(&telemetry) {
        eprintln!("Error querying data: {}", e);
    }
}

This documentation provides an overview of how to set up and use the Rust SDK for Telemetry, with examples of logging and querying data.

Last updated