Package: ddtrace-graphql
Python library to trace graphql calls with Datadog.graphql-coreDatadog APM (Tracing)Datadog Trace ClientCompatibilityddtrace-graphqlis tested with:Python versions: 3.5, 3.6, nightlygraphql-core: 2.0, 1.1.0, latestddtrace: 0.11.1, 0.10.1, latestScreenshots for pyramid app serving GraphQL with tracing enabled:GraphQL service detail.GraphQL query detail.InstallationUsing pip$pipinstallddtrace-graphqlFrom source$gitclonehttps://github.com/beezz/ddtrace-graphql.git$cdddtrace-graphql&&pythonsetup.pyinstallUsageTo trace all GraphQL requests patch the library. Put this snippet to your
application main entry point.__import__('ddtrace_graphql').patch()# ORfromddtrace_graphqlimportpatchpatch()Check out thedatadog trace clientfor all supported libraries and frameworks.NoteFor the patching to work properly,patchneeds to be called
before any other imports of thegraphqlfunction.# app/__init__.py__import__('ddtrace_graphql').patch()# from that point all calls to graphql are tracedfromgraphqlimportgraphqlresult=graphql(schema,query)Trace only certain calls withtraced_graphqlfunctionfromddtrace_graphqlimporttraced_graphqltraced_graphql(schema,query)ConfigurationEnvironment variablesDDTRACE_GRAPHQL_SERVICE:Define service name under which traces are shown in Datadog. Default value isgraphql$exportDDTRACE_GRAPHQL_SERVICE=foobar.graphqlspan_kwargsDefault arguments passed to the tracing context manager can be updated usingspan_kwargsargument ofddtrace_graphql.patchorddtrace_graphql.traced_graphqlfunctions.Default values:name:Wrapped resource name. Defaultgraphql.graphql.span_type:Span type. Defaultgraphql.service:Service name. Defaults toDDTRACE_GRAPHQL_SERVICEenvironment variable if present, elsegraphql.resource:Processed resource. Defaults to query / mutation signature.For more information visitddtrace.Tracer.tracedocumentation.fromddtrace_graphqlimportpatchpatch(span_kwargs=dict(service='foo.graphql'))fromddtrace_graphqlimporttraced_graphqltraced_graphql(schema,query,span_kwargs=dict(resource='bar.resource'))span_callbackIn case you want to postprocess trace span you may usespan_callbackargument.span_callbackmust be function with signaturedef callback(result=result, span=span)whereresultis graphql execution result orNonein case of fatal error and span is trace span object
(ddtrace.span.Span).What is it good for? Unfortunately one cannot filter/alarm on span metrics resp.
meta information even if those are numeric (why Datadog?) so you can use it to
send metrics based on span, result attributes.fromdatadogimportstatsdfromddtrace_graphqlimportpatch,CLIENT_ERROR,INVALIDdefcallback(result,span):tags=['resource:{}'.format(span.resource.replace(' ','_'))]statsd.increment('{}.request'.format(span.service),tags=tags)ifspan.error:statsd.increment('{}.error'.format(span.service),tags=tags)elifspan.get_metric(CLIENT_ERROR):statsd.increment('{}.{}'.format(span.service,CLIENT_ERROR),tags=tags)ifspan.get_metric(INVALID):statsd.increment('{}.{}'.format(span.service,INVALID),tags=tags)patch(span_callback=callback)ignore_exceptionsSome frameworks use exceptions to handle 404s etc. you may want to ignore some
exceptions resp. not consider them server error. To do this you can supplyignore_exceptionsargument as list of exception classes to ignore.ignore_exceptionswill be used in python’sisinstancethus you can ignore
also using base classes.fromddtrace_graphqlimportpatchpatch(ignore_exceptions=(ObjectNotFound,PermissionsDenied))fromddtrace_graphqlimporttraced_graphqltraced_graphql(schema,query,ignore_exceptions=(ObjectNotFound,PermissionsDenied))DevelopmentInstall from source in development mode$gitclonehttps://github.com/beezz/ddtrace-graphql.git$pipinstall--editableddtrace-graphql[test]Run tests$cdddtrace-graphql$tox