-
Notifications
You must be signed in to change notification settings - Fork 38
add health and datasource proxy #5
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,3 +112,5 @@ venv.bak/ | |
| # mypy | ||
| .mypy_cache/ | ||
|
|
||
| # visual code launch... | ||
| .vscode/* | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from .base import Base | ||
|
|
||
|
|
||
| class Health(Base): | ||
| def __init__(self, client): | ||
| super(Health, self).__init__(client) | ||
| self.client = client | ||
|
|
||
| def check(self): | ||
| """ | ||
|
|
||
| :return: | ||
| """ | ||
| path = "/health" | ||
| r = self.client.GET(path) | ||
| return r |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import json | ||
| import unittest | ||
|
|
||
| import requests_mock | ||
|
|
||
| from grafana_client import GrafanaApi | ||
| from grafana_client.client import ( | ||
| GrafanaBadInputError, | ||
| GrafanaClientError, | ||
| GrafanaServerError, | ||
| GrafanaUnauthorizedError, | ||
| ) | ||
|
|
||
|
|
||
| class DatasourceTestCase(unittest.TestCase): | ||
| def setUp(self): | ||
| self.grafana = GrafanaApi(("admin", "admin"), host="localhost", url_path_prefix="", protocol="http") | ||
|
|
||
|
|
||
| @requests_mock.Mocker() | ||
| def test_find_datasource(self, m): | ||
| m.get( | ||
| "http://localhost/api/datasources/name/Prometheus", | ||
| json={ | ||
| "id":1, | ||
| "uid":"h8KkCLt7z", | ||
| "orgId":1, | ||
| "name":"Prometheus", | ||
| "type":"prometheus", | ||
| "typeName":"Prometheus", | ||
| "typeLogoUrl":"public/app/plugins/datasource/prometheus/img/prometheus_logo.svg", | ||
| "access":"proxy", | ||
| "url":"http://localhost:9090", | ||
| "password":"", | ||
| "user":"", | ||
| "database":"", | ||
| "basicAuth": False, | ||
| "isDefault": True, | ||
| "jsonData":{ | ||
| "httpMethod":"POST" | ||
| }, | ||
| "readOnly": False | ||
| } | ||
| ) | ||
|
|
||
| result = self.grafana.datasource.find_datasource('Prometheus') | ||
| self.assertEqual(result["type"], 'prometheus') | ||
|
|
||
| @requests_mock.Mocker() | ||
| def test_find_datasource_not_existing(self, m): | ||
| m.get( | ||
| "http://localhost/api/datasources/name/it_doesnot_exist", | ||
| json= {"message": "Data source not found"}, | ||
| status_code=400 | ||
| ) | ||
|
|
||
| with self.assertRaises(GrafanaBadInputError): | ||
| result = self.grafana.datasource.find_datasource('it_doesnot_exist') | ||
|
|
||
|
|
||
| @requests_mock.Mocker() | ||
| def test_get_datasource_id_by_name(self, m): | ||
| m.get( | ||
| "http://localhost/api/datasources/id/Prometheus", | ||
| json={"id": 1} | ||
| ) | ||
|
|
||
| result = self.grafana.datasource.get_datasource_id_by_name('Prometheus') | ||
| self.assertEqual(result["id"], 1) | ||
|
|
||
|
|
||
| @requests_mock.Mocker() | ||
| def test_list_datasources(self, m): | ||
| m.get( | ||
| "http://localhost/api/datasources", | ||
| json=[ | ||
| { | ||
| "id":1, | ||
| "uid":"h8KkCLt7z", | ||
| "orgId":1, | ||
| "name":"Prometheus", | ||
| "type":"prometheus", | ||
| "typeName":"Prometheus", | ||
| "typeLogoUrl":"public/app/plugins/datasource/prometheus/img/prometheus_logo.svg", | ||
| "access":"proxy", | ||
| "url":"http://localhost:9090", | ||
| "password":"", | ||
| "user":"", | ||
| "database":"", | ||
| "basicAuth": False, | ||
| "isDefault": True, | ||
| "jsonData":{ | ||
| "httpMethod":"POST" | ||
| }, | ||
| "readOnly": False | ||
| } | ||
| ], | ||
| ) | ||
|
|
||
| result = self.grafana.datasource.list_datasources() | ||
| self.assertEqual(result[0]["type"], 'prometheus') | ||
| self.assertEqual(len(result), 1) | ||
|
|
||
| @requests_mock.Mocker() | ||
| def test_get_datasource_proxy_data(self, m): | ||
| # http://localhost:3000/api/datasources/proxy/1/api/v1/query_range?query=up%7binstance%3d%22localhost:9090%22%7d&start=1644164339&end=1644164639&step=60 | ||
| m.get( | ||
| "http://localhost/api/datasources/proxy/1/api/v1/query_range", | ||
| json={ | ||
| "status": "success", | ||
| "data": { | ||
| "resultType": "matrix", | ||
| "result":[ | ||
| { | ||
| "metric":{ | ||
| "__name__":"up", | ||
| "instance":"localhost:9090", | ||
| "job":"prometheus" | ||
| }, | ||
| "values":[ | ||
| [1644164339,"1"], | ||
| [1644164399,"1"], | ||
| [1644164459,"1"], | ||
| [1644164519,"1"], | ||
| [1644164579,"1"], | ||
| [1644164639,"1"] | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ) | ||
| result = self.grafana.datasource.get_datasource_proxy_data( | ||
| 1, # datasource_id | ||
| query_type='query_range', | ||
| expr="up{instance=\"localhost:9090\"}", | ||
| start=1644164339, | ||
| end=1644164639, | ||
| step=60, | ||
| ) | ||
| self.assertEqual(result["status"], 'success') | ||
| self.assertEqual(result["data"]["result"][0]["metric"]["job"], 'prometheus') | ||
| self.assertEqual(len(result["data"]["result"][0]["values"]), 6) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import unittest | ||
|
|
||
| import requests_mock | ||
|
|
||
| from grafana_client import GrafanaApi | ||
| from grafana_client.client import ( | ||
| GrafanaBadInputError, | ||
| GrafanaClientError, | ||
| GrafanaServerError, | ||
| GrafanaUnauthorizedError, | ||
| ) | ||
|
|
||
|
|
||
| class HealthTestCase(unittest.TestCase): | ||
| def setUp(self): | ||
| self.grafana = GrafanaApi(("admin", "admin"), host="localhost", url_path_prefix="", protocol="http") | ||
|
|
||
| @requests_mock.Mocker() | ||
| def test_search_dashboards(self, m): | ||
| m.get( | ||
| "http://localhost/api/health", | ||
| json=[ | ||
| { | ||
| "commit": "6f8c1d9fe4", | ||
| "database": "ok", | ||
| "version": "7.5.11" | ||
| } | ||
| ], | ||
| ) | ||
|
|
||
| result = self.grafana.health.check() | ||
| self.assertEqual(result[0]["database"], 'ok') | ||
| self.assertEqual(len(result), 1) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't see where
get_datasource_proxy_data()will be called from any code, including test cases. This is probably why Codecov reported about a 0.52% decline in code coverage at #5 (comment).In order to learn something about the general concept, I would like to refer you to 1. Maybe you can add a corresponding test case for this function? Please let me know if you need further assistance or clarification on this detail.
Footnotes
https://en.wikipedia.org/wiki/Code_coverage ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that the documentation section 1 does not contain any example about how to use the
datasourceelement. Feel free to add a short example there about its baseline usage, and in particular about its usage withget_datasource_proxy_data. Do you think this is a good idea?Footnotes
https://github.com/panodata/grafana-client#getting-started ↩