Skip to content

Commit

Permalink
Data source health check: Support data source jaeger
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Jun 29, 2022
1 parent 7b290dd commit 8df46b7
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -164,6 +164,7 @@ implemented as of June 2022.
- Elasticsearch
- Graphite
- InfluxDB
- Jaeger
- OpenTSDB
- PostgreSQL
- Prometheus
Expand Down
1 change: 0 additions & 1 deletion docs/datasource-state.md
Expand Up @@ -19,7 +19,6 @@ stackdriver
testdata

### UNKNOWN
jaeger
loki
mssql
tempo
Expand Down
8 changes: 8 additions & 0 deletions examples/datasource-health-probe.rst
Expand Up @@ -134,6 +134,14 @@ InfluxDB 2.x
influx bucket list --org=example


Jaeger
======
::

docker run --rm -it --name=jaeger --publish=16686:16686 jaegertracing/all-in-one:1
python examples/datasource-health-probe.py --type=jaeger --url=http://host.docker.internal:16686


MariaDB / MySQL
===============
::
Expand Down
18 changes: 18 additions & 0 deletions grafana_client/elements/datasource.py
Expand Up @@ -340,6 +340,8 @@ def health_check(self, datasource: Union[DatasourceIdentifier, Dict]) -> Datasou
else:
if "results" in response:
success, message = self.parse_health_response_results(response=response)
elif "data" in response:
success, message = self.parse_health_response_data(response=response)
else:
message = f"Response lacks expected keys 'results' or 'data'"

Expand Down Expand Up @@ -542,3 +544,19 @@ def parse_health_response_results(response: Dict) -> Tuple[bool, str]:
message = f"FATAL: Unknown response type '{type(results)}'. Expected: dictionary or list."

return success, message

@staticmethod
def parse_health_response_data(response: Dict) -> Tuple[bool, str]:
"""
Response from Jaeger::
{"data":["jaeger-query"],"total":1,"limit":0,"offset":0,"errors":null}
"""
success = False
message = str(response["data"])
if "errors" in response and response["errors"]:
message = str(response["errors"])
else:
success = True

return success, message
5 changes: 5 additions & 0 deletions grafana_client/knowledge.py
Expand Up @@ -68,6 +68,8 @@ def datasource_factory(datasource: DatasourceModel) -> DatasourceModel:
datasource.secureJsonFields = {
"token": False,
}
elif datasource.type == "jaeger":
datasource.access = "proxy"
elif datasource.type == "opentsdb":
datasource.access = "proxy"
datasource.jsonData = {
Expand Down Expand Up @@ -139,6 +141,8 @@ def query_factory(datasource, expression: str, store: Optional[str] = None) -> U
)
else:
raise KeyError(f"InfluxDB dialect '{dialect}' unknown")
elif datasource_type == "jaeger":
query = {}
elif datasource_type == "mysql":
query = {
"refId": "test",
Expand Down Expand Up @@ -205,6 +209,7 @@ def query_factory(datasource, expression: str, store: Optional[str] = None) -> U
"influxdb": "SHOW RETENTION POLICIES on _internal",
"influxdb+influxql": "SHOW RETENTION POLICIES on _internal",
"influxdb+flux": "buckets()",
"jaeger": "url:///datasources/proxy/{datasource_id}/api/services",
"mysql": "SELECT 1",
"opentsdb": "url:///datasources/proxy/{datasource_id}/api/suggest?type=metrics&q=cpu&max=100",
"postgres": "SELECT 1",
Expand Down
8 changes: 8 additions & 0 deletions test/elements/test_datasource_fixtures.py
Expand Up @@ -32,6 +32,14 @@
"jsonData": {"httpMode": "POST", "version": "InfluxQL"},
}

JAEGER_DATASOURCE = {
"id": 53,
"uid": "DbtFe237k",
"name": "Jaeger",
"type": "jaeger",
"access": "proxy",
}

MYSQL_DATASOURCE = {
"id": 51,
"uid": "7CpzLp37z",
Expand Down
59 changes: 59 additions & 0 deletions test/elements/test_datasource_health.py
Expand Up @@ -7,6 +7,7 @@
ELASTICSEARCH_DATASOURCE,
GRAPHITE_DATASOURCE,
INFLUXDB1_DATASOURCE,
JAEGER_DATASOURCE,
MYSQL_DATASOURCE,
OPENTSDB_DATASOURCE,
POSTGRES_DATASOURCE,
Expand Down Expand Up @@ -262,6 +263,64 @@ def test_health_check_influxdb1(self, m):
),
)

@requests_mock.Mocker()
def test_health_check_jaeger_success(self, m):
m.get(
"http://localhost/api/datasources/uid/DbtFe237k",
json=JAEGER_DATASOURCE,
)
m.get(
"http://localhost/api/datasources/proxy/53/api/services",
json={"data": ["jaeger-query"], "total": 1, "limit": 0, "offset": 0, "errors": None},
)
response = self.grafana.datasource.health_check(DatasourceIdentifier(uid="DbtFe237k"))
response.duration = None
response.response = None
self.assertEqual(
response,
DatasourceHealthResponse(
uid="DbtFe237k",
type="jaeger",
success=True,
status="OK",
message="['jaeger-query']",
duration=None,
response=None,
),
)

@requests_mock.Mocker()
def test_health_check_jaeger_error_response_failure(self, m):
m.get(
"http://localhost/api/datasources/uid/DbtFe237k",
json=JAEGER_DATASOURCE,
)
m.get(
"http://localhost/api/datasources/proxy/53/api/services",
json={
"data": ["jaeger-query"],
"total": 1,
"limit": 0,
"offset": 0,
"errors": [{"code": 418, "message": "foobar"}],
},
)
response = self.grafana.datasource.health_check(DatasourceIdentifier(uid="DbtFe237k"))
response.duration = None
response.response = None
self.assertEqual(
response,
DatasourceHealthResponse(
uid="DbtFe237k",
type="jaeger",
success=False,
status="ERROR",
message="[{'code': 418, 'message': 'foobar'}]",
duration=None,
response=None,
),
)

@requests_mock.Mocker()
def test_health_check_mysql(self, m):
m.get(
Expand Down

0 comments on commit 8df46b7

Please sign in to comment.