Skip to content

Commit

Permalink
feat(api): Add support for equation in 'query' query param for sentry…
Browse files Browse the repository at this point in the history
… end points
  • Loading branch information
adrien2p committed Oct 13, 2022
1 parent d14d8f2 commit 19c5e1e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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
);
Expand All @@ -16,6 +16,7 @@ export default (token: string) => {
transaction,
organisation,
project,
query,
statsPeriod,
perPage,
cursor,
Expand All @@ -28,4 +29,8 @@ export default (token: string) => {
export class GetSentryTransactionEventsParams extends GetSentryTransactionsParams {
@IsString()
transaction: string;

@IsOptional()
@IsString()
query?: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -40,4 +41,8 @@ export class GetSentryTransactionsParams {
@IsOptional()
@IsString()
cursor?: string;

@IsOptional()
@IsString()
query?: string;
}
11 changes: 8 additions & 3 deletions packages/medusa-plugin-sentry/src/services/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -91,6 +95,7 @@ export default class SentryService extends TransactionBaseService {
transaction,
organisation,
project,
query,
statsPeriod,
perPage,
token,
Expand All @@ -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).
Expand Down

0 comments on commit 19c5e1e

Please sign in to comment.