Skip to content

Commit

Permalink
feat: add statsd metrics to FAB rest API post, put and delete (apache…
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar authored and auxten committed Nov 20, 2020
1 parent 2e5ae5e commit 93aeb12
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 106 deletions.
24 changes: 24 additions & 0 deletions superset/views/base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,30 @@ def get_list_headless(self, **kwargs: Any) -> Response:
self.send_stats_metrics(response, self.get_list.__name__, duration)
return response

def post_headless(self) -> Response:
"""
Add statsd metrics to builtin FAB POST endpoint
"""
duration, response = time_function(super().post_headless)
self.send_stats_metrics(response, self.post.__name__, duration)
return response

def put_headless(self, pk: int) -> Response:
"""
Add statsd metrics to builtin FAB PUT endpoint
"""
duration, response = time_function(super().put_headless, pk)
self.send_stats_metrics(response, self.put.__name__, duration)
return response

def delete_headless(self, pk: int) -> Response:
"""
Add statsd metrics to builtin FAB DELETE endpoint
"""
duration, response = time_function(super().delete_headless, pk)
self.send_stats_metrics(response, self.delete.__name__, duration)
return response

@expose("/related/<column_name>", methods=["GET"])
@protect()
@safe
Expand Down
12 changes: 6 additions & 6 deletions tests/css_templates/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_get_css_template_not_found(self):
max_id = db.session.query(func.max(CssTemplate.id)).scalar()
self.login(username="admin")
uri = f"api/v1/css_template/{max_id + 1}"
rv = self.client.get(uri)
rv = self.get_assert_metric(uri, "get")
assert rv.status_code == 404

def test_create_css_template(self):
Expand All @@ -210,7 +210,7 @@ def test_create_css_template(self):

self.login(username="admin")
uri = f"api/v1/css_template/"
rv = self.client.post(uri, json=post_data)
rv = self.post_assert_metric(uri, post_data, "post")
data = json.loads(rv.data.decode("utf-8"))
assert rv.status_code == 201

Expand Down Expand Up @@ -241,7 +241,7 @@ def test_update_css_template(self):

self.login(username="admin")
uri = f"api/v1/css_template/{css_template.id}"
rv = self.client.put(uri, json=put_data)
rv = self.put_assert_metric(uri, put_data, "put")
assert rv.status_code == 200

model = db.session.query(CssTemplate).get(css_template.id)
Expand All @@ -262,7 +262,7 @@ def test_update_css_template_not_found(self):
}

uri = f"api/v1/css_template/{max_id + 1}"
rv = self.client.put(uri, json=put_data)
rv = self.put_assert_metric(uri, put_data, "put")
assert rv.status_code == 404

@pytest.mark.usefixtures("create_css_templates")
Expand All @@ -278,7 +278,7 @@ def test_delete_css_template(self):

self.login(username="admin")
uri = f"api/v1/css_template/{css_template.id}"
rv = self.client.delete(uri)
rv = self.delete_assert_metric(uri, "delete")
assert rv.status_code == 200

model = db.session.query(CssTemplate).get(css_template.id)
Expand All @@ -292,7 +292,7 @@ def test_delete_css_template_not_found(self):
max_id = db.session.query(func.max(CssTemplate.id)).scalar()
self.login(username="admin")
uri = f"api/v1/css_template/{max_id + 1}"
rv = self.client.delete(uri)
rv = self.delete_assert_metric(uri, "delete")
assert rv.status_code == 404

@pytest.mark.usefixtures("create_css_templates")
Expand Down
11 changes: 4 additions & 7 deletions tests/databases/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,13 +795,13 @@ def test_get_database_related_objects_not_found(self):
invalid_id = max_id + 1
uri = f"api/v1/database/{invalid_id}/related_objects/"
self.login(username="admin")
rv = self.client.get(uri)
rv = self.get_assert_metric(uri, "related_objects")
self.assertEqual(rv.status_code, 404)
self.logout()
self.login(username="gamma")
database = get_example_database()
uri = f"api/v1/database/{database.id}/related_objects/"
rv = self.client.get(uri)
rv = self.get_assert_metric(uri, "related_objects")
self.assertEqual(rv.status_code, 404)

def test_export_database(self):
Expand All @@ -812,8 +812,7 @@ def test_export_database(self):
database = get_example_database()
argument = [database.id]
uri = f"api/v1/database/export/?q={prison.dumps(argument)}"
rv = self.client.get(uri)

rv = self.get_assert_metric(uri, "export")
assert rv.status_code == 200

buf = BytesIO(rv.data)
Expand All @@ -828,7 +827,6 @@ def test_export_database_not_allowed(self):
argument = [database.id]
uri = f"api/v1/database/export/?q={prison.dumps(argument)}"
rv = self.client.get(uri)

assert rv.status_code == 401

def test_export_database_non_existing(self):
Expand All @@ -842,6 +840,5 @@ def test_export_database_non_existing(self):
self.login(username="admin")
argument = [invalid_id]
uri = f"api/v1/database/export/?q={prison.dumps(argument)}"
rv = self.client.get(uri)

rv = self.get_assert_metric(uri, "export")
assert rv.status_code == 404
Loading

0 comments on commit 93aeb12

Please sign in to comment.