-
Notifications
You must be signed in to change notification settings - Fork 8
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 support for metrics #27
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
96de935
bump opencensus
mszczygiel 90edf2f
exporting opencensus metrics
mszczygiel 40a5105
readme was updated
mszczygiel cf04f7e
rename new_measure -> new
mszczygiel 3db26b8
using defdelegate
mszczygiel f17dcc3
make tags last argument
mszczygiel d81582b
accepting keyword list as tags
mszczygiel e909dab
make tests async
mszczygiel 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,96 @@ | ||
defmodule Opencensus.Metrics do | ||
@moduledoc """ | ||
Functions to help Elixir programmers use OpenCensus metrics. | ||
|
||
First, a measure must be created: | ||
```elixir | ||
Metrics.new(:name, "description", :unit) | ||
Metrics.new(:another, "", :unit) | ||
``` | ||
|
||
Next step is to choose aggregations to be exported: | ||
```elixir | ||
Metrics.aggregate_count(:name_count, :name, "count", [:tag1, :tag2]) | ||
Metrics.aggregate_gauge(:name_gauge, :name, "gauge", [:tag1, :tag2]) | ||
Metrics.aggregate_sum(:name_sum, :name, "sum", [:tag1, :tag2]) | ||
Metrics.aggregate_distribution(:name_distribution, :name, "distribution", [:tag1, :tag2], [0, 100, 1000]) | ||
``` | ||
|
||
After aggregations are decided, measures may be recorded by explicitly providing tags: | ||
```elixir | ||
Metrics.record(:name}, 3, %{tag1: "v1", tag2: "v2"}) | ||
Metrics.record([another: 1, name: 100], tag1: "v1", tag2: "v2) | ||
``` | ||
or using tag values that are present in process dictionary: | ||
```elixir | ||
Metrics.record(:name, 3) | ||
``` | ||
""" | ||
|
||
defdelegate new(name, description, unit), to: :oc_stat_measure | ||
|
||
@doc """ | ||
count of how many times `measure` was recorded will be exported | ||
""" | ||
def aggregate_count(name, measure, description, tags) do | ||
:oc_stat_view.subscribe(name, measure, description, tags, :oc_stat_aggregation_count) | ||
end | ||
|
||
@doc """ | ||
only latest recorded value of `measure` will be exported | ||
""" | ||
def aggregate_gauge(name, measure, description, tags) do | ||
:oc_stat_view.subscribe(name, measure, description, tags, :oc_stat_aggregation_latest) | ||
end | ||
|
||
@doc """ | ||
sum of all recorded values of `measure` will be exported | ||
""" | ||
def aggregate_sum(name, measure, description, tags) do | ||
:oc_stat_view.subscribe(name, measure, description, tags, :oc_stat_aggregation_sum) | ||
end | ||
|
||
@doc """ | ||
distribution of all recorded values of `measure` across all `buckets` will be exported | ||
""" | ||
def aggregate_distribution(name, measure, description, tags, buckets) do | ||
:oc_stat_view.subscribe( | ||
name, | ||
measure, | ||
description, | ||
tags, | ||
{:oc_stat_aggregation_distribution, buckets: buckets} | ||
) | ||
end | ||
|
||
@doc """ | ||
records single measure | ||
""" | ||
def record(measure, value, %{} = tags) when is_number(value) do | ||
:oc_stat.record(tags, measure, value) | ||
end | ||
|
||
def record(measure, value, tags) when is_list(tags) and is_number(value) do | ||
tags = Enum.into(tags, %{}) | ||
record(measure, value, tags) | ||
end | ||
|
||
@doc """ | ||
records multiple measures | ||
""" | ||
def record(measures, %{} = tags) when is_list(measures) do | ||
:oc_stat.record(tags, measures) | ||
end | ||
|
||
def record(measures, tags) when is_list(measures) and is_list(tags) do | ||
tags = Enum.into(tags, %{}) | ||
record(measures, tags) | ||
end | ||
|
||
@doc """ | ||
records single measure, takes tags from process dictionary | ||
""" | ||
def record(measure, value) when is_atom(measure) and is_number(value) do | ||
:ocp.record(measure, value) | ||
end | ||
end |
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,39 @@ | ||
defmodule Opencensus.TestSupport.MetricsCaptureExporter do | ||
@moduledoc """ | ||
An `:oc_stat_exporter` to capture exported metrics. To wait for next exported data to be returned, call `capture_next`. | ||
""" | ||
use GenServer | ||
@behaviour :oc_stat_exporter | ||
|
||
def start_link do | ||
GenServer.start_link(__MODULE__, nil, name: __MODULE__) | ||
end | ||
|
||
def capture_next do | ||
GenServer.call(__MODULE__, :capture_next) | ||
end | ||
|
||
@impl GenServer | ||
def init(_arg) do | ||
{:ok, [send_to: nil]} | ||
end | ||
|
||
@impl :oc_stat_exporter | ||
def export(view, _config) do | ||
GenServer.cast(__MODULE__, {:export, view}) | ||
end | ||
|
||
@impl GenServer | ||
def handle_call(:capture_next, from, send_to: nil) do | ||
{:noreply, send_to: from} | ||
end | ||
|
||
@impl GenServer | ||
def handle_cast({:export, view}, send_to: pid) do | ||
unless pid == nil do | ||
GenServer.reply(pid, view) | ||
end | ||
|
||
{:noreply, send_to: nil} | ||
end | ||
end |
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
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.
I would dynamically attach exporter in tests as it will allow to make test async.
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.
Hmm, I don't think attaching it dynamically makes any difference. I think it's fine to just make tests async (which I just did)