from telemetry_sh import Telemetryfrom datetime import datetime, timezone
Initialize Client
API_KEY="YOUR_API_KEY"# Replace with your actual API keytelemetry =Telemetry()telemetry.init(API_KEY)
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.
You can query the data using SQL through the query API.
Async Usage
In codebases that utilize asyncio, using synchronous requests to log data can block all ongoing asyncio tasks until the request completes. To address this, we've introduced TelemetryAsync, which leverages aiohttp internally to provide non-blocking, asynchronous logging.
Installation
Make sure you have the telemetry-sh package installed:
Import Library
Initialize Client
Log Some Data Asynchronously
TelemetryAsync allows you to log data without blocking other asyncio tasks. 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.
Query Some Data Asynchronously
Just like logging, querying data can also be performed asynchronously.
Bulk Ingestion Support
TelemetryAsync also supports bulk ingestion. The log method can accept a list of dictionaries in addition to a single dictionary.
query = """
SELECT
city,
AVG(price) AS average_price
FROM
uber_rides
GROUP BY
city
"""
try:
query_response = telemetry.query(query)
print("Query response:", query_response)
except Exception as e:
print("Error querying data:", e)
pip3 install telemetry-sh
from telemetry_sh import TelemetryAsync
from datetime import datetime, timezone
import asyncio
API_KEY = "YOUR_API_KEY" # Replace with your actual API key
telemetry = TelemetryAsync()
await telemetry.init(API_KEY)
data = {
"city": "paris",
"price": 42,
"timestamp": datetime.now(timezone.utc).isoformat()
}
async def log_data():
try:
response = await telemetry.log("uber_rides", data)
print("Log response:", response)
except Exception as e:
print("Error logging data:", e)
# Run the async logging function
asyncio.run(log_data())
query = """
SELECT
city,
AVG(price) AS average_price
FROM
uber_rides
GROUP BY
city
"""
async def query_data():
try:
query_response = await telemetry.query(query)
print("Query response:", query_response)
except Exception as e:
print("Error querying data:", e)
# Run the async query function
asyncio.run(query_data())