error-tracker icon indicating copy to clipboard operation
error-tracker copied to clipboard

Detect & format GQL queries / mutations

Open tlvenn opened this issue 1 year ago • 1 comments

Would be great to detect GraphQL queries / mutations and format them along with their variables.

tlvenn avatar Aug 15 '24 07:08 tlvenn

ErrorTracker uses Telemetry to integrate with third-party packages such as Phoenix, Phoenix LiveView and Oban.

If Absinthe provides Telemetry events it should be theoretically possible to attach to those events, extract useful information and include it in the error context.

crbelaus avatar Aug 15 '24 07:08 crbelaus

I've been investigating this and I found out that the Absinthe telemetry events include the query and variables in the same format as we present them in the ErrorTracker.

# [(phoenix_graphql 0.1.0) lib/phoenix_graphql/telemetry.ex:25: PhoenixGraphql.Telemetry.handle_event/4]
metadata #=> %{
  id: -576460752303422751,
  options: [
    event: [:execute, :operation, :start],
    validation_result_phase: Absinthe.Phase.Document.Validation.Result,
    result_phase: Absinthe.Phase.Document.Result,
    jump_phases: true,
    schema: PhoenixGraphqlWeb.Schema,
    context: %{
      pubsub: PhoenixGraphqlWeb.Endpoint,
      __absinthe_plug__: %{uploads: %{}}
    },
    params: %{
      "query" => "query ($id: ID!){\n  user(id: $id) {\n    name\n    email\n    posts {\n      title\n      body\n    }\n  }\n}",
      "variables" => %{"id" => 1}
    },
    variables: %{"id" => 1},
    adapter: Absinthe.Adapter.LanguageConventions,
    pipeline: [],
    document: "query ($id: ID!){\n  user(id: $id) {\n    name\n    email\n    posts {\n      title\n      body\n    }\n  }\n}",
    document_provider: {Absinthe.Plug.DocumentProvider.Default, []},
    root_value: %{},
    document_provider_key: nil,
    operation_name: nil
  ],
  telemetry_span_context: -576460752303422751
}

Since the query and variables are already recorded using the regular Plug and Phoenix integration we won't make any changes to the current behaviour.

image

If you want to include additional information in the error context you can attach to the Absinthe telemetry events and use ErrorTracker.set_context/1 from there.

crbelaus avatar Nov 13 '24 15:11 crbelaus