Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ Returns data matching the given query.
.. code-block:: python

source.query_by_string(query='@message:"^PHP Fatal"', limit=2000)
source.query_by_string(query='@message:"^PHP Fatal"', fields=['@message', '@source_host'], limit=2000)

Returns data matching the given query string.

`query`: query string to be run against Kibana log messages (ex. @message:"^PHP Fatal").
`fields`: optional list of fields to fetch
`limit`: the number of results (defaults to 10).

.. code-block:: python
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

VERSION = '2.2.4'
VERSION = '2.2.5'

# @see https://github.com/pypa/sampleproject/blob/master/setup.py
setup(
Expand All @@ -21,6 +21,6 @@
},
install_requires=[
"elasticsearch>=6.0.0,<7.0.0",
"python-dateutil==2.2",
"python-dateutil==2.7.2",
]
)
19 changes: 14 additions & 5 deletions wikia_common_kibana.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ def _get_timestamp_filer(self):
}
}

def _search(self, query, limit=50000, sampling=None):
def _search(self, query, fields=None, limit=50000, sampling=None):
"""
Perform the search and return raw rows

:type query object
:type fields list[str] or None
:type limit int
:type sampling int or None

Expand All @@ -142,6 +143,12 @@ def _search(self, query, limit=50000, sampling=None):
}
}

# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
if fields:
body['_source'] = {
"includes": fields
}

# add @timestamp range
# @see http://stackoverflow.com/questions/40996266/elasticsearch-5-1-unknown-key-for-a-start-object-in-filters
# @see https://discuss.elastic.co/t/elasticsearch-watcher-error-for-range-query/70347/2
Expand Down Expand Up @@ -184,11 +191,12 @@ def _search(self, query, limit=50000, sampling=None):
self._logger.info("{:d} rows returned".format(len(rows)))
return rows

def get_rows(self, match, limit=10, sampling=None):
def get_rows(self, match, fields=None, limit=10, sampling=None):
"""
Returns raw rows that matches given query

:arg match: query to be run against Kibana log messages (ex. {"@message": "Foo Bar DB queries"})
:type fields list[str] or None
:arg limit: the number of results (defaults to 10)
:type sampling int or None
:arg sampling: Percentage of results to be returned (0,100)
Expand All @@ -197,13 +205,14 @@ def get_rows(self, match, limit=10, sampling=None):
"match": match,
}

return self._search(query, limit, sampling)
return self._search(query, fields, limit, sampling)

def query_by_string(self, query, limit=10, sampling=None):
def query_by_string(self, query, fields=None, limit=10, sampling=None):
"""
Returns raw rows that matches the given query string

:arg query: query string to be run against Kibana log messages (ex. @message:"^PHP Fatal").
:type fields list[str] or None
:arg limit: the number of results (defaults to 10)
:type sampling int or None
:arg sampling: Percentage of results to be returned (0,100)
Expand All @@ -214,7 +223,7 @@ def query_by_string(self, query, limit=10, sampling=None):
}
}

return self._search(query, limit, sampling)
return self._search(query, fields, limit, sampling)

def get_to_timestamp(self):
""" Return the upper time boundary to returned data """
Expand Down