Skip to content

Commit 13d19c9

Browse files
authored
Merge pull request #8 from macbre/rename-to-elasticsearch-query
Rename this package to elasticsearch-query
2 parents 110a2f6 + 1d0ae0d commit 13d19c9

File tree

8 files changed

+156
-149
lines changed

8 files changed

+156
-149
lines changed

MANIFEST.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
exclude README.rst
21
prune test

Makefile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
coverage_options = --include='wikia_common_kibana.py' --omit='test/*'
1+
coverage_options = --include='elasticsearch_query.py' --omit='test/*'
22

33
install:
44
pip install -e .[dev]
55

66
test:
7-
pytest -v
7+
pytest -vv
88

99
coverage:
1010
rm -f .coverage*
1111
rm -rf htmlcov/*
12-
coverage run -p -m pytest -v
12+
coverage run -p -m pytest -vv
1313
coverage combine
1414
coverage html -d htmlcov $(coverage_options)
1515
coverage xml -i
1616
coverage report $(coverage_options)
1717

1818
lint:
19-
pylint wikia_common_kibana.py
19+
pylint elasticsearch_query.py
2020

2121
publish:
2222
# run git tag -a v0.0.0 before running make publish
23-
python setup.py sdist upload -r pypi
23+
python setup.py sdist
24+
twine upload dist/*
2425

2526
.PHONY: test

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
elasticsearch-query
2+
===================
3+
4+
[![PyPI](https://img.shields.io/pypi/v/elasticsearch-query.svg)](https://pypi.python.org/pypi/elasticsearch-query)
5+
[![Build Status](https://travis-ci.org/macbre/elasticsearch-query.svg?branch=master)](https://travis-ci.org/macbre/elasticsearch-query)
6+
7+
Run queries against Kibana's Elasticsearch that gets logs from Logstash. Forked from [Wikia's `kibana.py`](https://github.com/Wikia/python-commons/blob/master/wikia/common/kibana/kibana.py).
8+
9+
```
10+
pip install elasticsearch-query
11+
```
12+
13+
## Basic Usage
14+
15+
```python
16+
from elasticsearch_query import ElasticsearchQuery
17+
es_query = ElasticsearchQuery(es_host='es.prod', since=12345, period=900)
18+
```
19+
20+
`es_host` needs to be specified with a host of Elasticsearch instance to connect.
21+
22+
Provide either `since` (absolute timestamp) or `period` (last N seconds):
23+
24+
* `since`: UNIX timestamp data should be fetched since (if None, then period specifies the last n seconds).
25+
* `period`: period (in seconds) before now() to be used when since is empty (defaults to last 15 minutes).
26+
27+
### `get_rows`
28+
29+
> Returns data matching the given query (provided as a `dict`).
30+
31+
```python
32+
es_query.get_rows(match={"tags": 'edge-cache-requestmessage'}, limit=2000)
33+
```
34+
35+
* `match`: query to be run against log messages (ex. {"@message": "Foo Bar DB queries"}).
36+
* `limit`: the number of results (defaults to 10).
37+
38+
### `query_by_string`
39+
40+
> Returns data matching the given query string (provided as a [Lucene query](https://lucene.apache.org/core/2_9_4/queryparsersyntax.html)).
41+
42+
```python
43+
es_query.query_by_string(query='@message:"^PHP Fatal"', limit=2000)
44+
es_query.query_by_string(query='@message:"^PHP Fatal"', fields=['@message', '@es_query_host'], limit=2000)
45+
```
46+
47+
* `query`: query string to be run against log messages (ex. `@message:"^PHP Fatal"`).
48+
* `fields`: optional list of fields to fetch
49+
* `limit`: the number of results (defaults to 10).
50+
51+
### `get_to_timestamp`
52+
53+
> Returns the upper time boundary for the requested data.
54+
55+
```python
56+
es_query.get_to_timestamp()
57+
```

README.rst

Lines changed: 0 additions & 49 deletions
This file was deleted.

wikia_common_kibana.py renamed to elasticsearch_query.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Run queries against Kibana's elasticsearch
2+
Run queries against Kibana's Elasticsearch that gets logs from Logstash.
33
@see http://elasticsearch-py.readthedocs.org/en/master/
44
"""
55
import json
@@ -15,14 +15,14 @@
1515
from elasticsearch.helpers import scan
1616

1717

18-
class KibanaError(Exception):
18+
class ElasticsearchQueryError(Exception):
1919
"""
20-
Error that can be raised by Kibana class
20+
Error that can be raised by ElasticsearchQuery class
2121
"""
2222
pass
2323

2424

25-
class Kibana(object):
25+
class ElasticsearchQuery(object):
2626
"""
2727
Elasticsearch client
2828
"""
@@ -32,32 +32,30 @@ class Kibana(object):
3232
# seconds in 24h used to get the es index for yesterday
3333
DAY = 86400
3434

35-
ELASTICSEARCH_HOST = 'logs-prod.es.service.sjc.consul' # ES5
36-
37-
""" Interface for querying Kibana's storage """
35+
""" Interface for querying Elasticsearch storage """
3836
def __init__(
39-
self, since=None, period=900, es_host=None,
37+
self, es_host, since=None, period=900,
4038
read_timeout=10, index_prefix='logstash-other', index_sep='-', batch_size=1000):
4139
"""
40+
:type es_host str
4241
:type since int
4342
:type period int
44-
:type es_host str
4543
:type read_timeout int
4644
:type index_prefix str
4745
:type index_sep str
4846
:type batch_size int
4947
48+
:arg es_host: Elasticsearch host(s) that should be used for querying
5049
:arg since: UNIX timestamp data should be fetched since
5150
:arg period: period (in seconds) before now() to be used when since is empty(defaults to last 15 minutes)
52-
:arg es_host: customize Elasticsearch host(s) that should be used for querying
5351
:arg read_timeout: customize Elasticsearch read timeout (defaults to 10 s)
5452
:arg index_prefix name of the Elasticsearch index (defaults to 'logstash-other')
5553
:arg batch_size size of the batch sent in every requests of the ELK scroll API (defaults to 1000)
5654
"""
57-
self._es = Elasticsearch(hosts=es_host if es_host else self.ELASTICSEARCH_HOST, timeout=read_timeout)
55+
self._es = Elasticsearch(hosts=es_host, timeout=read_timeout)
5856
self._batch_size = batch_size
5957

60-
self._logger = logging.getLogger('kibana')
58+
self._logger = logging.getLogger(self.__class__.__name__)
6159

6260
# if no timestamp provided, fallback to now() in UTC
6361
now = int(time.time())

setup.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22

33
VERSION = '2.2.7'
44

5+
# @see https://packaging.python.org/tutorials/packaging-projects/#creating-setup-py
6+
with open("README.md", "r") as fh:
7+
long_description = fh.read()
8+
59
# @see https://github.com/pypa/sampleproject/blob/master/setup.py
610
setup(
7-
name='wikia_common_kibana',
11+
name='elasticsearch-query',
812
version=VERSION,
9-
author='Wikia Engineering',
10-
author_email='techteam-l@wikia-inc.com',
13+
author='Maciej Brencz',
14+
author_email='macbre@wikia-inc.com',
1115
license='MIT',
12-
description='Run queries against Kibana\'s Elasticsearch 6',
16+
description='Run queries against Kibana\'s Elasticsearch that gets logs from Logstash.',
1317
keywords='logstash kibana elasticsearch logging',
14-
url='https://github.com/macbre/wikia-common-kibana',
15-
py_modules=["wikia_common_kibana"],
18+
long_description=long_description,
19+
long_description_content_type="text/markdown",
20+
url='https://github.com/macbre/elasticsearch-query',
21+
py_modules=["elasticsearch_query"],
1622
extras_require={
1723
'dev': [
1824
'coverage==4.5.1',

test/test_ElasticsearchQuery.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
Set of unit tests for elastic_search.py
3+
"""
4+
import time
5+
6+
from elasticsearch_query import ElasticsearchQuery
7+
8+
9+
def test_indexes():
10+
es_query = ElasticsearchQuery(es_host='foo')
11+
assert es_query._index.startswith('logstash-')
12+
13+
14+
def test_indexes_prefix():
15+
es_query = ElasticsearchQuery(es_host='foo', index_prefix='syslog-ng')
16+
assert es_query._index.startswith('syslog-ng-')
17+
18+
19+
def test_indexes_prefix_with_separator():
20+
es_query = ElasticsearchQuery(es_host='foo', index_prefix='syslog-ng', index_sep="_")
21+
assert es_query._index.startswith('syslog-ng_')
22+
assert ',syslog-ng_' in es_query._index
23+
24+
25+
def test_format_index():
26+
assert ElasticsearchQuery.format_index(prefix='logstash', timestamp=1) == 'logstash-1970.01.01'
27+
assert ElasticsearchQuery.format_index(prefix='logstash', timestamp=1408450795) == 'logstash-2014.08.19'
28+
assert ElasticsearchQuery.format_index(prefix='logstash-foo', timestamp=1408450795) == 'logstash-foo-2014.08.19'
29+
assert ElasticsearchQuery.format_index(prefix='syslog-ng', timestamp=1408450795, sep="_") == 'syslog-ng_2014.08.19'
30+
31+
32+
def test_time():
33+
now = int(time.time())
34+
35+
cases = [
36+
# till now
37+
{
38+
"since": None,
39+
"expected_since": now - 60,
40+
"expected_to": now - 5,
41+
"period": 60
42+
},
43+
# strictly defined time period
44+
{
45+
"since": 12345,
46+
"expected_since": 12346,
47+
"expected_to": now - 5,
48+
"period": 600
49+
}
50+
]
51+
52+
for case in cases:
53+
check_time(**case)
54+
55+
56+
def check_time(since, expected_since, expected_to, period):
57+
es_query = ElasticsearchQuery('foo.host.net', since, period)
58+
59+
assert es_query._since == expected_since
60+
assert es_query.get_to_timestamp() == expected_to
61+
62+
63+
def test_get_timestamp_filer():
64+
es_query = ElasticsearchQuery(es_host='foo', since=123456, period=60)
65+
res = es_query._get_timestamp_filer()
66+
67+
print(res)
68+
69+
assert res['range']['@timestamp'] is not None
70+
assert res['range']['@timestamp']['gte'] == '1970-01-02T10:17:37.000Z'
71+
assert res['range']['@timestamp']['lte'] is not None

test/test_kibana.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)