Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for app analytics #23

Merged
merged 1 commit into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions lib/spandex_datadog/api_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,10 @@ defmodule SpandexDatadog.ApiServer do
service: span.service,
type: span.type,
meta: meta(span),
metrics: %{
_sampling_priority_v1: priority
}
metrics:
metrics(span, %{
_sampling_priority_v1: priority
})
}
end

Expand Down Expand Up @@ -310,6 +311,8 @@ defmodule SpandexDatadog.ApiServer do
defp add_tags(meta, %{tags: nil}), do: meta

defp add_tags(meta, %{tags: tags}) do
tags = tags |> Keyword.delete(:analytics_event)

Map.merge(
meta,
tags
Expand All @@ -318,6 +321,30 @@ defmodule SpandexDatadog.ApiServer do
)
end

@spec metrics(Span.t(), map) :: map
defp metrics(span, initial_value = %{}) do
initial_value
|> add_metrics(span)
|> Enum.reject(fn {_k, v} -> is_nil(v) end)
|> Enum.into(%{})
end

@spec add_metrics(map, Span.t()) :: map
defp add_metrics(metrics, %{tags: nil}), do: metrics

defp add_metrics(metrics, %{tags: tags}) do
with analytics_event <- tags |> Keyword.get(:analytics_event),
true <- analytics_event != nil do
Map.merge(
metrics,
%{"_dd1.sr.eausr" => 1}
)
else
_ ->
metrics
end
end

@spec error(nil | Keyword.t()) :: integer
defp error(nil), do: 0

