-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement pool telemetry #248
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c45123b
feat: rebase from main and implement multi pool metrics for http1
oliveigah 9149b24
chore: mix format
oliveigah 91ee8ab
chore: remove mimic dependency
oliveigah 0e004e8
fix: Finch.Pool get_pool_status callback
oliveigah 57a71e9
feat: add multi pool metrics for http2
oliveigah fd73f84
feat: store atomics ref on pool state instead of persistent_term
oliveigah 8ab3a9c
feat: add auto start option to get_pool
oliveigah ef7dd23
docs: add docs
oliveigah 9ade72a
chore: mix format
oliveigah 9782aa4
chore: remove metrics test from http2 pool test
oliveigah dc56bfe
fix: search available port for http2 test server
oliveigah b9f8f73
fix: dynamic supervisor restart bug
oliveigah 89401b1
fix: search available port on tests
oliveigah be09ce4
fix: reuse same http2 server for all tests
oliveigah b443c64
chore: pr requested changes
oliveigah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
defmodule Finch.HTTP1.PoolMetrics do | ||
@moduledoc """ | ||
HTTP1 Pool metrics. | ||
|
||
Available metrics: | ||
|
||
* `:pool_index` - Index of the pool | ||
* `:pool_size` - Total number of connections of the pool | ||
* `:available_connections` - Number of avaialable connections | ||
* `:in_use_connections` - Number of connections currently in use | ||
|
||
Caveats: | ||
|
||
* A given number X of `available_connections` does not mean that currently | ||
exists X connections to the server sitting on the pool. Because Finch uses | ||
a lazy strategy for workers initialization, every pool starts with it's | ||
size as available connections even if they are not started yet. In practice | ||
this means that `available_connections` may be connections sitting on the pool | ||
or available space on the pool for a new one if required. | ||
|
||
""" | ||
@type t :: %__MODULE__{} | ||
|
||
defstruct [ | ||
:pool_index, | ||
:pool_size, | ||
:available_connections, | ||
oliveigah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
:in_use_connections | ||
] | ||
|
||
@atomic_idx [ | ||
pool_idx: 1, | ||
pool_size: 2, | ||
in_use_connections: 3 | ||
] | ||
|
||
def init(registry, shp, pool_idx, pool_size) do | ||
ref = :atomics.new(length(@atomic_idx), []) | ||
:atomics.add(ref, @atomic_idx[:pool_idx], pool_idx) | ||
:atomics.add(ref, @atomic_idx[:pool_size], pool_size) | ||
|
||
:persistent_term.put({__MODULE__, registry, shp, pool_idx}, ref) | ||
{:ok, ref} | ||
end | ||
|
||
def maybe_add(nil, _metrics_list), do: :ok | ||
|
||
def maybe_add(ref, metrics_list) when is_reference(ref) do | ||
Enum.each(metrics_list, fn {metric_name, val} -> | ||
:atomics.add(ref, @atomic_idx[metric_name], val) | ||
end) | ||
end | ||
|
||
def get_pool_status(name, shp, pool_idx) do | ||
{__MODULE__, name, shp, pool_idx} | ||
|> :persistent_term.get(nil) | ||
|> get_pool_status() | ||
end | ||
|
||
def get_pool_status(ref) when is_reference(ref) do | ||
%{ | ||
pool_idx: pool_idx, | ||
pool_size: pool_size, | ||
in_use_connections: in_use_connections | ||
} = | ||
@atomic_idx | ||
|> Enum.map(fn {k, idx} -> {k, :atomics.get(ref, idx)} end) | ||
|> Map.new() | ||
|
||
result = %__MODULE__{ | ||
pool_index: pool_idx, | ||
pool_size: pool_size, | ||
available_connections: pool_size - in_use_connections, | ||
in_use_connections: in_use_connections | ||
} | ||
|
||
{:ok, result} | ||
end | ||
|
||
def get_pool_status(nil), do: {:error, :not_found} | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're already passing the Finch name in, could we add it to the metadata so it's available in telemetry? It would be really useful in monitoring wait time per Finch instance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes exactly @aselder I was thinking these changes enable #199 . I think that should be implemented in a new PR once this is complete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good idea indeed. But just to be clear, I've only added the finch_name on the Finch.Pool request callback because of averages and maxes metrics.
If we gonna move on only with available and in use connections this is not necessary.
Should I keep it here unused anyway in order to enable this change? What you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering the modifications on #252 I think would be better to remove these changes from this PR in order to avoid conflicts.