Skip to content

Commit

Permalink
feat: sql compare (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomer-friedman authored Jun 13, 2023
1 parent 4177f62 commit 5834c74
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/jest-otel/syntax/db-pg.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ So, a complete assertion can look like:
expectTrace(traceloop.serviceByName('orders-service'))
.toQueryPostgreSQL()
.withDatabaseName('postgres')
.withOperationAndTable('INSERT', 'orders')
.withStatement(
/INSERT INTO orders \(id, price_in_cents\) VALUES \('[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}', [0-9]+\)/,
{ compareType: COMPARE_TYPE.REGEX },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('postgresql query', () => {
expectTrace(traceloop.serviceByName('orders-service'))
.toQueryPostgreSQL()
.withDatabaseName('postgres', { compareType: COMPARE_TYPE.EQUALS })
.withOperationAndTable('INSERT', 'orders')
.withStatement(
/INSERT INTO orders \(id, price_in_cents\) VALUES \('[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}', [0-9]+\)/,
{ compareType: COMPARE_TYPE.REGEX },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const filterByAttributeKey = (
attName: string,
) =>
spans.filter((span) => {
span.attributes?.find((attribute) => {
return span.attributes?.find((attribute) => {
return attribute.key === attName;
});
});
Expand Down
28 changes: 26 additions & 2 deletions packages/expect-opentelemetry/src/resources/postgresql-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class PostgreSQLQuery {
private readonly serviceName: string,
) {}

withDatabaseName(name: string | RegExp, options: CompareOptions) {
withDatabaseName(name: string | RegExp, options?: CompareOptions) {
const filteredSpans = filterByAttributeStringValue(
this.spans,
SemanticAttributes.DB_NAME,
Expand All @@ -28,7 +28,7 @@ export class PostgreSQLQuery {
return new PostgreSQLQuery(filteredSpans, this.serviceName);
}

withStatement(statement: string | RegExp, options: CompareOptions) {
withStatement(statement: string | RegExp, options?: CompareOptions) {
const filteredSpans = filterByAttributeStringValue(
this.spans,
SemanticAttributes.DB_STATEMENT,
Expand All @@ -44,4 +44,28 @@ export class PostgreSQLQuery {

return new PostgreSQLQuery(filteredSpans, this.serviceName);
}

withOperationAndTable(operation: string, table: string) {
const regex = new RegExp(`${operation}.*${table}`, 'i'); // case insensitive

const filteredSpans = this.spans.filter((span) => {
const statement = span.attributes?.find(
(attribute) => attribute.key === SemanticAttributes.DB_STATEMENT,
)?.value?.stringValue;

if (!statement) {
return false;
}

return regex.test(statement);
});

if (filteredSpans.length === 0) {
throw new Error(
`No query by ${this.serviceName} to postgresql with operation ${operation} and table ${table} was found`,
);
}

return new PostgreSQLQuery(filteredSpans, this.serviceName);
}
}

0 comments on commit 5834c74

Please sign in to comment.