Skip to content

Commit 7ea637c

Browse files
committed
Adds configurable JSON log parsing(zalando-incubator#49)
With the new configuration parameter `WATCHER_SCALYR_PARSE_LINES_JSON` the Scalyr agent can be configured to parse log lines as JSON entries which is useful to process raw Docker logs. This behaviour is disabled by default.
1 parent ceb6139 commit 7ea637c

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

README.rst

+3
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ WATCHER_SCALYR_DEST_PATH
291291
WATCHER_SCALYR_CONFIG_PATH
292292
Scalyr configuration file path. (Default: ``/etc/scalyr-agent-2/agent.json``)
293293

294+
WATCHER_SCALYR_PARSE_LINES_JSON
295+
Parse lines lines on the client as JSON. Useful for raw docker logs. (Default: ``False``)
296+
294297
WATCHER_SCALYR_JOURNALD
295298
Scalyr should follow Journald logs. This is for node system processes log shipping (e.g. docker, kube) (Default: ``False``)
296299

kube_log_watcher/agents/scalyr.py

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self, cluster_id: str, load_template):
3333
self.api_key = os.environ.get('WATCHER_SCALYR_API_KEY')
3434
self.dest_path = os.environ.get('WATCHER_SCALYR_DEST_PATH')
3535
self.scalyr_server = os.environ.get('WATCHER_SCALYR_SERVER')
36+
self.parse_lines_json = os.environ.get('WATCHER_SCALYR_PARSE_LINES_JSON', False)
3637

3738
if not all([self.api_key, self.dest_path]):
3839
raise RuntimeError('Scalyr watcher agent initialization failed. Env variables WATCHER_SCALYR_API_KEY and '
@@ -205,6 +206,7 @@ def flush(self):
205206
'logs': self.logs,
206207
'monitor_journald': self.journald,
207208
'scalyr_server': self.scalyr_server,
209+
'parse_lines_json': self.parse_lines_json,
208210
}
209211

210212
current_paths = self._get_current_log_paths()

kube_log_watcher/templates/scalyr.json.jinja2

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
"redaction_rules": {{ log.redaction_rules | tojson }},
2626
{% endif %}
2727

28+
{% if parse_lines_json %}
29+
"parse_lines_as_json": true,
30+
{% endif %}
31+
2832
"copy_from_start": true,
2933

3034
"attributes": {{ log.attributes | tojson }}

tests/conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def fx_containers_sync(request):
202202
'scalyr_key': SCALYR_KEY,
203203
'cluster_id': CLUSTER_ID,
204204
'monitor_journald': None,
205+
'parse_lines_json': False,
205206
'logs': [
206207
{
207208
'path': os.path.join(SCALYR_DEST_PATH, 'container-1', 'app-1-v1.log'),

tests/test_scalyr.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
},
3232
)
3333

34-
KWARGS_KEYS = ('scalyr_key', 'cluster_id', 'logs', 'monitor_journald')
34+
KWARGS_KEYS = ('scalyr_key', 'parse_lines_json', 'cluster_id', 'logs', 'monitor_journald')
3535

3636

3737
SCALYR_MONITOR_JOURNALD = copy.deepcopy(SCALYR_JOURNALD_DEFAULTS)
@@ -489,6 +489,38 @@ def test_remove_log_target(monkeypatch, env, exc):
489489
],
490490
},
491491
),
492+
(
493+
{
494+
'scalyr_key': SCALYR_KEY,
495+
'cluster_id': CLUSTER_ID,
496+
'parse_lines_json': True,
497+
'monitor_journald': None,
498+
'logs': [
499+
{
500+
'path': '/p1',
501+
'attributes': {'a1': 'v1', 'parser': 'c-parser'},
502+
'copy_from_start': True,
503+
'redaction_rules': {'match_expression': 'match-expression'}
504+
}
505+
]
506+
},
507+
{
508+
'api_key': 'scalyr-key-123',
509+
'implicit_metric_monitor': False,
510+
'implicit_agent_process_metrics_monitor': False,
511+
'server_attributes': {'serverHost': 'kube-cluster'},
512+
'monitors': [],
513+
'logs': [
514+
{
515+
'attributes': {'a1': 'v1', 'parser': 'c-parser'},
516+
'path': '/p1',
517+
'parse_lines_as_json': True,
518+
'copy_from_start': True,
519+
'redaction_rules': {'match_expression': 'match-expression'}
520+
}
521+
],
522+
},
523+
),
492524
)
493525
)
494526
def test_tpl_render(monkeypatch, kwargs, expected):

0 commit comments

Comments
 (0)