Instruments
Collect measurements with standard and custom instruments
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.
An instrument in the router collects data and reports measurements to a metric backend. Supported instruments include standard instruments from OpenTelemetry, standard instruments for the router's request lifecycle, and custom instruments. Supported instrument kinds are counters and histograms.
You can configure instruments in router.yaml
with telemetry.instrumentation.instruments
.
OpenTelemetry standard instruments
OpenTelemetry specifies multiple standard metric instruments that are available in the router:
In the router service:
http.server.active_requests
- The number of active requests in flight.http.server.request.body.size
- A histogram of request body sizes for requests handled by the router.http.server.request.duration
- A histogram of request durations for requests handled by the router.
In the subgraph service:
http.client.request.body.size
- A histogram of request body sizes for requests handled by subgraphs.http.client.request.duration
- A histogram of request durations for requests handled by subgraphs.http.client.response.body.size
- A histogram of response body sizes for requests handled by subgraphs.
These instruments are configurable in router.yaml
:
telemetry:instrumentation:instruments:router:http.server.active_requests: true # (default false)http.server.request.body.size: true # (default false)http.server.request.duration: true # (default false)subgraph:http.client.request.body.size: true # (default false)http.client.request.duration: true # (default false)http.client.response.body.size: true # (default false)
They can be customized by attaching or removing attributes. See attributes to learn more about configuring attributes.
telemetry:instrumentation:instruments:default_requirement_level: requiredrouter:http.server.active_requests:attributes:http.request.method: truesubgraph:http.client.request.duration:attributes:subgraph.name: true
Apollo standard instruments
To learn about Apollo-provided standard metric instruments for the router's request lifecycle, see router instruments.
Custom instruments
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.
You can define custom instruments on the router, supergraph, and subgraph services in the router pipeline. You can also define custom instruments for each JSON element in the response data the router returns to clients.
The example configuration below defines four custom instruments:
acme.request.duration
on therouter
serviceacme.graphql.requests
on thesupergraph
serviceacme.graphql.subgraph.errors
on thesubgraph
serviceacme.graphql.list.lengths
on each JSON element returned to the client (defined ongraphql
)
telemetry:instrumentation:instruments:router:http.server.active_requests: trueacme.request.duration:value: durationtype: counterunit: kbdescription: "my description"condition:eq:- 200- response_status: codeattributes:http.response.status_code: true"my_attribute":response_header: "x-my-header"supergraph:acme.graphql.requests:value: unittype: counterunit: countdescription: "supergraph requests"subgraph:acme.graphql.subgraph.errors:value: unittype: counterunit: countdescription: "my description"graphql:acme.graphql.list.lengths:value:list_length: valuetype: histogramunit: countdescription: "my description"
ⓘ 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.
Instrument naming conventions
When defining a custom instrument, make sure to reference OpenTelemetry (OTel) semantic conventions. The OTel semantic conventions help guide you to:
- Choose a good name for your instrument.
- See which standard attributes can be attached to your instrument.
Some particular guidelines to note:
- Don't include the unit name in the metric name. For example,
size_kb
should besize
and the unit should bekb
. - Don't include
_total
as a suffix. For example, usehttp.server.active_requests
, nothttp.server.active_requests_total
. - Use dot notation to separate namespaces in the metric name. For example, use
http.server.active_requests
, nothttp_server_active_requests
.
Instrument configuration
default_requirement_level
The default_requirement_level
option sets the default attributes to attach to default standard instruments, as defined by OpenTelemetry semantic conventions.
Valid values:
required
(default) - required attributes will be attached to standard instruments by default.recommended
- recommended attributes will be attached to standard instruments by default.
telemetry:instrumentation:instruments:# Set the default requirement leveldefault_requirement_level: required
Attributes can be configured individually, so that required
attributes can be overridden or disabled. For example, http.response.status_code
is set individually to override the standard value:
telemetry:instrumentation:instruments:# Set the default requirement leveldefault_requirement_level: requiredrouter:# Standard metricshttp.server.request.body.size:attributes:# Standard attributeshttp.response.status_code: false# Custom attribute"acme.my_attribute":response_header: "x-my-header"# Standard metricshttp.server.active_requests:attributes:# Standard attributes, different than other ones provides in standard metrics, custom attributes are not available on this standard metrichttp.request.method: falseserver.address: trueserver.port: trueurl.scheme: true
ⓘ NOTE
The attributes that the OpenTelemetry spec defines as opt-in
must be configured individually.
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.
Additionally, you can define instruments on graphql
for each JSON element returned to the client.
To define a custom instrument, add a new key to router.yaml
as telemetry.instruments.<service>.<custom-instrument>
. For example, add a custom instrument acme.request.duration
:
telemetry:instrumentation:instruments:router:acme.request.duration: # The name of your custom instrument/metricvalue: durationtype: counterunit: sdescription: "my description"
value
The service you define an instrument on determines its possible values.
Value | Definition | Available services |
---|---|---|
| The duration of the pipeline service. |
|
| The number of times the pipeline service has been executed. |
|
| A custom value extracted from the pipeline service. See selectors for more information. |
|
| The duration of an event in the pipeline service. |
|
| The number of times an event in the pipeline service has been executed. |
|
| A custom value extracted from the event in the pipeline service. See selectors for more information. |
|
ⓘ NOTE
event_*
are mandantory when you want to use a selector on the supergraph response body (response_data
and response_errors
).
Values of custom metrics can be extracted from the pipeline using custom attributes. For example, to sum the contents of a request header, create a counter with value set as the request header:
telemetry:instrumentation:instruments:router:acme.metric:# ...type: countervalue:request_header: "x-my-header"
ⓘ NOTE
The value must be of the expected type for the instrument. For example, a counter must have a numeric value.
type
Instruments come in two different types:
counter
- A monotonic counter. For example, requests served, tasks completed, or errors occurred.histogram
- A histogram of values. For example, request durations or response body sizes.
telemetry:instrumentation:instruments:router:acme.metric:# ...type: counter # counter, histogram
unit
A free format unit that is displayed in your APM.
A unit
is recommended to use SI units and definitions from The Unified Code for Units of Measure.
telemetry:instrumentation:instruments:router:acme.metric:# ...unit: s # seconds
description
A free format description of the instrument that will be displayed in your APM.
telemetry:instrumentation:instruments:router:acme.metric:# ...description: "my description"
condition
You may only want to mutate an instrument under certain conditions. For example, you may only want to increment a counter if the response status code is 200.
To do this use a condition:
telemetry:instrumentation:instruments:router:acme.metric:# ...condition:eq:- 200- response_status: code
attributes
Instruments may have attributes attached to them from the router pipeline. These attributes are used to filter and group metrics in your APM.
Attributes may be drawn from standard attributes or selectors except for the standard metric http.server.active_requests
.
The attributes available depend on the service of the pipeline.
telemetry:instrumentation:instruments:router:# Standard metricshttp.server.request.body.size:attributes:# Standard attributeshttp.response.status_code: false# Custom attribute"acme.my_attribute":response_header: "x-my-header"# Standard metricshttp.server.active_requests:attributes:# Standard attributes, different than other ones provides in standard metrics, custom attributes are not available on this standard metrichttp.request.method: falseserver.address: trueserver.port: trueurl.scheme: true# Custom metricacme.metric:value: durationtype: counterunit: sdescription: "my description"attributes:http.response.status_code: true"my_attribute":# ...response_header: "x-my-header"subgraph:requests.timeout:value: unittype: counterunit: requestdescription: "subgraph requests containing subgraph timeout"attributes:subgraph.name: truecondition:eq:- "request timed out"- error: reasongraphql:acme.graphql.list.lengths:value:list_length: valuetype: histogramunit: countdescription: "my description"attributes:graphql.type.name: true
Instrument configuration reference
Option | Values | Default | Description |
---|---|---|---|
<attribute-name> | The name of the custom attribute. | ||
<instrument-name> | The name of the custom instrument. | ||
attributes | standard attributes or selectors | The attributes of the custom instrument. | |
condition | conditions | The condition for mutating the instrument. | |
default_requirement_level | required |recommended | required | The default attribute requirement level. |
type | counter |histogram | The name of the custom instrument. | |
unit | A unit name, for example By or {request} . | ||
description | The description of the custom instrument. | ||
value | unit |duration |<custom> |event_unit |event_duration |event_custom | The value of the instrument. |