How to trace mysql database calls #2027
-
I have a lambda function making database calls to aws rds mysql. Currently im using the prisma orm. I saw in the aws xray documentation there is a way to capture mysql https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-sqlclients.html
Ive tried using the escape hatch https://docs.powertools.aws.dev/lambda/typescript/latest/core/tracer/#escape-hatch-mechanism But there is no captureMySql on the .provider
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, at the moment the Tracer does not support that X-Ray feature, we have a feature request (#749) that has been open for a while but hasn't gotten a lot of traction (so far at least). The reason for this not being available in Powertools is that to keep bundle size and performance in check we are using only the Given our project's tenets, specifically the "Keep it lean" one, we usually add additional dependencies only whenever strictly necessary. This usually means whenever there's enough demand. As a first step, I'd encourage you to chime in on the issue I linked (#749) as well as reacting with a 👍 on the original message. Every quarter or so we re-evaluate our project's roadmap and in most cases we rank feature requests based on how many customers are interested in them. As an alternative solution, and until the feature is available (if ever), you could still trace the queries by creating a custom segment and annotating it with the query you're running, for example: import { Tracer } from '@aws-lambda-powertools/tracer';
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
const mySqlClient = {}
const runTracedQuery = async (query: unknown): Promise<unknown> => {
const parentSubsegment = tracer.getSegment(); // This is the subsegment currently active
let subsegment;
if (parentSubsegment) {
// Create subsegment for the function & set it as active
subsegment = parentSubsegment.addNewSubsegment(`### chargeId`);
tracer.setSegment(subsegment);
}
// add the query as metadata
tracer.putMetadata('query', query);
let res;
try {
/* run your query here using your client */
res = await mySqlClient(query);
// Add the response as metadata
tracer.addResponseAsMetadata(res, 'queryResult');
} catch (err) {
// Add the error as metadata
tracer.addErrorAsMetadata(err as Error);
throw err;
}
if (parentSubsegment && subsegment) {
// Close subsegment (the AWS Lambda one is closed automatically)
subsegment.close();
// Set the facade segment as active again
tracer.setSegment(parentSubsegment);
}
return res;
}; Note that I have not tested the code above, but it's a refactoring of the snippets found here and here. Hope it helps! |
Beta Was this translation helpful? Give feedback.
Hi, at the moment the Tracer does not support that X-Ray feature, we have a feature request (#749) that has been open for a while but hasn't gotten a lot of traction (so far at least).
The reason for this not being available in Powertools is that to keep bundle size and performance in check we are using only the
aws-xray-sdk-core
package (npm) while the features you've read about are available as a separate packageaws-xray-sdk-mysql
(npm).Given our project's tenets, specifically the "Keep it lean" one, we usually add additional dependencies only whenever strictly necessary. This usually means whenever there's enough demand.
As a first step, I'd encourage you to chime in on the issue I l…