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.
Property | Expected type | Is required | Description |
---|---|---|---|
operation | string | true | The GraphQL query handled by the origin |
method | string | true | The HTTP method used to send the GraphQL request |
responseSize | number | true | The length of the stringified response body |
responseHash | number | true | A blake3 hash of the JSON-stringified execution result |
elapsed | number | true | The time (in ms) it took to handle the GraphQL request |
operationName | string | false | The name of the operation that has been executed |
variables | object | false | The variables sent with the GraphQL requests (stored as hash to count distinct variables for the given GraphQL operation) |
variablesHash | number | false | A blake3 hash of the JSON-stringified variables object (takes precendence over the variables property) |
ip | string | false | The IP that send the GraphQL request (will be hashed with SHA-256 before storing it) |
errors | object[] | false | The list of GraphQL errors that are part of the execution result |
statusCode | number | true | The HTTP status code of the response |
statusText | string | false | The text sent with the above HTTP status code (by default we use the standardized status texts) |
userAgent | string | false | The value of the user-agent header sent with the HTTP request |
referer | string | false | The value of the referer header sent with the HTTP request |
hasSetCookie | boolean | false | The value of the set-cookie header sent with the HTTP response |
graphqlClientName | string | false | The name of the Client reaching out to your GraphQL API i.e. mobile |
graphqlClientVersion | string | false | The version of the Client reaching out to your GraphQL API i.e. 1.0.0 |
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
Get Started with GraphQL Metrics