Skip to content

Commit 6e2e5d5

Browse files
authored
Enable LLM Observability with agentless_enabled=True by default with a parsed API key (#572)
* enable llmobs agentless with parsed api_key * extract getting api key to its own function * lint
1 parent fc0beaa commit 6e2e5d5

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

datadog_lambda/api.py

+36-22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
logger = logging.getLogger(__name__)
66
KMS_ENCRYPTION_CONTEXT_KEY = "LambdaFunctionName"
7+
api_key = None
78

89

910
def decrypt_kms_api_key(kms_client, ciphertext):
@@ -46,6 +47,40 @@ def decrypt_kms_api_key(kms_client, ciphertext):
4647
return plaintext
4748

4849

50+
def get_api_key() -> str:
51+
"""
52+
Gets the Datadog API key from the environment variables or secrets manager.
53+
Extracts the result to a global value to avoid repeated calls to the
54+
secrets manager from different products.
55+
"""
56+
global api_key
57+
if api_key:
58+
return api_key
59+
60+
import boto3
61+
62+
DD_API_KEY_SECRET_ARN = os.environ.get("DD_API_KEY_SECRET_ARN", "")
63+
DD_API_KEY_SSM_NAME = os.environ.get("DD_API_KEY_SSM_NAME", "")
64+
DD_KMS_API_KEY = os.environ.get("DD_KMS_API_KEY", "")
65+
DD_API_KEY = os.environ.get("DD_API_KEY", os.environ.get("DATADOG_API_KEY", ""))
66+
67+
if DD_API_KEY_SECRET_ARN:
68+
api_key = boto3.client("secretsmanager").get_secret_value(
69+
SecretId=DD_API_KEY_SECRET_ARN
70+
)["SecretString"]
71+
elif DD_API_KEY_SSM_NAME:
72+
api_key = boto3.client("ssm").get_parameter(
73+
Name=DD_API_KEY_SSM_NAME, WithDecryption=True
74+
)["Parameter"]["Value"]
75+
elif DD_KMS_API_KEY:
76+
kms_client = boto3.client("kms")
77+
api_key = decrypt_kms_api_key(kms_client, DD_KMS_API_KEY)
78+
else:
79+
api_key = DD_API_KEY
80+
81+
return api_key
82+
83+
4984
def init_api():
5085
if not os.environ.get("DD_FLUSH_TO_LOG", "").lower() == "true":
5186
# Make sure that this package would always be lazy-loaded/outside from the critical path
@@ -54,28 +89,7 @@ def init_api():
5489
from datadog import api
5590

5691
if not api._api_key:
57-
import boto3
58-
59-
DD_API_KEY_SECRET_ARN = os.environ.get("DD_API_KEY_SECRET_ARN", "")
60-
DD_API_KEY_SSM_NAME = os.environ.get("DD_API_KEY_SSM_NAME", "")
61-
DD_KMS_API_KEY = os.environ.get("DD_KMS_API_KEY", "")
62-
DD_API_KEY = os.environ.get(
63-
"DD_API_KEY", os.environ.get("DATADOG_API_KEY", "")
64-
)
65-
66-
if DD_API_KEY_SECRET_ARN:
67-
api._api_key = boto3.client("secretsmanager").get_secret_value(
68-
SecretId=DD_API_KEY_SECRET_ARN
69-
)["SecretString"]
70-
elif DD_API_KEY_SSM_NAME:
71-
api._api_key = boto3.client("ssm").get_parameter(
72-
Name=DD_API_KEY_SSM_NAME, WithDecryption=True
73-
)["Parameter"]["Value"]
74-
elif DD_KMS_API_KEY:
75-
kms_client = boto3.client("kms")
76-
api._api_key = decrypt_kms_api_key(kms_client, DD_KMS_API_KEY)
77-
else:
78-
api._api_key = DD_API_KEY
92+
api._api_key = get_api_key()
7993

8094
logger.debug("Setting DATADOG_API_KEY of length %d", len(api._api_key))
8195

datadog_lambda/wrapper.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@
5656
if profiling_env_var:
5757
from ddtrace.profiling import profiler
5858

59+
llmobs_api_key = None
5960
llmobs_env_var = os.environ.get("DD_LLMOBS_ENABLED", "false").lower() in ("true", "1")
6061
if llmobs_env_var:
62+
from datadog_lambda.api import get_api_key
6163
from ddtrace.llmobs import LLMObs
6264

65+
llmobs_api_key = get_api_key()
66+
6367
logger = logging.getLogger(__name__)
6468

6569
DD_FLUSH_TO_LOG = "DD_FLUSH_TO_LOG"
@@ -229,7 +233,10 @@ def __init__(self, func):
229233

230234
# Enable LLM Observability
231235
if llmobs_env_var:
232-
LLMObs.enable()
236+
LLMObs.enable(
237+
agentless_enabled=True,
238+
api_key=llmobs_api_key,
239+
)
233240

234241
logger.debug("datadog_lambda_wrapper initialized")
235242
except Exception as e:

0 commit comments

Comments
 (0)