sentry v6.4.2 Sentry.Client

This module is the default client for sending an event to Sentry via HTTP.

It makes use of Task.Supervisor to allow sending tasks synchronously or asynchronously, and defaulting to asynchronous. See Sentry.Client.send_event/2 for more information.

Configuration

  • :before_send_event - allows performing operations on the event before it is sent. Accepts an anonymous function or a {module, function} tuple, and the event will be passed as the only argument.

  • :after_send_event - callback that is called after attempting to send an event. Accepts an anonymous function or a {module, function} tuple. The result of the HTTP call as well as the event will be passed as arguments. The return value of the callback is not returned.

Example configuration of putting Logger metadata in the extra context:

config :sentry,
  before_send_event: fn(event) ->
    metadata = Map.new(Logger.metadata)
    %{event | extra: Map.merge(event.extra, metadata)}
  end,

  after_send_event: fn(event, result) ->
    case result do
      {:ok, id} ->
        Logger.info("Successfully sent event!")
      _ ->
        Logger.info(fn -> "Did not successfully send event! #{inspect(event)}" end)
    end
  end

Link to this section Summary

Functions

Generates a Sentry API authorization header

Get a Sentry DSN which is simply a URI

Transform the Event struct into JSON map

Makes the HTTP request to Sentry using hackney

Attempts to send the event to the Sentry API up to 4 times with exponential backoff

Link to this section Types

Link to this type dsn()
dsn() :: {String.t(), String.t(), String.t()} | :error
Link to this type send_event_result()
send_event_result() ::
  {:ok, Task.t() | String.t() | pid()} | :error | :unsampled

Link to this section Functions

Link to this function authorization_header(public_key, secret_key)
authorization_header(String.t(), String.t()) :: String.t()

Generates a Sentry API authorization header.

Link to this function get_dsn()
get_dsn() :: dsn()

Get a Sentry DSN which is simply a URI.

{PROTOCOL}://{PUBLIC_KEY}[:{SECRET_KEY}]@{HOST}/{PATH}{PROJECT_ID}

Link to this function hackney_pool_name()
Link to this function maybe_call_after_send_event(result, event)
maybe_call_after_send_event(send_event_result(), Sentry.Event.t()) ::
  Sentry.Event.t()
Link to this function maybe_call_before_send_event(event)
maybe_call_before_send_event(Sentry.Event.t()) :: Sentry.Event.t()
Link to this function render_event(event)
render_event(Sentry.Event.t()) :: map()

Transform the Event struct into JSON map.

Most Event attributes map directly to JSON map, with stacktrace being the exception. If the event does not have stacktrace frames, it should not be included in the JSON body.

Link to this function request(url, headers, body)
request(String.t(), [{String.t(), String.t()}], String.t()) ::
  {:ok, String.t()} | :error

Makes the HTTP request to Sentry using hackney.

Hackney options can be set via the hackney_opts configuration option.

Link to this function send_event(event, opts \\ [])

Attempts to send the event to the Sentry API up to 4 times with exponential backoff.

The event is dropped if it all retries fail.

Options

  • :result - Allows specifying how the result should be returned. Options include :sync, :none, and :async. :sync will make the API call synchronously, and return {:ok, event_id} if successful. :none sends the event from an unlinked child process under Sentry.TaskSupervisor and will return {:ok, ""} regardless of the result. :async will start an unlinked task and return a tuple of {:ok, Task.t} on success where the Task can be awaited upon to receive the result asynchronously. When used in an OTP behaviour like GenServer, the task will send a message that needs to be matched with GenServer.handle_info/2. See Task.Supervisor.async_nolink/2 for more information. :async is the default.
  • :sample_rate - The sampling factor to apply to events. A value of 0.0 will deny sending any events, and a value of 1.0 will send 100% of events.