Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Commit 1869982

Browse files
authoredMay 11, 2020
fix(logging trace id): dict or list object issues (#233)
* fix(logging trace id): dict or list object issues * fix(acceptance): adding tests with logging
1 parent 763b510 commit 1869982

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed
 

‎acceptance/acceptance.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,19 @@ def test_labels(self, input):
5959
body = json.loads(content['body'])
6060
assert body['input'] == input
6161

62-
63-
62+
@pytest.mark.parametrize("input", [
63+
'',
64+
'{afwe',
65+
[],
66+
[1, 2, 3],
67+
{},
68+
{'test': 'test'},
69+
{'test': 'test', 'more': [1, 2, '3']},
70+
])
71+
def test_logging(self, input):
72+
response = invoke('logging', json.dumps(input))
73+
assert response['StatusCode'] == 200
74+
content = json.loads(response['Payload'].read())
75+
assert content['statusCode'] == 200
76+
body = json.loads(content['body'])
77+
assert body['input'] == input

‎acceptance/lambda-handlers/handler.py

+27
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
"""
44
import platform
55
import json
6+
import logging
67
import epsagon
78

9+
logging.getLogger().setLevel(logging.INFO)
10+
811

912
epsagon.init(
1013
token='acceptance-test',
@@ -63,3 +66,27 @@ def labels(event, _):
6366
epsagon.label('12', None)
6467

6568
return response
69+
70+
71+
@epsagon.lambda_wrapper
72+
def logging_test(event, _):
73+
"""
74+
Basic test, using the Epsagon lambda-wrapper
75+
:param event: events args
76+
:param _: context, unused
77+
:return: Success indication
78+
"""
79+
body = {
80+
'message': 'Epsagon: General Acceptance Test (py {})'.format(
81+
platform.python_version()
82+
),
83+
'input': event
84+
}
85+
logging.info(event)
86+
87+
response = {
88+
'statusCode': 200,
89+
'body': json.dumps(body)
90+
}
91+
92+
return response

‎acceptance/lambda-handlers/serverless.yml

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ functions:
2424
handler: handler.sanity
2525
labels:
2626
handler: handler.labels
27+
logging:
28+
handler: handler.logging_test
2729

2830
plugins:
2931
- serverless-python-requirements

‎epsagon/modules/logging.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ def _wrapper(wrapped, _instance, args, kwargs):
3838
return wrapped(*args, **kwargs)
3939

4040

41+
def _add_log_id(trace_log_id, msg):
42+
"""
43+
adds log id to the msg
44+
"""
45+
try:
46+
# Check if message is in json format
47+
json_log = json.loads(msg)
48+
json_log['epsagon'] = {'trace_id': trace_log_id}
49+
return json.dumps(json_log)
50+
except Exception: # pylint: disable=broad-except
51+
# message is a regular string, add the ID to the beginning
52+
if not isinstance(msg, str):
53+
msg = str(msg)
54+
return ' '.join([trace_log_id, msg])
55+
56+
4157
def _epsagon_trace_id_wrapper(msg_index, wrapped, _instance, args, kwargs):
4258
"""
4359
Wrapper for logging module.
@@ -56,13 +72,10 @@ def _epsagon_trace_id_wrapper(msg_index, wrapped, _instance, args, kwargs):
5672
return wrapped(*args, **kwargs)
5773

5874
try:
59-
# Check if message is in json format
60-
json_log = json.loads(args[msg_index])
61-
json_log['epsagon'] = {'trace_id': trace_log_id}
62-
message = json.dumps(json_log)
63-
except Exception: # pylint: disable=broad-except
64-
# message is a regular string, add the ID to the beginning
65-
message = ' '.join([trace_log_id, args[msg_index]])
75+
message = _add_log_id(trace_log_id, args[msg_index])
76+
except Exception: # pylint: disable=broad-except
77+
# total failure to add log id
78+
return wrapped(*args, **kwargs)
6679
args = (
6780
args[0:msg_index] +
6881
(message,) +

0 commit comments

Comments
 (0)
This repository has been archived.