Skip to content

Commit

Permalink
feat: add release name, app env and hostname to datadog events (#1345)
Browse files Browse the repository at this point in the history
* additional tags

* additional tags cleanup

* append global tags
  • Loading branch information
Ino Murko authored Feb 25, 2020
1 parent 2ae0e84 commit f195f59
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ defmodule OMG.ChildChain.ReleaseTasks.SetTracer do
config = Keyword.put(config, :disabled?, get_dd_disabled())
config = Keyword.put(config, :env, get_app_env())

:ok = Application.put_env(:statix, :tags, ["application:child_chain", "app_env:#{get_app_env()}"], persistent: true)

:ok = Application.put_env(@app, OMG.ChildChain.Tracer, config, persistent: true)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ defmodule OMG.ChildChainRPC.ReleaseTasks.SetTracer do
config = Keyword.put(config, :disabled?, get_dd_disabled())
config = Keyword.put(config, :env, get_app_env())

:ok = Application.put_env(:statix, :tags, ["application:child_chain", "app_env:#{get_app_env()}"], persistent: true)

:ok = Application.put_env(@app, OMG.ChildChainRPC.Tracer, config, persistent: true)
:ok = Application.put_env(:spandex_phoenix, :tracer, OMG.ChildChainRPC.Tracer, persistent: true)
end
Expand Down
15 changes: 14 additions & 1 deletion apps/omg_status/lib/omg_status/release_tasks/set_tracer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule OMG.Status.ReleaseTasks.SetTracer do
@app :omg_status

@impl Provider
def init(_args) do
def init(args) do
_ = Application.ensure_all_started(:logger)
config = Application.get_env(:omg_status, Tracer)
config = Keyword.put(config, :disabled?, get_dd_disabled())
Expand All @@ -30,6 +30,9 @@ defmodule OMG.Status.ReleaseTasks.SetTracer do
# statix setup
:ok = Application.put_env(:statix, :host, get_dd_hostname(Application.get_env(:statix, :host)), persistent: true)
:ok = Application.put_env(:statix, :port, get_dd_port(Application.get_env(:statix, :port)), persistent: true)
release = Keyword.get(args, :release)
tags = ["application:#{release}", "app_env:#{get_app_env()}", "hostname:#{get_hostname()}"]
:ok = Application.put_env(:statix, :tags, tags, persistent: true)
# spandex_datadog setup

:ok =
Expand All @@ -46,6 +49,13 @@ defmodule OMG.Status.ReleaseTasks.SetTracer do
:ok = Application.put_env(:spandex_datadog, :sync_threshold, get_sync_threshold(), persistent: true)
end

defp get_hostname() do
hostname = validate_hostname(get_env("HOSTNAME"))

_ = Logger.info("CONFIGURATION: App: #{@app} Key: HOSTNAME Value: #{inspect(hostname)}.")
hostname
end

defp get_dd_disabled() do
dd_disabled? =
validate_bool(
Expand Down Expand Up @@ -88,6 +98,9 @@ defmodule OMG.Status.ReleaseTasks.SetTracer do
batch_size
end

defp validate_hostname(value) when is_binary(value), do: value
defp validate_hostname(_), do: exit("HOSTNAME is not set correctly.")

def get_sync_threshold() do
sync_threshold =
validate_integer(
Expand Down
40 changes: 35 additions & 5 deletions apps/omg_status/test/omg_status/release_tasks/set_tracer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ defmodule OMG.Status.ReleaseTasks.SetTracerTest do
# it got fiddled before
:ok = Application.put_env(@app, Tracer, @configuration_old, persistent: true)
:ok = Application.put_env(@app, :spandex_datadog, @configuration_old_spandex_datadog, persistent: true)
:ok = System.delete_env("HOSTNAME")
:ok = System.delete_env("DD_DISABLED")
:ok = System.delete_env("APP_ENV")
:ok = System.delete_env("DD_PORT")
:ok = System.delete_env("DD_HOSTNAME")
:ok = System.delete_env("DD_APM_PORT")
:ok = System.delete_env("BATCH_SIZE")
:ok = System.delete_env("SYNC_THRESHOLD")
end)

:ok
Expand All @@ -39,6 +47,7 @@ defmodule OMG.Status.ReleaseTasks.SetTracerTest do
test "if environment variables get applied in the configuration" do
:ok = System.put_env("DD_DISABLED", "TRUE")
:ok = System.put_env("APP_ENV", "YOLO")
:ok = System.put_env("HOSTNAME", "this is my tracer test 3")
:ok = SetTracer.init([])
configuration = Application.get_env(@app, Tracer)
disabled_updated = configuration[:disabled?]
Expand All @@ -56,6 +65,7 @@ defmodule OMG.Status.ReleaseTasks.SetTracerTest do
:ok = Application.put_env(@app, Tracer, @configuration_old, persistent: true)
:ok = System.delete_env("DD_DISABLED")
:ok = System.delete_env("APP_ENV")
:ok = System.put_env("HOSTNAME", "this is my tracer test 3")
:ok = SetTracer.init([])
configuration = Application.get_env(@app, Tracer)
sorted_configuration = Enum.sort(configuration)
Expand All @@ -65,7 +75,9 @@ defmodule OMG.Status.ReleaseTasks.SetTracerTest do
test "if environment variables get applied in the statix configuration" do
:ok = System.put_env("DD_HOSTNAME", "cluster")
:ok = System.put_env("DD_PORT", "1919")
:ok = SetTracer.init([])
:ok = System.put_env("HOSTNAME", "this is my tracer test 1")
:ok = System.put_env("APP_ENV", "test 1")
:ok = SetTracer.init(release: :test_case_1)
configuration = Enum.sort(Application.get_all_env(:statix))
host = configuration[:host]
port = configuration[:port]
Expand All @@ -76,7 +88,7 @@ defmodule OMG.Status.ReleaseTasks.SetTracerTest do
@configuration_old_statix
|> Keyword.put(:host, "cluster")
|> Keyword.put(:port, 1919)
|> Keyword.put(:tags, nil)
|> Keyword.put(:tags, ["application:test_case_1", "app_env:test 1", "hostname:this is my tracer test 1"])
|> Enum.sort()
end

Expand All @@ -88,18 +100,21 @@ defmodule OMG.Status.ReleaseTasks.SetTracerTest do

:ok = System.delete_env("DD_HOSTNAME")
:ok = System.delete_env("DD_PORT")
:ok = SetTracer.init([])
:ok = System.put_env("HOSTNAME", "this is my tracer test 2")
:ok = System.put_env("APP_ENV", "test 2")
:ok = SetTracer.init(release: :test_case_2)
configuration = Application.get_all_env(:statix)
sorted_configuration = Enum.sort(configuration)

assert sorted_configuration == @configuration_old_statix |> Keyword.put(:tags, nil) |> Enum.sort()
expected_tags = ["application:test_case_2", "app_env:test 2", "hostname:this is my tracer test 2"]
assert sorted_configuration == @configuration_old_statix |> Keyword.put(:tags, expected_tags) |> Enum.sort()
end

test "if environment variables get applied in the spandex_datadog configuration" do
:ok = System.put_env("DD_HOSTNAME", "cluster")
:ok = System.put_env("DD_APM_PORT", "1919")
:ok = System.put_env("BATCH_SIZE", "7000")
:ok = System.put_env("SYNC_THRESHOLD", "900")
:ok = System.put_env("HOSTNAME", "this is my tracer test 4")
:ok = SetTracer.init([])
configuration = Enum.sort(Application.get_all_env(:spandex_datadog))
host = configuration[:host]
Expand Down Expand Up @@ -130,6 +145,7 @@ defmodule OMG.Status.ReleaseTasks.SetTracerTest do
:ok = System.delete_env("DD_APM_PORT")
:ok = System.delete_env("BATCH_SIZE")
:ok = System.delete_env("SYNC_THRESHOLD")
:ok = System.put_env("HOSTNAME", "this is my tracer test 5")
:ok = SetTracer.init([])
configuration = Application.get_all_env(:spandex_datadog)
sorted_configuration = Enum.sort(configuration)
Expand All @@ -142,4 +158,18 @@ defmodule OMG.Status.ReleaseTasks.SetTracerTest do
catch_exit(SetTracer.init([]))
:ok = System.delete_env("DD_DISABLED")
end

test "if environment variables get applied in the statix tags configuration" do
:ok = System.put_env("HOSTNAME", "this is my tracer test")
:ok = System.put_env("APP_ENV", "YOLO")
:ok = SetTracer.init([])
assert Enum.member?(Application.get_env(:statix, :tags), "hostname:this is my tracer test")
end

test "if exit is thrown when faulty configuration for hostname is used" do
:ok = System.put_env("DD_DISABLED", "TRUE")
:ok = System.put_env("APP_ENV", "YOLO")
assert catch_exit(SetTracer.init([])) == "HOSTNAME is not set correctly."
:ok = System.delete_env("DD_DISABLED")
end
end
1 change: 0 additions & 1 deletion apps/omg_utils/test/omg_utils/http_rpc/response_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ defmodule OMG.Utils.HttpRPC.ResponseTest do
txbytes: nil,
txhash: nil,
txindex: nil,
metadata: nil,
txtype: nil,
metadata: nil,
inserted_at: nil,
Expand Down
2 changes: 0 additions & 2 deletions apps/omg_watcher/lib/omg_watcher/release_tasks/set_tracer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ defmodule OMG.Watcher.ReleaseTasks.SetTracer do
config = Keyword.put(config, :disabled?, get_dd_disabled())
config = Keyword.put(config, :env, get_app_env())

:ok = Application.put_env(:statix, :tags, ["application:watcher", "app_env:#{get_app_env()}"], persistent: true)

:ok = Application.put_env(@app, OMG.Watcher.Tracer, config, persistent: true)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ defmodule OMG.Watcher.ReleaseTasks.SetTracerTest do
test "if environment variables get applied in the configuration" do
:ok = System.put_env("DD_DISABLED", "TRUE")
:ok = System.put_env("APP_ENV", "YOLO")
:ok = System.put_env("HOSTNAME", "this is my tracer test 1")
:ok = SetTracer.init([])
configuration = Application.get_env(@app, Tracer)
disabled_updated = configuration[:disabled?]
Expand All @@ -46,6 +47,7 @@ defmodule OMG.Watcher.ReleaseTasks.SetTracerTest do

test "if default configuration is used when there's no environment variables" do
:ok = System.delete_env("DD_DISABLED")
:ok = System.put_env("HOSTNAME", "this is my tracer test2")
:ok = System.put_env("APP_ENV", "YOLO")
:ok = SetTracer.init([])
configuration = Application.get_env(@app, Tracer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ defmodule OMG.WatcherInfo.ReleaseTasks.SetTracer do
config = Keyword.put(config, :disabled?, get_dd_disabled())
config = Keyword.put(config, :env, get_app_env())

:ok =
Application.put_env(:statix, :tags, ["application:watcher_info", "app_env:#{get_app_env()}"], persistent: true)

:ok = Application.put_env(@app, OMG.WatcherInfo.Tracer, config, persistent: true)
end

Expand Down
6 changes: 3 additions & 3 deletions rel/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ release :watcher do
{OMG.WatcherRPC.ReleaseTasks.SetTracer, []},
{OMG.WatcherRPC.ReleaseTasks.SetApiMode, :watcher},
{OMG.Status.ReleaseTasks.SetSentry, []},
{OMG.Status.ReleaseTasks.SetTracer, []},
{OMG.Status.ReleaseTasks.SetTracer, [release: :watcher]},
{OMG.Status.ReleaseTasks.SetApplication,
[release: :watcher, current_version: current_version(:omg_child_chain) <> "+" <> sha]},
{OMG.Watcher.ReleaseTasks.SetChildChain, []},
Expand Down Expand Up @@ -104,7 +104,7 @@ release :watcher_info do
{OMG.WatcherRPC.ReleaseTasks.SetTracer, []},
{OMG.WatcherRPC.ReleaseTasks.SetApiMode, :watcher_info},
{OMG.Status.ReleaseTasks.SetSentry, []},
{OMG.Status.ReleaseTasks.SetTracer, []},
{OMG.Status.ReleaseTasks.SetTracer, [release: :watcher_info]},
{OMG.Status.ReleaseTasks.SetApplication,
[release: :watcher_info, current_version: current_version(:omg_child_chain) <> "+" <> sha]},
{OMG.Watcher.ReleaseTasks.SetChildChain, []},
Expand Down Expand Up @@ -159,7 +159,7 @@ release :child_chain do
{OMG.ChildChainRPC.ReleaseTasks.SetEndpoint, []},
{OMG.ChildChainRPC.ReleaseTasks.SetTracer, []},
{OMG.Status.ReleaseTasks.SetSentry, []},
{OMG.Status.ReleaseTasks.SetTracer, []},
{OMG.Status.ReleaseTasks.SetTracer, [release: :child_chain]},
{OMG.Status.ReleaseTasks.SetApplication,
[release: :child_chain, current_version: current_version(:omg_child_chain) <> "+" <> sha]}
]
Expand Down

0 comments on commit f195f59

Please sign in to comment.