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:
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.http.server.response.body.size
- A histogram of response body sizes for requests handled by the router.
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)http.server.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: true
Apollo standard instruments
To learn about standard metric instruments for the router's request lifecycle, see Apollo 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.
ⓘ NOTE
When defining a custom instrument, make sure to reference OpenTelemetry semantic conventions.
In the example configuration below, three custom instruments are defined (acme.request.duration
, acme.graphql.requests
, acme.graphql.subgraph.errors
), one for each service of the router pipeline respectively (router
, supergraph
, subgraph
):
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"
Instrument naming conventions
Make sure to follow naming conventions for instruments. For reference, the OpenTelemetry semantic conventions can 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
The Apollo 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 instruments for each service.
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 value
of an instrument is the value which will be drawn from. This can be one of the following:
duration
- the duration of the pipeline service.unit
- the number of times the pipeline service has been executed.custom
- a custom value extracted from the pipeline service. See selectors for more information.
telemetry:instrumentation:instruments:router:acme.metric:# ...value: duration
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"
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 a 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> | The value of the instrument. |