Snap (Snap v0.2.1) View Source
Snap is split into 3 main components:
Snap.Cluster
- clusters are wrappers around the Elasticsearch HTTP API. We can use this to perform low-level HTTP requests.Snap.Bulk
- a convenience wrapper around bulk operations, usingStream
to stream actions (such asSnap.Bulk.Action.Create
) to be performed against theSnap.Cluster
.Snap.Indexes
- a convenience wrapper around the Elasticsearch indexes APIs, allowing the creation, deleting and aliasing of indexes, along with hotswap functionality to bulk load documents into an aliased index, switching to it atomically.
Additionally, there are other supporting modules:
Snap.Auth
- defines how an HTTP request is modified to include authentication headers.Snap.Auth.Plain
implements HTTP Basic Auth.
Set up
Snap.Cluster
is a wrapped around an Elasticsearch cluster. We can define
it like so:
defmodule MyApp.Cluster do
use Snap.Cluster, otp_app: :my_app
end
The configuration for the cluster can be defined in your config:
config :my_app, MyApp.Cluster,
url: "http://localhost:9200",
username: "username",
password: "password"
Or you can load it dynamically by implementing Snap.Cluster.init/1
.
Each cluster defines start_link/1
which must be invoked before using the
cluster and optionally accepts an explicit config. It creates the
supervision tree, including the connection pool.
Include it in your application:
def start(_type, _args) do
children = [
{MyApp.Cluster, []}
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
Config
The following configuration options are supported:
url
- the URL of the Elasticsearch HTTP endpoint (required)username
- the username used to access the clusterpassword
- the password used to access the clusterauth
- the auth module used to configure the HTTP authentication headers (defaults toSnap.Auth.Plain
)pool_size
- the maximum size of the HTTP connection pool (defaults to 5)telemetry_prefix
- the prefix of the telemetry events (default to[:my_app, :snap]
)
Telemetry
Snap supports sending Telemetry
events on each HTTP request. It sends one
event per query, of the name [:my_app, :snap, :request]
.
The telemetry event has the following measurements:
response_time
- how long the request took to returndecode_time
- how long the response took to decode into a map or exceptiontotal_time
- how long everything took in total
In addition, the metadata contains a map of:
method
- the HTTP method usedpath
- the path requestedheaders
- a list of the headers sentbody
- the body sentresult
- the result returned to the user