From 79257f1cbecc4fddb2080d49eaf7f900218b03ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tao=20Bojl=C3=A9n?= <2803708-taobojlen@users.noreply.gitlab.com> Date: Sat, 27 Jul 2019 10:13:42 +0000 Subject: [PATCH] Setup appsignal --- backend/config/config.exs | 3 +- backend/config/releases.exs | 2 + backend/lib/backend/application.ex | 41 ++++++++++++++++--- backend/lib/backend/crawler/crawler.ex | 9 +++- .../backend/crawler/stale_instance_manager.ex | 2 + backend/lib/backend/release.ex | 4 ++ backend/lib/backend_web/endpoint.ex | 1 + backend/mix.exs | 5 ++- backend/mix.lock | 2 + 9 files changed, 59 insertions(+), 10 deletions(-) diff --git a/backend/config/config.exs b/backend/config/config.exs index db2fbb8..8a193c1 100644 --- a/backend/config/config.exs +++ b/backend/config/config.exs @@ -15,7 +15,8 @@ config :backend, BackendWeb.Endpoint, url: [host: "localhost"], secret_key_base: "XL4NKGBN9lZMrQbMEI1KJOlwAt8S7younVJl90TdAgzmwyapr3g7BRYSNYvX0sZ9", render_errors: [view: BackendWeb.ErrorView, accepts: ~w(json)], - pubsub: [name: Backend.PubSub, adapter: Phoenix.PubSub.PG2] + pubsub: [name: Backend.PubSub, adapter: Phoenix.PubSub.PG2], + instrumenters: [Appsignal.Phoenix.Instrumenter] config :backend, Backend.Repo, queue_target: 5000 diff --git a/backend/config/releases.exs b/backend/config/releases.exs index cd7736a..da9ca8d 100644 --- a/backend/config/releases.exs +++ b/backend/config/releases.exs @@ -17,6 +17,8 @@ config :backend, Backend.Repo, config :backend, Backend.Elasticsearch.Cluster, url: System.get_env("ELASTICSEARCH_URL") || "http://localhost:9200" +config :appsignal, :config, revision: System.get_env("GIT_REV") + port = String.to_integer(System.get_env("PORT") || "4000") config :backend, BackendWeb.Endpoint, diff --git a/backend/lib/backend/application.ex b/backend/lib/backend/application.ex index 9a42828..1e9c298 100644 --- a/backend/lib/backend/application.ex +++ b/backend/lib/backend/application.ex @@ -7,6 +7,13 @@ defmodule Backend.Application do import Backend.Util def start(_type, _args) do + :telemetry.attach( + "appsignal-ecto", + [:backend, :repo, :query], + &Appsignal.Ecto.handle_event/4, + nil + ) + crawl_worker_count = get_config(:crawl_workers) children = [ @@ -16,11 +23,15 @@ defmodule Backend.Application do BackendWeb.Endpoint, # Crawler children :hackney_pool.child_spec(:crawler, timeout: 15000, max_connections: crawl_worker_count), - {Task, - fn -> - Honeydew.start_queue(:crawl_queue, failure_mode: Honeydew.FailureMode.Abandon) - Honeydew.start_workers(:crawl_queue, Backend.Crawler, num: crawl_worker_count) - end}, + Supervisor.child_spec( + {Task, + fn -> + Honeydew.start_queue(:crawl_queue, failure_mode: Honeydew.FailureMode.Abandon) + Honeydew.start_workers(:crawl_queue, Backend.Crawler, num: crawl_worker_count) + end}, + id: :start_honeydew + ), + Supervisor.child_spec({Task, fn -> HTTPoison.start() end}, id: :start_httpoison), Backend.Scheduler, Backend.Elasticsearch.Cluster ] @@ -31,6 +42,8 @@ defmodule Backend.Application do false -> children ++ [Backend.Crawler.StaleInstanceManager] end + add_appsignal_probes() + # See https://hexdocs.pm/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :one_for_one, name: Backend.Supervisor] @@ -43,4 +56,22 @@ defmodule Backend.Application do BackendWeb.Endpoint.config_change(changed, removed) :ok end + + defp add_appsignal_probes do + Appsignal.Probes.register(:crawler, fn -> + %{ + queue: %{ + count: count, + in_progress: in_progress, + mnesia: mnesia + } + } = Honeydew.status(:crawl_queue) + + memory = mnesia |> Map.get(:"honeydew_:crawl_queue") |> Keyword.get(:memory) + + Appsignal.set_gauge("queue_length", count) + Appsignal.set_gauge("in_progress", in_progress) + Appsignal.set_gauge("mnesia_memory", memory) + end) + end end diff --git a/backend/lib/backend/crawler/crawler.ex b/backend/lib/backend/crawler/crawler.ex index 1f487f8..35c1d8d 100644 --- a/backend/lib/backend/crawler/crawler.ex +++ b/backend/lib/backend/crawler/crawler.ex @@ -10,6 +10,7 @@ defmodule Backend.Crawler do import Ecto.Query import Backend.Util require Logger + use Appsignal.Instrumentation.Decorators defstruct [ # the instance domain (a string) @@ -31,9 +32,9 @@ defmodule Backend.Crawler do error: String.t() | nil } + @decorate transaction() def run(domain) do - Logger.info("Crawling #{domain}...") - HTTPoison.start() + Logger.debug("Starting crawl of #{domain}") state = %Crawler{ domain: domain, @@ -59,11 +60,13 @@ defmodule Backend.Crawler do # Recursive function to check whether `domain` has an API that the head of the api_crawlers list can read. # If so, crawls it. If not, continues with the tail of the api_crawlers list. + @decorate transaction_event() defp crawl(%Crawler{api_crawlers: [], domain: domain} = state) do Logger.debug("Found no compatible API for #{domain}") Map.put(state, :found_api?, false) end + @decorate transaction_event() defp crawl(%Crawler{domain: domain, api_crawlers: [curr | remaining_crawlers]} = state) do if curr.is_instance_type?(domain) do Logger.debug("Found #{curr} instance") @@ -94,6 +97,7 @@ defmodule Backend.Crawler do end # Save the state (after crawling) to the database. + @decorate transaction_event() defp save(%Crawler{ domain: domain, result: result, @@ -210,6 +214,7 @@ defmodule Backend.Crawler do |> Repo.insert_all(interactions) end + @decorate transaction_event() defp save(%{domain: domain, error: error, allows_crawling?: allows_crawling}) do error = cond do diff --git a/backend/lib/backend/crawler/stale_instance_manager.ex b/backend/lib/backend/crawler/stale_instance_manager.ex index 4e7a17a..4306b9c 100644 --- a/backend/lib/backend/crawler/stale_instance_manager.ex +++ b/backend/lib/backend/crawler/stale_instance_manager.ex @@ -16,6 +16,8 @@ defmodule Backend.Crawler.StaleInstanceManager do @impl true def init(_opts) do + Logger.info("Starting crawler manager...") + instance_count = Instance |> where([i], not is_nil(i.version)) diff --git a/backend/lib/backend/release.ex b/backend/lib/backend/release.ex index f44a63e..47b2c4e 100644 --- a/backend/lib/backend/release.ex +++ b/backend/lib/backend/release.ex @@ -16,7 +16,11 @@ defmodule Backend.Release do end def index do + # TODO: this isn't the right way to handle this. + # See https://github.com/danielberkompas/elasticsearch-elixir/issues/76 + Application.ensure_all_started(@app) Index.hot_swap(Cluster, "instances") + :init.stop() end def rollback(repo, version) do diff --git a/backend/lib/backend_web/endpoint.ex b/backend/lib/backend_web/endpoint.ex index 46f7bf4..d2028dd 100644 --- a/backend/lib/backend_web/endpoint.ex +++ b/backend/lib/backend_web/endpoint.ex @@ -1,5 +1,6 @@ defmodule BackendWeb.Endpoint do use Phoenix.Endpoint, otp_app: :backend + use Appsignal.Phoenix socket("/socket", BackendWeb.UserSocket, websocket: true, diff --git a/backend/mix.exs b/backend/mix.exs index af593e0..30cfc62 100644 --- a/backend/mix.exs +++ b/backend/mix.exs @@ -20,7 +20,7 @@ defmodule Backend.MixProject do def application do [ mod: {Backend.Application, []}, - extra_applications: [:logger, :runtime_tools, :mnesia, :gollum, :ex_twilio] + extra_applications: [:logger, :runtime_tools, :mnesia, :gollum, :ex_twilio, :appsignal] ] end @@ -53,7 +53,8 @@ defmodule Backend.MixProject do {:idna, "~> 5.1.2", override: true}, {:swoosh, "~> 0.23.3"}, {:ex_twilio, "~> 0.7.0"}, - {:elasticsearch, "~> 1.0"} + {:elasticsearch, "~> 1.0"}, + {:appsignal, "~> 1.10.1"} ] end diff --git a/backend/mix.lock b/backend/mix.lock index 0c17276..d170dc7 100644 --- a/backend/mix.lock +++ b/backend/mix.lock @@ -1,4 +1,5 @@ %{ + "appsignal": {:hex, :appsignal, "1.10.11", "5df2546d6ea15e392a4384b175ebc1bb33f4ccf8fe9872c11542d3ae2043ff88", [:make, :mix], [{:decorator, "~> 1.2.3", [hex: :decorator, repo: "hexpm", optional: false]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.2.0", [hex: :phoenix, repo: "hexpm", optional: true]}, {:plug, ">= 1.1.0", [hex: :plug, repo: "hexpm", optional: true]}, {:poison, ">= 1.3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"}, "artificery": {:hex, :artificery, "0.4.2", "3ded6e29e13113af52811c72f414d1e88f711410cac1b619ab3a2666bbd7efd4", [:mix], [], "hexpm"}, "base64url": {:hex, :base64url, "0.0.1", "36a90125f5948e3afd7be97662a1504b934dd5dac78451ca6e9abf85a10286be", [:rebar], [], "hexpm"}, "certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"}, @@ -10,6 +11,7 @@ "crontab": {:hex, :crontab, "1.1.7", "b9219f0bdc8678b94143655a8f229716c5810c0636a4489f98c0956137e53985", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm"}, "db_connection": {:hex, :db_connection, "2.1.0", "122e2f62c4906bf2e49554f1e64db5030c19229aa40935f33088e7d543aa79d0", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"}, "decimal": {:hex, :decimal, "1.8.0", "ca462e0d885f09a1c5a342dbd7c1dcf27ea63548c65a65e67334f4b61803822e", [:mix], [], "hexpm"}, + "decorator": {:hex, :decorator, "1.2.4", "31dfff6143d37f0b68d0bffb3b9f18ace14fea54d4f1b5e4f86ead6f00d9ff6e", [:mix], [], "hexpm"}, "distillery": {:hex, :distillery, "2.1.1", "f9332afc2eec8a1a2b86f22429e068ef35f84a93ea1718265e740d90dd367814", [:mix], [{:artificery, "~> 0.2", [hex: :artificery, repo: "hexpm", optional: false]}], "hexpm"}, "ecto": {:hex, :ecto, "3.1.7", "fa21d06ef56cdc2fdaa62574e8c3ba34a2751d44ea34c30bc65f0728421043e5", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"}, "ecto_sql": {:hex, :ecto_sql, "3.1.6", "1e80e30d16138a729c717f73dcb938590bcdb3a4502f3012414d0cbb261045d8", [:mix], [{:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.1.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.9.1", [hex: :mariaex, repo: "hexpm", optional: true]}, {:myxql, "~> 0.2.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14.0 or ~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"},