-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d84ad55
commit 7625f6b
Showing
23 changed files
with
617 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"], | ||
import_deps: [:absinthe, :plug] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# The directory Mix will write compiled artifacts to. | ||
/_build/ | ||
|
||
# If you run "mix test --cover", coverage assets end up here. | ||
/cover/ | ||
|
||
# The directory Mix downloads your dependencies sources to. | ||
/deps/ | ||
|
||
# Where third-party dependencies like ExDoc output generated docs. | ||
/doc/ | ||
|
||
# Ignore .fetch files in case you like to edit your project deps locally. | ||
/.fetch | ||
|
||
# If the VM crashes, it generates a dump, let's ignore it too. | ||
erl_crash.dump | ||
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
|
||
# Ignore package tarball (built via "mix hex.build"). | ||
absinthe_example-*.tar | ||
|
||
|
||
# Temporary files for e.g. tests | ||
/tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Absinthe | ||
|
||
An example of an `Absinthe` GraphQL API instrumented by the New Relic Agent via `telemetry`. | ||
|
||
This instrumentation is fully automatic. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import Config | ||
|
||
config :absinthe_example, | ||
http_port: 4006 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule AbsintheExample.Application do | ||
@moduledoc false | ||
use Application | ||
|
||
def start(_type, _args) do | ||
http_port = Application.get_env(:absinthe_example, :http_port) | ||
|
||
children = [ | ||
Plug.Cowboy.child_spec( | ||
scheme: :http, | ||
plug: AbsintheExample.Router, | ||
options: [port: http_port] | ||
) | ||
] | ||
|
||
opts = [strategy: :one_for_one, name: AbsintheExample.Supervisor] | ||
Supervisor.start_link(children, opts) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule AbsintheExample.Resolvers do | ||
use NewRelic.Tracer | ||
|
||
def echo(_source, %{this: this}, _res) do | ||
{:ok, do_echo(this)} | ||
end | ||
|
||
@trace :do_echo | ||
defp do_echo(this), do: this | ||
|
||
def one(_source, _args, _res) do | ||
Process.sleep(1) | ||
{:ok, %{two: %{}}} | ||
end | ||
|
||
def three(_source, _args, _res) do | ||
Process.sleep(2) | ||
{:ok, do_three()} | ||
end | ||
|
||
@trace :do_three | ||
def do_three() do | ||
Process.sleep(2) | ||
3 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule AbsintheExample.Router do | ||
use Plug.Builder | ||
use Plug.ErrorHandler | ||
|
||
plug Plug.Parsers, | ||
parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser], | ||
pass: ["*/*"], | ||
json_decoder: Jason | ||
|
||
plug Absinthe.Plug, schema: AbsintheExample.Schema | ||
|
||
def handle_errors(conn, error) do | ||
send_resp(conn, conn.status, "Something went wrong: #{inspect(error)}") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule AbsintheExample.Schema do | ||
use Absinthe.Schema | ||
|
||
query do | ||
field :echo, :string do | ||
arg :this, :string | ||
resolve &AbsintheExample.Resolvers.echo/3 | ||
end | ||
|
||
field :one, :one_thing do | ||
resolve &AbsintheExample.Resolvers.one/3 | ||
end | ||
end | ||
|
||
object :one_thing do | ||
field :two, :two_thing | ||
end | ||
|
||
object :two_thing do | ||
field :three, :integer do | ||
resolve &AbsintheExample.Resolvers.three/3 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule AbsintheExample.MixProject do | ||
use Mix.Project | ||
|
||
def project do | ||
[ | ||
app: :absinthe_example, | ||
version: "0.1.0", | ||
build_path: "../../_build", | ||
config_path: "../../config/config.exs", | ||
deps_path: "../../deps", | ||
lockfile: "../../mix.lock", | ||
elixir: "~> 1.9", | ||
start_permanent: Mix.env() == :prod, | ||
deps: deps() | ||
] | ||
end | ||
|
||
def application do | ||
[ | ||
extra_applications: [:logger], | ||
mod: {AbsintheExample.Application, []} | ||
] | ||
end | ||
|
||
defp deps do | ||
[ | ||
{:new_relic_agent, path: "../../../"}, | ||
{:test_support, in_umbrella: true}, | ||
{:absinthe, "~> 1.6"}, | ||
{:absinthe_plug, "~> 1.5"}, | ||
{:plug_cowboy, "~> 2.0"} | ||
] | ||
end | ||
end |
95 changes: 95 additions & 0 deletions
95
examples/apps/absinthe_example/test/absinthe_example_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
defmodule AbsintheExampleTest do | ||
use ExUnit.Case | ||
|
||
alias NewRelic.Harvest.TelemetrySdk | ||
alias NewRelic.Harvest.Collector | ||
|
||
setup_all context, do: TestSupport.simulate_agent_run(context, trace_mode: :infinite) | ||
setup_all context, do: TestSupport.simulate_agent_enabled(context) | ||
|
||
test "Absinthe instrumentation" do | ||
TestSupport.restart_harvest_cycle(Collector.Metric.HarvestCycle) | ||
TestSupport.restart_harvest_cycle(TelemetrySdk.Spans.HarvestCycle) | ||
|
||
{:ok, %{body: _body}} = request("query TestQuery { one { two { three } } }") | ||
|
||
metrics = TestSupport.gather_harvest(Collector.Metric.Harvester) | ||
|
||
assert TestSupport.find_metric(metrics, "WebTransaction") | ||
|
||
assert TestSupport.find_metric( | ||
metrics, | ||
"WebTransactionTotalTime/Absinthe/AbsintheExample.Schema/query/one.two.three" | ||
) | ||
|
||
[%{spans: spans}] = TestSupport.gather_harvest(TelemetrySdk.Spans.Harvester) | ||
|
||
spansaction = | ||
Enum.find(spans, fn %{attributes: attr} -> | ||
attr[:name] == "Absinthe/AbsintheExample.Schema/query/one.two.three" | ||
end) | ||
|
||
tx_root_process = | ||
Enum.find(spans, fn %{attributes: attr} -> | ||
attr[:name] == "Transaction Root Process" | ||
end) | ||
|
||
process = | ||
Enum.find(spans, fn %{attributes: attr} -> | ||
attr[:name] == "Process" | ||
end) | ||
|
||
operation = | ||
Enum.find(spans, fn %{attributes: attr} -> | ||
attr[:name] == "query:TestQuery" | ||
end) | ||
|
||
one_resolver = | ||
Enum.find(spans, fn %{attributes: attr} -> | ||
attr[:name] == "&AbsintheExample.Resolvers.one/3" | ||
end) | ||
|
||
three_resolver = | ||
Enum.find(spans, fn %{attributes: attr} -> | ||
attr[:name] == "&AbsintheExample.Resolvers.three/3" | ||
end) | ||
|
||
do_three_fn_trace = | ||
Enum.find(spans, fn %{attributes: attr} -> | ||
attr[:name] == "AbsintheExample.Resolvers.do_three/0" | ||
end) | ||
|
||
assert operation.attributes[:"absinthe.operation.name"] == "TestQuery" | ||
assert operation.attributes[:"absinthe.operation.type"] == "query" | ||
|
||
assert spansaction.attributes[:"absinthe.operation.name"] == "TestQuery" | ||
assert spansaction.attributes[:"absinthe.operation.type"] == "query" | ||
|
||
assert one_resolver.attributes[:"absinthe.field.path"] == "one" | ||
assert three_resolver.attributes[:"absinthe.field.path"] == "one.two.three" | ||
|
||
assert one_resolver.attributes[:"parent.id"] == operation.id | ||
assert three_resolver.attributes[:"parent.id"] == operation.id | ||
assert do_three_fn_trace.attributes[:"parent.id"] == three_resolver.id | ||
assert operation.attributes[:"parent.id"] == process.id | ||
assert process.attributes[:"parent.id"] == tx_root_process.id | ||
assert tx_root_process.attributes[:"parent.id"] == spansaction.id | ||
assert spansaction.attributes[:"nr.entryPoint"] == true | ||
|
||
Enum.each(spans, fn span -> | ||
assert span[:"trace.id"] == spansaction[:"trace.id"] | ||
assert span.attributes[:transactionId] == spansaction.attributes[:transactionId] | ||
end) | ||
end | ||
|
||
defp request(query) do | ||
http_port = Application.get_env(:absinthe_example, :http_port) | ||
body = Jason.encode!(%{query: query}) | ||
request = {'http://localhost:#{http_port}/graphql', [], 'application/json', body} | ||
|
||
with {:ok, {{_, status_code, _}, _headers, body}} <- | ||
:httpc.request(:post, request, [], []) do | ||
{:ok, %{status_code: status_code, body: to_string(body)}} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ExUnit.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.