This is the Javascript/TypeScript SDK for GraphMetrics.
- Apollo:
yarn add @graphmetrics/apollo
/npm install @graphmetrics/apollo --save
We provide middlewares that are easily to plug in your server. If your server is not currently supported, please open an issue so we can fix that.
import { GraphMetrics, logging } from '@graphmetrics/apollo';
import { createContext, Context } from './context';
const metrics = GraphMetrics<Context>({
// SEE CONFIGURATION SECTION FOR DETAILS
apiKey: 'some_key',
serverVersion: '0.1.0',
logger: logging.fromWinston(logger),
// clientExtractor: (ctx) => { ... }
});
const apollo = new ApolloServer({
...
context: createContext,
plugins: [metrics],
});
By default Apollo Server handles signals to properly shutdown, but we found that it is not well implemented for versions before 2.21.2
. To avoid losing the last datapoint when doing a server rollout, we highly suggest to do the following:
const apollo = new ApolloServer({
...
stopOnTerminationSignals: false,
});
const signals: NodeJS.Signals[] = ['SIGINT', 'SIGTERM'];
signals.forEach((signal) => {
const handler: NodeJS.SignalsListener = async () => {
await apollo.stop();
process.exit(0);
};
process.on(signal, handler);
});
The SDK needs a few elements to be properly configured.
apiKey
: Environment api keyserverVersion
: (Optional) Version of the server (catch regressions between releases)clientExtractor
: (Optional) Function that retrieves the client details from the context (differentiate queries coming from different clients)logger
: (Optional) Structured logger,console.log
is used if not provided. Adapters are provided for popular loggers inlogging
.
- Client extractor fetches the client details from the context
- Override the function to set a custom name and version
- Default behaviour differs per integration:
Apollo
: Uses the Apollo client default headersOthers
: No details are fetched
Please let us know if you would like to see other clients supported by default
Jest
: Since we use asetInterval
internally, jest will complain that tests don't finish cleanly. We suggest not instantiating the plugin during tests.