Skip to content

Commit

Permalink
Merge pull request #812 from omisego/chrishunt/spandex
Browse files Browse the repository at this point in the history
Chore: Replace AppSignal with Spandex/Datadog in OMG.Utils.Metrics
  • Loading branch information
Chris Hunt committed Jul 17, 2019
2 parents 167f242 + 5913b87 commit 938a896
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 14 deletions.
5 changes: 5 additions & 0 deletions apps/omg/config/config.exs
Expand Up @@ -14,4 +14,9 @@ config :omg, :eip_712_domain,
version: "1",
salt: "0xfad5c7f626d80f9256ef01929f3beb96e058b8b4b0e3fe52d84f054c0e2a7a83"

config :omg, OMG.Utils.Tracer,
service: :omg,
adapter: SpandexDatadog.Adapter,
env: {:system, "APP_ENV"}

import_config "#{Mix.env()}.exs"
2 changes: 2 additions & 0 deletions apps/omg/config/test.exs
Expand Up @@ -6,3 +6,5 @@ config :omg,
coordinator_eth_height_check_interval_ms: 100,
client_monitor_interval_ms: 50,
environment: :test

config :omg, OMG.Utils.Tracer, env: "test"
1 change: 1 addition & 0 deletions apps/omg/lib/omg/application.ex
Expand Up @@ -24,6 +24,7 @@ defmodule OMG.Application do

def start(_type, _args) do
:ok = DeferredConfig.populate(:statix)
:ok = DeferredConfig.populate(:omg)
:ok = AlarmHandler.install()
:ok = Metrics.connect()

Expand Down
10 changes: 9 additions & 1 deletion apps/omg/lib/omg/supervisor.ex
Expand Up @@ -28,7 +28,15 @@ defmodule OMG.Supervisor do
children = [
{OMG.InternalEventBus, []},
{OMG.EthereumClientMonitor, [alarm_module: Alarm]},
{OMG.EthereumHeight, []}
{OMG.EthereumHeight, []},
{SpandexDatadog.ApiServer,
[
host: System.get_env("DATADOG_HOST") || "localhost",
port: System.get_env("DATADOG_PORT") || 8126,
batch_size: System.get_env("SPANDEX_BATCH_SIZE") || 10,
sync_threshold: System.get_env("SPANDEX_SYNC_THRESHOLD") || 100,
http: HTTPoison
]}
]

