Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
a code example that enables custom GraphQL tracing on NewRelic
class CustomGraphqlNewrelicTracing < GraphQL::Tracing::NewRelicTracing
# We need to create a singleton method here that inherit from the original/parent class.
self.platform_keys = GraphQL::Tracing::NewRelicTracing.platform_keys
# This is the only method we need to overwrite.
def platform_trace(platform_key, key, data)
if key == 'execute_query'
operation = data[:query].selected_operation
# Possible type: 'query', 'mutation', 'subscription'
type = operation.operation_type
# Differentiating GraphQL routes by the root field(s) name
field_key = operation.children&.map(&:name)&.join('.')
# (optional)
# - To log the the full query string, we can leverage the
# `add_custom_attributes` provided by NewRelic Ruby agent.
# - Below is a pre-processing of the original query string, in order
# to make sure the size is within the limit of 255 bytes.
# (see
# custom-data/insights-custom-data-requirements-limits).
query_str = operation.to_query_string.gsub(/[ \n]*/, '')
::NewRelic::Agent.add_custom_attributes({ query: query_str })
NewRelic::Agent::MethodTracerHelpers.trace_execution_scoped(platform_key) do
require 'path/to/custom_graphql_newrelic_tracing'
class RootSchema < GraphQL::Schema
# ... (omitting unrelated parts)
# Using the custom NewRelic tracing plugin to trace transactions on NewRelic with more granularity.
use CustomGraphqlNewrelicTracing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment