Skip to content

Commit e2e6f5b

Browse files
committed
increment statsd as warn
1 parent 50f1e2e commit e2e6f5b

File tree

4 files changed

+88
-62
lines changed

4 files changed

+88
-62
lines changed

superset/utils/decorators.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,16 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
4545
current_app.config["STATS_LOGGER"].gauge(f"{metric_prefix_}.ok", 1)
4646
return result
4747
except Exception as ex:
48-
current_app.config["STATS_LOGGER"].gauge(f"{metric_prefix_}.error", 1)
48+
if (
49+
hasattr(ex, "status") and ex.status < 500
50+
): # pylint: disable=no-member
51+
current_app.config["STATS_LOGGER"].gauge(
52+
f"{metric_prefix_}.warning", 1
53+
)
54+
else:
55+
current_app.config["STATS_LOGGER"].gauge(
56+
f"{metric_prefix_}.error", 1
57+
)
4958
raise ex
5059

5160
return wrapped

superset/views/base_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ def send_stats_metrics(
205205
"""
206206
if 200 <= response.status_code < 400:
207207
self.incr_stats("success", key)
208+
elif 400 <= response.status_code < 500:
209+
self.incr_stats("warning", key)
208210
else:
209211
self.incr_stats("error", key)
210212
if time_delta:

tests/integration_tests/utils/decorators_tests.py

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
19+
from enum import Enum
20+
from typing import Any
21+
from unittest.mock import call, Mock, patch
22+
23+
import pytest
24+
25+
from superset import app
26+
from superset.utils import decorators
27+
28+
29+
class ResponseValues(str, Enum):
30+
FAIL = "fail"
31+
WARN = "warn"
32+
OK = "ok"
33+
34+
35+
def test_debounce() -> None:
36+
mock = Mock()
37+
38+
@decorators.debounce()
39+
def myfunc(arg1: int, arg2: int, kwarg1: str = "abc", kwarg2: int = 2) -> int:
40+
mock(arg1, kwarg1)
41+
return arg1 + arg2 + kwarg2
42+
43+
# should be called only once when arguments don't change
44+
myfunc(1, 1)
45+
myfunc(1, 1)
46+
result = myfunc(1, 1)
47+
mock.assert_called_once_with(1, "abc")
48+
assert result == 4
49+
50+
# kwarg order shouldn't matter
51+
myfunc(1, 0, kwarg2=2, kwarg1="haha")
52+
result = myfunc(1, 0, kwarg1="haha", kwarg2=2)
53+
mock.assert_has_calls([call(1, "abc"), call(1, "haha")])
54+
assert result == 3
55+
56+
57+
def test_statsd_gauge() -> None:
58+
@decorators.statsd_gauge("custom.prefix")
59+
def my_func(response: ResponseValues, *args: Any, **kwargs: Any) -> str:
60+
if response == ResponseValues.FAIL:
61+
raise ValueError("Error")
62+
if response == ResponseValues.WARN:
63+
raise FileNotFoundError("Not found")
64+
return "OK"
65+
66+
with patch.object(app.config["STATS_LOGGER"], "gauge") as mock:
67+
my_func(ResponseValues.OK, 1, 2)
68+
mock.assert_called_once_with("custom.prefix.ok", 1)
69+
70+
with pytest.raises(ValueError):
71+
my_func(ResponseValues.FAIL, 1, 2)
72+
mock.assert_called_once_with("custom.prefix.error", 1)
73+
74+
with pytest.raises(FileNotFoundError):
75+
my_func(ResponseValues.WARN, 1, 2)
76+
mock.assert_called_once_with("custom.prefix.warn", 1)

0 commit comments

Comments
 (0)