Packages

appsignal

0.4.0
2.17.4 2.17.3 2.17.2 2.17.1 2.17.0 2.16.0 2.15.11 2.15.10 2.15.9 2.15.8 2.15.7 2.15.6 2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.1 2.14.0 2.13.4 2.13.3 2.13.2 2.13.1 2.13.0 2.12.3 2.12.2 2.12.1 2.12.0 2.11.0 2.10.2 2.10.1 2.10.0 2.9.2 2.9.1 2.9.0 2.8.3 2.8.2 2.8.1 2.8.0 2.7.13 2.7.12 2.7.11 2.7.10 2.7.9 2.7.8 2.7.7 2.7.6 2.7.5 2.7.4 2.7.3 2.7.2 2.7.1 2.7.0 2.7.0-beta.1 2.6.1 2.6.0 2.5.3 2.5.2 2.5.1 2.5.0 2.4.3 2.4.2 2.4.1 2.4.0 2.3.1 2.3.0 2.2.19 2.2.18 2.2.17 2.2.16 2.2.15 2.2.14 2.2.13 2.2.12 retired 2.2.11 2.2.10 2.2.9 2.2.8 2.2.7 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 2.2.1 2.2.0 2.1.15 2.1.14 2.1.13 2.1.12 2.1.11 2.1.10 2.1.9 2.1.8 2.1.7 2.1.7-beta.1 2.1.6 2.1.5 2.1.4 2.1.3 2.1.2 2.1.1 2.1.0 2.0.8 2.0.7 2.0.6 2.0.6-beta.1 2.0.5 retired 2.0.4 retired 2.0.3 retired 2.0.2 retired 2.0.1 retired 2.0.0 retired 2.0.0-beta.11 2.0.0-beta.10 2.0.0-beta.9 2.0.0-beta.8 2.0.0-beta.7 2.0.0-beta.6 2.0.0-beta.5 2.0.0-beta.4 2.0.0-beta.3 2.0.0-beta.2 2.0.0-beta.1 2.0.0-alpha.7 2.0.0-alpha.6 2.0.0-alpha.5 2.0.0-alpha.4 2.0.0-alpha.3 2.0.0-alpha.2 2.0.0-alpha.1 1.13.5 1.13.4 1.13.3 1.13.2 1.13.1 1.13.0 1.13.0-beta.2 1.13.0-beta.1 1.12.1 1.12.0 1.12.0-beta.3 1.12.0-beta.2 1.12.0-beta.1 1.11.8 1.11.7 1.11.6 1.11.5 1.11.4 1.11.3 1.11.2 1.11.1 1.11.0 1.11.0-beta.1 1.10.13 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.4 1.9.3 1.9.2 1.9.1 1.9.1-beta.1 1.9.0 1.9.0-beta.1 1.9.0-alpha.1 1.8.2 1.8.1 1.8.1-beta.2 1.8.1-beta.1 1.8.0 1.7.2 1.7.1 1.7.0 1.7.0-beta.2 1.7.0-beta.1 1.7.0-alpha.4 1.7.0-alpha.3 1.7.0-alpha.2 1.7.0-alpha.1 1.6.7 1.6.6 1.6.6-beta.2 1.6.6-beta.1 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.6.0-beta.1 1.6.0-alpha.1 1.5.1 1.5.0 1.5.0-beta.2 1.5.0-beta.1 1.4.11-beta.1 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.4.0-alpha.1 1.3.6 1.3.6-beta.1 1.3.5 1.3.4 1.3.4-beta.1 1.3.3 1.3.2 1.3.1 1.3.0 1.3.0-beta.3 1.3.0-beta.2 1.3.0-beta.1 1.2.3 1.2.2 1.2.1 1.2.0 1.1.1 1.1.0 1.0.4 1.0.3 1.0.2 1.0.1 1.0.0 1.0.0-rc.1 0.13.0 0.12.3 0.12.2 0.12.1 0.12.0 0.11.6 0.11.5 0.11.4 0.11.3 0.11.2 0.11.1 0.11.0 0.10.0 0.9.2 0.9.1 0.9.0 0.8.0 0.7.1 0.7.0 0.6.2 0.6.1 0.6.0 0.5.0 0.4.0 0.3.0 0.2.0 0.1.0 0.0.9 0.0.8 0.0.7 0.0.6 0.0.5 0.0.4 0.0.3 0.0.2 0.0.1

