From e2faad163c063ac8e7080e69f3c9770ff34280b3 Mon Sep 17 00:00:00 2001 From: Alberto Sartori Date: Fri, 18 Aug 2023 23:32:03 +0200 Subject: [PATCH 1/4] Add codeowner for utilities/opentelemetry_instrumentation_http (#191) * Add codeowner for utilities/opentelemetry_instrumentation_http * Update CODEOWNERS --------- Co-authored-by: Bryan Naegele --- CODEOWNERS | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index 8103895c..d64227de 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -14,12 +14,12 @@ @open-telemetry/erlang-approvers -/instrumentation/opentelemetry_cowboy @bryannaegele @tsloughter -/instrumentation/opentelemetry_ecto @bryannaegele @tsloughter -/instrumentation/opentelemetry_nebulex @andrewhr -/instrumentation/opentelemetry_oban @indrekj -/instrumentation/opentelemetry_phoenix @bryannaegele @tsloughter -/instrumentation/opentelemetry_redix @andrewhr -/utilities/opentelemetry_telemetry @bryannaegele @tsloughter -/instrumentation/opentelemetry_tesla @ricardoccpaiva -/instrumentation/opentelemetry_httpoison @primait/shared-services +/instrumentation/opentelemetry_cowboy @bryannaegele @tsloughter +/instrumentation/opentelemetry_ecto @bryannaegele @tsloughter +/instrumentation/opentelemetry_nebulex @andrewhr +/instrumentation/opentelemetry_oban @indrekj +/instrumentation/opentelemetry_phoenix @bryannaegele @tsloughter +/instrumentation/opentelemetry_redix @andrewhr +/utilities/opentelemetry_telemetry @bryannaegele @tsloughter +/utilities/opentelemetry_instrumentation_http @tsloughter +/instrumentation/opentelemetry_tesla @ricardoccpaiva From 5380a94bc9e3b25071518db724fe85806036ac2f Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Sat, 19 Aug 2023 00:29:00 +0200 Subject: [PATCH 2/4] opentelemetry_req: Don't assume `request.options` is a map (#192) --- .../opentelemetry_req/lib/opentelemetry_req.ex | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/instrumentation/opentelemetry_req/lib/opentelemetry_req.ex b/instrumentation/opentelemetry_req/lib/opentelemetry_req.ex index 6f3aeff5..7e00a5a2 100644 --- a/instrumentation/opentelemetry_req/lib/opentelemetry_req.ex +++ b/instrumentation/opentelemetry_req/lib/opentelemetry_req.ex @@ -209,12 +209,8 @@ defmodule OpentelemetryReq do end defp require_path_params_option(request) do - unless request.options[:no_path_params] do - unless Map.has_key?(request.options, :path_params) do - {Req.Request.halt(request), __MODULE__.PathParamsOptionError.new()} - else - request - end + if !request.options[:no_path_params] and !request.options[:path_params] do + {Req.Request.halt(request), __MODULE__.PathParamsOptionError.new()} else request end @@ -229,7 +225,7 @@ defmodule OpentelemetryReq do @impl true def message(_) do - "req_path_params path parameter options must be provided" + ":path_params option must be set" end end end From 0b6ccafab584a3dca061b148460bce31d7f13d46 Mon Sep 17 00:00:00 2001 From: Bryan Naegele Date: Tue, 22 Aug 2023 10:13:32 -0600 Subject: [PATCH 3/4] Relax Req's elixir version (#196) Failing on the test matrix update PR. --- instrumentation/opentelemetry_req/mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry_req/mix.exs b/instrumentation/opentelemetry_req/mix.exs index 8c709bef..06665a4c 100644 --- a/instrumentation/opentelemetry_req/mix.exs +++ b/instrumentation/opentelemetry_req/mix.exs @@ -6,7 +6,7 @@ defmodule OpentelemetryReq.MixProject do app: :opentelemetry_req, description: description(), version: "0.1.2", - elixir: "~> 1.12", + elixir: "~> 1.11", start_permanent: Mix.env() == :prod, deps: deps(), name: "Opentelemetry Req", From 79aae558fef61170ada36edf9aa4a4ba97cec464 Mon Sep 17 00:00:00 2001 From: Derek Kraan Date: Wed, 23 Aug 2023 13:35:31 +0200 Subject: [PATCH 4/4] Add the ability to trace Liveviews to the Phoenix instrumentation (#198) --- .../lib/opentelemetry_phoenix.ex | 120 +++++++++++++++++- 1 file changed, 119 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry_phoenix/lib/opentelemetry_phoenix.ex b/instrumentation/opentelemetry_phoenix/lib/opentelemetry_phoenix.ex index 2138f510..23867d64 100644 --- a/instrumentation/opentelemetry_phoenix/lib/opentelemetry_phoenix.ex +++ b/instrumentation/opentelemetry_phoenix/lib/opentelemetry_phoenix.ex @@ -10,6 +10,11 @@ defmodule OpentelemetryPhoenix do default: nil, doc: "The phoenix server adapter being used.", type_doc: ":atom" + ], + liveview: [ + type: :boolean, + default: true, + doc: "Whether LiveView traces will be instrumented." ] ) @@ -78,6 +83,10 @@ defmodule OpentelemetryPhoenix do attach_router_start_handler() attach_router_dispatch_exception_handler() + if opts[:liveview] do + attach_liveview_handlers() + end + :ok end @@ -121,6 +130,30 @@ defmodule OpentelemetryPhoenix do ) end + def attach_liveview_handlers do + :telemetry.attach_many( + {__MODULE__, :live_view}, + [ + [:phoenix, :live_view, :mount, :start], + [:phoenix, :live_view, :mount, :stop], + [:phoenix, :live_view, :mount, :exception], + [:phoenix, :live_view, :handle_params, :start], + [:phoenix, :live_view, :handle_params, :stop], + [:phoenix, :live_view, :handle_params, :exception], + [:phoenix, :live_view, :handle_event, :start], + [:phoenix, :live_view, :handle_event, :stop], + [:phoenix, :live_view, :handle_event, :exception], + [:phoenix, :live_component, :handle_event, :start], + [:phoenix, :live_component, :handle_event, :stop], + [:phoenix, :live_component, :handle_event, :exception] + ], + &__MODULE__.handle_liveview_event/4, + %{} + ) + + :ok + end + @doc false def handle_endpoint_start(_event, _measurements, meta, config) do Process.put({:otel_phoenix, :adapter}, config.adapter) @@ -156,7 +189,8 @@ defmodule OpentelemetryPhoenix do SemanticConventions.Trace.http_target() => conn.request_path, SemanticConventions.Trace.http_user_agent() => user_agent, SemanticConventions.Trace.net_host_name() => conn.host, - SemanticConventions.Trace.net_sock_host_addr() => to_string(:inet_parse.ntoa(conn.remote_ip)), + SemanticConventions.Trace.net_sock_host_addr() => + to_string(:inet_parse.ntoa(conn.remote_ip)), SemanticConventions.Trace.net_host_port() => conn.port, SemanticConventions.Trace.net_sock_peer_addr() => to_string(:inet_parse.ntoa(peer_ip)), SemanticConventions.Trace.net_peer_port() => peer_data.port, @@ -232,6 +266,90 @@ defmodule OpentelemetryPhoenix do end end + def handle_liveview_event( + [:phoenix, _live, :mount, :start], + _measurements, + meta, + _handler_configuration + ) do + %{socket: socket} = meta + %{view: live_view} = socket + + attributes = %{} + + OpentelemetryTelemetry.start_telemetry_span( + @tracer_id, + "#{inspect(live_view)}.mount", + meta, + %{kind: :server} + ) + |> OpenTelemetry.Span.set_attributes(attributes) + end + + def handle_liveview_event( + [:phoenix, _live, :handle_params, :start], + _measurements, + meta, + _handler_configuration + ) do + %{socket: socket} = meta + %{view: live_view} = socket + + attributes = %{} + + OpentelemetryTelemetry.start_telemetry_span( + @tracer_id, + "#{inspect(live_view)}.handle_params", + meta, + %{kind: :server} + ) + |> OpenTelemetry.Span.set_attributes(attributes) + end + + def handle_liveview_event( + [:phoenix, _live, :handle_event, :start], + _measurements, + meta, + _handler_configuration + ) do + %{socket: socket, event: event, params: _params} = meta + %{view: live_view} = socket + + attributes = %{} + + OpentelemetryTelemetry.start_telemetry_span( + @tracer_id, + "#{inspect(live_view)}.handle_event##{event}", + meta, + %{kind: :server} + ) + |> OpenTelemetry.Span.set_attributes(attributes) + end + + def handle_liveview_event( + [:phoenix, _live, _event, :stop], + _measurements, + meta, + _handler_configuration + ) do + OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta) + end + + def handle_liveview_event( + [:phoenix, _live, _action, :exception], + _, + %{kind: kind, reason: reason, stacktrace: stacktrace} = meta, + _ + ) do + ctx = OpentelemetryTelemetry.set_current_telemetry_span(@tracer_id, meta) + + exception = Exception.normalize(kind, reason, stacktrace) + + OpenTelemetry.Span.record_exception(ctx, exception, stacktrace, []) + OpenTelemetry.Span.set_status(ctx, OpenTelemetry.status(:error, "")) + OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta) + end + defp http_flavor({_adapter_name, meta}) do case Map.get(meta, :version) do :"HTTP/1.0" -> :"1.0"