-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure correct run context for 'graphql' instrumentation
Refs: #2430
- Loading branch information
Showing
3 changed files
with
72 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// A small example showing Elastic APM tracing of a script using `graphql`. | ||
// Adapted from https://graphql.org/graphql-js/#writing-code | ||
|
||
const apm = require('../').start({ // elastic-apm-node | ||
serviceName: 'example-trace-graphql' | ||
}) | ||
var { graphql, buildSchema } = require('graphql') | ||
|
||
// Construct a schema, using GraphQL schema language | ||
var schema = buildSchema(` | ||
type Query { | ||
hello: String | ||
bye: String | ||
} | ||
`) | ||
|
||
// The root provides a resolver function for each API endpoint | ||
var root = { | ||
hello: () => { | ||
return 'Hello world!' | ||
}, | ||
bye: () => { | ||
return 'Farewell!' | ||
} | ||
} | ||
|
||
// For tracing spans to be created, there must be an active transaction. | ||
// Typically, a transaction is automatically started for incoming HTTP | ||
// requests to a Node.js server. However, because this script is not running | ||
// an HTTP server, we manually start a transaction. More details at: | ||
// https://www.elastic.co/guide/en/apm/agent/nodejs/current/custom-transactions.html | ||
const t1 = apm.startTransaction('t1') | ||
|
||
// Run the GraphQL query '{ hello }' and print out the response | ||
graphql(schema, '{ hello }', root).then((response) => { | ||
console.log('hello response:', response) | ||
}) | ||
graphql(schema, '{ bye }', root).then((response) => { | ||
console.log('bye response:', response) | ||
t1.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters