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

Commit 1a7317f

Browse files
authored
feat(tencent): support tencent functions (#338)
1 parent 6241e6a commit 1a7317f

File tree

8 files changed

+584
-1
lines changed

8 files changed

+584
-1
lines changed

epsagon/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def _inner_wrapper(func):
5858
python_wrapper = dummy_python_wrapper # pylint: disable=C0103
5959
flask_wrapper = dummy_wrapper # pylint: disable=C0103
6060
gcp_wrapper = dummy_wrapper # pylint: disable=C0103
61+
tencent_function_wrapper = dummy_wrapper # pylint: disable=C0103
6162
else:
6263
# Environments.
6364
from .wrappers import (
@@ -66,7 +67,8 @@ def _inner_wrapper(func):
6667
chalice_wrapper,
6768
azure_wrapper,
6869
python_wrapper,
69-
gcp_wrapper
70+
gcp_wrapper,
71+
tencent_function_wrapper
7072
)
7173

7274
# Frameworks.
@@ -93,6 +95,7 @@ def _inner_wrapper(func):
9395
'flask_wrapper',
9496
'wrapper',
9597
'gcp_wrapper',
98+
'tencent_function_wrapper',
9699
'chalice_wrapper',
97100
'auto_load',
98101
'measure',

epsagon/events/qcloud_cos.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
Cloud Object Storage events module.
3+
"""
4+
5+
from __future__ import absolute_import
6+
from uuid import uuid4
7+
import traceback
8+
9+
from ..event import BaseEvent
10+
from ..trace import trace_factory
11+
12+
13+
class COSEvent(BaseEvent):
14+
"""
15+
Represents base Cloud Object Storage event.
16+
"""
17+
ORIGIN = 'tencent-cos'
18+
RESOURCE_TYPE = 'cos'
19+
20+
# pylint: disable=W0613
21+
def __init__(self, wrapped, instance, args, kwargs, start_time, response,
22+
exception):
23+
"""
24+
Initialize the Cloud Object Storage event
25+
:param wrapped: wrapt's wrapped
26+
:param instance: wrapt's instance
27+
:param args: wrapt's args
28+
:param kwargs: wrapt's kwargs
29+
:param start_time: Start timestamp (epoch)
30+
:param response: response data
31+
:param exception: Exception (if happened)
32+
"""
33+
34+
super(COSEvent, self).__init__(start_time)
35+
self.event_id = 'cos-{}'.format(str(uuid4()))
36+
self.resource['name'] = kwargs['bucket']
37+
self.resource['operation'] = kwargs['method']
38+
self.resource['metadata'] = {
39+
# pylint: disable=protected-access
40+
'tencent.region': instance._conf._region,
41+
'tencent.cos.object_key': kwargs['url'].split('myqcloud.com/')[-1],
42+
}
43+
if response:
44+
self.resource['metadata'].update({
45+
'tencent.cos.request_id': response.headers['x-cos-request-id'],
46+
'tencent.status_code': response.status_code,
47+
})
48+
49+
if exception is not None:
50+
self.resource['metadata'].update({
51+
'tencent.cos.request_id': exception.get_request_id(),
52+
'tencent.status_code': exception.get_status_code(),
53+
})
54+
self.set_exception(exception, traceback.format_exc())
55+
56+
57+
class COSEventFactory(object):
58+
"""
59+
Factory class, generates Cloud Object Storage event.
60+
"""
61+
@staticmethod
62+
def create_event(wrapped, instance, args, kwargs, start_time, response,
63+
exception):
64+
"""
65+
Create a Cloud Object Storage event.
66+
"""
67+
trace_factory.add_event(COSEvent(
68+
wrapped,
69+
instance,
70+
args,
71+
kwargs,
72+
start_time,
73+
response,
74+
exception
75+
))

epsagon/http_filters.py

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
str.endswith: [
3333
'epsagon.com',
3434
'.amazonaws.com',
35+
'.myqcloud.com',
3536
],
3637
str.__contains__: [
3738
'accounts.google.com',

epsagon/modules/qcloud_cos.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Cloud Object Storage patcher module.
3+
"""
4+
5+
from __future__ import absolute_import
6+
import wrapt
7+
from epsagon.modules.general_wrapper import wrapper
8+
from ..events.qcloud_cos import COSEventFactory
9+
10+
11+
def _wrapper(wrapped, instance, args, kwargs):
12+
"""
13+
Cloud Object Storage wrapper for Tencent 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(COSEventFactory, wrapped, instance, args, kwargs)
21+
22+
23+
def patch():
24+
"""
25+
patch module.
26+
:return: None
27+
"""
28+
wrapt.wrap_function_wrapper(
29+
'qcloud_cos',
30+
'CosS3Client.send_request',
31+
_wrapper
32+
)

epsagon/runners/tencent_function.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Runner for Tencent SCF.
3+
"""
4+
5+
from __future__ import absolute_import
6+
from ..event import BaseEvent
7+
from .. import constants
8+
from ..common import ErrorCode
9+
10+
11+
class TencentFunctionRunner(BaseEvent):
12+
"""
13+
Represents Tencent SCF event runner.
14+
"""
15+
ORIGIN = 'runner'
16+
RESOURCE_TYPE = 'tencent_function'
17+
OPERATION = 'invoke'
18+
19+
def __init__(self, start_time, context):
20+
"""
21+
Initialize.
22+
:param start_time: event's start time (epoch)
23+
:param context: SCF's context (passed from entry point)
24+
"""
25+
super(TencentFunctionRunner, self).__init__(start_time)
26+
27+
# Creating a unique ID for local runs.
28+
self.event_id = context['request_id']
29+
self.resource['name'] = context['function_name']
30+
self.resource['operation'] = self.OPERATION
31+
32+
self.resource['metadata'].update({
33+
'tencent.scf.version': context['function_version'],
34+
'tencent.scf.memory': context['memory_limit_in_mb'],
35+
'tencent.scf.cold_start': constants.COLD_START,
36+
'tencent.namespace': context['namespace'],
37+
'tencent.uin': context['tencentcloud_uin'],
38+
'tencent.app_id': context['tencentcloud_appid'],
39+
'tencent.region': context['tencentcloud_region'],
40+
})
41+
42+
def set_timeout(self):
43+
"""
44+
Sets timeout error code.
45+
:return: None
46+
"""
47+
# Don't override exceptions
48+
if self.error_code != ErrorCode.EXCEPTION:
49+
self.error_code = ErrorCode.TIMEOUT

0 commit comments

Comments
 (0)