Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
Add raw_result option to appdynamics query_logs
Browse files Browse the repository at this point in the history
Useful when caller interested in aggregation results and not only hits.

Resolve #127
  • Loading branch information
mohabusama committed Aug 31, 2016
1 parent 1f3cd16 commit 68abeb9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
6 changes: 5 additions & 1 deletion tests/test_appdynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def fx_exception(request):
('https://es-url', {'q': 'application_id:my-app'}, {'hits': {'hits': ['res1', 'res2']}}),
('https://es-url', {'q': 'application_id:my-app', 'source_type': 'syslog'}, {'hits': {'hits': ['res1', 'res2']}}),
('https://es-url', {'body': {'query': {'query_str': 'my-app'}}}, {'hits': {'hits': ['res1']}}),
('https://es-url', {'body': {'query': {'query_str': 'my-app'}}, 'raw_result': True}, {'hits': {'hits': ['res1']}}),
])
def fx_log_hits(request):
return request.param
Expand Down Expand Up @@ -276,9 +277,12 @@ def test_appdynamics_log_query(monkeypatch, fx_log_hits):

result = cli.query_logs(**kwargs)

assert result == res['hits']['hits']
expected = res['hits']['hits'] if not kwargs.get('raw_result', False) else res

assert result == expected

kwargs.pop('source_type', None)
kwargs.pop('raw_result', None)
kwargs['q'] = exp_q
kwargs['size'] = 100
kwargs['indices'] = ['PREFIX_*']
Expand Down
12 changes: 8 additions & 4 deletions zmon_worker_monitor/builtins/plugins/appdynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ def healthrule_violations(self, application, time_range_type=BEFORE_NOW, duratio
logger.exception('AppDynamics request failed')
raise

def query_logs(self, q='', body=None, size=100, source_type=SOURCE_TYPE_APPLICATION_LOG, duration_in_mins=5):
def query_logs(self, q='', body=None, size=100, source_type=SOURCE_TYPE_APPLICATION_LOG, duration_in_mins=5,
raw_result=False):
"""
Perform search query on AppDynamics ES logs.
Expand All @@ -213,8 +214,11 @@ def query_logs(self, q='', body=None, size=100, source_type=SOURCE_TYPE_APPLICAT
:param duration_in_mins: Duration in mins before current time. Default is 5 mins.
:type duration_in_mins: int
:return: ES query result ``hits``.
:rtype: list
:param raw_result: Return query raw result instead of only ``hits``. Default is False.
:type raw_result: bool
:return: ES query result ``hits``. If ``raw_result`` is True, then full query result will be returned.
:rtype: list, dict
"""
if not self.es_url:
raise RuntimeError('AppDynamics plugin improperly configured. ES URL is required to query logs!')
Expand All @@ -226,7 +230,7 @@ def query_logs(self, q='', body=None, size=100, source_type=SOURCE_TYPE_APPLICAT
res = (ElasticsearchWrapper(url=self.es_url, oauth2=self.__oauth2)
.search(indices=indices, q=q, body=body, size=size))

return res['hits']['hits']
return res['hits']['hits'] if not raw_result else res

def count_logs(self, q='', body=None, source_type=SOURCE_TYPE_APPLICATION_LOG, duration_in_mins=5):
"""
Expand Down
2 changes: 1 addition & 1 deletion zmon_worker_monitor/builtins/plugins/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def __request(self, url, params=None, body=None):

if not response.ok:
raise Exception(
'Elasticsearch query failed: {} with body: {}'.format(url, json.dumps(response.text)))
'Elasticsearch query failed: {} with response: {}'.format(url, json.dumps(response.text)))

return response.json()
except requests.Timeout:
Expand Down

0 comments on commit 68abeb9

Please sign in to comment.