Build a Metrics Logging Request

The Request Logging API allows you to identify your cacheable traffic using plugins or through a manual fetch call.

Before you begin

Before you start, read the About GraphQL Metrics and Get Started with GraphQL Metrics topics for preliminary instructions on setting up logging requests through the UI. The following sections provide 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:

  • 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.
  • If you use Apollo Server, GraphQL Yoga, GraphQL Mesh, or GraphQL Envelop we have plugins for Request Logging. Read Metrics Plugins for more information

Construct a Manual fetch Call for Metrics

An alternate method for getting Stellate GraphQL Metrics is to do a manual request. To do this you need to set up the following:

Authorization

To authorize, you need to send a token with the request using the Stellate-Logging-Token header.

Obtain the Token

You will need to include a logging-token in the Stellate-Logging-Token header. These tokens can be generated in the service-settings. To create the token, go to your service settings on the Stellate dashboard.

Specify the Endpoint

Send a POST request to https://<service-name>.stellate.sh/log. The following properties can be sent in the request body.

PropertyExpected typeIs requiredDescription
operationstringtrueThe GraphQL query handled by the origin
methodstringtrueThe HTTP method used to send the GraphQL request
responseSizenumbertrueThe length of the stringified response body
responseHashnumbertrueA blake3 hash of the JSON-stringified execution result
elapsednumbertrueThe time (in ms) it took to handle the GraphQL request
operationNamestringfalseThe name of the operation that has been executed
variablesobjectfalseThe variables sent with the GraphQL requests (stored as hash to count distinct variables for the given GraphQL operation)
variablesHashnumberfalseA blake3 hash of the JSON-stringified variables object (takes precendence over the variables property)
ipstringfalseThe IP that send the GraphQL request (will be hashed with SHA-256 before storing it)
errorsobject[]falseThe list of GraphQL errors that are part of the execution result
statusCodenumbertrueThe HTTP status code of the response
statusTextstringfalseThe text sent with the above HTTP status code (by default we use the standardized status texts)
userAgentstringfalseThe value of the user-agent header sent with the HTTP request
refererstringfalseThe value of the referer header sent with the HTTP request
hasSetCookiebooleanfalseThe value of the set-cookie header sent with the HTTP response

Endpoint Response

For a successful log intake, the endpoint defined in the prior section will respond with a 204 status code. If there was an error, it'll respond with a 400 status code.

How to hash values

To generate an integer hash, you can use the following function. This is needed for the responseBodyHash:

function createIntHash(str) {
  let val = 0
  const strlen = str.length
  if (strlen === 0) {
    return val
  }
  for (let i = 0; i < strlen; ++i) {
    const code = str.charCodeAt(i)
    val = (val << 5) - val + code
    val &= val // Int32
  }
  return val >>> 0 // uInt32
}

Discover more

About GraphQL Metrics

Get Started with GraphQL Metrics

Metrics Logging Plugins

Use the Edge Proxy for GraphQL Metrics

Build a Metrics Logging Call