Skip to content

Commit

Permalink
feat: Add Scout APM monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Nov 20, 2020
1 parent f75cc2d commit 1624e04
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 4 deletions.
2 changes: 2 additions & 0 deletions apps/cf/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ config :guardian, Guardian.DB, repo: DB.Repo
config :rollbax,
enabled: :log

config :scout_apm, name: "CaptainFact"

# Import environment specific config
import_config "#{Mix.env()}.exs"
1 change: 1 addition & 0 deletions apps/cf/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ defmodule CF.Mixfile do
{:sweet_xml, "~> 0.6"},
{:burnex, "~> 1.0"},
{:yaml_elixir, "~> 2.4.0"},
{:scout_apm, "~> 1.0.6"},

# ---- Internal ----
{:db, in_umbrella: true},
Expand Down
1 change: 1 addition & 0 deletions apps/cf_graphql/lib/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule CF.GraphQLWeb.Endpoint do
json_decoder: Poison
)

plug(ScoutApm.Absinthe.Plug)
plug(Plug.MethodOverride)
plug(Plug.Head)
plug(CF.GraphQLWeb.Router)
Expand Down
32 changes: 32 additions & 0 deletions apps/cf_graphql/lib/scout_apm_absinthe_plug.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule ScoutApm.Absinthe.Plug do
alias ScoutApm.Internal.Layer

def init(default), do: default

def call(conn, _default) do
ScoutApm.TrackedRequest.start_layer("Controller", action_name(conn))

conn
|> Plug.Conn.register_before_send(&before_send/1)
end

def before_send(conn) do
full_name = action_name(conn)
uri = "#{conn.request_path}"

ScoutApm.TrackedRequest.stop_layer(fn layer ->
layer
|> Layer.update_name(full_name)
|> Layer.update_uri(uri)
end)

conn
end

# Takes a connection, extracts the phoenix controller & action, then manipulates & cleans it up.
# Returns a string like "PageController#index"
defp action_name(conn) do
action_name = conn.params["operationName"]
"GraphQL##{action_name}"
end
end
1 change: 1 addition & 0 deletions apps/cf_graphql/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule CF.Graphql.Mixfile do
{:absinthe_plug, "~> 1.4.1"},
{:kaur, "~> 1.1"},
{:poison, "~> 3.1"},
{:scout_apm, "~> 1.0.6"},

# Internal dependencies
{:db, in_umbrella: true},
Expand Down
1 change: 1 addition & 0 deletions apps/cf_jobs/lib/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule CF.Jobs.Job do
@type t :: module

use GenServer
import ScoutApm.Tracing

def init(args) do
{:ok, args}
Expand Down
2 changes: 1 addition & 1 deletion apps/cf_jobs/lib/jobs/create_notifications.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule CF.Jobs.CreateNotifications do
end

# --- Server callbacks ---

@transaction_opts [type: "background", name: "update_notifications"]
def handle_call({:update, force}, _from, _state) do
last_action_id = ReportManager.get_last_action_id(@analyser_id)

Expand Down
2 changes: 1 addition & 1 deletion apps/cf_jobs/lib/jobs/flags.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule CF.Jobs.Flags do
end

# --- Server callbacks ---

@transaction_opts [type: "background", name: "update_flags"]
def handle_call(:update_flags, _from, _state) do
last_action_id = ReportManager.get_last_action_id(@analyser_id)

Expand Down
1 change: 1 addition & 0 deletions apps/cf_jobs/lib/jobs/moderation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ defmodule CF.Jobs.Moderation do

# --- Internal API ---

@transaction_opts [type: "background", name: "update_moderation"]
def handle_call(:update, _, _) do
UserAction
|> join(:inner, [a], uf in ModerationUserFeedback, uf.action_id == a.id)
Expand Down
3 changes: 2 additions & 1 deletion apps/cf_jobs/lib/jobs/reputation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ defmodule CF.Jobs.Reputation do
do: max(change, @daily_loss_limit - today_change)

# --- Server callbacks ---

@transaction_opts [type: "background", name: "update_reputation"]
def handle_call(:update_reputations, _from, _state) do
last_action_id = ReportManager.get_last_action_id(@analyser_id)

Expand All @@ -114,6 +114,7 @@ defmodule CF.Jobs.Reputation do
{:reply, :ok, :ok}
end

