Skip to content
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

Add duration_unit option to Ecto plugin #140

Merged
merged 1 commit into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/prom_ex/plugins/absinthe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ if Code.ensure_loaded?(Absinthe) do
in your `dashboard_assigns` to the snakecase version of your prefix, the default
`absinthe_metric_prefix` is `{otp_app}_prom_ex_absinthe`.

- `duration_unit`: This is an OPTIONAL option and is a `Telemetry.Metrics.time_unit()`. It can be one of:
`:second | :millisecond | :microsecond | :nanosecond`. It is `:millisecond` by default.

This plugin exposes the following metric groups:
- `:absinthe_execute_event_metrics`
- `:absinthe_subscription_event_metrics`
Expand Down Expand Up @@ -50,6 +53,8 @@ if Code.ensure_loaded?(Absinthe) do

use PromEx.Plugin

alias PromEx.Utils

# @operation_execute_start_event [:absinthe, :execute, :operation, :start]
@operation_execute_stop_event [:absinthe, :execute, :operation, :stop]
# @subscription_publish_start_event [:absinthe, :subscription, :publish, :start]
Expand Down Expand Up @@ -78,14 +83,17 @@ if Code.ensure_loaded?(Absinthe) do
|> Keyword.get(:ignored_entrypoints, [])
|> MapSet.new()

duration_unit = Keyword.get(opts, :duration_unit, :millisecond)
duration_unit_plural = Utils.make_plural_atom(duration_unit)

event_tags = [:schema, :operation_type, :entrypoint]

Event.build(
:absinthe_subscription_event_metrics,
[
# Capture GraphQL request duration information
distribution(
metric_prefix ++ [:subscription, :duration, :milliseconds],
metric_prefix ++ [:subscription, :duration, duration_unit_plural],
event_name: @subscription_publish_stop_event,
measurement: :duration,
description: "The time it takes for the Absinthe to publish subscription data.",
Expand All @@ -94,7 +102,7 @@ if Code.ensure_loaded?(Absinthe) do
],
tag_values: &subscription_stop_tag_values/1,
tags: event_tags,
unit: {:native, :millisecond},
unit: {:native, duration_unit},
drop: entrypoint_in_ignore_set?(ignored_entrypoints)
)
]
Expand All @@ -108,14 +116,17 @@ if Code.ensure_loaded?(Absinthe) do
|> Keyword.get(:ignored_entrypoints, [])
|> MapSet.new()

duration_unit = Keyword.get(opts, :duration_unit, :millisecond)
duration_unit_plural = Utils.make_plural_atom(duration_unit)

event_tags = [:schema, :operation_type, :entrypoint]

