InfluxElixir (InfluxElixir v0.1.14)

Copy Markdown View Source

Elixir client library for InfluxDB v3 with v2 compatibility.

All public API operations go through this facade module. Delegates to the configured client implementation (InfluxElixir.Client.HTTP or InfluxElixir.Client.Local).

Named Connections

All facade functions accept either a keyword config or an atom name. When an atom is passed, it is resolved via InfluxElixir.Connection.fetch!/1 from the persistent-term registry (populated automatically by InfluxElixir.ConnectionSupervisor on startup).

# Using a named connection (registered at startup)
InfluxElixir.health(:trading)
InfluxElixir.write(:trading, "cpu value=1.0", database: "prices")

# Using a raw config (e.g. LocalClient in tests)
InfluxElixir.health(conn)

Configuration

# config/config.exs
config :influx_elixir, :client, InfluxElixir.Client.HTTP

config :influx_elixir, :connections,
  trading: [
    host: "influx-trading:8086",
    token: "...",
    default_database: "prices"
  ]

# config/test.exs
config :influx_elixir, :client, InfluxElixir.Client.Local

Summary

Functions

Adds a new named connection dynamically at runtime.

Returns the configured client implementation module.

Creates a bucket in InfluxDB v2 (backwards compatibility).

Creates a database in InfluxDB v3.

Creates an API token in InfluxDB v3.

Deletes a bucket in InfluxDB v2 (backwards compatibility).

Deletes a database in InfluxDB v3.

Deletes an API token in InfluxDB v3.

Executes a non-SELECT SQL statement (DELETE, INSERT INTO ... SELECT).

Forces an immediate flush of the batch writer for a connection.

Checks the health of an InfluxDB instance.

Lists all buckets in InfluxDB v2 (backwards compatibility).

Lists all databases in InfluxDB v3.

Constructs a new Point struct.

Executes a Flux query against InfluxDB v2 (backwards compatibility).

Executes an InfluxQL query against InfluxDB v3.

Executes a SQL query against InfluxDB v3.

Executes a streaming SQL query, returning a lazy Stream.

Removes a named connection dynamically at runtime.

Resolves a connection reference to a config keyword list.

Returns batch writer statistics for a connection.

Writes points to InfluxDB using the configured client.

Functions

add_connection(name, opts)

@spec add_connection(
  atom(),
  keyword()
) :: {:ok, pid()} | {:error, term()}

Adds a new named connection dynamically at runtime.

client()

@spec client() :: module()

Returns the configured client implementation module.

create_bucket(connection, bucket_name, opts \\ [])

@spec create_bucket(
  InfluxElixir.Client.connection(),
  binary(),
  keyword()
) :: :ok | {:error, term()}

Creates a bucket in InfluxDB v2 (backwards compatibility).

create_database(connection, db_name, opts \\ [])

@spec create_database(
  InfluxElixir.Client.connection(),
  binary(),
  keyword()
) :: :ok | {:error, term()}

Creates a database in InfluxDB v3.

create_token(connection, description, opts \\ [])

@spec create_token(
  InfluxElixir.Client.connection(),
  binary(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Creates an API token in InfluxDB v3.

delete_bucket(connection, bucket_name)

@spec delete_bucket(InfluxElixir.Client.connection(), binary()) ::
  :ok | {:error, term()}

Deletes a bucket in InfluxDB v2 (backwards compatibility).

delete_database(connection, db_name)

@spec delete_database(InfluxElixir.Client.connection(), binary()) ::
  :ok | {:error, term()}

Deletes a database in InfluxDB v3.

delete_token(connection, token_id)

@spec delete_token(InfluxElixir.Client.connection(), binary()) ::
  :ok | {:error, term()}

Deletes an API token in InfluxDB v3.

execute_sql(connection, sql, opts \\ [])

@spec execute_sql(
  InfluxElixir.Client.connection(),
  binary(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Executes a non-SELECT SQL statement (DELETE, INSERT INTO ... SELECT).

flush(connection_name)

@spec flush(atom()) :: :ok | {:error, :no_batch_writer}

Forces an immediate flush of the batch writer for a connection.

Returns :ok on success or {:error, :no_batch_writer} if no batch writer is configured for the given connection.

health(connection)

@spec health(InfluxElixir.Client.connection()) :: {:ok, map()} | {:error, term()}

Checks the health of an InfluxDB instance.

list_buckets(connection)

@spec list_buckets(InfluxElixir.Client.connection()) ::
  {:ok, [map()]} | {:error, term()}

Lists all buckets in InfluxDB v2 (backwards compatibility).

list_databases(connection)

@spec list_databases(InfluxElixir.Client.connection()) ::
  {:ok, [map()]} | {:error, term()}

Lists all databases in InfluxDB v3.

point(measurement, fields, opts \\ [])

Constructs a new Point struct.

Parameters

  • measurement - measurement name
  • fields - field key-value pairs
  • opts - optional :tags and :timestamp

Examples

InfluxElixir.point("cpu", %{"value" => 0.64},
  tags: %{"host" => "server01"}
)

query_flux(connection, flux, opts \\ [])

Executes a Flux query against InfluxDB v2 (backwards compatibility).

query_influxql(connection, influxql, opts \\ [])

Executes an InfluxQL query against InfluxDB v3.

query_sql(connection, sql, opts \\ [])

Executes a SQL query against InfluxDB v3.

Supports transport: :http | :flight option for transport selection.

query_sql_stream(connection, sql, opts \\ [])

@spec query_sql_stream(
  InfluxElixir.Client.connection(),
  binary(),
  keyword()
) :: Enumerable.t()

Executes a streaming SQL query, returning a lazy Stream.

Use for large result sets to avoid loading all rows into memory.

remove_connection(name)

@spec remove_connection(atom()) :: :ok | {:error, term()}

Removes a named connection dynamically at runtime.

resolve_connection(name)

Resolves a connection reference to a config keyword list.

Accepts either an atom name (looked up via Connection.fetch!/1) or a keyword/map config (returned as-is).

Examples

resolve_connection(:trading)
resolve_connection(host: "localhost", token: "t")

stats(connection_name)

@spec stats(atom()) :: {:ok, map()} | {:error, :no_batch_writer}

Returns batch writer statistics for a connection.

Returns {:ok, stats_map} or {:error, :no_batch_writer} if no batch writer is configured for the given connection.

write(connection, line_protocol, opts \\ [])

Writes points to InfluxDB using the configured client.