Skip to content

Commit

Permalink
Add subscriptions event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
aej committed Apr 10, 2024
1 parent dc6ec99 commit 47fc87b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/instrumentation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,36 @@ defmodule OpentelemetryAbsinthe.Instrumentation do
{__MODULE__, :operation_start},
[:absinthe, :execute, :operation, :start],
&__MODULE__.handle_operation_start/4,
config
Map.put(config, :type, :operation)
)

:telemetry.attach(
{__MODULE__, :operation_stop},
[:absinthe, :execute, :operation, :stop],
&__MODULE__.handle_operation_stop/4,
config
Map.put(config, :type, :operation)
)

:telemetry.attach(
{__MODULE__, :publish_start},
[:absinthe, :subscription, :publish, :start],
&__MODULE__.handle_operation_start/4,
Map.put(config, :type, :publish)
)

:telemetry.attach(
{__MODULE__, :publish_stop},
[:absinthe, :subscription, :publish, :stop],
&__MODULE__.handle_operation_stop/4,
Map.put(config, :type, :publish)
)
end

def teardown do
:telemetry.detach({__MODULE__, :operation_start})
:telemetry.detach({__MODULE__, :operation_stop})
:telemetry.detach({__MODULE__, :publish_start})
:telemetry.detach({__MODULE__, :publish_stop})
end

def handle_operation_start(_event_name, _measurements, metadata, config) do
Expand All @@ -99,6 +115,7 @@ defmodule OpentelemetryAbsinthe.Instrumentation do
{:"graphql.request.variables", Jason.encode!(variables)}
)
|> put_if(config.trace_request_query, {@graphql_document, document})
|> put_if(true, {:"graphql.event.type", config.type})

save_parent_ctx()

Expand Down Expand Up @@ -129,6 +146,7 @@ defmodule OpentelemetryAbsinthe.Instrumentation do
config.trace_request_selections,
fn -> {:"graphql.request.selections", data |> get_graphql_selections() |> Jason.encode!()} end
)
|> put_if(true, {:"graphql.event.type", config.type})
|> Tracer.set_attributes()

:telemetry.execute(
Expand Down
4 changes: 4 additions & 0 deletions test/configuration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule OpentelemetryAbsintheTest.Configuration do
@graphql_operation_name :"graphql.operation.name"
@graphql_operation_type :"graphql.operation.type"
@graphql_request_selections :"graphql.request.selections"
@graphql_event_type :"graphql.event.type"

doctest OpentelemetryAbsinthe.Instrumentation

Expand All @@ -19,6 +20,7 @@ defmodule OpentelemetryAbsintheTest.Configuration do

assert [
@graphql_document,
@graphql_event_type,
@graphql_operation_name,
@graphql_operation_type,
@graphql_request_selections
Expand All @@ -35,6 +37,7 @@ defmodule OpentelemetryAbsintheTest.Configuration do
attributes = Query.query_for_attrs(Queries.query(), variables: %{"isbn" => "A1"})

assert [
@graphql_event_type,
@graphql_operation_name,
@graphql_operation_type,
@graphql_request_selections,
Expand All @@ -53,6 +56,7 @@ defmodule OpentelemetryAbsintheTest.Configuration do

assert [
@graphql_document,
@graphql_event_type,
@graphql_operation_name,
@graphql_operation_type
] = attributes |> Map.keys() |> Enum.sort()
Expand Down
1 change: 1 addition & 0 deletions test/instrumentation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule OpentelemetryAbsintheTest.Instrumentation do

@trace_attributes [
:"graphql.document",
:"graphql.event.type",
:"graphql.operation.name",
:"graphql.operation.type",
:"graphql.request.selections",
Expand Down

0 comments on commit 47fc87b

Please sign in to comment.