1717import time
1818
1919from jaeger_client import Config
20+ import opentracing
2021from opentracing .ext import tags
22+ import signalfx_lambda
2123
2224
2325# Roulette address/position maps, helpful for "00"
@@ -43,7 +45,7 @@ def __init__(self):
4345 # idempotent Tracer registration and access per AWS Lambda best practices:
4446 # https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html#function-code
4547 # 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
4749
4850 def handle_request (self , event , context ):
4951 # Create the root span, specifying a span.kind tag in the
@@ -84,41 +86,8 @@ def handle_request(self, event, context):
8486 span .set_tag (tags .HTTP_STATUS_CODE , status_code )
8587 response = dict (statusCode = status_code , body = json .dumps (response_body ))
8688
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-
9189 return response
9290
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-
12291 def get_choice (self , event , span ):
12392 """
12493 Retrieve a user's spin choice from an API Gateway request.
@@ -165,17 +134,10 @@ def spin_roulette_wheel(self, span):
165134 span .set_tag ('position' , position )
166135 return position
167136
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-
177137
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
179141def request_handler (event , context ):
180142 player = RoulettePlayer ()
181143 return player .handle_request (event , context )
0 commit comments