Greptimex
Elixir client for GreptimeDB using gRPC.
Installation
def deps do
[
{:greptimex, "~> 0.2"}
]
endUsage
Define a connection module:
defmodule MyApp.Greptime do
use Greptimex.Connection,
pool: [
pool_size: 5,
channel: [address: "localhost:4001", opts: []]
],
header: [
catalog: "greptime",
dbname: "public"
]
endOr with config:
# config/config.exs
config :my_app, MyApp.Greptime,
pool: [
pool_size: 5,
channel: [address: "localhost:4001", opts: []]
],
header: [
catalog: "greptime",
dbname: "public"
]
# lib/my_app/greptime.ex
defmodule MyApp.Greptime do
use Greptimex.Connection, otp_app: :my_app
endInsert Data
Single row:
MyApp.Greptime.insert({"metrics", %{
tags: %{host: "server1", region: "us-west"},
fields: %{cpu: 0.8, memory: 1024},
timestamp: ~U[2025-01-01 00:00:00Z]
}})
# {:ok, 1}Multiple rows:
MyApp.Greptime.insert({"metrics", [
%{tags: %{host: "server1"}, fields: %{cpu: 0.8}, timestamp: ~U[2025-01-01 00:00:00Z]},
%{tags: %{host: "server2"}, fields: %{cpu: 0.5}, timestamp: ~U[2025-01-01 00:00:01Z]}
]})
# {:ok, 2}Multiple tables:
MyApp.Greptime.insert([
{"metrics", [...]},
{"logs", [...]}
])PromQL Queries
Instant query:
MyApp.Greptime.query_instant("up{job='api'}")
# {:ok, [%{metric: %{"__name__" => "up", "job" => "api"}, value: {~U[...], 1.0}}]}Range query:
MyApp.Greptime.query_range(
"cpu_usage",
~U[2025-01-01 00:00:00Z],
~U[2025-01-01 01:00:00Z],
"5m"
)
# {:ok, [%{metric: %{...}, values: [{~U[...], 0.8}, ...]}]}Telemetry
Events emitted:
[:greptimex, :insert, :start | :success | :failure][:greptimex, :query_instant, :start | :success | :failure][:greptimex, :query_range, :start | :success | :failure]
Payload Structure
| Event | Measurements | Metadata |
|---|---|---|
:insert :start | system_time | row_count, dbname |
:insert :success | duration, affected_rows | row_count, dbname |
:insert :failure | duration | row_count, dbname, kind, reason |
:query_instant :start | system_time | query, time, dbname |
:query_instant :success | duration, result_count | query, time, dbname |
:query_instant :failure | duration | query, time, dbname, kind, reason |
:query_range :start | system_time | query, start_time, end_time, step, dbname |
:query_range :success | duration, result_count | query, start_time, end_time, step, dbname |
:query_range :failure | duration | query, start_time, end_time, step, dbname, kind, reason |