Skip to content

Commit c0ec1a0

Browse files
authored
Use python lambda wrapper (signalfx#24)
* Update example to use wrapper * Remove wait for flushed spans * Use the common-endpoint SIGNALFX_ENDPOINT_URL in readme
1 parent 1da45eb commit c0ec1a0

File tree

3 files changed

+12
-48
lines changed

3 files changed

+12
-48
lines changed

Diff for: aws-lambda/jaeger-python/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# OpenTracing and Jaeger in AWS Lambda Python Runtime
22

3-
This is an example of a simple, traced Lambda function using the Jaeger Python tracer with SignalFx.
3+
This is an example of a simple, traced Lambda function using the SignalFx Lambda Wrapper and Jaeger Python tracer with SignalFx.
44
See [example.py](./example.py) for the example code.
55

66
## Building and creating a Deployment Package
@@ -24,8 +24,10 @@ $ zip -r my_traced_python_lambda.zip *
2424

2525
The resulting `my_traced_python_lambda.zip` can be uploaded to S3 or in your browser via the AWS Console
2626
during function creation. Register your handler as `example.request_handler` and don't forget to set the
27-
`SIGNALFX_INGEST_URL` environment variable to point to your Gateway. You should be able test the application
28-
with the following test event:
27+
`SIGNALFX_ENDPOINT_URL` environment variable to point to your Gateway. Set
28+
`SIGNALFX_SERVICE_NAME` to `signalfx-lambda-python-example` or something else
29+
descriptive. You should be able test the application with the following test
30+
event:
2931

3032
```
3133
{

Diff for: aws-lambda/jaeger-python/example.py

+6-44
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import time
1818

1919
from jaeger_client import Config
20+
import opentracing
2021
from 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
179141
def request_handler(event, context):
180142
player = RoulettePlayer()
181143
return player.handle_request(event, context)

Diff for: aws-lambda/jaeger-python/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
git+git://github.com/signalfx/jaeger-client-python@http_sender
1+
signalfx_lambda>=0.1.0

0 commit comments

Comments
 (0)