Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): Expose rate limits for metrics #3347

Merged
merged 14 commits into from
Mar 28, 2024
Prev Previous commit
Next Next commit
test: Add integration tests
  • Loading branch information
jan-auer committed Mar 27, 2024
commit b5663b5d8c4f9bd2524d7e945636da325425f54c
4 changes: 2 additions & 2 deletions tests/integration/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,12 @@ def send_metrics(self, project_id, payload):
envelope.add_item(
Item(payload=PayloadRef(bytes=payload.encode()), type="statsd")
)
self.send_envelope(project_id, envelope)
return self.send_envelope(project_id, envelope)

def send_metrics_buckets(self, project_id, payload):
envelope = Envelope()
envelope.add_item(Item(payload=PayloadRef(json=payload), type="metric_buckets"))
self.send_envelope(project_id, envelope)
return self.send_envelope(project_id, envelope)

def send_metrics_batch(self, payload):
packed, signature = SecretKey.parse(self.secret_key).pack(payload)
Expand Down
41 changes: 41 additions & 0 deletions tests/integration/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
from sentry_sdk.envelope import Envelope, Item, PayloadRef
import json
import signal
import time

import pytest
import requests
from requests.exceptions import HTTPError
import queue

from .test_envelope import generate_transaction_item
Expand Down Expand Up @@ -311,6 +313,45 @@ def test_metrics_max_batch_size(mini_sentry, relay, max_batch_size, expected_eve
mini_sentry.captured_events.get(timeout=1)


@pytest.mark.parametrize("ns", [None, "custom", "transactions"])
def test_metrics_rate_limits_namespace(mini_sentry, relay, ns):
relay = relay(mini_sentry, options=TEST_CONFIG)

project_id = 42
project_config = mini_sentry.add_basic_project_config(project_id)
project_config["config"]["quotas"] = [
{
"categories": ["metric_bucket"],
"limit": 0,
"reasonCode": "static_disabled_quota",
"namespace": ns,
}
]

timestamp = int(datetime.now(tz=timezone.utc).timestamp())
metrics_payload = (
f"transactions/foo:42|c|T{timestamp}\ntransactions/bar:17|c|T{timestamp}"
)

# Send and ignore first request to populate caches
relay.send_metrics(project_id, metrics_payload)
time.sleep(1)

with pytest.raises(HTTPError) as excinfo:
relay.send_metrics(project_id, metrics_payload)

ns_component = ""
if ns:
ns_component = ":" + ";".join(ns)

response = excinfo.value.response
assert response.status_code == 429
assert (
response.headers["x-sentry-rate-limits"]
== f"60:metric_bucket:organization:static_disabled_quota{ns_component}"
)


def test_global_metrics(mini_sentry, relay):
relay = relay(
mini_sentry, options={"http": {"global_metrics": True}, **TEST_CONFIG}
Expand Down
Loading