diff --git a/lib/plausible/ingestion/event.ex b/lib/plausible/ingestion/event.ex index 0acdebb69..49a730fe6 100644 --- a/lib/plausible/ingestion/event.ex +++ b/lib/plausible/ingestion/event.ex @@ -1,7 +1,9 @@ defmodule Plausible.Ingestion.Event do require OpenTelemetry.Tracer, as: Tracer alias Plausible.Ingestion.{Request, CityOverrides} + use OpenTelemetryDecorator + @decorate trace("ingestion.build_event_struct") @spec build(Request.t()) :: {:ok, Plausible.ClickhouseEvent.t()} | :skip | {:error, Ecto.Changeset.t()} @doc """ @@ -24,6 +26,7 @@ defmodule Plausible.Ingestion.Event do do: {:ok, event} end + @decorate trace("ingestion.generate_user_id") def get_user_id(%Request{domain: domain} = request) do salt = Plausible.Session.Salts.fetch().current @@ -139,13 +142,12 @@ defmodule Plausible.Ingestion.Event do end end + @decorate trace("ingestion.parse_user_agent") defp parse_user_agent(%Request{user_agent: user_agent}) when is_binary(user_agent) do - Tracer.with_span "parse_user_agent" do - case Cachex.fetch(:user_agents, user_agent, &UAInspector.parse/1) do - {:ok, user_agent} -> user_agent - {:commit, user_agent} -> user_agent - _ -> nil - end + case Cachex.fetch(:user_agents, user_agent, &UAInspector.parse/1) do + {:ok, user_agent} -> user_agent + {:commit, user_agent} -> user_agent + _ -> nil end end @@ -217,41 +219,40 @@ defmodule Plausible.Ingestion.Event do Map.put(event, :screen_size, screen_width) end + @decorate trace("ingestion.parse_visitor_location") defp put_geolocation(%{} = event, %Request{} = request) do - Tracer.with_span "parse_visitor_location" do - result = Geolix.lookup(request.remote_ip, where: :geolocation) + result = Geolix.lookup(request.remote_ip, where: :geolocation) - country_code = - get_in(result, [:country, :iso_code]) - |> ignore_unknown_country() + country_code = + get_in(result, [:country, :iso_code]) + |> ignore_unknown_country() - city_geoname_id = get_in(result, [:city, :geoname_id]) - city_geoname_id = Map.get(CityOverrides.get(), city_geoname_id, city_geoname_id) + city_geoname_id = get_in(result, [:city, :geoname_id]) + city_geoname_id = Map.get(CityOverrides.get(), city_geoname_id, city_geoname_id) - subdivision1_code = - case result do - %{subdivisions: [%{iso_code: iso_code} | _rest]} -> - country_code <> "-" <> iso_code + subdivision1_code = + case result do + %{subdivisions: [%{iso_code: iso_code} | _rest]} -> + country_code <> "-" <> iso_code - _ -> - "" - end + _ -> + "" + end - subdivision2_code = - case result do - %{subdivisions: [_first, %{iso_code: iso_code} | _rest]} -> - country_code <> "-" <> iso_code + subdivision2_code = + case result do + %{subdivisions: [_first, %{iso_code: iso_code} | _rest]} -> + country_code <> "-" <> iso_code - _ -> - "" - end + _ -> + "" + end - event - |> Map.put(:country_code, country_code) - |> Map.put(:subdivision1_code, subdivision1_code) - |> Map.put(:subdivision2_code, subdivision2_code) - |> Map.put(:city_geoname_id, city_geoname_id) - end + event + |> Map.put(:country_code, country_code) + |> Map.put(:subdivision1_code, subdivision1_code) + |> Map.put(:subdivision2_code, subdivision2_code) + |> Map.put(:city_geoname_id, city_geoname_id) end defp ignore_unknown_country("ZZ"), do: nil diff --git a/lib/plausible/ingestion/request.ex b/lib/plausible/ingestion/request.ex index ba8ecf733..d729ae2e5 100644 --- a/lib/plausible/ingestion/request.ex +++ b/lib/plausible/ingestion/request.ex @@ -3,6 +3,8 @@ defmodule Plausible.Ingestion.Request do The %Plausible.Ingestion.Request{} struct stores all needed fields to create an event downstream. """ + use OpenTelemetryDecorator + defstruct [ :remote_ip, :user_agent, @@ -29,6 +31,7 @@ defmodule Plausible.Ingestion.Request do query_params: map() } + @decorate trace("ingestion.build_request_struct") @spec build(Plug.Conn.t()) :: {:ok, [t()]} | {:error, :invalid_json} @doc """ Builds a list of %Plausible.Ingestion.Request{} struct from %Plug.Conn{}. diff --git a/lib/plausible/ingestion/session/actor.ex b/lib/plausible/ingestion/session/actor.ex index 0b0ca007c..734de4c95 100644 --- a/lib/plausible/ingestion/session/actor.ex +++ b/lib/plausible/ingestion/session/actor.ex @@ -7,6 +7,7 @@ defmodule Plausible.Ingestion.Session.Actor do alias Plausible.{Ingestion, Ingestion.Session} require Logger use GenServer, restart: :transient, shutdown: :timer.minutes(30) + use OpenTelemetryDecorator @spec send_event(pid(), map()) :: :ok def send_event(pid, request_or_event), do: GenServer.cast(pid, {:send_event, request_or_event}) @@ -34,6 +35,7 @@ defmodule Plausible.Ingestion.Session.Actor do end @impl true + @decorate trace("ingestion.session_send_event") def handle_cast({:send_event, request_or_event}, %State{} = state) do Logger.debug("Ingestion: Processing new event for session #{state.domain}@#{state.user_id}") diff --git a/lib/plausible/ingestion/session/dynamic_supervisor.ex b/lib/plausible/ingestion/session/dynamic_supervisor.ex index c1dcbd7c6..9982c21fc 100644 --- a/lib/plausible/ingestion/session/dynamic_supervisor.ex +++ b/lib/plausible/ingestion/session/dynamic_supervisor.ex @@ -1,6 +1,7 @@ defmodule Plausible.Ingestion.Session.DynamicSupervisor do alias Plausible.Ingestion.Session use DynamicSupervisor + use OpenTelemetryDecorator def start_link(_init_arg) do DynamicSupervisor.start_link(__MODULE__, :ok, name: __MODULE__) @@ -11,6 +12,7 @@ defmodule Plausible.Ingestion.Session.DynamicSupervisor do DynamicSupervisor.init(strategy: :one_for_one) end + @decorate trace("ingestion.find_or_spawn_session_event") def find_or_spawn(domain, user_id) do case Registry.lookup(Session.Registry, {domain, user_id}) do [{pid, _}] -> pid diff --git a/mix.exs b/mix.exs index 005a2d73e..6b38fa334 100644 --- a/mix.exs +++ b/mix.exs @@ -107,6 +107,7 @@ defmodule Plausible.MixProject do {:opentelemetry_phoenix, "~> 1.0"}, {:telemetry, "~> 1.0", override: true}, {:opentelemetry_ecto, "~> 1.0.0"}, + {:open_telemetry_decorator, "~> 1.0.0-rc.3"}, {:observer_cli, "~> 1.7"}, {:mimic, "~> 1.7", only: :test}, {:prom_ex, "~> 1.7.1"}, diff --git a/mix.lock b/mix.lock index 1d86ae062..f09cace54 100644 --- a/mix.lock +++ b/mix.lock @@ -25,6 +25,7 @@ "ctx": {:hex, :ctx, "0.6.0", "8ff88b70e6400c4df90142e7f130625b82086077a45364a78d208ed3ed53c7fe", [:rebar3], [], "hexpm", "a14ed2d1b67723dbebbe423b28d7615eb0bdcba6ff28f2d1f1b0a7e1d4aa5fc2"}, "db_connection": {:hex, :db_connection, "2.4.2", "f92e79aff2375299a16bcb069a14ee8615c3414863a6fef93156aee8e86c2ff3", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4fe53ca91b99f55ea249693a0229356a08f4d1a7931d8ffa79289b145fe83668"}, "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, + "decorator": {:hex, :decorator, "1.4.0", "a57ac32c823ea7e4e67f5af56412d12b33274661bb7640ec7fc882f8d23ac419", [:mix], [], "hexpm", "0a07cedd9083da875c7418dea95b78361197cf2bf3211d743f6f7ce39656597f"}, "dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"}, "double": {:hex, :double, "0.8.2", "8e1cfcccdaef76c18846bc08e555555a2a699b806fa207b6468572a60513cc6a", [:mix], [], "hexpm", "90287642b2ec86125e0457aaba2ab0e80f7d7050cc80a0cef733e59bd70aa67c"}, "ecto": {:hex, :ecto, "3.8.1", "35e0bd8c8eb772e14a5191a538cd079706ecb45164ea08a7523b4fc69ab70f56", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f1b68f8d5fe3ab89e24f57c03db5b5d0aed3602077972098b3a6006a1be4b69b"}, @@ -76,6 +77,7 @@ "oauther": {:hex, :oauther, "1.3.0", "82b399607f0ca9d01c640438b34d74ebd9e4acd716508f868e864537ecdb1f76", [:mix], [], "hexpm", "78eb888ea875c72ca27b0864a6f550bc6ee84f2eeca37b093d3d833fbcaec04e"}, "oban": {:hex, :oban, "2.12.0", "bd5a283770c6ab1284aad81e5566cfb89f4119b08f52508d92d73551283c8789", [:mix], [{:ecto_sql, "~> 3.6", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.16", [hex: :postgrex, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "1557b7b046b13c0b5360f55a9fb7e56975f6b5f8247e56f2c54575bd95435ca0"}, "observer_cli": {:hex, :observer_cli, "1.7.3", "25d094d485f47239f218b53df0691a102fef13071dfd0d04922b5142297cfc93", [:mix, :rebar3], [{:recon, "~>2.5.1", [hex: :recon, repo: "hexpm", optional: false]}], "hexpm", "a41b6d3e11a3444e063e09cc225f7f3e631ce14019e5fbcaebfda89b1bd788ea"}, + "open_telemetry_decorator": {:hex, :open_telemetry_decorator, "1.0.0-rc.3", "5508ee00005f7e3fc23bbf5bf527ebb48d5991691801acb8f8a815cbed985483", [:mix], [{:decorator, "~> 1.4.0", [hex: :decorator, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.0.0-rc.3", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "fc695170a5009737f64ef6cee75af2e26d10f9a46c3db8cbe3a02bcf0bd1f15b"}, "opentelemetry": {:hex, :opentelemetry, "1.0.2", "9ffa9ddcbec9356154681bc9d0a54bb20f0de0e8c6696ccc298b49633308782b", [:rebar3], [{:opentelemetry_api, "~> 1.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "e774506333c8afd9133fee4ba69d06e6413ccad6ee76f10e59c3ec624009af23"}, "opentelemetry_api": {:hex, :opentelemetry_api, "1.0.3", "77f9644c42340cd8b18c728cde4822ed55ae136f0d07761b78e8c54da46af93a", [:mix, :rebar3], [], "hexpm", "4293e06bd369bc004e6fad5edbb56456d891f14bd3f9f1772b18f1923e0678ea"}, "opentelemetry_ecto": {:hex, :opentelemetry_ecto, "1.0.0", "8463cc466429b3e125ae236a5cc32869ea38c88eb2f07722dd88e78ceca6963c", [:mix], [{:opentelemetry_api, "~> 1.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:opentelemetry_process_propagator, "~> 0.1.0", [hex: :opentelemetry_process_propagator, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "39cc60dbdd54e371bd12fb010ee960de851d782dca197efefa5c35eb6b4c18bd"},