# README
Instana instrumentation for graphql
This module contains instrumentation code for the graphql API
.
The instrumentation is compatible with graphql v0.8.0
. If you need support for newer versions, file an issue on this repository.
Installation
To add the module to your go.mod
file run the following command in your project directory:
$ go get github.com/instana/go-sensor/instrumentation/instagraphql
Usage
A complete working example can be found here.
instagraphql
offers the following method wrappers able to collect GraphQL query data:
instagraphql.Do
usage example
// create an instance of the Instana collector
collector := instana.InitCollector(&instana.Options{
Service: "graphql-app",
})
// setup GraphQL normally
...
schema, err := graphql.NewSchema(schemaConfig)
...
// Create a graphql.Params instance to prepare the query to be processed
query := `query myQuery {
myfield
}`
params := graphql.Params{Schema: schema, RequestString: query}
// Call instagraphql.Do instead of the original graphql.Do.
// Make sure to provide a valid context (usually an HTTP req.Context()) if any.
r := instagraphql.Do(context.Background(), sensor, params)
fmt.Println("do something with the result", r)
instagraphql.Subscribe
usage example
// create an instance of the Instana collector
collector := instana.InitCollector(&instana.Options{
Service: "graphql-app",
})
...
go func() {
ctx := context.Background()
subscribeParams := graphql.Params{
Context: ctx,
RequestString: mySubscriptionQuery,
Schema: schema,
}
instagraphql.Subscribe(ctx, sensor, subscribeParams)
}()
instagraphql.ResultCallbackFn
usage example
The instagraphql.ResultCallbackFn
depends on the handler.ResultCallbackFn function from the handler API, which provides a convenient HTTP handler, as well as a GraphQL playground and more.
// create an instance of the Instana collector
collector := instana.InitCollector(&instana.Options{
Service: "graphql-app",
})
h := handler.New(&handler.Config{
Schema: &schema,
Pretty: true,
GraphiQL: false,
Playground: true,
// The second argument is your previous provided callback function, or nil if you had none.
ResultCallbackFn: instagraphql.ResultCallbackFn(collector, nil),
})
http.Handle("/graphql", h)
if err := http.ListenAndServe("0.0.0.0:9191", nil); err != nil {
log.Fatal(err)
}
See the instagraphql
package documentation for detailed examples.
# Functions
Do wraps the original graphql.Do, traces the GraphQL query and returns the result of the original graphql.Do.
ResultCallbackFn traces the GraphQL query and executes the original handler.ResultCallbackFn if fn is provided.
Subscribe wraps the original graphql.Subscribe, traces the GraphQL query and returns the result of the original graphql.Subscribe.
# Constants
Version is the instrumentation module semantic version.
# Structs
ExpiringMap holds a map of spans that are automatically removed from this map after the provided duration expires.