Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy load boto client when using datadogpy for metrics. #558

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions datadog_lambda/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ def init_api():
from datadog import api

if not api._api_key:
import boto3

DD_API_KEY_SECRET_ARN = os.environ.get("DD_API_KEY_SECRET_ARN", "")
DD_API_KEY_SSM_NAME = os.environ.get("DD_API_KEY_SSM_NAME", "")
DD_KMS_API_KEY = os.environ.get("DD_KMS_API_KEY", "")
Expand All @@ -64,15 +62,15 @@ def init_api():
)

if DD_API_KEY_SECRET_ARN:
api._api_key = boto3.client("secretsmanager").get_secret_value(
api._api_key = _boto3_client("secretsmanager").get_secret_value(
SecretId=DD_API_KEY_SECRET_ARN
)["SecretString"]
elif DD_API_KEY_SSM_NAME:
api._api_key = boto3.client("ssm").get_parameter(
api._api_key = _boto3_client("ssm").get_parameter(
Name=DD_API_KEY_SSM_NAME, WithDecryption=True
)["Parameter"]["Value"]
elif DD_KMS_API_KEY:
kms_client = boto3.client("kms")
kms_client = _boto3_client("kms")
api._api_key = decrypt_kms_api_key(kms_client, DD_KMS_API_KEY)
else:
api._api_key = DD_API_KEY
Expand All @@ -87,3 +85,9 @@ def init_api():

# Unmute exceptions from datadog api client, so we can catch and handle them
api._mute = False


def _boto3_client(*args, **kwargs):
import botocore

return botocore.session.get_session().create_client(*args, **kwargs)
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ datadog = ">=0.51.0,<1.0.0"
wrapt = "^1.11.2"
ddtrace = ">=2.20.0"
ujson = ">=5.9.0"
boto3 = { version = "^1.34.0", optional = true }
botocore = { version = "^1.34.0", optional = true }
requests = { version ="^2.22.0", optional = true }
pytest = { version= "^8.0.0", optional = true }
pytest-benchmark = { version = "^4.0", optional = true }
flake8 = { version = "^5.0.4", optional = true }

[tool.poetry.extras]
dev = [
"boto3",
"botocore",
"flake8",
"pytest",
"pytest-benchmark",
Expand Down
Loading