@transaction_opts [type: "background", name: "reset_reputation_limits"]
def handle_call(:reset_daily_limits, _from, _state) do
Logger.info("[Jobs.Reputation] Reset daily limits")

Expand Down
1 change: 1 addition & 0 deletions apps/cf_jobs/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ defmodule CF.Jobs.Mixfile do
[
{:quantum, "~> 2.3"},
{:timex, "~> 3.0"},
{:scout_apm, "~> 1.0.6"},

# ---- Internal ----
{:cf, in_umbrella: true},
Expand Down
2 changes: 2 additions & 0 deletions apps/cf_rest_api/lib/channels/video_debate_channel.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule CF.RestApi.VideoDebateChannel do
use CF.RestApi, :channel
import ScoutApm.Tracing
alias CF.RestApi.Presence

import CF.Actions.ActionCreator,
Expand All @@ -25,6 +26,7 @@ defmodule CF.RestApi.VideoDebateChannel do
alias CF.Notifications.Subscriptions
alias CF.RestApi.{VideoView, SpeakerView, ChangesetView}

@transaction_opts [type: "web", name: "VideoDebateChannel.join"]
def join("video_debate:" <> video_hash_id, _payload, socket) do
Video
|> Video.with_speakers()
Expand Down
1 change: 1 addition & 0 deletions apps/cf_rest_api/lib/rest_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule CF.RestApi do
def controller do
quote do
use Phoenix.Controller, namespace: CF.RestApi
use ScoutApm.Instrumentation

alias DB.Repo
import Ecto
Expand Down
1 change: 1 addition & 0 deletions apps/cf_rest_api/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule CF.RestApi.Mixfile do
{:phoenix_html, "~> 2.6"},
{:phoenix_pubsub, "~> 1.0"},
{:poison, "~> 3.1"},
{:scout_apm, "~> 1.0.6"},

# ---- Internal ----
{:cf, in_umbrella: true},
Expand Down
6 changes: 5 additions & 1 deletion apps/db/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ config :db,
# Database: use postgres
config :db, DB.Repo,
adapter: Ecto.Adapters.Postgres,
pool_size: 3
pool_size: 3,
loggers: [
{Ecto.LogEntry, :log, []},
{ScoutApm.Instruments.EctoLogger, :log, []}
]

# Import environment specific config
import_config "#{Mix.env()}.exs"
1 change: 1 addition & 0 deletions apps/db/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ defmodule DB.Mixfile do
{:kaur, "~> 1.1"},
{:mime, "~> 1.2"},
{:scrivener_ecto, "~> 1.0"},
{:scout_apm, "~> 1.0.6"},

# Dev only
{:exsync, "~> 0.2", only: :dev},
Expand Down
3 changes: 3 additions & 0 deletions config/releases.exs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ config :cf,
]
]

config :scout_apm,
key: load_secret.("scout_apm_key")

config :cf, CF.Authenticator.GuardianImpl, secret_key: load_secret.("secret_key_base")

