Sentry.Client (sentry v8.0.4) View Source

This module interfaces directly with Sentry via HTTP.

The client itself can be configured via the :client configuration. It must implement the Sentry.HTTPClient behaviour and it defaults to Sentry.HackneyClient.

It makes use of Task.Supervisor to allow sending tasks synchronously or asynchronously, and defaulting to asynchronous. See 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: {MyModule, :before_send},
  before_send_event: {MyModule, :after_send}

where:

defmodule MyModule do
  def before_send(event) do
    metadata = Map.new(Logger.metadata)
    %{event | extra: Map.merge(event.extra, metadata)}
  end

  def after_send_event(event, result) do
    case result do
      {:ok, id} ->
        Logger.info("Successfully sent event!")
      _ ->
        Logger.info(fn -> "Did not successfully send event! #{inspect(event)}" end)
    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 the configured HTTP client.

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

Link to this section Types

Specs

dsn() :: {String.t(), String.t(), String.t()}

Specs

result() :: :sync | :none | :async

Specs

send_event_result() ::
  {:ok, Task.t() | String.t() | pid()}
  | {:error, any()}
  | :unsampled
  | :excluded

Link to this section Functions

Link to this function

authorization_header(public_key, secret_key)

View Source

Specs

authorization_header(String.t(), String.t()) :: String.t()

Generates a Sentry API authorization header.

Specs

get_dsn() :: dsn() | {:error, :invalid_dsn}

Get a Sentry DSN which is simply a URI.

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

Link to this function

maybe_call_after_send_event(result, event)

View Source

Specs

maybe_call_after_send_event(send_event_result(), Sentry.Event.t()) ::
  Sentry.Event.t()
Link to this function

maybe_call_before_send_event(event)

View Source

Specs

maybe_call_before_send_event(Sentry.Event.t()) :: Sentry.Event.t() | false
Link to this function

maybe_log_result(result)

View Source

Specs

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)

View Source

Specs

request(String.t(), [{String.t(), String.t()}], String.t()) ::
  {:ok, String.t()} | {:error, term()}

Makes the HTTP request to Sentry using the configured HTTP client.

Link to this function

send_event(event, opts \\ [])

View Source

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. Errors will be logged unless the source is the Sentry.LoggerBackend, which can deadlock by logging within a logger.

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 should be awaited upon to receive the result asynchronously. If you do not call Task.await/2, messages will be leaked to the inbox of the current process. See Task.Supervisor.async_nolink/2 for more information. :none 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.

  • Other options, such as :stacktrace or :extra will be passed to Sentry.Event.create_event/1 downstream. See Sentry.Event.create_event/1 for available options.