-
Notifications
You must be signed in to change notification settings - Fork 0
Add requester field and enricher hook to controller metrics #58
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
Open
DylanBlakemore
wants to merge
4
commits into
master
Choose a base branch
from
controller-metrics-enricher
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3952042
Add requester field and enricher hook to controller metrics
DylanBlakemore 5498975
Avoid piping into case in enricher resolution
DylanBlakemore b3620a0
Stabilise metrics test cleanup against supervisor races
DylanBlakemore a9059c4
Name unused exit reason to satisfy credo
DylanBlakemore 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 hidden or 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 hidden or 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 hidden or 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,60 @@ | ||
| defmodule Zexbox.Metrics.ControllerSeriesEnricher do | ||
| @moduledoc """ | ||
| Behaviour for enriching a `Zexbox.Metrics.ControllerSeries` with values | ||
| derived from the request before it is written to InfluxDB. | ||
|
|
||
| This is the extension point for setting tags and fields on the controller | ||
| series that `Zexbox` does not know how to populate itself — for example, | ||
| identifying the caller via an API key description, propagating a tenant | ||
| identifier, or attaching anything else that lives on the `conn`. | ||
|
|
||
| ## Configuring an enricher | ||
|
|
||
| Pass the enricher when starting `Zexbox.Metrics`: | ||
|
|
||
| children = [ | ||
| {Zexbox.Metrics, enricher: {MyApp.MetricsEnricher, []}} | ||
| ] | ||
|
|
||
| Or configure it via application env: | ||
|
|
||
| config :zexbox, :metrics_enricher, {MyApp.MetricsEnricher, []} | ||
|
|
||
| The form `MyApp.MetricsEnricher` (without options) is also accepted and is | ||
| equivalent to `{MyApp.MetricsEnricher, []}`. | ||
|
|
||
| ## Implementing an enricher | ||
|
|
||
| Read whatever you need off the `conn` — typically values placed there by an | ||
| earlier plug — and return a new series. The recommended pattern is to do any | ||
| expensive work (e.g. database lookups) in your auth plug and stash the result | ||
| on `conn.assigns`, so the enricher just reads it: | ||
|
|
||
| defmodule MyApp.MetricsEnricher do | ||
| @behaviour Zexbox.Metrics.ControllerSeriesEnricher | ||
|
|
||
| alias Zexbox.Metrics.ControllerSeries | ||
|
|
||
| @impl true | ||
| def init(opts), do: opts | ||
|
|
||
| @impl true | ||
| def call(series, conn, _opts) do | ||
| case conn.assigns[:api_key_description] do | ||
| nil -> series | ||
| description -> ControllerSeries.field(series, :requester, description) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Enricher exceptions are caught by `Zexbox.Metrics.MetricHandler` and logged; | ||
| the un-enriched series is still written. Avoid blocking work inside `call/3` | ||
| — it runs in the request process. | ||
| """ | ||
|
|
||
| alias Zexbox.Metrics.ControllerSeries | ||
|
|
||
| @callback init(opts :: any()) :: any() | ||
| @callback call(series :: ControllerSeries.t(), conn :: map(), opts :: any()) :: | ||
| ControllerSeries.t() | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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'm in two minds about this. On one hand, it's swallowing an error instead of raising loudly. On the other hand, I'd rather not have metrics crash the process. Thoughts?
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'm not quite sure what the best approach is either. I agree that metrics shouldn't crash a process however I also think that just swallowing errors here is opening the door to bugs slipping under the radar. If nothing else I think we should make it clear in the documentation that uncaught exceptions will be swallowed so enricher modules will need to have some kind of error handling of their own? Having said that, if we're basically telling people that they need their own
rescueclauses in their enrichers is there any point in having one here?Uh oh!
There was an error while loading. Please reload this page.
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.
@KentHawkings after some digging, I think this is necessary. An exception raised when gathering controller metrics here would detach
:telemetryentirely, and metric collection would break until the app restarts. We have an outer rescue entirely, but this at least guarantees the minimal series will be written.