17
17
import time
18
18
19
19
from jaeger_client import Config
20
+ import opentracing
20
21
from opentracing .ext import tags
22
+ import signalfx_lambda
21
23
22
24
23
25
# Roulette address/position maps, helpful for "00"
@@ -43,7 +45,7 @@ def __init__(self):
43
45
# idempotent Tracer registration and access per AWS Lambda best practices:
44
46
# https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html#function-code
45
47
# https://docs.aws.amazon.com/lambda/latest/dg/running-lambda-code.html
46
- self .tracer = self . create_tracer () # Create a tracer for the lifetime of the request
48
+ self .tracer = opentracing . tracer # get the OpenTracing global tracer that was set by the lambda wrapper
47
49
48
50
def handle_request (self , event , context ):
49
51
# Create the root span, specifying a span.kind tag in the
@@ -84,41 +86,8 @@ def handle_request(self, event, context):
84
86
span .set_tag (tags .HTTP_STATUS_CODE , status_code )
85
87
response = dict (statusCode = status_code , body = json .dumps (response_body ))
86
88
87
- # Known issue with Jaeger's Python client
88
- # https://github.com/jaegertracing/jaeger-client-python/issues/50
89
- self .wait_for_flushed_spans ()
90
-
91
89
return response
92
90
93
- def create_tracer (self ):
94
- access_token = os .getenv ('SIGNALFX_ACCESS_TOKEN' )
95
- ingest_url = os .getenv ('SIGNALFX_INGEST_URL' )
96
-
97
- if ingest_url is None :
98
- if access_token is None :
99
- raise Exception ("You must set the SIGNALFX_ACCESS_TOKEN Lambda environment variable to be your token "
100
- "if you don't set SIGNALFX_INGEST_URL to point to your Gateway." )
101
- ingest_url = 'https://ingest.signalfx.com/v1/trace'
102
-
103
- # Establish a jaeger-client-python configuration to report spans to SignalFx
104
- config = {'sampler' : {'type' : 'const' ,
105
- 'param' : 1 },
106
- 'jaeger_endpoint' : ingest_url ,
107
- 'reporter_flush_interval' : .01 ,
108
- 'logging' : False } # Report helpful span submission statements and errors to log handler
109
-
110
- if access_token is not None :
111
- # Required static username for Access Token authentication via Basic Access
112
- config .update (dict (jaeger_user = 'auth' , jaeger_password = access_token ))
113
-
114
- config = Config (
115
- config = config ,
116
- service_name = 'signalfx-lambda-python-example' ,
117
- validate = True , # Have jaeger_client fail quickly on invalid configuration
118
- )
119
-
120
- return config .new_tracer ()
121
-
122
91
def get_choice (self , event , span ):
123
92
"""
124
93
Retrieve a user's spin choice from an API Gateway request.
@@ -165,17 +134,10 @@ def spin_roulette_wheel(self, span):
165
134
span .set_tag ('position' , position )
166
135
return position
167
136
168
- def wait_for_flushed_spans (self ):
169
- # Because the Jaeger client's span reporting is asynchronous via tornado, it's not possible to reliably
170
- # ensure all spans have been flushed without adding a wait before using an implementation-specific
171
- # best attempt.
172
- time .sleep (.1 )
173
- while self .tracer .reporter .queue .qsize () or self .tracer .reporter ._sender .span_count :
174
- time .sleep (.01 )
175
- self .tracer .close ()
176
-
177
137
178
- # Our registered Lambda handler entrypoint (example.request_handler)
138
+ # Our registered Lambda handler entrypoint (example.request_handler) with the
139
+ # SignalFx Lambda wrapper
140
+ @signalfx_lambda .is_traced
179
141
def request_handler (event , context ):
180
142
player = RoulettePlayer ()
181
143
return player .handle_request (event , context )
0 commit comments