Skip to content

Commit 7484091

Browse files
committed
replace config hackney with finch
1 parent baaf11c commit 7484091

File tree

11 files changed

+88
-130
lines changed

11 files changed

+88
-130
lines changed

config/config.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if config_env() == :test do
66
tags: %{},
77
enable_source_code_context: true,
88
root_source_code_paths: [File.cwd!()],
9-
hackney_opts: [recv_timeout: 50, pool: :sentry_pool],
9+
finch_opts: [recv_timeout: 50, pool: :sentry_pool],
1010
send_result: :sync,
1111
send_max_attempts: 1,
1212
dedup_events: false,

lib/mix/tasks/sentry.send_test_event.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ defmodule Mix.Tasks.Sentry.SendTestEvent do
6767
end
6868

6969
Mix.shell().info("current environment_name: #{inspect(to_string(Config.environment_name()))}")
70-
Mix.shell().info("hackney_opts: #{inspect(Config.hackney_opts())}\n")
70+
Mix.shell().info("finch_opts: #{inspect(Config.finch_opts())}\n")
7171
end
7272

7373
defp send_event(opts) do

lib/sentry/config.ex

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ defmodule Sentry.Config do
276276
client: [
277277
type: :atom,
278278
type_doc: "`t:module/0`",
279-
default: Sentry.HackneyClient,
279+
default: Sentry.FinchClient,
280280
doc: """
281281
A module that implements the `Sentry.HTTPClient`
282-
behaviour. Defaults to `Sentry.HackneyClient`, which uses
283-
[hackney](https://github.com/benoitc/hackney) as the HTTP client.
282+
behaviour. Defaults to `Sentry.FinchClient`, which uses
283+
[finch](https://github.com/sneako/finch) as the HTTP client.
284284
"""
285285
],
286286
send_max_attempts: [
@@ -290,30 +290,30 @@ defmodule Sentry.Config do
290290
The maximum number of attempts to send an event to Sentry.
291291
"""
292292
],
293-
hackney_opts: [
293+
finch_opts: [
294294
type: :keyword_list,
295295
default: [pool: :sentry_pool],
296296
doc: """
297-
Options to be passed to `hackney`. Only
298-
applied if `:client` is set to `Sentry.HackneyClient`.
297+
Options to be passed to `finch`. Only
298+
applied if `:client` is set to `Sentry.FinchClient`.
299299
"""
300300
],
301-
hackney_pool_timeout: [
301+
finch_pool_timeout: [
302302
type: :timeout,
303303
default: 5000,
304304
doc: """
305305
The maximum time to wait for a
306306
connection to become available. Only applied if `:client` is set to
307-
`Sentry.HackneyClient`.
307+
`Sentry.FinchClient`.
308308
"""
309309
],
310-
hackney_pool_max_connections: [
310+
finch_pool_max_connections: [
311311
type: :pos_integer,
312312
default: 50,
313313
doc: """
314314
The maximum number of
315315
connections to keep in the pool. Only applied if `:client` is set to
316-
`Sentry.HackneyClient`.
316+
`Sentry.FinchClient`.
317317
"""
318318
]
319319
]
@@ -537,11 +537,11 @@ defmodule Sentry.Config do
537537
@spec environment_name() :: String.t() | nil
538538
def environment_name, do: fetch!(:environment_name)
539539

540-
@spec max_hackney_connections() :: pos_integer()
541-
def max_hackney_connections, do: fetch!(:hackney_pool_max_connections)
540+
@spec max_finch_connections() :: pos_integer()
541+
def max_finch_connections, do: fetch!(:finch_pool_max_connections)
542542

543-
@spec hackney_timeout() :: timeout()
544-
def hackney_timeout, do: fetch!(:hackney_pool_timeout)
543+
@spec finch_timeout() :: timeout()
544+
def finch_timeout, do: fetch!(:finch_pool_timeout)
545545

546546
@spec tags() :: map()
547547
def tags, do: fetch!(:tags)
@@ -579,8 +579,8 @@ defmodule Sentry.Config do
579579
@spec sample_rate() :: float()
580580
def sample_rate, do: fetch!(:sample_rate)
581581

582-
@spec hackney_opts() :: keyword()
583-
def hackney_opts, do: fetch!(:hackney_opts)
582+
@spec finch_opts() :: keyword()
583+
def finch_opts, do: fetch!(:finch_opts)
584584

585585
@spec before_send() :: (Sentry.Event.t() -> Sentry.Event.t()) | {module(), atom()} | nil
586586
def before_send, do: get(:before_send)

lib/sentry/finch_client.ex

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,47 @@ defmodule Sentry.FinchClient do
1313
1414
Sentry starts its own finch pool called `:sentry_pool`. If you need to set other
1515
[hackney configuration options](https://github.com/benoitc/hackney/blob/master/doc/hackney.md#request5)
16-
for things such as proxies, using your own pool, or response timeouts, the `:hackney_opts`
16+
for things such as proxies, using your own pool, or response timeouts, the `:finch_opts`
1717
configuration is passed directly to hackney for each request. See the configuration
1818
documentation in the `Sentry` module.
1919
"""
20+
@finch_pool_name :sentry_pool
2021

2122
@impl true
2223
def child_spec do
23-
Supervisor.child_spec(
24-
{Finch,
25-
name: __MODULE__,
26-
pools: %{
27-
:default => [size: 10]
28-
}},
29-
id: __MODULE__
30-
)
24+
if Code.ensure_loaded?(Finch) do
25+
case Application.ensure_all_started(:finch) do
26+
{:ok, _apps} -> :ok
27+
{:error, reason} -> raise "failed to start the :finch application: #{inspect(reason)}"
28+
end
29+
30+
Finch.child_spec(
31+
name: __MODULE__,
32+
pools: %{
33+
:default => [
34+
size: Sentry.Config.max_finch_connections(),
35+
conn_max_idle_time: Sentry.Config.finch_timeout()
36+
]
37+
}
38+
)
39+
else
40+
raise """
41+
cannot start the :sentry application because the HTTP client is set to \
42+
Sentry.FinchClient (which is the default), but the Finch library is not loaded. \
43+
Add :finch to your dependencies to fix this.
44+
"""
45+
end
3146
end
3247

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

37-
IO.inspect(request)
38-
39-
opts = Keyword.put_new([], :pool, :sentry_pool)
52+
finch_opts =
53+
Sentry.Config.finch_opts()
54+
|> Keyword.put_new(:pool, @finch_pool_name)
4055

41-
case Finch.request(request, __MODULE__, opts) do
56+
case Finch.request(request, __MODULE__, finch_opts) do
4257
{:ok, %Finch.Response{status: status, headers: headers, body: body}} ->
4358
{:ok, status, headers, body}
4459

lib/sentry/hackney_client.ex

Lines changed: 0 additions & 56 deletions
This file was deleted.

lib/sentry/http_client.ex

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Sentry.HTTPClient do
22
@moduledoc """
33
A behaviour for HTTP clients that Sentry can use.
44
5-
The default HTTP client is `Sentry.HackneyClient`.
5+
The default HTTP client is `Sentry.FinchClient`.
66
77
To configure a different HTTP client, implement the `Sentry.HTTPClient` behaviour and
88
change the `:client` configuration:
@@ -25,46 +25,45 @@ defmodule Sentry.HTTPClient do
2525
## Alternative Clients
2626
2727
Let's look at an example of using an alternative HTTP client. In this example, we'll
28-
use [Finch](https://github.com/sneako/finch), a lightweight HTTP client for Elixir.
28+
use [Hackney](https://github.com/benoitc/hackney), a lightweight HTTP client for Elixir.
2929
30-
First, we need to add Finch to our dependencies:
30+
First, we need to add Hackney to our dependencies:
3131
3232
# In mix.exs
3333
defp deps do
3434
[
3535
# ...
36-
{:finch, "~> 0.16"}
36+
{:hackney, "~> 1.8"}
3737
]
3838
end
3939
4040
Then, we need to define a module that implements the `Sentry.HTTPClient` behaviour:
4141
42-
defmodule MyApp.SentryFinchHTTPClient do
42+
defmodule MyApp.SentryHackneyHTTPClient do
4343
@behaviour Sentry.HTTPClient
4444
45-
@impl true
46-
def child_spec do
47-
Supervisor.child_spec({Finch, name: __MODULE__}, id: __MODULE__)
45+
# @impl true
46+
# def child_spec do
47+
# :hackney_pool.child_spec(
48+
# @hackney_pool_name,
49+
# timeout: Sentry.Config.hackney_timeout(),
50+
# max_connections: Sentry.Config.max_hackney_connections()
51+
# )
52+
# end
53+
54+
# @impl true
55+
# def post(url, headers, body) do
56+
# case :hackney.request(:post, url, headers, body) do
57+
# {:ok, _status, _headers, _body} = result -> result
58+
# {:error, _reason} = error -> error
59+
# end
60+
# end
4861
end
4962
50-
@impl true
51-
def post(url, headers, body) do
52-
request = Finch.build(:post, url, headers, body)
53-
54-
case Finch.request(request, __MODULE__) do
55-
{:ok, %Finch.Response{status: status, headers: headers, body: body}} ->
56-
{:ok, status, headers, body}
57-
58-
{:error, error} ->
59-
{:error, error}
60-
end
61-
end
62-
end
63-
6463
Last, we need to configure Sentry to use our new HTTP client:
6564
6665
config :sentry,
67-
client: MyApp.SentryFinchHTTPClient
66+
client: Sentry.HackneyClient
6867
6968
### Umbrella Apps
7069

mix.exs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ defmodule Sentry.Mixfile do
5151
"Plug and Phoenix": [Sentry.PlugCapture, Sentry.PlugContext, Sentry.LiveViewHook],
5252
Loggers: [Sentry.LoggerBackend, Sentry.LoggerHandler],
5353
"Data Structures": [Sentry.Attachment, Sentry.CheckIn, Sentry.ClientReport],
54-
HTTP: [Sentry.HTTPClient, Sentry.HackneyClient],
54+
HTTP: [Sentry.HTTPClient, Sentry.FinchClient],
5555
Interfaces: [~r/^Sentry\.Interfaces/],
5656
Testing: [Sentry.Test]
5757
],
@@ -65,8 +65,8 @@ defmodule Sentry.Mixfile do
6565
],
6666
authors: ["Mitchell Henke", "Jason Stiebs", "Andrea Leopardi"]
6767
],
68-
xref: [exclude: [:hackney, :hackney_pool, Plug.Conn, :telemetry]],
69-
aliases: aliases()
68+
xref: [exclude: [Finch, Plug.Conn, :telemetry]],
69+
aliases: [aliases()]
7070
]
7171
end
7272

@@ -94,7 +94,6 @@ defmodule Sentry.Mixfile do
9494
{:nimble_ownership, "~> 0.3.0 or ~> 1.0"},
9595

9696
# Optional dependencies
97-
{:hackney, "~> 1.8", optional: true},
9897
{:finch, "~> 0.18.0", optional: true},
9998
{:jason, "~> 1.1", optional: true},
10099
{:phoenix, "~> 1.6", optional: true},

test/mix/sentry.send_test_event_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
55
import Sentry.TestHelpers
66

77
test "prints if :dsn is not set" do
8-
put_test_config(dsn: nil, hackney_opts: [], environment_name: "some_env")
8+
put_test_config(dsn: nil, finch_opts: [], environment_name: "some_env")
99

1010
output =
1111
capture_io(fn ->
@@ -15,7 +15,7 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
1515
assert output =~ """
1616
Client configuration:
1717
current environment_name: "some_env"
18-
hackney_opts: []
18+
finch_opts: []
1919
"""
2020

2121
assert output =~ ~s(Event not sent because the :dsn option is not set)
@@ -35,7 +35,7 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
3535
put_test_config(
3636
dsn: "http://public:secret@localhost:#{bypass.port}/1",
3737
environment_name: "test",
38-
hackney_opts: []
38+
finch_opts: []
3939
)
4040

4141
output =
@@ -49,7 +49,7 @@ defmodule Mix.Tasks.Sentry.SendTestEventTest do
4949
public_key: public
5050
secret_key: secret
5151
current environment_name: "test"
52-
hackney_opts: []
52+
finch_opts: []
5353
"""
5454

5555
assert output =~ "Sending test event..."

test/sentry/client_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ defmodule Sentry.ClientTest do
331331
{:error, %Sentry.ClientError{} = error} =
332332
Client.send_event(event, result: :sync, request_retries: [])
333333

334-
assert error.reason == {:request_failure, :econnrefused}
334+
assert error.reason == {:request_failure, %Mint.TransportError{reason: :econnrefused}}
335335
end
336336

337337
test "logs an error when unable to encode JSON" do

0 commit comments

Comments
 (0)