element-hq / element-hq/synapse

`RequestMetrics.update_metrics()` and `_BackgroundProcess.update_metrics()` can result in negative counts

Open
#18,765 0 comments 0 reactions 0 assignees View on GitHub
A-Metrics O-Frequent S-Tolerable T-Defect
Dominant language
Python
Stars
4.6k
Forks
600
Avg merge
5d 22h
Merged PRs (30d)
51

Description

`RequestMetrics.update_metrics()` and `_BackgroundProcess.update_metrics()` can result in negative counts.

### `RequestMetrics.update_metrics()`

Sentry error: https://sentry.tools.element.io/organizations/element/issues/11196863
```
ValueError: Counters can only be incremented by non-negative amounts.
```

Relevant code:

[mypy `Counter.inc(...)` error](https://github.com/prometheus/client_python/blob/73680284ce63f0bc0f23cfc42af06e74fd7e3ccf/prometheus_client/metrics.py#L286-L290)

https://github.com/element-hq/synapse/blob/e16fbdcdcc95d626f6d3dcd0b6a85fdb0a06456a/synapse/http/request_metrics.py#L278-L299

https://github.com/element-hq/synapse/blob/e16fbdcdcc95d626f6d3dcd0b6a85fdb0a06456a/synapse/logging/context.py#L184-L191

### `_BackgroundProcess.update_metrics()`

Occurrences in Sentry but for the background process metrics:

- https://sentry.tools.element.io/organizations/element/issues/11196776
- https://sentry.tools.element.io/organizations/element/issues/289

Relevant code:

https://github.com/element-hq/synapse/blob/19fe3f001ed0aff5a5f136e440ae53c04340be88/synapse/metrics/background_process_metrics.py#L182-L188

---

This is a historical error before any of the [recent changes to metrics](https://github.com/element-hq/synapse/issues/18592).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.