Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Omit queries from QueryLog plugin #77

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions plugins/query-log/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class QueryLogPlugin extends StarbasePlugin {
executionContext?: ExecutionContext
// Add TTL configuration (default 1 day)
private readonly ttl: number = 1
// Enables the omission of queries who contain the prefix
omitEnabled?: boolean = false
// Prefix that if a query contains it won't be logged
omitPrefix: string = '--OMIT'

state = {
startTime: new Date(),
Expand All @@ -28,9 +32,18 @@ export class QueryLogPlugin extends StarbasePlugin {
query: '',
}

constructor(opts?: { ctx?: ExecutionContext }) {
constructor(opts?: {
ctx?: ExecutionContext
enableOmit?: boolean
omitPrefix?: string
}) {
super('starbasedb:query-log')
this.executionContext = opts?.ctx
this.omitEnabled = opts?.enableOmit

if (opts?.omitPrefix) {
this.omitPrefix = opts?.omitPrefix
}
}

override async register(app: StarbaseApp) {
Expand Down Expand Up @@ -74,8 +87,18 @@ export class QueryLogPlugin extends StarbasePlugin {
this.state.totalTime =
this.state.endTime.getTime() - this.state.startTime.getTime()

// Queries can be omitted from
if (opts.dataSource) {
this.addQuery(opts?.dataSource)
if (
(this.omitEnabled &&
!opts.sql
.toUpperCase()
.trim()
.startsWith(this.omitPrefix)) ||
!this.omitEnabled
) {
this.addQuery(opts?.dataSource)
}
}

// Do a purge action for older than TTL items
Expand Down
Loading