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 1 commit
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
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})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be

Suggested change
|> put_if(true, {:"graphql.event.type", config.type})
|> List.insert_at(0, {:"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
Loading