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

Commit 5f977ff

Browse files
authored
feat(greengrasssdk.py): add greengrass support (#289)
1 parent f7948f2 commit 5f977ff

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ Epsagon provides out-of-the-box instrumentation (tracing) for many popular frame
443443
|azure.cosmos |`>=4.0.0` |
444444
|celery |`>=4.0.0` |
445445
|grpc |`>=0.3-10` |
446+
|greengrasssdk |`>=1.4.0` |
446447
|SQLAlchemy |`>=1.2.0` |
447448

448449

epsagon/events/greengrasssdk.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
Greengrass events module.
3+
"""
4+
5+
from __future__ import absolute_import
6+
import traceback
7+
from uuid import uuid4
8+
9+
from epsagon.utils import add_data_if_needed
10+
from ..trace import trace_factory
11+
from ..event import BaseEvent
12+
13+
14+
class GreengrassPublishEvent(BaseEvent):
15+
"""
16+
Represents Greengrass publish event.
17+
"""
18+
19+
ORIGIN = 'greengrasssdk'
20+
RESOURCE_TYPE = 'greengrass'
21+
22+
# pylint: disable=W0613
23+
def __init__(self, wrapped, instance, args, kwargs, start_time, response,
24+
exception):
25+
"""
26+
Initialize.
27+
:param wrapped: wrapt's wrapped
28+
:param instance: wrapt's instance
29+
:param args: wrapt's args
30+
:param kwargs: wrapt's kwargs
31+
:param start_time: Start timestamp (epoch)
32+
:param response: response data
33+
:param exception: Exception (if happened)
34+
"""
35+
super(GreengrassPublishEvent, self).__init__(start_time)
36+
37+
self.event_id = 'greengrass-{}'.format(str(uuid4()))
38+
39+
self.resource['name'] = kwargs.get('topic', 'N/A')
40+
self.resource['operation'] = 'publish'
41+
if kwargs.get('queueFullPolicy'):
42+
self.resource['metadata']['aws.greengrass.queueFullPolicy'] = (
43+
kwargs.get('queueFullPolicy')
44+
)
45+
46+
add_data_if_needed(
47+
self.resource['metadata'],
48+
'aws.greengrass.payload',
49+
kwargs.get('payload')
50+
)
51+
52+
if exception is not None:
53+
self.set_exception(exception, traceback.format_exc())
54+
55+
56+
class GreengrassEventFactory(object):
57+
"""
58+
Factory class, generates Greengrass event.
59+
"""
60+
61+
@staticmethod
62+
def create_event(wrapped, instance, args, kwargs, start_time, response,
63+
exception):
64+
"""
65+
Create an event according to the given api_name.
66+
"""
67+
event = GreengrassPublishEvent(
68+
wrapped,
69+
instance,
70+
args,
71+
kwargs,
72+
start_time,
73+
response,
74+
exception
75+
)
76+
77+
trace_factory.add_event(event)

epsagon/modules/greengrasssdk.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
greengrasssdk patcher module.
3+
"""
4+
5+
from __future__ import absolute_import
6+
import wrapt
7+
from epsagon.modules.general_wrapper import wrapper
8+
from ..events.greengrasssdk import GreengrassEventFactory
9+
10+
11+
def _wrapper(wrapped, instance, args, kwargs):
12+
"""
13+
General wrapper for greengrasssdk instrumentation.
14+
:param wrapped: wrapt's wrapped
15+
:param instance: wrapt's instance
16+
:param args: wrapt's args
17+
:param kwargs: wrapt's kwargs
18+
:return: None
19+
"""
20+
return wrapper(GreengrassEventFactory, wrapped, instance, args, kwargs)
21+
22+
23+
def patch():
24+
"""
25+
Patch module.
26+
:return: None
27+
"""
28+
wrapt.wrap_function_wrapper(
29+
'greengrasssdk.IoTDataPlane',
30+
'Client.publish',
31+
_wrapper
32+
)

tests/modules/test_greengrasssdk.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import mock
2+
from epsagon.events.greengrasssdk import GreengrassEventFactory
3+
4+
5+
@mock.patch('epsagon.trace.TraceFactory.add_event')
6+
def test_sanity(add_event_mock):
7+
params = {
8+
'topic': 'name',
9+
'queueFullPolicy': True,
10+
'payload': 'test',
11+
}
12+
GreengrassEventFactory.create_event(None, None, None, params, None, None, None)
13+
add_event_mock.assert_called_once()
14+
event = add_event_mock.call_args_list[0].args[0]
15+
assert event.event_id.startswith('greengrass-')
16+
assert event.resource['name'] == 'name'
17+
assert event.resource['operation'] == 'publish'
18+
assert event.resource['metadata']['aws.greengrass.queueFullPolicy'] == True
19+
assert event.resource['metadata']['aws.greengrass.payload'] == 'test'

0 commit comments

Comments
 (0)