Collects error and performance data from your Elixir applications and sends it to AppSignal

Current section

Files

Jump to
appsignal lib appsignal transaction.ex
Raw

lib/appsignal/transaction.ex

defmodule Appsignal.Transaction do
@moduledoc """
Functions related to Appsignal transactions
This module contains functions for starting and stopping an
Appsignal transaction, recording events and collecting metrics
within a transaction, et cetera.
"""
defstruct [:resource, :id]
alias Appsignal.{Nif, Transaction, TransactionRegistry}
@typedoc """
Datatype which is used as a handle to the current Appsignal transaction.
"""
@type t :: %Transaction{}
@typedoc """
The transaction's namespace
"""
@type namespace :: :http_request | :background_job
@valid_namespaces [:http_request, :background_job]
@doc """
Start a transaction
Call this when a transaction such as a http request or background job starts.
Parameters:
- `transaction_id` The unique identifier of this transaction
- `namespace` The namespace of this transaction. Must be one of `:http_request`, `:background_job`.
The function returns a `%Transaction{}` struct for use with the
other transaction functions in this module.
The returned transaction is also associated with the calling
process, so that processes / callbacks which don't get the
transaction passed in can still look it up through the
`Appsignal.TransactionRegistry`.
"""
@spec start(String.t, namespace) :: Transaction.t
def start(transaction_id, namespace)
when is_binary(transaction_id) and namespace in @valid_namespaces do
{:ok, resource} = Nif.start_transaction(transaction_id, Atom.to_string(namespace))
transaction = %Appsignal.Transaction{resource: resource, id: transaction_id}
TransactionRegistry.register(transaction)
transaction
end
@doc """
Start an event
Call this when an event within a transaction you want to measure starts, such as
an SQL query or http request.
- `transaction`: The pointer to the transaction this event occurred in.
"""
@spec start_event(Transaction.t) :: Transaction.t
def start_event(%Transaction{} = transaction) do
:ok = Nif.start_event(transaction.resource)
transaction
end
@doc """
Finish an event
Call this when an event ends.
- `transaction`: The pointer to the transaction this event occurred in
- `name`: Name of the category of the event (sql.query, net.http)
- `title`: Title of the event ('User load', 'Http request to google.com')
- `body`: Body of the event, should not contain unique information per specific event (`select * from users where id=?`)
- `body_format` Format of the event's body which can be used for sanitization, 0 for general and 1 for sql currently.
"""
@spec finish_event(Transaction.t, String.t, String.t, String.t, integer) :: Transaction.t
def finish_event(%Transaction{} = transaction, name, title, body, body_format \\ 0) do
:ok = Nif.finish_event(transaction.resource, name, title, body, body_format)
transaction
end
@doc """
Record a finished event
Call this when an event which you cannot track the start for
ends. This function can only be used for events that do not have
children such as database queries. GC metrics and allocation counts
will be tracked in the parent of this event.
- `transaction`: The pointer to the transaction this event occurred in
- `name`: Name of the category of the event (sql.query, net.http)
- `title`: Title of the event ('User load', 'Http request to google.com')
- `body`: Body of the event, should not contain unique information per specific event (`select * from users where id=?`)
- `duration`: Duration of this event in nanoseconds
- `body_format` Format of the event's body which can be used for sanitization, 0 for general and 1 for sql currently.
"""
@spec record_event(Transaction.t, String.t, String.t, String.t, integer, integer) :: Transaction.t
def record_event(%Transaction{} = transaction, name, title, body, duration, body_format \\ 0) do
:ok = Nif.record_event(transaction.resource, name, title, body, duration, body_format)
transaction
end
@doc """
Set an error for a transaction
Call this when an error occurs within a transaction.
- `transaction`: The pointer to the transaction this event occurred in
- `name`: Name of the error (RuntimeError)
- `message`: Message of the error ('undefined method call for something')
- `backtrace`: Backtrace of the error; will be JSON encoded
"""
@spec set_error(Transaction.t, String.t, String.t, any) :: Transaction.t
def set_error(%Transaction{} = transaction, error, message, backtrace) do
:ok = Nif.set_error(transaction.resource, error, message, Poison.encode!(backtrace))
transaction
end
@doc """
Set sample data for a transaction
Use this to add sample data if finish_transaction returns true.
- `transaction`: The pointer to the transaction this event occurred in
- `key`: Key of this piece of metadata (params, session_data)
- `payload`: Metadata (e.g. `%{user_id: 1}`); will be JSON encoded
"""
@spec set_sample_data(Transaction.t, String.t, any) :: Transaction.t
def set_sample_data(%Transaction{} = transaction, key, payload) do
:ok = Nif.set_sample_data(transaction.resource, key, Poison.encode!(payload))
transaction
end
@doc """
Set action of a transaction
Call this when the identifying action of a transaction is known.
- `transaction`: The pointer to the transaction this event occurred in
- `action`: This transactions action (`"HomepageController.show"`)
"""
@spec set_action(Transaction.t, String.t) :: Transaction.t
def set_action(%Transaction{} = transaction, action) do
:ok = Nif.set_action(transaction.resource, action)
transaction
end
@doc """
Set queue start time of a transaction
Call this when the queue start time in miliseconds is known.
- `transaction`: The pointer to the transaction this event occurred in
- `queue_start`: Transaction queue start time in ms if known, otherwise -1
"""
@spec set_queue_start(Transaction.t, integer) :: Transaction.t
def set_queue_start(%Transaction{} = transaction, start \\ -1) do
:ok = Nif.set_queue_start(transaction.resource, start)
transaction
end
@doc """
Set metadata for a transaction
Call this when an error occurs within a transaction to set more detailed data about the error
- `transaction`: The pointer to the transaction this event occurred in
- `key`: Key of this piece of metadata (`"email"`)
- `value`: Value of this piece of metadata (`"thijs@appsignal.com"`)
"""
@spec set_meta_data(Transaction.t, String.t, String.t) :: Transaction.t
def set_meta_data(%Transaction{} = transaction, key, value) do
:ok = Nif.set_meta_data(transaction.resource, key, value)
transaction
end
@doc """
Finish a transaction
Call this when a transaction such as a http request or background job ends.
- `transaction`: The pointer to the transaction this event occurred in
Returns `:sample` wether sample data for this transaction should be
collected.
"""
@spec finish(Transaction.t) :: :sample | :no_sample
def finish(%Transaction{} = transaction) do
Nif.finish(transaction.resource)
end
@doc """
Complete a transaction
Call this after finishing a transaction (and adding sample data if necessary).
- `transaction`: The pointer to the transaction this event occurred in
"""
@spec complete(Transaction.t) :: :ok
def complete(%Transaction{} = transaction) do
:ok = Nif.complete(transaction.resource)
end
@doc """
Generate a random id as a string to use as transaction identifier.
"""
@spec generate_id :: String.t
def generate_id do
:crypto.strong_rand_bytes(8) |> Base.hex_encode32(case: :lower, padding: false)
end
defimpl Inspect do
def inspect(transaction, _opts) do
"AppSignal.Transaction{#{transaction.id}}"
end
end
alias Plug.Conn
@doc """
Set the request metadata, given a Plug.Conn.t.
"""
@spec set_request_metadata(Transaction.t, Plug.Conn.t) :: Transaction.t
def set_request_metadata(%Transaction{} = transaction, %Conn{} = conn) do
# preprocess conn
conn = conn
|> Conn.fetch_query_params
# collect sample data
transaction
|> Transaction.set_sample_data("params", conn.params)
|> Transaction.set_sample_data("environment", request_environment(conn))
transaction
end
@conn_fields ~w(host method script_name request_path port schema query_string)a
defp request_environment(conn) do
@conn_fields
|> Enum.map(fn(k) -> {k, Map.get(conn, k)} end)
|> Enum.into(%{})
|> Map.put(:request_uri, url(conn))
|> Map.put(:peer, peer(conn))
end
defp url(%Plug.Conn{scheme: scheme, host: host, port: port} = conn) do
"#{scheme}://#{host}:#{port}#{conn.request_path}"
end
defp peer(%Plug.Conn{peer: {host, port}}) do
"#{:inet_parse.ntoa host}:#{port}"
end
@doc """
Given the transaction and a %PlugConn{}, try to set the Phoenix controller module / action in the transaction.
"""
def try_set_action(transaction, conn) do
try do
action_str = "#{Phoenix.Controller.controller_module(conn)}##{Phoenix.Controller.action_name(conn)}"
<<"Elixir.", action :: binary>> = action_str
Transaction.set_action(transaction, action)
catch
_, _ -> :ok
end
end
end