Skip to content

feat: Make ApiKeyFactory return Option<String> #25

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

Merged
merged 2 commits into from
Jul 16, 2025

Conversation

lym953
Copy link
Contributor

@lym953 lym953 commented Jul 7, 2025

What does this PR do?

  1. Right now ApiKeyFactory returns String. This PR makes it return Option<String>.
  2. In Flusher, if API key resolution fails, then make flush_metrics() a no-op by returning early.

Motivation

API key resolution can fail. In this case, we should allow the resolver function to return None.

Additional Notes

Describe how to test/QA your changes

Used and tested in bottlecap: DataDog/datadog-lambda-extension#732

@lym953 lym953 marked this pull request as ready for review July 7, 2025 21:12
@lym953 lym953 merged commit 9851203 into main Jul 16, 2025
26 checks passed
@lym953 lym953 deleted the yiming.luo/api-key-option branch July 16, 2025 21:05
lym953 added a commit to DataDog/datadog-lambda-extension that referenced this pull request Jul 17, 2025
# Context
The previous PR
#717 defers API
key resolution from extension init stage to flush time. However, that PR
doesn't well handle the failure case.
- Before that PR, if resolution fails in init stage, the extension will
run an idle loop.
- After that PR, the extension will crash at flush time, which will kill
the runtime as well, which is not desired.

# What does this PR do?
1. For traces, defer key resolution from
`TraceProcessor.process_traces()` to `TraceFlusher.flush()`.
- (This should ideally be in the previous PR, but since that is already
approved, let me add this change in this new PR.)
2. If resolution fails at flush time, then make flush a no-op, so the
extension can keep running and consume events without crashing.

# Dependencies
1. DataDog/serverless-components#25
2. DataDog/libdatadog#1140

# Manual Test

## Steps
1. Create a layer in sandbox
2. Apply the layer to a Lambda function
3. Set the env var `DD_API_KEY_SECRET_ARN` to an invalid value
5. Run the Lambda
6. Then set `DD_API_KEY_SECRET_ARN` to a valid value
7. Run the Lambda

## Result
1. The function was successful
<img width="319" alt="image"
src="https://github.com/user-attachments/assets/f8a5cb36-f678-4643-ba1c-85f41256ffa1"
/>

2. The extension printed some error logs
<img width="737" height="33" alt="image"
src="https://github.com/user-attachments/assets/22553d24-e1f5-4ee5-9a91-0d18e3e2f297"
/>

<img width="603" height="186" alt="image"
src="https://github.com/user-attachments/assets/e797f991-ecba-45f0-8f49-7b7b59dd9e7b"
/>

3. With valid secret ARN, the Lambda runs successfully and reports to
Datadog
<img width="678" height="150" alt="image"
src="https://github.com/user-attachments/assets/073089f8-1e9a-4728-b8d1-1db7aa85d031"
/>

<img width="533" height="96" alt="image"
src="https://github.com/user-attachments/assets/d5f2b81c-5e02-42bc-b3ef-85e611228fc6"
/>


# Automated Test

I didn't add any automated test because from what I see in the codebase,
existing tests are usually unit tests for short functions and not for
long functions that this PR touches. Please let me know if you think I
should add automated tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants