This repository was archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtracing.py
More file actions
66 lines (49 loc) · 1.78 KB
/
tracing.py
File metadata and controls
66 lines (49 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import functools
import os
import opentracing
from opentracing.ext import tags as ext_tags
from jaeger_client import Config
from . import utils
def wrapper(func):
@functools.wraps(func)
def call(*args, **kwargs):
context = args[1]
tracer = init_jaeger_tracer(context)
span_tags = utils.get_tracing_fields(context)
span_tags['component'] = 'python-lambda-wrapper'
span_tags[ext_tags.SPAN_KIND] = ext_tags.SPAN_KIND_RPC_SERVER
environment = os.getenv('SIGNALFX_ENVIRONMENT')
if environment:
span_tags['environment'] = environment
span_prefix = os.getenv('SIGNALFX_SPAN_PREFIX', 'lambda_python_')
try:
with tracer.start_active_span(span_prefix + context.function_name, tags=span_tags) as scope:
# call the original handler
return func(*args, **kwargs)
except BaseException as e:
scope.span.set_tag('error', True)
scope.span.log_kv({'message': e})
raise
finally:
tracer.close()
return call
def init_jaeger_tracer(context):
endpoint = utils.get_tracing_url()
service_name = os.getenv('SIGNALFX_SERVICE_NAME', context.function_name)
access_token = utils.get_access_token()
tracer_config = {
'sampler': {
'type': 'const',
'param': 1
},
'propagation': 'b3',
'jaeger_endpoint': endpoint,
'logging': True,
}
if access_token:
tracer_config['jaeger_user'] = 'auth'
tracer_config['jaeger_password'] = access_token
config = Config(config=tracer_config, service_name=service_name)
tracer = config.new_tracer()
opentracing.tracer = tracer
return tracer