Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add circuit breaker event count metric (#18516)

Cezary Miącz38150ba2cb21
diff --git a/saleor/core/telemetry/saleor_attributes.py b/saleor/core/telemetry/saleor_attributes.py
index 6cb0f80434..d1bdc20509 100644
--- a/saleor/core/telemetry/saleor_attributes.py
+++ b/saleor/core/telemetry/saleor_attributes.py
@@ -26,3 +26,6 @@ SALEOR_APP_NAME: Final = "saleor.app.name"
SALEOR_WEBHOOK_EXECUTION_MODE: Final = "saleor.webhook.execution_mode"
SALEOR_WEBHOOK_EVENT_TYPE: Final = "saleor.webhook.event_type"
SALEOR_WEBHOOK_PAYLOAD_SIZE: Final = "saleor.webhook.payload.size"
+
+# Circuit Breaker
+SALEOR_CIRCUIT_BREAKER_STATE: Final = "saleor.circuit_breaker.state"
diff --git a/saleor/core/telemetry/utils.py b/saleor/core/telemetry/utils.py
index 3f41516435..194be49241 100644
--- a/saleor/core/telemetry/utils.py
+++ b/saleor/core/telemetry/utils.py
@@ -35,6 +35,7 @@ class Unit(Enum):
REQUEST = "{request}"
BYTE = "By"
COST = "{cost}"
+ EVENT = "{event}"
UNIT_CONVERSIONS: dict[tuple[Unit, Unit], float] = {
diff --git a/saleor/webhook/circuit_breaker/breaker_board.py b/saleor/webhook/circuit_breaker/breaker_board.py
index 13d743d477..9cafd2cefe 100644
--- a/saleor/webhook/circuit_breaker/breaker_board.py
+++ b/saleor/webhook/circuit_breaker/breaker_board.py
@@ -6,6 +6,13 @@ from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.module_loading import import_string
+from ...core.telemetry import (
+ MetricType,
+ Scope,
+ Unit,
+ meter,
+ saleor_attributes,
+)
from ...graphql.app.enums import CircuitBreakerState
from ...webhook.event_types import WebhookEventSyncType
@@ -38,6 +45,24 @@ BREAKER_BOARD_SUCCESS_COUNT_RECOVERY: int = 50
# Time to keep circuit breaker in opened state before starting recovery (half-open state).
BREAKER_BOARD_COOLDOWN_SECONDS: int = 2 * 60
+METRIC_CIRCUIT_BREAKER_EVENT_COUNT = meter.create_metric(
+ "saleor.external_request.sync.circuit_breaker.event_count",
+ scope=Scope.SERVICE,
+ type=MetricType.COUNTER,
+ unit=Unit.EVENT,
+ description="Number of circuit breaker state changes.",
+)
+
+
+def record_circuit_breaker_state_change(app: "App", state: str) -> None:
+ attributes = {
+ saleor_attributes.SALEOR_APP_IDENTIFIER: app.identifier,
+ saleor_attributes.SALEOR_CIRCUIT_BREAKER_STATE: state,
+ }
+ meter.record(
+ METRIC_CIRCUIT_BREAKER_EVENT_COUNT, 1, Unit.EVENT, attributes=attributes
+ )
+
class BreakerBoard:
"""Base class for breaker board implementations.
@@ -112,12 +137,14 @@ class BreakerBoard:
changed_at = int(time.time())
self.storage.set_app_state(app.id, state, changed_at)
+ record_circuit_breaker_state_change(app, state)
logger.info(
"[App ID: %r] Circuit breaker changed state to %s.",
app.id,
state,
extra={
"app_name": app.name,
+ "app_identifier": app.identifier,
"webhooks_total_count": total,
"webhooks_errors_count": errors,
"webhooks_cooldown_seconds": self.cooldown_seconds,