Metrics Logging Plugins

The Request Logging API allows you to identify your cacheable traffic using plugins or through a manual fetch call. If you use Apollo Server, GraphQL Yoga, GraphQL Mesh, or GraphQL Envelop, we have plugins for metrics logging. If not, you can copy our boilerplate fetch call example and customize it. For information about using our example, read the section on plugin integration in Get Started with Metrics. You can also construct your own call. To use this option, read Build a Metrics Logging Call .

Using the Request Logging API allows you to securely collect data that enables Stellate to identify cacheable requests and their potential bandwidth and request savings.

Note: We are also interested in finding out if you have a particular plugin that you would like to see for GraphQL metrics.

An image presenting table listing caching opportunities in Stellate analytics dashboard

Before you begin

Before you start, read the About GraphQL Metrics and Get Started with GraphQL Metrics topics for preliminary instructions on setting up metrics logging requests through the UI. The following sections provide additional plugins that you can use and details about manually constructing your own fetch call for collecting and sending metrics.

Logging Requests to GraphQL Metrics

You can easily use Stellate GraphQL Metrics without proxying all your requests through Stellate. To achieve that you can push data about your GraphQL requests to our logging endpoint directly from your origin server. Stellate offers two options for collecting GraphQL Metrics:

  • Plugins for multiple servers as described in the following sections.
  • Manual requests to the logging API. Stellate provides two options for collecting GraphQL metrics, a convenient fetch call that you can copy and paste into your GraphQL environment or you can use the information in this topic to construct your own call.

The easiest method to get GraphQL metrics is by using one of the Stellate metrics plugins. Let’s look at those f.

Types of Plugins

All of the following metrics plugins take a fetch function as an argument. This enables the use of these plugins in any JavaScript environment. If you are running NodeJS, you can use the node-fetch package and in an edge environment (such as Cloudflare Workers) pass the global fetch function.

Plugin for apollo-server

Install @apollo/server and stellate:

$ npm install @apollo/server stellate

Include it in your plugins file:

import { createStellateLoggerPlugin } from 'stellate/apollo-server'
import { startStandaloneServer } from '@apollo/server/standalone'
import { ApolloServer } from '@apollo/server'

const stellatePlugin = createStellateLoggerPlugin({
  serviceName: '${serviceName}',
  token: '${loggingToken}',
  // a fetch compliant function, can be from `node-fetch` if you are running in NodeJS,
  // or the global fetch function if running in a v8 environment (e.g. Cloudflare Workers)
  fetch: fetch,
})
const server = new ApolloServer({
  typeDefs,
  resolvers,
  plugins: [stellatePlugin],
})
const { url } = await startStandaloneServer(server, { listen: { port: 4000 } })

Plugin for graphql-yoga

Install @graphql-yoga and stellate:

$ npm install @graphql-yoga stellate

Include it in your plugins file:

import { createStellateLoggerPlugin } from "stellate/graphql-yoga";
import { createServer } from "node:http";
import { createYoga, createSchema } from "graphql-yoga";

const stellatePlugin = createStellateLoggerPlugin({
  serviceName: "${serviceName}",
  token: "${loggingToken}",
  // a fetch compliant function, can be from `node-fetch` if you are running in NodeJS,
  // or the global fetch function if running in a v8 environment (e.g. Cloudflare Workers)
  fetch: fetch,
});

const yoga = createYoga({ schema, plugins: [stellatePlugin] });
const server = createServer(yoga);

server.listen(4000);

Plugin for GraphQL Mesh

Install @envelop/core and stellate:

$ npm install @envelop/core stellate

Include it in your plugins file:

// envelopPlugins.ts
import { createStellateLoggerPlugin } from "stellate/envelop";
import { MeshPlugin } from "@graphql-mesh/types";

const plugins: MeshPlugin = [
  createStellateLoggerPlugin({
    serviceName: "${serviceName}",
    token: "${loggingToken}",
    // a fetch compliant function, can be from `node-fetch` if you are running in NodeJS,
    // or the global fetch function if running in a v8 environment (e.g. Cloudflare Workers)
    fetch: fetch,
  }),
];

// reference this file using 'additionalEnvelopPlugins' in your .meshrc.yaml config fileexport default plugins

Plugin for GraphQL Envelop

You can use this plugin with any envelop compatible plugin system, such as GraphQL Mesh, and others. Start by installing @envelop/core and stellate:

$ npm install @envelop/core stellate

Then add it to your envelop plugins:

import { createStellateLoggerPlugin } from "stellate/envelop";
import { envelop } from "@envelop/core";

export const getEnveloped = envelop({
  plugins: [
    // ... other plugins
    createStellateLoggerPlugin({
      serviceName: "${serviceName}",
      token: "${loggingToken}",
      // a fetch compliant function, can be from `node-fetch` if you are running in NodeJS,
      // or the global fetch function if running in a v8 environment (e.g. Cloudflare Workers)
      fetch: fetch,
    }),
  ],
});

server.on("req", async (req, res) => {
  // The request entry is important here so the stellage plugin has the request context
  getEnveloped({ request: req });
});

Discover more

About GraphQL Metrics

Get Started with GraphQL Metrics

Use the Edge Proxy for GraphQL Metrics

Build a Metrics Manual Call