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

Commit

Permalink
Remove sourceType filtering
Browse files Browse the repository at this point in the history
Fix #129
  • Loading branch information
mohabusama committed Aug 31, 2016
1 parent 68abeb9 commit fe2d936
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
17 changes: 6 additions & 11 deletions tests/test_appdynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from zmon_worker_monitor.zmon_worker.common.http import get_user_agent

from zmon_worker_monitor.builtins.plugins.appdynamics import CRITICAL, WARNING
from zmon_worker_monitor.builtins.plugins.appdynamics import SOURCE_TYPE_APPLICATION_LOG
from zmon_worker_monitor.builtins.plugins.appdynamics import BEFORE_TIME, BEFORE_NOW, AFTER_TIME, BETWEEN_TIMES
from zmon_worker_monitor.builtins.plugins.appdynamics import AppdynamicsWrapper

Expand Down Expand Up @@ -111,7 +110,7 @@ def fx_exception(request):

@pytest.fixture(params=[
('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', {'q': 'application_id:my-app'}, {'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']}}),
])
Expand All @@ -121,7 +120,7 @@ def fx_log_hits(request):

@pytest.fixture(params=[
('https://es-url', {'q': 'application_id:my-app'}, {'count': 2}),
('https://es-url', {'q': 'application_id:my-app', 'source_type': 'syslog'}, {'count': 3}),
('https://es-url', {'q': 'application_id:my-app'}, {'count': 3}),
('https://es-url', {'body': {'query': {'query_str': 'my-app'}}}, {'count': 1}),
])
def fx_log_count(request):
Expand Down Expand Up @@ -270,9 +269,8 @@ def test_appdynamics_log_query(monkeypatch, fx_log_hits):
url = 'https://appdynamics'
cli = AppdynamicsWrapper(url=url, es_url=es_url, username=USER, password=PASS, index_prefix='PREFIX_')

exp_source_type = SOURCE_TYPE_APPLICATION_LOG if 'source_type' not in kwargs else kwargs.get('source_type')
exp_q = ('{} AND sourceType:{} AND eventTimestamp:>{}'
.format(kwargs.get('q', ''), exp_source_type, timestamp)
exp_q = ('{} AND eventTimestamp:>{}'
.format(kwargs.get('q', ''), timestamp)
.lstrip(' AND '))

result = cli.query_logs(**kwargs)
Expand All @@ -281,7 +279,6 @@ def test_appdynamics_log_query(monkeypatch, fx_log_hits):

assert result == expected

kwargs.pop('source_type', None)
kwargs.pop('raw_result', None)
kwargs['q'] = exp_q
kwargs['size'] = 100
Expand All @@ -308,16 +305,14 @@ def test_appdynamics_log_count(monkeypatch, fx_log_count):
url = 'https://appdynamics'
cli = AppdynamicsWrapper(url=url, es_url=es_url, username=USER, password=PASS, index_prefix='PREFIX_')

exp_source_type = SOURCE_TYPE_APPLICATION_LOG if 'source_type' not in kwargs else kwargs.get('source_type')
exp_q = ('{} AND sourceType:{} AND eventTimestamp:>{}'
.format(kwargs.get('q', ''), exp_source_type, timestamp)
exp_q = ('{} AND eventTimestamp:>{}'
.format(kwargs.get('q', ''), timestamp)
.lstrip(' AND '))

result = cli.count_logs(**kwargs)

assert result == res['count']

kwargs.pop('source_type', None)
kwargs['q'] = exp_q
kwargs['indices'] = ['PREFIX_*']
if 'body' not in kwargs:
Expand Down
23 changes: 7 additions & 16 deletions zmon_worker_monitor/builtins/plugins/appdynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
CRITICAL,
)

SOURCE_TYPE_APPLICATION_LOG = 'application-log'


# will use OAUTH2_ACCESS_TOKEN_URL environment variable by default
# will try to read application credentials from CREDENTIALS_DIR
Expand Down Expand Up @@ -111,12 +109,12 @@ def __init__(self, url=None, username=None, password=None, es_url=None, index_pr
def __get_timestamp(self, minutes=5):
return int(time.mktime((datetime.utcnow() - timedelta(minutes=minutes)).timetuple())) * 1000

def __get_search_q(self, q, source_type, duration):
def __get_search_q(self, q, duration):
timestamp = self.__get_timestamp(minutes=duration)

q_str = ' AND '.join([q, 'sourceType:{}', 'eventTimestamp:>{}']).lstrip(' AND ')
q_str = ' AND '.join([q, 'eventTimestamp:>{}']).lstrip(' AND ')

return q_str.format(source_type, timestamp)
return q_str.format(timestamp)

def healthrule_violations_url(self, application):
return os.path.join(self.url, 'applications', application, 'problems', 'healthrule-violations')
Expand Down Expand Up @@ -194,8 +192,7 @@ 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,
raw_result=False):
def query_logs(self, q='', body=None, size=100, duration_in_mins=5, raw_result=False):
"""
Perform search query on AppDynamics ES logs.
Expand All @@ -208,9 +205,6 @@ def query_logs(self, q='', body=None, size=100, source_type=SOURCE_TYPE_APPLICAT
:param size: Number of hits to return. Default is 100.
:type size: int
:param source_type: ``sourceType`` field filtering. Default to application-log, and will be part of ``q``.
:type source_type: str
:param duration_in_mins: Duration in mins before current time. Default is 5 mins.
:type duration_in_mins: int
Expand All @@ -223,7 +217,7 @@ def query_logs(self, q='', body=None, size=100, source_type=SOURCE_TYPE_APPLICAT
if not self.es_url:
raise RuntimeError('AppDynamics plugin improperly configured. ES URL is required to query logs!')

q = self.__get_search_q(q, source_type, duration_in_mins)
q = self.__get_search_q(q, duration_in_mins)

indices = ['{}*'.format(self.index_prefix)]

Expand All @@ -232,7 +226,7 @@ def query_logs(self, q='', body=None, size=100, source_type=SOURCE_TYPE_APPLICAT

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):
def count_logs(self, q='', body=None, duration_in_mins=5):
"""
Perform count query on AppDynamics ES logs.
Expand All @@ -242,9 +236,6 @@ def count_logs(self, q='', body=None, source_type=SOURCE_TYPE_APPLICATION_LOG, d
:param body: (dict) holding an ES query DSL.
:type body: dict
:param source_type: ``sourceType`` field filtering. Default to application-log, and will be part of ``q``.
:type source_type: str
:param duration_in_mins: Duration in mins before current time. Default is 5 mins.
:type duration_in_mins: int
Expand All @@ -254,7 +245,7 @@ def count_logs(self, q='', body=None, source_type=SOURCE_TYPE_APPLICATION_LOG, d
if not self.es_url:
raise RuntimeError('AppDynamics plugin improperly configured. ES URL is required to query logs!')

q = self.__get_search_q(q, source_type, duration_in_mins)
q = self.__get_search_q(q, duration_in_mins)

indices = ['{}*'.format(self.index_prefix)]

Expand Down

0 comments on commit fe2d936

Please sign in to comment.