test_middleware.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import pytest
  2. from django.test import override_settings
  3. from sentry.metrics.middleware import (
  4. BadMetricTags,
  5. _filter_tags,
  6. add_global_tags,
  7. get_current_global_tags,
  8. global_tags,
  9. )
  10. def test_filter_tags_dev():
  11. with override_settings(SENTRY_METRICS_DISALLOW_BAD_TAGS=True):
  12. _filter_tags("x", {"foo": "bar"})
  13. with pytest.raises(
  14. BadMetricTags,
  15. match=r"discarded illegal metric tags: \['event', 'foo_id', 'project'\] for metric 'x'",
  16. ):
  17. _filter_tags("x", {"foo": "bar", "foo_id": 42, "project": 42, "event": 22})
  18. def test_filter_tags_prod():
  19. with override_settings(SENTRY_METRICS_DISALLOW_BAD_TAGS=False):
  20. assert _filter_tags("x", {"foo": "bar"}) == {"foo": "bar"}
  21. assert _filter_tags("x", {"foo": "bar", "foo_id": 42, "project": 42, "event": 22}) == {
  22. "foo": "bar"
  23. }
  24. def test_global():
  25. assert get_current_global_tags() == {}
  26. with global_tags(tag_a=123):
  27. assert get_current_global_tags() == {"tag_a": 123}
  28. add_global_tags(tag_b=123)
  29. assert get_current_global_tags() == {"tag_a": 123, "tag_b": 123}
  30. assert get_current_global_tags() == {}