Event.build(
:absinthe_execute_event_metrics,
[
# Capture GraphQL request duration information
distribution(
metric_prefix ++ [:execute, :duration, :milliseconds],
metric_prefix ++ [:execute, :duration, duration_unit_plural],
event_name: @operation_execute_stop_event,
measurement: :duration,
description: "The time it takes for the Absinthe to complete the operation.",
Expand All @@ -124,7 +135,7 @@ if Code.ensure_loaded?(Absinthe) do
],
tag_values: &operation_execute_stop_tag_values/1,
tags: event_tags,
unit: {:native, :millisecond},
unit: {:native, duration_unit},
drop: entrypoint_in_ignore_set?(ignored_entrypoints)
),

Expand Down
12 changes: 9 additions & 3 deletions lib/prom_ex/plugins/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ defmodule PromEx.Plugins.Application do
in your `dashboard_assigns` to the snakecase version of your prefix, the default
`application_metric_prefix` is `{otp_app}_prom_ex_application`.

- `duration_unit`: This is an OPTIONAL option and is a `Telemetry.Metrics.time_unit()`. It can be one of:
`:second | :millisecond | :microsecond | :nanosecond`. It is `:millisecond` by default.

This plugin exposes the following metric groups:
- `:application_versions_manual_metrics`

Expand Down Expand Up @@ -119,18 +122,21 @@ defmodule PromEx.Plugins.Application do
otp_app = Keyword.fetch!(opts, :otp_app)
poll_rate = Keyword.get(opts, :poll_rate, 5_000)
metric_prefix = Keyword.get(opts, :metric_prefix, PromEx.metric_prefix(otp_app, :application))
duration_unit = Keyword.get(opts, :duration_unit, :millisecond)
duration_unit_plural = PromEx.Utils.make_plural_atom(duration_unit)

Polling.build(
:application_time_polling_metrics,
poll_rate,
{__MODULE__, :execute_time_metrics, []},
[
last_value(
metric_prefix ++ [:uptime, :milliseconds, :count],
metric_prefix ++ [:uptime, duration_unit_plural, :count],
event_name: [:prom_ex, :plugin, :application, :uptime, :count],
description: "The total number of wall clock milliseconds that have passed since the application started.",
description:
"The total number of wall clock #{duration_unit_plural} that have passed since the application started.",
measurement: :count,
unit: :millisecond
unit: duration_unit
)
]
)
Expand Down
17 changes: 12 additions & 5 deletions lib/prom_ex/plugins/beam.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ defmodule PromEx.Plugins.Beam do
in your `dashboard_assigns` to the snakecase version of your prefix, the default
`beam_metric_prefix` is `{otp_app}_prom_ex_beam`.

- `duration_unit`: This is an OPTIONAL option and is a `Telemetry.Metrics.time_unit()`. It can be one of:
`:second | :millisecond | :microsecond | :nanosecond`. It is `:millisecond` by default.

This plugin exposes the following metric groups:
- `:beam_memory_polling_metrics`
- `:beam_internal_polling_metrics`
Expand Down Expand Up @@ -57,6 +60,7 @@ defmodule PromEx.Plugins.Beam do
poll_rate = Keyword.get(opts, :poll_rate, 5_000)
otp_app = Keyword.fetch!(opts, :otp_app)
metric_prefix = Keyword.get(opts, :metric_prefix, PromEx.metric_prefix(otp_app, :beam))
duration_unit = Keyword.get(opts, :duration_unit, :millisecond)

# TODO: Investigate Microstate accounting metrics
# http://erlang.org/doc/man/erlang.html#statistics_microstate_accounting
Expand All @@ -69,7 +73,7 @@ defmodule PromEx.Plugins.Beam do
memory_metrics(metric_prefix, poll_rate),
mnesia_metrics(metric_prefix, poll_rate),
distribution_metrics(metric_prefix, poll_rate),
beam_internal_metrics(metric_prefix, poll_rate)
beam_internal_metrics(metric_prefix, poll_rate, duration_unit)
]
end

Expand Down Expand Up @@ -104,7 +108,9 @@ defmodule PromEx.Plugins.Beam do
)
end

defp beam_internal_metrics(metric_prefix, poll_rate) do
defp beam_internal_metrics(metric_prefix, poll_rate, duration_unit) do
duration_unit_plural = String.to_atom("#{duration_unit}s")