Expand Down
50 changes: 25 additions & 25 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"certifi": {:hex, :certifi, "2.4.2", "75424ff0f3baaccfd34b1214184b6ef616d89e420b258bb0a5ea7d7bc628f7f0", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"excoveralls": {:hex, :excoveralls, "0.10.1", "407d50ac8fc63dfee9175ccb4548e6c5512b5052afa63eedb9cd452a32a91495", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"git_cli": {:hex, :git_cli, "0.2.5", "40673235d088d2784d4e03511e62cac2ef520495cb6cc459f00b4e13aaf0fbf4", [:mix], [], "hexpm"},
"git_ops": {:hex, :git_ops, "0.3.4", "4cac61a23a30950877740e10bc985485c08107a5a48cee56421870875cf4cae7", [:mix], [{:git_cli, "~> 0.2", [hex: :git_cli, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 0.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"hackney": {:hex, :hackney, "1.14.0", "66e29e78feba52176c3a4213d42b29bdc4baff93a18cfe480f73b04677139dee", [:rebar3], [{:certifi, "2.4.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.4", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"httpoison": {:hex, :httpoison, "0.13.0", "bfaf44d9f133a6599886720f3937a7699466d23bb0cd7a88b6ba011f53c6f562", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"certifi": {:hex, :certifi, "2.4.2", "75424ff0f3baaccfd34b1214184b6ef616d89e420b258bb0a5ea7d7bc628f7f0", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "01d479edba0569a7b7a2c8bf923feeb6dc6a358edc2965ef69aea9ba288bb243"},
"earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm", "b42a23e9bd92d65d16db2f75553982e58519054095356a418bb8320bbacb58b1"},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "dc87f778d8260da0189a622f62790f6202af72f2f3dee6e78d91a18dd2fcd137"},
"excoveralls": {:hex, :excoveralls, "0.10.1", "407d50ac8fc63dfee9175ccb4548e6c5512b5052afa63eedb9cd452a32a91495", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "afa0d55f95f050d89692bc1ce99557972ff4f5adaac812a2361014f16439d646"},
"git_cli": {:hex, :git_cli, "0.2.5", "40673235d088d2784d4e03511e62cac2ef520495cb6cc459f00b4e13aaf0fbf4", [:mix], [], "hexpm", "a28f672acc536ffe01269dffcde5e94083f6f138491d6287ac2c4e4122ee5f0a"},
"git_ops": {:hex, :git_ops, "0.3.4", "4cac61a23a30950877740e10bc985485c08107a5a48cee56421870875cf4cae7", [:mix], [{:git_cli, "~> 0.2", [hex: :git_cli, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 0.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "071f336307688b13972dc372f52dbc8a2bcbcac7e5a1852df2afd3468f17af15"},
"hackney": {:hex, :hackney, "1.14.0", "66e29e78feba52176c3a4213d42b29bdc4baff93a18cfe480f73b04677139dee", [:rebar3], [{:certifi, "2.4.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.4", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "ac2d3e5e8c93778e3b6a7ea3bd5859bfc7d7e80051fcdc1a2d99a5b561d4fd8a"},
"httpoison": {:hex, :httpoison, "0.13.0", "bfaf44d9f133a6599886720f3937a7699466d23bb0cd7a88b6ba011f53c6f562", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "4846958172d6401c4f34ecc5c2c4607b5b0d90b8eec8f6df137ca4907942ed0f"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "4bdd305eb64e18b0273864920695cb18d7a2021f31a11b9c5fbcd9a253f936e2"},
"inch_ex": {:git, "https://github.com/rrrene/inch_ex.git", "ff86f20c3ba25de3434d70a87904c1250f795400", []},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"makeup": {:hex, :makeup, "0.5.5", "9e08dfc45280c5684d771ad58159f718a7b5788596099bdfb0284597d368a882", [:mix], [{:nimble_parsec, "~> 0.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"makeup_elixir": {:hex, :makeup_elixir, "0.10.0", "0f09c2ddf352887a956d84f8f7e702111122ca32fbbc84c2f0569b8b65cbf7fa", [:mix], [{:makeup, "~> 0.5.5", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
"msgpax": {:hex, :msgpax, "2.2.1", "645c8bc89d1d73264396a825b7988926dd1707998ad5197e8b1e4dee2c4b2065", [:mix], [{:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.4.0", "ee261bb53214943679422be70f1658fff573c5d0b0a1ecd0f18738944f818efe", [:mix], [], "hexpm"},
"optimal": {:hex, :optimal, "0.3.6", "46bbf52fbbbd238cda81e02560caa84f93a53c75620f1fe19e81e4ae7b07d1dd", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"plug": {:hex, :plug, "1.7.1", "8516d565fb84a6a8b2ca722e74e2cd25ca0fc9d64f364ec9dbec09d33eb78ccd", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}], "hexpm"},
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},
"spandex": {:hex, :spandex, "2.3.0", "fcf214e57b601456369e9028482ccdb7c64ee1ed84cf89d5cc63fd83d46f2dec", [:mix], [{:optimal, "~> 0.3.3", [hex: :optimal, repo: "hexpm", optional: false]}, {:plug, ">= 1.0.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fdf843bca858203ae1de16da2ee206f53416bbda5dc8c9e78f43243de4bc3afe"},
"makeup": {:hex, :makeup, "0.5.5", "9e08dfc45280c5684d771ad58159f718a7b5788596099bdfb0284597d368a882", [:mix], [{:nimble_parsec, "~> 0.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d7152ff93f2eac07905f510dfa03397134345ba4673a00fbf7119bab98632940"},
"makeup_elixir": {:hex, :makeup_elixir, "0.10.0", "0f09c2ddf352887a956d84f8f7e702111122ca32fbbc84c2f0569b8b65cbf7fa", [:mix], [{:makeup, "~> 0.5.5", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "4a36dd2d0d5c5f98d95b3f410d7071cd661d5af310472229dd0e92161f168a44"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm", "6cbe761d6a0ca5a31a0931bf4c63204bceb64538e664a8ecf784a9a6f3b875f1"},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm", "7a4c8e1115a2732a67d7624e28cf6c9f30c66711a9e92928e745c255887ba465"},
"msgpax": {:hex, :msgpax, "2.2.1", "645c8bc89d1d73264396a825b7988926dd1707998ad5197e8b1e4dee2c4b2065", [:mix], [{:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "b1ad9f6ff1d328a8582a6fcfc438b3a75021f366e3f2029e64126acf821da0e2"},
"nimble_parsec": {:hex, :nimble_parsec, "0.4.0", "ee261bb53214943679422be70f1658fff573c5d0b0a1ecd0f18738944f818efe", [:mix], [], "hexpm", "ebb595e19456a72786db6dcd370d320350cb624f0b6203fcc7e23161d49b0ffb"},
"optimal": {:hex, :optimal, "0.3.6", "46bbf52fbbbd238cda81e02560caa84f93a53c75620f1fe19e81e4ae7b07d1dd", [:mix], [], "hexpm", "1a06ea6a653120226b35b283a1cd10039550f2c566edcdec22b29316d73640fd"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
"plug": {:hex, :plug, "1.7.1", "8516d565fb84a6a8b2ca722e74e2cd25ca0fc9d64f364ec9dbec09d33eb78ccd", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}], "hexpm", "daa5fee4209c12c3c48b05a96cf88c320b627c9575f987554dcdc1fdcdf2c15e"},
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm", "73c1682f0e414cfb5d9b95c8e8cd6ffcfdae699e3b05e1db744e58b7be857759"},
"spandex": {:hex, :spandex, "2.3.0", "fcf214e57b601456369e9028482ccdb7c64ee1ed84cf89d5cc63fd83d46f2dec", [:mix], [{:optimal, "~> 0.3.3", [hex: :optimal, repo: "hexpm", optional: false]}, {:plug, ">= 1.0.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "6d897441192484cb9de48c39f2e3d53af0357253dd20e811db17e5be942fd87e"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm", "603561dc0fd62f4f2ea9b890f4e20e1a0d388746d6e20557cafb1b16950de88c"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"},
}
104 changes: 77 additions & 27 deletions test/support/api_server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,19 @@ defmodule SpandexDatadog.ApiServerTest do
trace_id: trace_id
)

trace = %Trace{spans: [span_1, span_2]}
{:ok, span_3} =
Span.new(
id: 4_743_029_846_331_200_906,
start: 1_527_752_052_216_578_001,
completion_time: 1_527_752_052_316_578_001,
service: :bar,
env: "local",
name: "bar",
trace_id: trace_id,
tags: [analytics_event: true]
)

trace = %Trace{spans: [span_1, span_2, span_3]}

{
:ok,
Expand Down Expand Up @@ -86,36 +98,55 @@ defmodule SpandexDatadog.ApiServerTest do
"duration" => 100_000,
"error" => 0,
"meta" => %{
"env" => "local",
"foo" => "123",
"bar" => "321",
"buz" => "blitz",
"baz" => "{1, 2}",
"buz" => "blitz",
"env" => "local",
"foo" => "123",
"zyx" => "[xyz: {1, 2}]"
},
"metrics" => %{
"_sampling_priority_v1" => 1
},
"name" => "foo",
"service" => "foo",
"resource" => "foo",
"service" => "foo",
"span_id" => 4_743_028_846_331_200_906,
"start" => 1_527_752_052_216_478_000,
"trace_id" => 4_743_028_846_331_200_905,
"metrics" => %{
"_sampling_priority_v1" => 1
}
"trace_id" => 4_743_028_846_331_200_905
},
%{
"duration" => 100_000_000,
"error" => 0,
"meta" => %{"env" => "local"},
"meta" => %{
"env" => "local"
},
"metrics" => %{
"_sampling_priority_v1" => 1
},
"name" => "bar",
"service" => "bar",
"resource" => "bar",
"service" => "bar",
"span_id" => 4_743_029_846_331_200_906,
"start" => 1_527_752_052_216_578_001,
"trace_id" => 4_743_028_846_331_200_905,
"trace_id" => 4_743_028_846_331_200_905
},
%{
"duration" => 100_000_000,
"error" => 0,
"meta" => %{
"env" => "local"
},
"metrics" => %{
"_dd1.sr.eausr" => 1,
"_sampling_priority_v1" => 1
}
},
"name" => "bar",
"resource" => "bar",
"service" => "bar",
"span_id" => 4_743_029_846_331_200_906,
"start" => 1_527_752_052_216_578_001,
"trace_id" => 4_743_028_846_331_200_905
}
]

Expand All @@ -124,7 +155,7 @@ defmodule SpandexDatadog.ApiServerTest do
{"X-Datadog-Trace-Count", 1}
]

assert_received {:put_datadog_spans, ^formatted, ^url, ^headers}
assert_received {:put_datadog_spans, ^formatted, url, headers}
end

test "doesn't care about the response result", %{trace: trace, state: state, url: url} do
Expand All @@ -140,7 +171,7 @@ defmodule SpandexDatadog.ApiServerTest do
|> String.split("\n")
|> Enum.reject(fn s -> s == "" end)

assert processing =~ ~r/Sending 1 traces, 2 spans/
assert processing =~ ~r/Sending 1 traces, 3 spans/

assert received_spans =~ ~r/Trace: \[%Spandex.Trace{/

Expand All @@ -149,36 +180,55 @@ defmodule SpandexDatadog.ApiServerTest do
"duration" => 100_000,
"error" => 0,
"meta" => %{
"env" => "local",
"foo" => "123",
"bar" => "321",
"buz" => "blitz",
"baz" => "{1, 2}",
"buz" => "blitz",
"env" => "local",
"foo" => "123",
"zyx" => "[xyz: {1, 2}]"
},
"metrics" => %{
"_sampling_priority_v1" => 1
},
"name" => "foo",
"service" => "foo",
"resource" => "foo",
"service" => "foo",
"span_id" => 4_743_028_846_331_200_906,
"start" => 1_527_752_052_216_478_000,
"trace_id" => 4_743_028_846_331_200_905,
"metrics" => %{
"_sampling_priority_v1" => 1
}
"trace_id" => 4_743_028_846_331_200_905
},
%{
"duration" => 100_000_000,
"error" => 0,
"meta" => %{"env" => "local"},
"meta" => %{
"env" => "local"
},
"metrics" => %{
"_sampling_priority_v1" => 1
},
"name" => "bar",
"service" => "bar",
"resource" => "bar",
"service" => "bar",
"span_id" => 4_743_029_846_331_200_906,
"start" => 1_527_752_052_216_578_001,
"trace_id" => 4_743_028_846_331_200_905,
"trace_id" => 4_743_028_846_331_200_905
},
%{
"duration" => 100_000_000,
"error" => 0,
"meta" => %{
"env" => "local"
},
"metrics" => %{
"_dd1.sr.eausr" => 1,
"_sampling_priority_v1" => 1
}
},
"name" => "bar",
"resource" => "bar",
"service" => "bar",
"span_id" => 4_743_029_846_331_200_906,
"start" => 1_527_752_052_216_578_001,
"trace_id" => 4_743_028_846_331_200_905
}
]

Expand Down