Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL Federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free

Events

Capture events from the router's request lifecycle


An event is used to signal when something of note happens in the GraphOS Router's request lifecycle. Events are output to both logs and traces.

You can configure events for each service in router.yaml. Events can be standard or custom, and they can be triggered by configurable conditions.

Event configuration

Router request lifecycle services

A 's request lifecycle has three major services:

  • Router service - Handles an incoming request before it is parsed. Works within a context of opaque bytes.
  • Supergraph service - Handles a request after it has been parsed and before it is sent to the . Works within a context.
  • Subgraph service - Handles a request after it has been sent to the subgraph. Works within a GraphQL context.

The router, supergraph and subgraph sections are used to define custom event configuration for each service:

future.router.yaml
telemetry:
instrumentation:
events:
router:
# ...
supergraph:
# ...
subgraph:
# ...

Standard events

Each service has a set of standard events that can be configured:

  • request - The request has been received.
  • response - The response has been sent.
  • error - An error in the request lifecycle has occurred.

NOTE

The error level applies only to request lifecycle errors, not GraphQL errors.

To configure these events, set the level to trace, info, warn, error or off (default).

For example:

router.yaml
telemetry:
instrumentation:
events:
router:
request: off
response: off
error: error
# ...

But you can also enable these standard events based on conditions (not supported on batched requests).

For example:

router.yaml
telemetry:
instrumentation:
events:
router:
request:
level: info
condition: # Only log the router request if you sent `x-log-request` with the value `enabled`
eq:
- request_header: x-log-request
- "enabled"
response: off
error: error
supergraph:
response:
level: info
condition: # Only log the supergraph response containing graphql errors
eq:
- on_graphql_error: true
- true
error: error
# ...

Custom events

This feature is only available with a GraphOS Dedicated or Enterprise plan.
To compare GraphOS feature support across all plan types, see the pricing page.

For each service you can also configure custom events.

future.router.yaml
telemetry:
instrumentation:
events:
router:
# Custom events
my.event: # This key will automatically be added as a 'type' attribute of the event
# Custom event configuration

NOTE

Keep in mind that the amount of telemetry you add can impact your router's performance.

  • Custom metrics, events, and attributes consume more processing resources than standard metrics. Adding too many (standard or custom) can slow your router down.
  • Configurations such as events.*.request|error|response that produce output for all router lifecycle services should only be used for development or debugging, not for production.

For properly logged telemetry, you should use a log verbosity of info. Set the values of RUST_LOG or APOLLO_ROUTER_LOG environment variables and the --log CLI option to info. Using less verbose logging, such as error, can cause some attributes to be dropped.

message

Each custom event must have a message. This is a fixed value, and custom attributes should be used to add additional information.

future.router.yaml
telemetry:
instrumentation:
events:
router:
acme.event:
message: "my event message"
# ...

on

Each custom event must indicate when it should be triggered. This can be request, response, event_response or error.

future.router.yaml
telemetry:
instrumentation:
events:
router:
acme.event:
on: request # request, response, event_response, error
# ...

event_response is useful when you want to directly access to the json response body. It also works for events and @defer chunks.

level

Custom events have a level, trace, debug, info, warn, error or off (if you want to disable this event). The level determines the severity of the event.

To set the level:

future.router.yaml
telemetry:
instrumentation:
events:
router:
acme.event:
level: info # trace, debug, info, warn, error, off
# ...

condition

Custom events can be configured to emit under specific conditions, for example if the response status code is 200.

In router.yaml, set a condition with an equality (eq) check:

future.router.yaml
telemetry:
instrumentation:
events:
router:
acme.event:
# ...
condition:
eq:
- 200
- response_status: Code

For more details, see Conditions.

attributes

Custom events may have attributes attached to them from the router's request lifecycle. These attributes are used to filter and group spans in your APM.

Attributes may be drawn from standard attributes or selectors. The attributes available depend on the service of the request lifecycle.

future.router.yaml
telemetry:
instrumentation:
events:
router:
my.event:
# ...
attributes:
# Standard attributes
http.response.status_code: true
# Custom attributes
"my_attribute":
response_header: "x-my-header"

Event configuration example

For example, the router service can be configured with standard events (request, response, error), and a custom event (my.event) with a condition:

future.router.yaml
telemetry:
instrumentation:
events:
router:
# Standard events
request: info
response: info
error: info
# Custom events
my.event:
message: "my event message"
level: info
on: request
attributes:
http.response.body.size: false
# Only log when the x-log-request header is `log`
condition:
eq:
- "log"
- request_header: "x-log-request"
supergraph:
# Custom event configuration for supergraph service ...
subgraph:
# Custom event configuration for subgraph service ...

Event configuration reference

OptionValuesDefaultDescription
<attribute-name>The name of the custom attribute.
attributesstandard attributes or selectorsThe attributes of the custom log event.
conditionconditionsThe condition that must be met for the event to be emitted.
errortrace|info|warn|error| offoffThe level of the error log event.
leveltrace|info|warn|error| offoffThe level of the custom log event.
messageThe message of the custom log event.
onrequest|response|errorWhen to trigger the event.
requesttrace|info|warn|error| offoffThe level of the request log event.
responsetrace|info|warn|error| offoffThe level of the response log event.
Previous
Instruments
Next
Conditions
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc., d/b/a Apollo GraphQL.

Privacy Policy

Company