-
Notifications
You must be signed in to change notification settings - Fork 153
Feature request: set correlation ID in Logger #2863
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
Comments
I'm sad to see this feature having only 1 👍🏻 I'm sure this is something multiple people would (should?) be using, especially in event source contexts. We have a lot of services that works like It would be great to have this functionality baked in PowerTools so we could just do Hopefully this gets more traction so we can get feature parity with the Python version 🙏🏻 |
Hi @thiagomeireless - thank you for the interest in the feature. My main concern in adding the feature is having an extra dependency to Logger, which is widely used. I'd like to find a way to make it optional unless you're using the feature, but I don't know how yet. |
I made a quick PoC of how this could look, and the DX would look something like this: import { Logger } from '@aws-lambda-powertools/logger';
import { search } from '@aws-lambda-powertools/logger/correlationId';
const logger = new Logger({
logLevel: 'debug',
correlationIdSearchFn: search,
}); Behind the scenes the import { search as JMESPathSearch } from '@aws-lambda-powertools/jmespath';
import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
const search = (expression, data) => JMESPathSearch(expression, data, { customFunctions: new PowertoolsFunctions() })
export { search } Then later on customers could specify a By passing the reference to a JMESPath search function and exposing the JMESPath search function under a sub-path export, we can make the search function optional, and the JMESPath package a peer dependency so that customers who are not using the feature don't need to pay the overhead for it. Implementation details
{
correlationIdSearchFn?: (expression: string, data: JSONObject) => unknown;
}
|
I ran some tests to understand the impact of the jmespath dependency for the logger, and the results were around 70kb of added code that customers won't use, if they don't use this feature. I arrived at the same conclusion to have a wrapper around the The only disadvantage I see is that we would replicate the search signature in the logger, or other functions that we would add over time, i.e. envelopes. But I don't see the changes happening often, thus it's acceptable to maintain it in two places. I will now move ahead and submit a PR for review. |
This issue is now closed. Please be mindful that future comments are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so. |
This is now released under v2.17.0 version! |
Use case
When working with logs in distributed systems like AWS Lambda, customers want to be able to set arbitrary correlation IDs for their workloads.
Today this is possible using existing Logger features (see "Alternative solutions" section), however the experience is somewhat verbose and could be improved.
Powertools for AWS Lambda (Python) has a feature that allows customers to include a
correlation_id
key to each log. This feature request proposes to implement the same feature in this version of Powertools for AWS.This would be a first, low hanging, effort for allowing customers to more easily work with correlation IDs with Powertools for AWS (see #129).
Important
If you're interested in this feature, please consider leaving a 👍 under this post, or leave a comment - measuring demand and having customer names helps us prioritize features in our backlog.
Solution/User Experience
The new feature is primarily aimed at customers using the injectLambdaContext class method decorator and Middy.js middleware.
Given an event with this type:
When using decorators, the experience would look like this:
When using the Middy.js middleware, the experience would instead look like this:
For the manual instrumentation the method is less compelling, but also a very low hanging fruit as it would just be an alias for
logger.appendKeys({ correlation_id: ... })
(see docs):Each one of these snippets would generate a log entry similar to this:
The feature would also come with a handful built-in correlation ID expressions, for example:
API_GATEWAY_REST
=>"requestContext.requestId"
APPSYNC_RESOLVER
=>'request.headers."x-amzn-trace-id"'
These would be used like this:
Open questions
Note
The points below should be addressed before the feature is ready to be implemented, if you want to contribute please help us instead of opening a PR.
The current implementation in Powertools for AWS Lambda (Python) relies on JMESPath expressions for extracting the correlation ID from the payload. This choice was sensible for them both because of their ecosystem but also because they already have the
jmespath
module in their dependency tree because of other features.For us this is still a decision point to be addressed. Taking a dependency on
@aws-lambda-powertools/jmespath
that we released for the Idempotency utility would be easy in terms of complexity, however before moving forward with this I’d like us to explore 1/ whether it’s possible to set the dependency as optionalpeerDependency
and load it only when using the feature, and if not, 2/ whether the impact on the bundle is acceptable.I am not particularly inclined towards other JSON-query languages at this stage, but I am open to consider proposals if any. However implementing a function-based query feature (i.e.
correlationId: (event) => (event.headers.my_request_id_header)
) is out of question because of the reasons described in #1644 (unless anyone has compelling arguments).Alternative solutions
import { Logger } from '@aws-lambda-powertools/logger'; const logger = new Logger(); export const handler = async (event: MyEvent) => { logger.setCorrelationId(event.headers?.headers.my_request_id_heeader); // ... }
Acknowledgment
Future readers
Please react with 👍 and your use case to help us understand customer demand.
The text was updated successfully, but these errors were encountered: