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 router'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 subgraph. Works within a GraphQL 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:
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:
telemetry:instrumentation:events:router:request: offresponse: offerror: error# ...
But you can also enable these standard events based on conditions (not supported on batched requests).
For example:
telemetry:instrumentation:events:router:request:level: infocondition: # Only log the router request if you sent `x-log-request` with the value `enabled`eq:- request_header: x-log-request- "enabled"response: offerror: errorsupergraph:response:level: infocondition: # Only log the supergraph response containing graphql errorseq:- on_graphql_error: true- trueerror: 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.
telemetry:instrumentation:events:router:# Custom eventsmy.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.
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
.
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 subscription 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:
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:
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.
telemetry:instrumentation:events:router:my.event:# ...attributes:# Standard attributeshttp.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:
telemetry:instrumentation:events:router:# Standard eventsrequest: inforesponse: infoerror: info# Custom eventsmy.event:message: "my event message"level: infoon: requestattributes: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
Option | Values | Default | Description |
---|---|---|---|
<attribute-name> | The name of the custom attribute. | ||
attributes | standard attributes or selectors | The attributes of the custom log event. | |
condition | conditions | The condition that must be met for the event to be emitted. | |
error | trace |info |warn |error | off | off | The level of the error log event. |
level | trace |info |warn |error | off | off | The level of the custom log event. |
message | The message of the custom log event. | ||
on | request |response |error | When to trigger the event. | |
request | trace |info |warn |error | off | off | The level of the request log event. |
response | trace |info |warn |error | off | off | The level of the response log event. |