aws / aws-xray-sdk-node

The official AWS X-Ray SDK for Node.js.
Apache License 2.0
272 stars 155 forks source link

TypeORM support? #272

Open agustinhaller opened 4 years ago

agustinhaller commented 4 years ago

Hi, I'm using AWS RDS (Postgres) through TypeORM in a NestJs framework API. I would like to enable tracing RDS with X-Ray, but I'm unable to do so as TypeORM handles creating the pg connection.

Can you point me in the right direction? I found this issue but seems abandoned.

willarmiros commented 4 years ago

Hi @agustinhaller, Thanks for the request. It seems that issue got closed due to a narrower PR, but a request for TypeORM support is noted. We've also recently added a generic SQL recorder in our Java SDK and are adding support for entity framework in .NET.

eltonio450 commented 4 years ago

hi there,

would it be possible to make it work via

var mock = require('mock-require');
var AWSXRay = require('aws-xray-sdk');
var spiedPg = AWSXRay.capturePostgres(require('pg'));
mock('pg',spiedPg);

I'll give a try asap but if you have insights on why it could/could not work, it'd be cool :)

Best

Nicoowr commented 4 years ago

Hi,

Using AWSXRay.capturePostgres(pg); seems to be enough in order to capture the requests to the database.

However, I could not find how to capture the SQL request as well. It seems there is some complicated ways to do it here though: https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-nodejs-sqlclients.html

Would be glad to have any insight on this issue :)

willarmiros commented 4 years ago

Hi @Nicoowr, Currently the X-Ray SDK does not add SQL queries to subsegments, and the workaround you linked is the only way to add them at this point. We did not deem it secure to add queries with potentially sensitive information to subsegments by default. However we have been phasing in an option to add the query string to SQL subsegments on an opt-in basis, e.g. like they are in our .NET SDK. If you or another member of the community would be interested in making such a PR we'd be open to it!

revmischa commented 4 years ago

Would really like TypeORM support

ravitejavgyan commented 3 years ago

Hi,

I am using sequelize with mysql db for nodejs. DB Queries are not traced by x-ray. Is it possible or not. Can you please tell me and may i know the status?

Thanks, Raviteja

willarmiros commented 3 years ago

@ravitejavgyan this issue is open as a feature request for TypeORM instrumentation. Sequelize is a separate matter but also not directly supported yet.

@revmischa I do not have updates on adding TypeORM support in this SDK. However AWS X-Ray provides beta support for the OpenTelemetry JS SDK & API. There are extensions for the OpenTelemetry SDK that allow for TypeORM instrumentation. To learn more about OpenTelemetry JavaScript instrumentation with X-Ray as the backend, you can checkout the documentation and our AWS Observability repos.

DanEscudero commented 2 years ago

Hi

I managed to get X-Ray working with TypeORM, although it's a sketchy solution.

Based on this documentation, I managed to override QueryRunner.query, similar to the way X-Ray SDK does.

In my case, I'm using mysql, so my code looks like:

const { MysqlQueryRunner } = require('typeorm/driver/mysql/MysqlQueryRunner');
MysqlQueryRunner.prototype.old_query = MysqlQueryRunner.prototype.query;

MysqlQueryRunner.prototype.query = function (
  query: string,
  parameters?: any[],
  useStructuredResult?: boolean
) {
  return captureAsyncFunc('QueryRunner.query', async (segment: Subsegment | undefined) => {
    const result = await MysqlQueryRunner.prototype.old_query.bind(this)(
      query,
      parameters,
      useStructuredResult
    );

    if (segment) {
      const _segment = segment as any;

      _segment.sql = { sanitized_query: query };
      _segment.close();
    }

    return result;
  });
};
allonzy commented 2 years ago

I have a pretty smooth workaround using webpack (already have it in the project):

//webpack.config.js
...
const bootstrapMysqlXray = new webpack.NormalModuleReplacementPlugin(/^mysql2$/, '/path/to/bootstrapMysql.ts');
...
plugins: [
        ...
        bootstrapMysqlXray
        ...
    ]
//bootstrapMysql.ts
import { captureMySQL } from 'aws-xray-sdk';
import mysql from 'mysql';
// /!\ Do not import 'mysql2', it is replace by this module, creating a dependency loop
import mysql2 from '../../../node_modules/mysql2';

module.exports = captureMySQL(mysql2 as unknown as typeof mysql) as unknown as typeof mysql2;

I'm using TS, but it works in vanilla js too (replace imports by require)

I'm also applying this trick to inject xray on http call (using axios), and on aws-sdk