Skip to content

Commit

Permalink
feat(telemetry): add configurable histogram buckets for metrics (#3098)
Browse files Browse the repository at this point in the history
You can customize the buckets for all generated histograms:

```yaml title="router.yaml"
telemetry:
  metrics:
    common:
      buckets:
        - 0.05
        - 0.10
        - 0.25
        - 0.50
        - 1.00
        - 2.50
        - 5.00
        - 10.00
        - 20.00
```

Fixes #2333

**Checklist**

Complete the checklist (and note appropriate exceptions) before a final
PR is raised.

- [x] Changes are compatible[^1]
- [x] Documentation[^2] completed
- [ ] Performance impact assessed and acceptable
- Tests added and passing[^3]
    - [x] Unit Tests
    - [ ] Integration Tests
    - [x] Manual Tests

**Exceptions**

*Note any exceptions here*

**Notes**

[^1]. It may be appropriate to bring upcoming changes to the attention
of other (impacted) groups. Please endeavour to do this before seeking
PR approval. The mechanism for doing this will vary considerably, so use
your judgement as to how and when to do this.
[^2]. Configuration is an important part of many changes. Where
applicable please try to document configuration examples.
[^3]. Tick whichever testing boxes are applicable. If you are adding
Manual Tests:
- please document the manual testing (extensively) in the Exceptions.
- please raise a separate issue to automate the test and label it (or
ask for it to be labeled) as `manual test`

---------

Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
  • Loading branch information
bnjjj authored May 30, 2023
1 parent 3ae91ba commit 1259982
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 8 deletions.
21 changes: 21 additions & 0 deletions .changesets/feat_swan_cub_foot_audience.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### Configurable histogram buckets for metrics ([Issue #2333](https://github.com/apollographql/router/issues/2333))

You can customize the buckets for all generated histograms:

```yaml title="router.yaml"
telemetry:
metrics:
common:
buckets:
- 0.05
- 0.10
- 0.25
- 0.50
- 1.00
- 2.50
- 5.00
- 10.00
- 20.00
```
By [@bnjjj](https://github.com/bnjjj) in https://github.com/apollographql/router/pull/3098
Original file line number Diff line number Diff line change
Expand Up @@ -3711,6 +3711,28 @@ expression: "&schema"
"additionalProperties": false,
"nullable": true
},
"buckets": {
"description": "Custom buckets for histograms",
"default": [
0.001,
0.005,
0.015,
0.05,
0.1,
0.2,
0.3,
0.4,
0.5,
1.0,
5.0,
10.0
],
"type": "array",
"items": {
"type": "number",
"format": "double"
}
},
"resources": {
"description": "Resources",
"default": {},
Expand All @@ -3721,11 +3743,13 @@ expression: "&schema"
},
"service_name": {
"description": "Set a service.name resource in your metrics",
"default": null,
"type": "string",
"nullable": true
},
"service_namespace": {
"description": "Set a service.namespace attribute in your metrics",
"default": null,
"type": "string",
"nullable": true
}
Expand Down
12 changes: 10 additions & 2 deletions apollo-router/src/plugins/telemetry/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,25 @@ pub(crate) struct Metrics {
}

#[derive(Clone, Default, Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
#[serde(deny_unknown_fields, rename_all = "snake_case", default)]
pub(crate) struct MetricsCommon {
/// Configuration to add custom labels/attributes to metrics
pub(crate) attributes: Option<MetricsAttributesConf>,
/// Set a service.name resource in your metrics
pub(crate) service_name: Option<String>,
/// Set a service.namespace attribute in your metrics
pub(crate) service_namespace: Option<String>,
#[serde(default)]
/// Resources
pub(crate) resources: HashMap<String, String>,
/// Custom buckets for histograms
#[serde(default = "default_buckets")]
pub(crate) buckets: Vec<f64>,
}

fn default_buckets() -> Vec<f64> {
vec![
0.001, 0.005, 0.015, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 1.0, 5.0, 10.0,
]
}

/// Tracing configuration
Expand Down
4 changes: 1 addition & 3 deletions apollo-router/src/plugins/telemetry/metrics/otlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ impl MetricsConfigurator for super::super::otlp::Config {
Some(exporter) => {
let exporter = opentelemetry_otlp::new_pipeline()
.metrics(
selectors::simple::histogram([
0.001, 0.005, 0.015, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 1.0, 5.0, 10.0,
]),
selectors::simple::histogram(metrics_config.buckets.clone()),
aggregation::stateless_temporality_selector(),
opentelemetry::runtime::Tokio,
)
Expand Down
4 changes: 1 addition & 3 deletions apollo-router/src/plugins/telemetry/metrics/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ impl MetricsConfigurator for Config {
if self.enabled {
let mut controller = controllers::basic(
processors::factory(
selectors::simple::histogram([
0.001, 0.005, 0.015, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 1.0, 5.0, 10.0,
]),
selectors::simple::histogram(metrics_config.buckets.clone()),
aggregation::stateless_temporality_selector(),
)
.with_memory(true),
Expand Down
Loading

0 comments on commit 1259982

Please sign in to comment.