Greptimex

Hex.pmHex Docs

Elixir client for GreptimeDB using gRPC.

Installation

def deps do
  [
    {:greptimex, "~> 0.2"}
  ]
end

Usage

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"
    ]
end

Or 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
end

Insert 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:

Payload Structure

Event Measurements Metadata
:insert :startsystem_timerow_count, dbname
:insert :successduration, affected_rowsrow_count, dbname
:insert :failuredurationrow_count, dbname, kind, reason
:query_instant :startsystem_timequery, time, dbname
:query_instant :successduration, result_countquery, time, dbname
:query_instant :failuredurationquery, time, dbname, kind, reason
:query_range :startsystem_timequery, start_time, end_time, step, dbname
:query_range :successduration, result_countquery, start_time, end_time, step, dbname
:query_range :failuredurationquery, start_time, end_time, step, dbname, kind, reason