-
Notifications
You must be signed in to change notification settings - Fork 2
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
Replace Statix with Prometheus #17
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ defmodule Imager do | |
|
||
require Logger | ||
|
||
alias Imager.Stats | ||
alias Imager.Instrumenter | ||
alias Imager.Store | ||
alias Imager.Tool | ||
|
||
|
@@ -26,34 +26,31 @@ defmodule Imager do | |
def process(store, file_name, commands, opts \\ []) | ||
|
||
def process(%{store: store}, file_name, [], opts) do | ||
Stats.increment("imager.passthrough", 1, tags: Stats.tags()) | ||
Instrumenter.Cache.passthrough(store) | ||
|
||
Store.retrieve(store, file_name, opts) | ||
end | ||
|
||
def process(%{store: store, cache: cache}, file_name, commands, opts) do | ||
{mime, result_name} = Tool.result(file_name, commands) | ||
args = Tool.build_command(commands) | ||
tags = Stats.tags(~w( | ||
store:#{elem(store, 0)} | ||
cache:#{elem(cache, 0)} | ||
)) | ||
|
||
Instrumenter.Processing.command(commands) | ||
|
||
Logger.metadata(input: file_name, commands: inspect(commands)) | ||
Logger.debug(inspect(args)) | ||
|
||
with :error <- Store.retrieve(cache, result_name, opts), | ||
{:ok, {_, _, in_stream}} <- Store.retrieve(store, file_name, opts) do | ||
Logger.debug("Start processing") | ||
Stats.increment("imager.process.started", 1, tags: tags) | ||
|
||
{pid, out_stream} = runner().stream(executable(), args) | ||
|
||
runner().feed_stream(pid, in_stream) | ||
|
||
case runner().wait(pid) do | ||
:ok -> | ||
Stats.increment("imager.process.succeeded", 1, tags: tags) | ||
Instrumenter.Processing.succeeded(store) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nested modules could be aliased at the top of the invoking module. |
||
|
||
stream = | ||
out_stream | ||
|
@@ -62,14 +59,14 @@ defmodule Imager do | |
{:ok, {:unknown, mime, stream}} | ||
|
||
_ -> | ||
Stats.increment("imager.process.failed", 1, tags: tags) | ||
Instrumenter.Processing.failed(store) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nested modules could be aliased at the top of the invoking module. |
||
|
||
:failed | ||
end | ||
else | ||
{:ok, {_, _, _}} = result -> | ||
Logger.debug("Cache hit") | ||
Stats.increment("imager.process.cache_hit", 1, tags: tags) | ||
Instrumenter.Cache.cache_hit(cache) | ||
|
||
result | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Imager.Instrumenter.Cache do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modules should have a @moduledoc tag. |
||
def setup do | ||
:prometheus_counter.declare( | ||
name: :cache_hits_total, | ||
help: "Count of cached images hits", | ||
labels: [:type, :store] | ||
) | ||
end | ||
|
||
def passthrough({store, _}), | ||
do: :prometheus_counter.inc(:cache_hits_total, [:passthrough, store]) | ||
|
||
def cache_hit({store, _}), | ||
do: :prometheus_counter.inc(:cache_hits_total, [:cache, store]) | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Imager.Instrumenter.Processing do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modules should have a @moduledoc tag. |
||
def setup do | ||
:prometheus_counter.declare( | ||
name: :processed_total, | ||
help: "Count of processed images", | ||
labels: [:status, :store] | ||
) | ||
|
||
:prometheus_counter.declare( | ||
name: :process_commands_total, | ||
help: "Processing commands defined", | ||
labels: [:command, :argument] | ||
) | ||
end | ||
|
||
def succeeded({store, _}), | ||
do: :prometheus_counter.inc(:processed_total, ["ok", store]) | ||
|
||
def failed({store, _}), | ||
do: :prometheus_counter.inc(:processed_total, ["failed", store]) | ||
|
||
def command({command, argument}), | ||
do: :prometheus_counter.inc(:process_commands_total, [command, argument]) | ||
|
||
def command(list) when is_list(list), do: Enum.each(list, &command/1) | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule Imager.Instrumenter.Storage do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modules should have a @moduledoc tag. |
||
def setup do | ||
:prometheus_counter.declare( | ||
name: :store_retrieved_total, | ||
help: "Count of retrieves from the given store", | ||
labels: [:store] | ||
) | ||
|
||
:prometheus_counter.declare( | ||
name: :store_saved_total, | ||
help: "Count of saves into the given store", | ||
labels: [:store] | ||
) | ||
|
||
:prometheus_summary.declare( | ||
name: :store_retrieved_bytes, | ||
help: "Bytes retrieved from the store", | ||
labels: [:store] | ||
) | ||
|
||
:prometheus_summary.declare( | ||
name: :store_saved_bytes, | ||
help: "Saved bytes to given store", | ||
labels: [:store] | ||
) | ||
end | ||
|
||
def retrieved(stream, store) do | ||
:prometheus_counter.inc(:store_retrieved_total, [store]) | ||
|
||
Stream.each( | ||
stream, | ||
&:prometheus_summary.observe( | ||
:store_retrieved_bytes, | ||
[store], | ||
byte_size(&1) | ||
) | ||
) | ||
end | ||
|
||
def saved(stream, store) do | ||
:prometheus_counter.inc(:store_saved_total, [store]) | ||
|
||
Stream.each( | ||
stream, | ||
&:prometheus_summary.observe(:store_saved_bytes, [store], byte_size(&1)) | ||
) | ||
end | ||
end |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ defmodule Imager.Store do | |
in application. | ||
""" | ||
|
||
alias Imager.Stats | ||
alias Imager.Instrumenter | ||
|
||
@type size :: non_neg_integer() | ||
@type stream :: Enumerable.t() | ||
|
@@ -31,21 +31,18 @@ defmodule Imager.Store do | |
@spec retrieve(store, binary, keyword) :: | ||
{:ok, {size, mime, stream}} | :error | ||
def retrieve({store, glob_opts}, path, options) do | ||
Stats.increment( | ||
"imager.store.retrieve", | ||
1, | ||
Stats.tags(~w(module:#{store})) | ||
) | ||
|
||
store.retrieve(path, Keyword.merge(glob_opts, options)) | ||
with {:ok, {size, mime, stream}} <- | ||
store.retrieve(path, Keyword.merge(glob_opts, options)), | ||
do: {:ok, {size, mime, Instrumenter.Storage.retrieved(stream, store)}} | ||
end | ||
|
||
@doc """ | ||
Save file in store. | ||
""" | ||
@spec store(stream, store, mime, binary, keyword) :: stream | ||
def store(stream, {store, glob_opts}, mime, path, options) do | ||
Stats.increment("imager.store.store", 1, Stats.tags(~w(module:#{store}))) | ||
store.store(path, mime, stream, Keyword.merge(glob_opts, options)) | ||
path | ||
|> store.store(mime, stream, Keyword.merge(glob_opts, options)) | ||
|> Instrumenter.Storage.saved(store) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pipe chain should start with a raw value. |
||
end | ||
end |
This file was deleted.
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.
Nested modules could be aliased at the top of the invoking module.