Polling.build(
:beam_internal_polling_metrics,
poll_rate,
Expand Down Expand Up @@ -158,11 +164,12 @@ defmodule PromEx.Plugins.Beam do
unit: :byte
),
last_value(
metric_prefix ++ [:stats, :uptime, :milliseconds, :count],
metric_prefix ++ [:stats, :uptime, duration_unit_plural, :count],
event_name: [:prom_ex, :plugin, :beam, :uptime, :count],
description: "The total number of wall clock milliseconds that have passed since the system started.",
description:
"The total number of wall clock #{duration_unit_plural} that have passed since the system started.",
measurement: :count,
unit: :millisecond
unit: duration_unit
),
last_value(
metric_prefix ++ [:stats, :port, :count],
Expand Down
65 changes: 40 additions & 25 deletions lib/prom_ex/plugins/broadway.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ if Code.ensure_loaded?(Broadway) do
alias Broadway.{BatchInfo, Options}
alias PromEx.Utils

@millisecond_duration_buckets [10, 100, 500, 1_000, 10_000, 30_000, 60_000]
@message_batch_size_buckets [1, 5, 10, 20, 50, 100]

@init_topology_event [:broadway, :topology, :init]
Expand All @@ -92,6 +91,7 @@ if Code.ensure_loaded?(Broadway) do
def event_metrics(opts) do
otp_app = Keyword.fetch!(opts, :otp_app)
metric_prefix = Keyword.get(opts, :metric_prefix, PromEx.metric_prefix(otp_app, :broadway))
duration_unit = Keyword.get(opts, :duration_unit, :millisecond)

# Telemetry metrics will emit warnings if multiple handlers with the same names are defined.
# As a result, this plugin supports gathering metrics on multiple processors and batches, but needs
Expand All @@ -102,13 +102,15 @@ if Code.ensure_loaded?(Broadway) do

# Event metrics definitions
[
topology_init_events(metric_prefix),
handle_message_events(metric_prefix),
handle_batch_events(metric_prefix)
topology_init_events(metric_prefix, duration_unit),
handle_message_events(metric_prefix, duration_unit),
handle_batch_events(metric_prefix, duration_unit)
]
end

defp topology_init_events(metric_prefix) do
defp topology_init_events(metric_prefix, duration_unit) do
duration_unit_plural = Utils.make_plural_atom(duration_unit)

Event.build(
:broadway_init_event_metrics,
[
Expand All @@ -121,29 +123,29 @@ if Code.ensure_loaded?(Broadway) do
tag_values: &extract_init_tag_values/1
),
last_value(
metric_prefix ++ [:init, :hibernate_after, :default, :milliseconds],
metric_prefix ++ [:init, :hibernate_after, :default, duration_unit_plural],
event_name: @init_topology_event,
description: "The Broadway supervisor's hibernate after default value.",
measurement: extract_default_config_measurement(:hibernate_after),
tags: [:name],
tag_values: &extract_init_tag_values/1
),
last_value(
metric_prefix ++ [:init, :resubscribe_interval, :default, :milliseconds],
metric_prefix ++ [:init, :resubscribe_interval, :default, duration_unit_plural],
event_name: @init_topology_event,
description: "The Broadway supervisor's resubscribe interval default value.",
measurement: extract_default_config_measurement(:resubscribe_interval),
tags: [:name],
tag_values: &extract_init_tag_values/1
),
last_value(
metric_prefix ++ [:init, :max, :duration, :default, :milliseconds],
metric_prefix ++ [:init, :max, :duration, :default, duration_unit_plural],
event_name: @init_topology_event,
description: "The Broadway supervisor's max seconds default value (in milliseconds).",
description: "The Broadway supervisor's max seconds default value (in #{duration_unit_plural}).",
measurement: extract_default_config_measurement(:max_seconds),
tags: [:name],
tag_values: &extract_init_tag_values/1,
unit: {:second, :millisecond}
unit: {:second, duration_unit}
),
last_value(
metric_prefix ++ [:init, :max_restarts, :default, :value],
Expand All @@ -154,15 +156,15 @@ if Code.ensure_loaded?(Broadway) do
tag_values: &extract_init_tag_values/1
),
last_value(
metric_prefix ++ [:init, :shutdown, :default, :milliseconds],
metric_prefix ++ [:init, :shutdown, :default, duration_unit_plural],
event_name: @init_topology_event,
description: "The Broadway supervisor's shutdown default value.",
measurement: extract_default_config_measurement(:shutdown),
tags: [:name],
tag_values: &extract_init_tag_values/1
),
last_value(
metric_prefix ++ [:init, :processor, :hibernate_after, :milliseconds],
metric_prefix ++ [:init, :processor, :hibernate_after, duration_unit_plural],
event_name: @init_topology_processors_proxy_event,
description: "The Broadway processors hibernate after value.",
measurement: fn _measurements, %{hibernate_after: hibernate_after} -> hibernate_after end,
Expand All @@ -183,7 +185,7 @@ if Code.ensure_loaded?(Broadway) do
tags: [:name, :processor]
),
last_value(
metric_prefix ++ [:init, :batcher, :hibernate_after, :milliseconds],
metric_prefix ++ [:init, :batcher, :hibernate_after, duration_unit_plural],
event_name: @init_topology_batchers_proxy_event,
description: "The Broadway batchers hibernate after value.",
measurement: fn _measurements, %{hibernate_after: hibernate_after} -> hibernate_after end,
Expand All @@ -204,7 +206,7 @@ if Code.ensure_loaded?(Broadway) do
tags: [:name, :batcher]
),
last_value(
metric_prefix ++ [:init, :batcher, :batch_timeout, :milliseconds],
metric_prefix ++ [:init, :batcher, :batch_timeout, duration_unit_plural],
event_name: @init_topology_batchers_proxy_event,
description: "The Broadway batchers timeout value.",
measurement: fn _measurements, %{batch_timeout: batch_timeout} -> batch_timeout end,
Expand Down Expand Up @@ -267,53 +269,57 @@ if Code.ensure_loaded?(Broadway) do
end
end

defp handle_message_events(metric_prefix) do
defp handle_message_events(metric_prefix, duration_unit) do
duration_unit_plural = Utils.make_plural_atom(duration_unit)

Event.build(
:broadway_message_event_metrics,
[
distribution(
metric_prefix ++ [:process, :message, :duration, :milliseconds],
metric_prefix ++ [:process, :message, :duration, duration_unit_plural],
event_name: @message_stop_event,
measurement: :duration,
description: "The time it takes Broadway to process a message.",
reporter_options: [
buckets: @millisecond_duration_buckets
buckets: buckets_for_unit(duration_unit)
],
tags: [:processor_key, :name],
tag_values: &extract_message_tag_values/1,
unit: {:native, :millisecond}
unit: {:native, duration_unit}
),
distribution(
metric_prefix ++ [:process, :message, :exception, :duration, :milliseconds],
metric_prefix ++ [:process, :message, :exception, :duration, duration_unit_plural],
event_name: @message_exception_event,
measurement: :duration,
description: "The time it takes Broadway to process a message that results in an error.",
reporter_options: [
buckets: @millisecond_duration_buckets
buckets: buckets_for_unit(duration_unit)
],
tags: [:processor_key, :name, :kind, :reason],
tag_values: &extract_exception_tag_values/1,
unit: {:native, :millisecond}
unit: {:native, duration_unit}
)
]
)
end

defp handle_batch_events(metric_prefix) do
defp handle_batch_events(metric_prefix, duration_unit) do
duration_unit_plural = Utils.make_plural_atom(duration_unit)

Event.build(
:broadway_batch_event_metrics,
[
distribution(
metric_prefix ++ [:process, :batch, :duration, :milliseconds],
metric_prefix ++ [:process, :batch, :duration, duration_unit_plural],
event_name: @batch_stop_event,
measurement: :duration,
description: "The time it takes Broadway to process a batch of messages.",
reporter_options: [
buckets: @millisecond_duration_buckets
buckets: buckets_for_unit(duration_unit)
],
tags: [:batcher, :name],
tag_values: &extract_batcher_tag_values/1,
unit: {:native, :millisecond}
unit: {:native, duration_unit}
),
distribution(
metric_prefix ++ [:process, :batch, :failure, :size],
Expand Down Expand Up @@ -387,6 +393,15 @@ if Code.ensure_loaded?(Broadway) do
name: Utils.normalize_module_name(name)
}
end

defp buckets_for_unit(unit) do
case unit do
:nanosecond -> [1, 100, 1_000, 2_000, 10_000, 50_000, 1_000_000]
:microsecond -> [10_000, 100_000, 500_000, 1_000_000, 10_000_000, 30_000_000, 60_000_000]
:millisecond -> [10, 100, 500, 1_000, 10_000, 30_000, 60_000]
:second -> [1, 5, 10, 100, 500, 1_000, 10_000]
end
end
end
else
defmodule PromEx.Plugins.Broadway do
Expand Down
Loading