View Source Sentry.HTTPClient behaviour (Sentry v9.1.0)

A behaviour for HTTP clients that Sentry can use.

The default HTTP client is Sentry.HackneyClient.

To configure a different HTTP client, implement the Sentry.HTTPClient behaviour and change the :client configuration:

config :sentry,
  client: MyHTTPClient

child-spec

Child Spec

The child_spec/0 callback is a callback that should be used when you want Sentry to start the HTTP client under its supervision tree. If you want to start your own HTTP client under your application's supervision tree, just don't implement the callback and Sentry won't do anything to start the client.

Optional Since v9.0.0

The child_spec/0 callback is optional only since v9.0.0 of Sentry, and was required before.

alternative-clients

Alternative Clients

Let's look at an example of using an alternative HTTP client. In this example, we'll use Finch, a lightweight HTTP client for Elixir.

First, we need to add Finch to our dependencies:

# In mix.exs
defp deps do
  [
    # ...
    {:finch, "~> 0.16"}
  ]
end

Then, we need to define a module that implements the Sentry.HTTPClient behaviour:

defmodule MyApp.SentryFinchHTTPClient do
  @behaviour Sentry.HTTPClient

  @impl true
  def child_spec do
    Supervisor.child_spec({Finch, name: __MODULE__}, id: __MODULE__)
  end

  @impl true
  def post(url, headers, body) do
    request = Finch.build(:post, url, headers, body)

    case Finch.request(request, __MODULE__) do
      {:ok, %Finch.Response{status: status, headers: headers, body: body}} ->
        {:ok, status, headers, body}

      {:error, error} ->
        {:error, error}
    end
  end
end

Last, we need to configure Sentry to use our new HTTP client:

config :sentry,
  client: MyApp.SentryFinchHTTPClient

Link to this section Summary

Types

HTTP request or response body.

HTTP request or response headers.

The response status for an HTTP request.

Callbacks

Should return a child specification to start the HTTP client.

Should make an HTTP POST request to url with the given headers and body.

Link to this section Types

@type body() :: binary()

HTTP request or response body.

@type headers() :: [{String.t(), String.t()}]

HTTP request or response headers.

Link to this type

status()

View Source (since 9.0.0)
@type status() :: 100..599

The response status for an HTTP request.

Link to this section Callbacks

@callback child_spec() :: :supervisor.child_spec()

Should return a child specification to start the HTTP client.

For example, this can start a pool of HTTP connections dedicated to Sentry. If not provided, Sentry won't do anything to start your HTTP client. See the module documentation for more info.

Link to this callback

post(url, request_headers, request_body)

View Source
@callback post(url :: String.t(), request_headers :: headers(), request_body :: body()) ::
  {:ok, status(), response_headers :: headers(), response_body :: body()}
  | {:error, term()}

Should make an HTTP POST request to url with the given headers and body.