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

Switch to code attributes for OpentelemetryPhoenix #432

Closed
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
2 changes: 2 additions & 0 deletions instrumentation/opentelemetry_phoenix/.formatter.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Used by "mix format"
[
import_deps: [:phoenix],
plugins: [Phoenix.LiveView.HTMLFormatter],
line_length: 120,
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule OpentelemetryPhoenix do
end

"""
alias OpenTelemetry.SemConv.Incubating.URLAttributes
alias OpenTelemetry.SemConv.Incubating.{CodeAttributes, URLAttributes}

alias OpenTelemetry.Tracer

Expand Down Expand Up @@ -148,8 +148,8 @@ defmodule OpentelemetryPhoenix do
@doc false
def handle_router_dispatch_start(_event, _measurements, meta, _config) do
attributes = %{
:"phoenix.plug" => meta.plug,
:"phoenix.action" => meta.plug_opts,
unquote(CodeAttributes.code_namespace()) => meta.plug,
unquote(CodeAttributes.code_function()) => meta.plug_opts,
URLAttributes.url_template() => meta.route
}

Expand All @@ -167,7 +167,7 @@ defmodule OpentelemetryPhoenix do
@tracer_id,
"#{inspect(live_view)}.mount",
meta,
%{kind: :server}
live_view_start_opts(meta, :mount)
)
end

Expand All @@ -181,7 +181,7 @@ defmodule OpentelemetryPhoenix do
@tracer_id,
"#{inspect(live_view)}.handle_params",
meta,
%{kind: :server}
live_view_start_opts(meta, :handle_params)
)
end

Expand All @@ -195,7 +195,7 @@ defmodule OpentelemetryPhoenix do
@tracer_id,
"#{inspect(live_view)}.handle_event##{event}",
meta,
%{kind: :server}
live_view_start_opts(meta, :handle_event)
)
end

Expand All @@ -222,4 +222,29 @@ defmodule OpentelemetryPhoenix do
OpenTelemetry.Span.set_status(ctx, OpenTelemetry.status(:error, ""))
OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta)
end

defp live_view_start_opts(meta, function) do
%{
kind: :server,
attributes: url_attributes(meta) ++ code_attributes(meta.socket.view, function)
}
end

defp url_attributes(%{uri: uri, socket: %{router: router}}) do
uri = URI.parse(uri)

case Phoenix.Router.route_info(router, "GET", uri.path, uri.host) do
:error -> []
route_info -> [{URLAttributes.url_template(), route_info.route}]
end
end

defp url_attributes(_meta), do: %{}

defp code_attributes(module, function) do
[
{unquote(CodeAttributes.code_namespace()), module},
{unquote(CodeAttributes.code_function()), function}
]
end
end
1 change: 1 addition & 0 deletions instrumentation/opentelemetry_phoenix/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ defmodule OpentelemetryPhoenix.MixProject do
{:opentelemetry_cowboy, "~> 1.0.0", only: [:dev, :test]},
{:ex_doc, "~> 0.36", only: [:dev], runtime: false},
{:phoenix, "~> 1.7", only: [:dev, :test]},
{:phoenix_live_view, "~> 1.0.0", only: [:dev, :test]},
{:phoenix_html, "~> 4.1", only: [:dev, :test]},
{:plug_cowboy, "~> 2.5", only: [:dev, :test]},
{:bandit, "~> 1.5", only: [:dev, :test]},
Expand Down
1 change: 1 addition & 0 deletions instrumentation/opentelemetry_phoenix/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"otel_http": {:hex, :otel_http, "0.2.0", "b17385986c7f1b862f5d577f72614ecaa29de40392b7618869999326b9a61d8a", [:rebar3], [], "hexpm", "f2beadf922c8cfeb0965488dd736c95cc6ea8b9efce89466b3904d317d7cc717"},
"phoenix": {:hex, :phoenix, "1.7.14", "a7d0b3f1bc95987044ddada111e77bd7f75646a08518942c72a8440278ae7825", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.7", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:websock_adapter, "~> 0.5.3", [hex: :websock_adapter, repo: "hexpm", optional: false]}], "hexpm", "c7859bc56cc5dfef19ecfc240775dae358cbaa530231118a9e014df392ace61a"},
"phoenix_html": {:hex, :phoenix_html, "4.1.1", "4c064fd3873d12ebb1388425a8f2a19348cef56e7289e1998e2d2fa758aa982e", [:mix], [], "hexpm", "f2f2df5a72bc9a2f510b21497fd7d2b86d932ec0598f0210fed4114adc546c6f"},
"phoenix_live_view": {:hex, :phoenix_live_view, "1.0.1", "5389a30658176c0de816636ce276567478bffd063c082515a6e8368b8fc9a0db", [:mix], [{:floki, "~> 0.36", [hex: :floki, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6.15 or ~> 1.7.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.3 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.15", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c0f517e6f290f10dbb94343ac22e0109437fb1fa6f0696e7c73967b789c1c285"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.3", "3168d78ba41835aecad272d5e8cd51aa87a7ac9eb836eabc42f6e57538e3731d", [:mix], [], "hexpm", "bba06bc1dcfd8cb086759f0edc94a8ba2bc8896d5331a1e2c2902bf8e36ee502"},
"phoenix_template": {:hex, :phoenix_template, "1.0.4", "e2092c132f3b5e5b2d49c96695342eb36d0ed514c5b252a77048d5969330d639", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "2c0c81f0e5c6753faf5cca2f229c9709919aba34fab866d3bc05060c9c444206"},
"plug": {:hex, :plug, "1.16.1", "40c74619c12f82736d2214557dedec2e9762029b2438d6d175c5074c933edc9d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a13ff6b9006b03d7e33874945b2755253841b238c34071ed85b0e86057f8cddc"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ if otp_vsn >= 27 do
alias OpenTelemetry.SemConv.NetworkAttributes
alias OpenTelemetry.SemConv.ServerAttributes
alias OpenTelemetry.SemConv.UserAgentAttributes
alias OpenTelemetry.SemConv.Incubating.CodeAttributes
alias OpenTelemetry.SemConv.Incubating.HTTPAttributes
alias OpenTelemetry.SemConv.Incubating.URLAttributes

Expand Down Expand Up @@ -170,8 +171,7 @@ if otp_vsn >= 27 do
}

on_exit(fn ->
:telemetry.list_handlers([])
|> Enum.each(fn h -> :telemetry.detach(h.id) end)
Enum.each(:telemetry.list_handlers([]), &:telemetry.detach(&1.id))
end)

adapters
Expand Down Expand Up @@ -239,8 +239,8 @@ if otp_vsn >= 27 do
{URLAttributes.url_query(), "a=1&b=abc"},
{URLAttributes.url_scheme(), :http},
{URLAttributes.url_template(), "/users/:user_id"},
{:"phoenix.action", :user},
{:"phoenix.plug", OpentelemetryPhoenix.Integration.TracingTest.TestController}
{CodeAttributes.code_function(), :user},
{CodeAttributes.code_namespace(), OpentelemetryPhoenix.Integration.TracingTest.TestController}
]

for {attr, val} <- expected_attrs do
Expand Down
Loading
Loading