# Rust

### Installation

Add the following dependency to your `Cargo.toml` file:

```toml
[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:

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

**Initialize Client**

Initialize the `Telemetry` client with your API key:

```rust
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`.

```rust
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:

```rust
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:

```rust
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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.telemetry.sh/sdks/rust.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