config :cf, CF.Mailer,
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"absinthe": {:hex, :absinthe, "1.4.13", "81eb2ff41f1b62cd6e992955f62c22c042d1079b7936c27f5f7c2c806b8fc436", [:mix], [{:dataloader, "~> 1.0.0", [hex: :dataloader, repo: "hexpm", optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "ddb55fba830d5a25add2d707a81d11b40096bf6f2cd43b95e117610e7bb1df1a"},
"absinthe_ecto": {:hex, :absinthe_ecto, "0.1.3", "420b68129e79fe4571a4838904ba03e282330d335da47729ad52ffd7b8c5fcb1", [:mix], [{:absinthe, "~> 1.3.0 or ~> 1.4.0", [hex: :absinthe, repo: "hexpm", optional: false]}, {:ecto, ">= 0.0.0", [hex: :ecto, repo: "hexpm", optional: false]}], "hexpm", "355b9db34abfab96ae1e025434b66e11002babcf4fe6b7144d26ff7548985f52"},
"absinthe_plug": {:hex, :absinthe_plug, "1.4.6", "ac5d2d3d02acf52fda0f151b294017ab06e2ed1c6c15334e06aac82c94e36e08", [:mix], [{:absinthe, "~> 1.4.11", [hex: :absinthe, repo: "hexpm", optional: false]}, {:plug, "~> 1.3.2 or ~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "fd7656de35c5db29bff0ee90ac860cec197f2863f44b280486b586ff4d2974c5"},
"approximate_histogram": {:hex, :approximate_histogram, "0.1.1", "198eb36681e763ed4baab6ca0682acec4ef642f60ba272f251d3059052f4f378", [:mix], [], "hexpm", "6cce003d09656efbfe80b4a50f19e6c1f8eaf1424f08e4a96036b340fc67019d"},
"arc": {:git, "https://github.com/Betree/arc.git", "3b3e2037c6afe9f8b0fa2a09b0969b9180faa1b8", []},
"arc_ecto": {:hex, :arc_ecto, "0.10.0", "d2efcc17c45169a13aaac3cd32115ccc832585b7942ea640083280edfd54f514", [:mix], [{:arc, "~> 0.10.0", [hex: :arc, repo: "hexpm", optional: false]}, {:ecto, "~> 2.1", [hex: :ecto, repo: "hexpm", optional: false]}], "hexpm", "45347355728066e0b994af9900189461e70c2b3829f2876f8e1ef0b8ba3c8c0e"},
"artificery": {:hex, :artificery, "0.4.2", "3ded6e29e13113af52811c72f414d1e88f711410cac1b619ab3a2666bbd7efd4", [:mix], [], "hexpm", "514586f4312ef3709a3ccbd8e55f69455add235c1729656687bb781d10d0afdb"},
Expand Down Expand Up @@ -76,6 +77,7 @@
"quantum": {:hex, :quantum, "2.3.3", "83f565de81ac43b8fda4dd4266b209eaed29545d1c41e17aa6b75b08736c80f6", [:mix], [{:calendar, "~> 0.17", [hex: :calendar, repo: "hexpm", optional: true]}, {:crontab, "~> 1.1", [hex: :crontab, repo: "hexpm", optional: false]}, {:gen_stage, "~> 0.12", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:swarm, "~> 3.3", [hex: :swarm, repo: "hexpm", optional: false]}, {:timex, "~> 3.1", [hex: :timex, repo: "hexpm", optional: true]}], "hexpm", "01a63089a17f00f360ddad6c2f068c26d4e280999c2a6c2bce170d0bd6b2bd2e"},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm", "6e56493a862433fccc3aca3025c946d6720d8eedf6e3e6fb911952a7071c357f"},
"rollbax": {:hex, :rollbax, "0.10.0", "f4f5578767cedd272548d12fa7b14335938ef321e677fc3497d4bd725a193316", [:mix], [{:hackney, "~> 1.1", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "d783640ab1a80fec1b88bd0536ce0a66ed66eb35a289572fedb3da5a865ce460"},
"scout_apm": {:hex, :scout_apm, "1.0.7", "0ca260f2c7f3c29bf6a5b361e90339bdce0a5f3ae0cf7b0ce166bfb22eefb89c", [:mix], [{:approximate_histogram, "~>0.1.1", [hex: :approximate_histogram, repo: "hexpm", optional: false]}, {:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:plug, "~>1.0", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0 or ~> 0.3.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "a4a3c8318fb84e4586e68fcd7e889c5bfe17c1caa218cc6f333fb0e4c0ff4ec1"},
"scrivener": {:hex, :scrivener, "2.5.0", "e1f78c62b6806d91cc9c4778deef1ea4e80aa9fadfce2c16831afe0468cc8a2c", [:mix], [], "hexpm", "c3e484da6bb7084b5a24c7e38a8ca09310d5fbf5241db05f625fb8af557ef667"},
"scrivener_ecto": {:hex, :scrivener_ecto, "1.3.0", "69698428e22810ac8a47abc12d1df5b2f5d8f6b36dc5d5bfe6dd93fde857c576", [:mix], [{:ecto, "~> 2.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.11.0 or ~> 0.12.0 or ~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:scrivener, "~> 2.4", [hex: :scrivener, repo: "hexpm", optional: false]}], "hexpm", "3a67401751861d758fc90bdf2cc145bf3d89711967db0e160ffd197037e92dc9"},
"slugger": {:hex, :slugger, "0.3.0", "efc667ab99eee19a48913ccf3d038b1fb9f165fa4fbf093be898b8099e61b6ed", [:mix], [], "hexpm", "20d0ded0e712605d1eae6c5b4889581c3460d92623a930ddda91e0e609b5afba"},
Expand Down

0 comments on commit 1624e04

Please sign in to comment.