Skip to content

Instantly share code, notes, and snippets.

@jenny-codes
Last active February 21, 2020 13:20
Show Gist options
  • Save jenny-codes/31a8d48df545489621bfdf5af1a02778 to your computer and use it in GitHub Desktop.
Save jenny-codes/31a8d48df545489621bfdf5af1a02778 to your computer and use it in GitHub Desktop.
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 https://docs.newrelic.com/docs/insights/insights-data-sources/
# custom-data/insights-custom-data-requirements-limits).
query_str = operation.to_query_string.gsub(/[ \n]*/, '')
::NewRelic::Agent.set_transaction_name("GraphQL/#{type}/#{field_key}")
::NewRelic::Agent.add_custom_attributes({ query: query_str })
end
NewRelic::Agent::MethodTracerHelpers.trace_execution_scoped(platform_key) do
yield
end
end
end
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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment