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

Attach to subscription publish events #145

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion .github/CODEOWNERS

This file was deleted.

37 changes: 26 additions & 11 deletions lib/instrumentation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ defmodule OpentelemetryAbsinthe.Instrumentation do
trace_request_selections: true,
trace_response_result: false,
trace_response_errors: false,
trace_subscriptions: false
trace_subscriptions: false,
status_function: nil
]

def setup(instrumentation_opts \\ []) do
Expand Down Expand Up @@ -137,7 +138,7 @@ defmodule OpentelemetryAbsinthe.Instrumentation do
|> Tracer.update_name()

errors = data.blueprint.result[:errors]
status = status(errors)
status = status(errors, data, config)
set_status(status)

[]
Expand Down Expand Up @@ -174,12 +175,15 @@ defmodule OpentelemetryAbsinthe.Instrumentation do
end

defp get_graphql_selections(%{blueprint: %Blueprint{} = blueprint}) do
blueprint
|> Blueprint.current_operation()
|> Kernel.||(%{})
|> Map.get(:selections, [])
|> Enum.map(& &1.name)
|> Enum.uniq()
case Blueprint.current_operation(blueprint) do
%{selections: [_ | _] = selections} ->
selections
|> Enum.map(&Map.fetch!(&1, :name))
|> Enum.uniq()

_ ->
[]
end
end

def default_config do
Expand Down Expand Up @@ -220,9 +224,20 @@ defmodule OpentelemetryAbsinthe.Instrumentation do
Tracer.set_current_span(ctx)
end

defp status(nil), do: :ok
defp status([]), do: :ok
defp status(_error), do: :error
defp status(_errors, data, %{status_function: {module, function}}) do
case apply(module, function, [data]) do
expected when expected in [:ok, :error] ->
expected

other ->
Logger.error("Unexpected value returned by status function #{inspect(other)}")
:error
end
end

defp status(nil, _, _), do: :ok
defp status([], _, _), do: :ok
defp status(_error, _, _), do: :error

defp set_status(:ok), do: :ok
defp set_status(:error), do: Tracer.set_status(OpenTelemetry.status(:error, ""))
Expand Down
2 changes: 2 additions & 0 deletions lib/opentelemetry_absinthe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ defmodule OpentelemetryAbsinthe do
* `trace_response_result`(default: #{Keyword.fetch!(@config, :trace_response_result)}): attaches the result returned by the server as an attribute
* `trace_response_errors`(default: #{Keyword.fetch!(@config, :trace_response_errors)}): attaches the errors returned by the server as an attribute
* `trace_subscriptions`(default: #{Keyword.fetch!(@config, :trace_subscriptions)}): attaches to `[:absinthe, :subscription, :publish]` (`:start` and `:stop`)
* `status_function`(default: #{Keyword.fetch!(@config, :status_function)}): a `{module, function}` tuple for a function which will be executed to infer the status
of the trace. This function __must__ return `:ok | :error`

## Telemetry

Expand Down
37 changes: 37 additions & 0 deletions test/instrumentation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,43 @@ defmodule OpentelemetryAbsintheTest.InstrumentationTest do
10
end

test "status function works", ctx do
defmodule Helper do
def status_function(_) do
:error
end
end

config = %{ctx.config | status_function: {Helper, :status_function}}

assert :ok =
:telemetry.attach(
ctx.test,
[:opentelemetry_absinthe, :graphql, :handled],
fn _telemetry_event, _measurements, metadata, _config ->
send(self(), metadata)
end,
nil
)

assert :ok =
Instrumentation.handle_stop(
"Test",
%{},
%{blueprint: BlueprintArchitect.blueprint(schema: __MODULE__)},
config
)

assert_receive %{
operation_name: "TestOperation",
operation_type: :query,
schema: __MODULE__,
errors: nil,
status: :error
},
10
end

test "standard values are returned alongside the metadata from context", ctx do
context = TelemetryMetadata.update_context(%{}, %{source: "TestSource", user_agent: "Insomnia"})

Expand Down