Skip to content

Commit 637d5a4

Browse files
author
Quentin Pierre
committed
Lambda Wrapper - open async connection on startup
1 parent 7a749a0 commit 637d5a4

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

datadog/threadstats/aws_lambda.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datadog.threadstats import ThreadStats
2-
from threading import Lock
2+
from threading import Lock, Thread
33
from datadog import api
44
import os
55

@@ -34,6 +34,11 @@ def _enter(cls):
3434
cls._was_initialized = True
3535
api._api_key = os.environ.get('DATADOG_API_KEY')
3636
api._api_host = os.environ.get('DATADOG_HOST', 'https://api.datadoghq.com')
37+
38+
# Async initialization of the TLS connection with our endpoints
39+
# This avoids adding execution time at the end of the lambda run
40+
t = Thread(target=init_connection)
41+
t.start()
3742
cls._counter = cls._counter + 1
3843

3944
@classmethod
@@ -70,3 +75,16 @@ def __call__(self, *args, **kw):
7075
def lambda_metric(*args, **kw):
7176
""" Alias to expose only distributions for lambda functions"""
7277
_lambda_stats.distribution(*args, **kw)
78+
79+
80+
def init_connection():
81+
""" No-op POST to initialize the requests connection with DD's endpoints
82+
83+
The goal here is to make the final flush faster:
84+
we keep alive the Requests session, this means that we can re-use the connection
85+
The consequence is that the HTTP Handshake, which can take hundreds of ms,
86+
is now made at the beginning of a lambda instead of at the end.
87+
88+
By making the initial request async, we spare a lot of execution time in the lambdas.
89+
"""
90+
api.api_client.APIClient.submit('GET', 'validate')

0 commit comments

Comments
 (0)