Skip to content

Commit ba7f05b

Browse files
author
Tomáš Daniel
committed
OpenSearch indexes partitioned to daily partitions
- allow to match them through pattern matching
1 parent 2261751 commit ba7f05b

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

example/apps/test_security/tests/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_call_command(*args, **kwargs):
3131

3232
def assert_equal_logstash(logstash_output, expected_index, expected_version, expected_logger_id, expected_data):
3333
prefix_and_index, version, logger_id, data = logstash_output.split(' ', 3)
34-
assert_equal(prefix_and_index, f'INFO:security.logstash:{expected_index}')
34+
assert_equal(prefix_and_index, f'INFO:security.logstash:{expected_index}*')
3535
assert_equal(version, str(expected_version))
3636
assert_equal(logger_id, str(expected_logger_id))
3737
parsed_data = json.loads(data)

logstash.example.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ filter {
3333
output {
3434
elasticsearch {
3535
ecs_compatibility => "v1"
36-
hosts => "elasticsearch:9200"
36+
hosts => "elasticsearch:9200-%{+YYYY-MM-dd}"
3737
index => "%{[@metadata][index]}"
3838
document_id => "%{[@metadata][id]}"
3939
version => "%{[@metadata][version]}"

security/backends/elasticsearch/connection.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ def init_documents(self):
2727
)
2828

2929
for document in InputRequestLog, OutputRequestLog, CommandLog, CeleryTaskRunLog, CeleryTaskInvocationLog:
30-
document.init()
30+
if isinstance(document_class, PartitionedLog):
31+
logs = document._index.as_template(document._index._name.replace("*", ""), order=0)
32+
logs.save()
33+
else:
34+
document.init()
3135

3236

3337
connection = ConnectionHandler()

security/backends/elasticsearch/models.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
2+
from datetime import datetime
33
from elasticsearch import NotFoundError
44

55
from django.db import router
@@ -23,11 +23,15 @@
2323
from .connection import set_connection
2424

2525

26-
def get_index_name(logger_name):
27-
return '{}-{}-log'.format(
26+
def get_index_name(logger_name, partitioned=True):
27+
index_name = '{}-{}-log'.format(
2828
settings.ELASTICSEARCH_DATABASE.get('prefix', 'security'),
2929
logger_name.value,
3030
)
31+
if partitioned:
32+
index_name += '*'
33+
34+
return index_name
3135

3236

3337
class JSONTextField(CustomField):
@@ -111,7 +115,31 @@ def update(
111115
)
112116

113117

114-
class RequestLog(Log):
118+
class PartitionedLog(Log):
119+
120+
DAY_FORMAT = "%Y-%m-%d"
121+
122+
def save(self, **kwargs):
123+
# assign now if no timestamp given
124+
if not self.start:
125+
self.start = datetime.now()
126+
127+
# override the index to go to the proper timeslot
128+
kwargs['index'] = self._format_index_name(self.start)
129+
return super().save(**kwargs)
130+
131+
@classmethod
132+
def get(cls, *args, **kwargs):
133+
now = datetime.now()
134+
kwargs['index'] = cls._format_index_name(now)
135+
return super().get(*args, **kwargs)
136+
137+
@staticmethod
138+
def _format_index_name(dt):
139+
return dt.strftime(f'{cls._index._name.replace("*", "")}-%Y-%m-%d')
140+
141+
142+
class RequestLog(PartitionedLog):
115143

116144
host = Keyword()
117145
method = Keyword()
@@ -152,7 +180,7 @@ class Index:
152180
name = get_index_name(LoggerName.OUTPUT_REQUEST)
153181

154182

155-
class CommandLog(CommandLogStrMixin, Log):
183+
class CommandLog(CommandLogStrMixin, PartitionedLog):
156184

157185
name = Keyword()
158186
input = Text()
@@ -166,7 +194,7 @@ class Index:
166194
name = get_index_name(LoggerName.COMMAND)
167195

168196

169-
class CeleryTaskInvocationLog(CeleryTaskInvocationLogStrMixin, Log):
197+
class CeleryTaskInvocationLog(CeleryTaskInvocationLogStrMixin, PartitionedLog):
170198

171199
celery_task_id = Keyword()
172200
name = Keyword()
@@ -195,7 +223,7 @@ class Index:
195223
name = get_index_name(LoggerName.CELERY_TASK_INVOCATION)
196224

197225

198-
class CeleryTaskRunLog(CeleryTaskRunLogStrMixin, Log):
226+
class CeleryTaskRunLog(CeleryTaskRunLogStrMixin, PartitionedLog):
199227

200228
celery_task_id = Keyword()
201229
state = EnumField(enum=CeleryTaskRunLogState)

security/backends/elasticsearch/tests.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ def enable(self):
2121
for document_class in (CommandLog, CeleryTaskRunLog, CeleryTaskInvocationLog,
2222
InputRequestLog, OutputRequestLog):
2323
document_class._index._name = f'{uuid}.{document_class._index._name}'
24-
document_class.init()
24+
if isinstance(document_class, PartitionedLog):
25+
logs = document_class._index.as_template(document_class._index._name.split("*")[0], order=0)
26+
logs.save()
27+
else:
28+
document_class.init()
2529

2630
def disable(self):
2731
for document_class in (CommandLog, CeleryTaskRunLog, CeleryTaskInvocationLog,

0 commit comments

Comments
 (0)