opts = [strategy: :one_for_one]
Expand Down
Expand Up @@ -45,6 +45,7 @@ defmodule OMG.RootChainCoordinatorTest do
{OMG.EthereumClientMonitor, [alarm_module: Alarm]},
{OMG.EthereumHeight, []},
{OMG.RootChainCoordinator, coordinator_setup},
{SpandexDatadog.ApiServer, [http: HTTPoison]},
OMG.EthereumEventListener.prepare_child(
service_name: :test,
synced_height_update_key: :last_depositor_eth_height,
Expand Down
3 changes: 2 additions & 1 deletion apps/omg_eth/lib/omg_eth/root_chain.ex
Expand Up @@ -141,7 +141,8 @@ defmodule OMG.Eth.RootChain do
end

@decorate measure_event()
@spec get_piggybacks(non_neg_integer, non_neg_integer, optional_addr_t) :: {:ok, [in_flight_exit_piggybacked_event]}
@spec get_piggybacks(non_neg_integer, non_neg_integer, optional_addr_t) ::
{:ok, [in_flight_exit_piggybacked_event]}
def get_piggybacks(block_from, block_to, contract \\ nil) do
contract = contract || from_hex(Application.get_env(:omg_eth, :contract_addr))
signature = "InFlightExitPiggybacked(address,bytes32,uint8)"
Expand Down
61 changes: 52 additions & 9 deletions apps/omg_utils/lib/omg_utils/metrics.ex
Expand Up @@ -29,7 +29,7 @@ defmodule OMG.Utils.Metrics do
_ -> use Statix, runtime_config: true
end

use Appsignal.Instrumentation.Decorators
alias OMG.Utils.Tracer

use Decorator.Define,
measure_start: 0,
Expand All @@ -40,18 +40,61 @@ defmodule OMG.Utils.Metrics do
def measure_start(body, context) do
# NOTE: the namespace and event group naming convention here is tentative.
# It is possible we'll revert to standard coarser division into `web` and `background` namespaces Appsignal suggests
namespace = context.module |> Module.split() |> List.last() |> String.to_existing_atom()

if Enum.find(@discard_namespace_metrics, &match?(^&1, namespace)),
if Enum.find(@discard_namespace_metrics, &match?(^&1, namespace(context))),
do: body,
else: Appsignal.Instrumentation.Decorators.transaction(namespace, body, context)
else: start_trace(body, context)
end

def measure_event(body, context) do
event_group = context.module |> Module.split() |> List.last() |> String.to_existing_atom()

if Enum.find(@discard_namespace_metrics, &match?(^&1, event_group)),
if Enum.find(@discard_namespace_metrics, &match?(^&1, namespace(context))),
do: body,
else: Appsignal.Instrumentation.Decorators.transaction_event(event_group, body, context)
else: start_span(body, context)
end

defp start_trace(body, context) do
trace_name = trace_name(context)

quote do
_ = unquote(Tracer).start_trace(unquote(trace_name))
Logger.metadata(span_id: unquote(Tracer).current_span_id())
result = unquote(body)
_ = unquote(Tracer).finish_trace()

result
end
end

defp start_span(body, context) do
trace_name = trace_name(context)

quote do
_ =
unquote(Tracer).start_span(
unquote(trace_name),
[{:resource, unquote(trace_name)}]
)

Logger.metadata(span_id: unquote(Tracer).current_span_id())
result = unquote(body)
_ = unquote(Tracer).finish_span()

result
end
end

defp trace_name(%{module: module, name: function, arity: arity}) do
module =
module
|> Atom.to_string()
|> String.trim_leading("Elixir.")

"#{module}.#{function}/#{arity}"
end

defp namespace(%{module: module}) do
module
|> Module.split()
|> List.last()
|> String.to_existing_atom()
end
end
21 changes: 21 additions & 0 deletions apps/omg_utils/lib/omg_utils/tracer.ex
@@ -0,0 +1,21 @@
# Copyright 2019 OmiseGO Pte Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

defmodule OMG.Utils.Tracer do
@moduledoc """
Trace requests and reports information to Datadog via Spandex
"""

use Spandex.Tracer, otp_app: :omg
end
10 changes: 9 additions & 1 deletion apps/omg_utils/mix.exs
Expand Up @@ -25,5 +25,13 @@ defmodule Utils.MixProject do
defp elixirc_paths(:prod), do: ["lib"]
defp elixirc_paths(_), do: ["lib", "test/support"]

defp deps, do: [{:statix, "~> 1.1"}, {:appsignal, "~> 1.9"}]
defp deps do
[
{:appsignal, "~> 1.9"},
{:statix, "~> 1.1"},
{:spandex, "~> 2.4"},
{:spandex_datadog, "~> 0.4"},
{:decorator, "~> 1.2"}
]
end
end
2 changes: 1 addition & 1 deletion apps/omg_watcher/lib/omg_watcher/api/transaction.ex
Expand Up @@ -48,7 +48,7 @@ defmodule OMG.Watcher.API.Transaction do
Length of the list is limited by `limit` argument
"""
@decorate measure_event()
@spec get_transactions(Keyword.t()) :: list(%DB.Transaction{})
@spec get_transactions(Keyword.t()) :: Paginator.t()
def get_transactions(constraints) do
paginator = Paginator.from_constraints(constraints, @default_transactions_limit)

Expand Down
3 changes: 3 additions & 0 deletions mix.exs
Expand Up @@ -38,6 +38,9 @@ defmodule OMG.Umbrella.MixProject do
{:statix, "~> 1.1"},
{:appsignal, "~> 1.9"},
{:sentry, "~> 7.0"},
{:spandex, "~> 2.4"},
{:spandex_datadog, "~> 0.4"},
{:decorator, "~> 1.2"},
{:libsecp256k1,
git: "https://github.com/InoMurko/libsecp256k1.git",
ref: "83d4c91b7b5ad79fdd3c020be8c57ff6e2212780",
Expand Down
6 changes: 5 additions & 1 deletion mix.lock
Expand Up @@ -46,7 +46,9 @@
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"},
"msgpax": {:hex, :msgpax, "2.2.3", "02be0be1b440a12d7e6f6c45662463d53d01a30b5bd4ab806276453b455f820a", [:mix], [{:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm"},
"optimal": {:hex, :optimal, "0.3.6", "46bbf52fbbbd238cda81e02560caa84f93a53c75620f1fe19e81e4ae7b07d1dd", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"phoenix": {:hex, :phoenix, "1.4.7", "e5bc9adcfa4c75a60d0952817d015a6caecc2a8812f4c53c1b61776dfa91d8ce", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.8.1 or ~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "1.1.2", "496c303bdf1b2e98a9d26e89af5bba3ab487ba3a3735f74bf1f4064d2a845a3e", [:mix], [], "hexpm"},
Expand All @@ -60,8 +62,10 @@
"propcheck": {:hex, :propcheck, "1.1.5", "be6e904bed62b556273338ce51e4246764f0439d7367c4de78542bb43059d636", [:mix], [{:proper, "~> 1.3", [hex: :proper, repo: "hexpm", optional: false]}], "hexpm"},
"proper": {:hex, :proper, "1.3.0", "c1acd51c51da17a2fe91d7a6fc6a0c25a6a9849d8dc77093533109d1218d8457", [:make, :mix, :rebar3], [], "hexpm"},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"},
"sentry": {:hex, :sentry, "7.0.6", "ed99dbb1fa9027c83677384c37208589ac419501c5b084f1a24558231980222b", [:mix], [{:hackney, "~> 1.8 or 1.6.5", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.3", [hex: :phoenix, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm"},
"rocksdb": {:hex, :rocksdb, "1.2.0", "5679e2289e4be1e0f53021d898e974b31f2cf0b3a3cec83529b7766d45f90faf", [:rebar3], [], "hexpm"},
"sentry": {:hex, :sentry, "7.0.6", "ed99dbb1fa9027c83677384c37208589ac419501c5b084f1a24558231980222b", [:mix], [{:hackney, "~> 1.8 or 1.6.5", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.3", [hex: :phoenix, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm"},
"spandex": {:hex, :spandex, "2.4.1", "f847dd2b7f95155a58c6f08b27aa236ed9a1e89bac62731227ae0e7397f6770a", [:mix], [{:decorator, "~> 1.2", [hex: :decorator, repo: "hexpm", optional: true]}, {:optimal, "~> 0.3.3", [hex: :optimal, repo: "hexpm", optional: false]}, {:plug, ">= 1.0.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"spandex_datadog": {:hex, :spandex_datadog, "0.4.0", "75113a73e843123074886a2e31994af07d6e0632749a8d97e9ca6157b120c287", [:mix], [{:msgpax, "~> 2.2.1", [hex: :msgpax, repo: "hexpm", optional: false]}, {:spandex, "~> 2.3", [hex: :spandex, repo: "hexpm", optional: false]}], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"statix": {:hex, :statix, "1.1.0", "af9a4586c5cd58f879e0595f06a4790878715de5b8f75f6346693aaa38da2424", [:mix], [], "hexpm"},
"telemetry": {:hex, :telemetry, "0.4.0", "8339bee3fa8b91cb84d14c2935f8ecf399ccd87301ad6da6b71c09553834b2ab", [:rebar3], [], "hexpm"},
Expand Down

0 comments on commit 938a896

Please sign in to comment.