-
Notifications
You must be signed in to change notification settings - Fork 84
Add support for OpenTelemetry tracing #1612
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a362ae1
Add support for OpenTelemetry tracing
joshk 4c5f21d
Use `open_telemetry_decorator`
joshk e05943a
Add some initial tracing to the device socket and channel
joshk fa4d0f5
More OTel decorators for Device related tracing
joshk 4f318f2
Disable tracing exporter during testing
joshk 75dad41
Add a custom OTel filtered sampler
joshk 37d263a
Update the span name for websocket requests
joshk ee79faa
Ignore the css static path
joshk 0352402
Move around the ignored url paths
joshk 89a59ea
Match on request path instead of status
joshk 875c6cc
`mix deps.get` after rebasing
joshk 9698b15
Add some extra span decorating
joshk 56164a7
Update `opentelemetry_oban` dep
joshk 4e000f8
Merge branch 'main' into open-telemetry-tracing
joshk 5ca3533
Formatting fun times
joshk 82458c6
Merge branch 'main' into open-telemetry-tracing
joshk 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
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,23 @@ | ||
| defmodule NervesHub.Telemetry.Customizations do | ||
| alias OpenTelemetry.Tracer | ||
| require OpenTelemetry.Tracer | ||
|
|
||
| def setup() do | ||
| :telemetry.attach_many( | ||
| {__MODULE__, :bandit_customizations}, | ||
| [ | ||
| [:bandit, :request, :stop] | ||
| ], | ||
| &__MODULE__.handle_request/4, | ||
| nil | ||
| ) | ||
| end | ||
|
|
||
| def handle_request([:bandit, :request, :stop], _measurements, %{conn: conn}, _config) do | ||
| if conn.request_path =~ ~r/\/websocket$/ do | ||
| Tracer.update_name("WEBSOCKET #{conn.request_path}") | ||
| end | ||
|
|
||
| :ok | ||
| end | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| defmodule NervesHub.Telemetry.FilteredSampler do | ||
| # Inspired by https://arathunku.com/b/2024/notes-on-adding-opentelemetry-to-an-elixir-app/ | ||
|
|
||
| # TODO: Add ratio sampling support | ||
|
|
||
| require OpenTelemetry.Tracer, as: Tracer | ||
| require Logger | ||
|
|
||
| @behaviour :otel_sampler | ||
|
|
||
| @ignored_static_paths ~r/^\/(assets|fonts|images|css)\/.*/ | ||
|
|
||
| @ignored_url_paths [ | ||
| "/status/alive", | ||
| "/phoenix/live_reload/socket/websocket", | ||
| "/live/websocket", | ||
| "/favicon.ico", | ||
| "/" | ||
| ] | ||
|
|
||
| @ignored_span_names [ | ||
| "Channels.DeviceSocket.heartbeat", | ||
| "nerves_hub.repo.query:schema_migrations" | ||
| ] | ||
|
|
||
| @impl :otel_sampler | ||
| def setup(probability \\ nil) do | ||
| if probability do | ||
| [ratio_sampler_config: :otel_sampler_trace_id_ratio_based.setup(probability)] | ||
| else | ||
| [] | ||
| end | ||
| end | ||
|
|
||
| @impl :otel_sampler | ||
| def description(_sampler_config), do: "NervesHub.Sampler" | ||
|
|
||
| @impl :otel_sampler | ||
| def should_sample( | ||
| ctx, | ||
| trace_id, | ||
| links, | ||
| span_name, | ||
| span_kind, | ||
| attributes, | ||
| sampler_config | ||
| ) do | ||
| result = drop_trace?(span_name, attributes) | ||
|
|
||
| tracestate = Tracer.current_span_ctx(ctx) |> OpenTelemetry.Span.tracestate() | ||
|
|
||
| case result do | ||
| true -> | ||
| {:drop, [], tracestate} | ||
|
|
||
| false -> | ||
| if config = sampler_config[:ratio_sampler_config] do | ||
| :otel_sampler_trace_id_ratio_based.should_sample( | ||
| ctx, | ||
| trace_id, | ||
| links, | ||
| span_name, | ||
| span_kind, | ||
| attributes, | ||
| config | ||
| ) | ||
| else | ||
| {:record_and_sample, [], tracestate} | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def drop_trace?(span_name, attributes) do | ||
| cond do | ||
| Enum.member?(@ignored_span_names, span_name) -> | ||
| true | ||
|
|
||
| span_name == :GET && Enum.member?(@ignored_url_paths, attributes[:"url.path"]) -> | ||
| true | ||
|
|
||
| span_name == :GET && (attributes[:"url.path"] || "") =~ @ignored_static_paths -> | ||
| true | ||
|
|
||
| true -> | ||
| false | ||
| end | ||
| end | ||
| 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
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.
I haven't seen this before, very cool 🤤