From 19c5e1eff1dde7a7a32e0c91985748483b0daaa7 Mon Sep 17 00:00:00 2001 From: adrien2p Date: Thu, 13 Oct 2022 22:46:00 +0200 Subject: [PATCH] feat(api): Add support for equation in 'query' query param for sentry end points --- .../src/api/handlers/sentry-transaction-events.ts | 9 +++++++-- .../src/api/handlers/sentry-transaction.ts | 9 +++++++-- packages/medusa-plugin-sentry/src/services/sentry.ts | 11 ++++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/packages/medusa-plugin-sentry/src/api/handlers/sentry-transaction-events.ts b/packages/medusa-plugin-sentry/src/api/handlers/sentry-transaction-events.ts index c8dd3ed..c489ecb 100644 --- a/packages/medusa-plugin-sentry/src/api/handlers/sentry-transaction-events.ts +++ b/packages/medusa-plugin-sentry/src/api/handlers/sentry-transaction-events.ts @@ -1,12 +1,12 @@ import { Request, Response } from 'express'; import { validator } from '@medusajs/medusa/dist/utils/validator'; import SentryService from '../../services/sentry'; -import { IsString } from 'class-validator'; +import { IsOptional, IsString } from 'class-validator'; import { GetSentryTransactionsParams } from './sentry-transaction'; export default (token: string) => { return async (req: Request, res: Response) => { - const { transaction, organisation, project, statsPeriod, perPage, cursor } = await validator( + const { transaction, organisation, project, statsPeriod, perPage, cursor, query } = await validator( GetSentryTransactionEventsParams, req.query ); @@ -16,6 +16,7 @@ export default (token: string) => { transaction, organisation, project, + query, statsPeriod, perPage, cursor, @@ -28,4 +29,8 @@ export default (token: string) => { export class GetSentryTransactionEventsParams extends GetSentryTransactionsParams { @IsString() transaction: string; + + @IsOptional() + @IsString() + query?: string } diff --git a/packages/medusa-plugin-sentry/src/api/handlers/sentry-transaction.ts b/packages/medusa-plugin-sentry/src/api/handlers/sentry-transaction.ts index 6d4229a..857a132 100644 --- a/packages/medusa-plugin-sentry/src/api/handlers/sentry-transaction.ts +++ b/packages/medusa-plugin-sentry/src/api/handlers/sentry-transaction.ts @@ -5,15 +5,16 @@ import { IsOptional, IsString } from 'class-validator'; export default (token: string) => { return async (req: Request, res: Response) => { - const { organisation, project, statsPeriod, perPage, cursor } = await validator( + const { organisation, project, statsPeriod, perPage, cursor, query } = await validator( GetSentryTransactionsParams, req.query ); const sentryService: SentryService = req.scope.resolve(SentryService.RESOLVE_KEY); - const result = await sentryService.fetchSentryTransactions({ + const result = await sentryService.fetchTransactions({ organisation, project, + query, statsPeriod, perPage, cursor, @@ -40,4 +41,8 @@ export class GetSentryTransactionsParams { @IsOptional() @IsString() cursor?: string; + + @IsOptional() + @IsString() + query?: string; } diff --git a/packages/medusa-plugin-sentry/src/services/sentry.ts b/packages/medusa-plugin-sentry/src/services/sentry.ts index e4bab2c..e0a4c4c 100644 --- a/packages/medusa-plugin-sentry/src/services/sentry.ts +++ b/packages/medusa-plugin-sentry/src/services/sentry.ts @@ -41,15 +41,17 @@ export default class SentryService extends TransactionBaseService { * Fetch paginated transactions from an organisation project on sentry * @param organisation The organisation on which to fetch the transactions * @param project The project in the organisation on which to fetch the transactions + * @param query Equation to filter the result (https://docs.sentry.io/product/sentry-basics/search/) * @param statsPeriod The period from when to fetch the transactions (default: 24h) * @param perPage The number of transaction per page * @param token The token to use to send request to sentry * @param cursor The cursor to send to fetch the transactions for a given page * @return The result is composed of the data and the next cursor for the pagination purpose */ - async fetchSentryTransactions({ + async fetchTransactions({ organisation, project, + query, statsPeriod, perPage, token, @@ -61,8 +63,9 @@ export default class SentryService extends TransactionBaseService { field: 'transaction', per_page: Number(perPage), project, - query: `event.type:transaction`, + query: `event.type:transaction${query ? " AND " + query : ""}`, statsPeriod, + sort: '-transaction', // The three values from cursor are: cursor identifier (integer, usually 0), row offset, and is_prev (1 or 0). // e.g 0:10:0 cursor, @@ -81,6 +84,7 @@ export default class SentryService extends TransactionBaseService { * @param transaction The transaction for which to fetch the events (e.g "GET /admin/users") * @param organisation The organisation on which to fetch the transactions * @param project The project in the organisation on which to fetch the transactions + * @param query Equation to filter the result (https://docs.sentry.io/product/sentry-basics/search/) * @param statsPeriod The period from when to fetch the transactions (default: 24h) * @param perPage The number of transaction per page * @param token The token to use to send request to sentry @@ -91,6 +95,7 @@ export default class SentryService extends TransactionBaseService { transaction, organisation, project, + query, statsPeriod, perPage, token, @@ -102,7 +107,7 @@ export default class SentryService extends TransactionBaseService { field: ['id', 'transaction.duration', 'timestamp', 'spans.db'], per_page: Number(perPage), project, - query: `event.type:transaction transaction:"${transaction}"`, + query: `event.type:transaction AND transaction:"${transaction}"${query ? " AND " + query : ""}`, statsPeriod, sort: '-timestamp', // The three values from cursor are: cursor identifier (integer, usually 0), row offset, and is_prev (1 or 0).