Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ Main (unreleased)

### Bugfixes


- (_Public Preview_) Additions to `database_observability.postgres` component:
- `schema_details`
- fixes collection of schema details for mixed case table names (@fridgepoet)
- do not track query samples for currently connected user, via option `exclude_current_user` (@cristiangreco)

- (_Public Preview_) Additions to `database_observability.mysql` component:
- replace the internal `server_id` label attribution in favor of a hash composed from `@@server_uuid` and `@@hostname`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ The `aws` block supplies the [ARN](https://docs.aws.amazon.com/IAM/latest/UserGu
|---------------------------|------------|---------------------------------------------------------------|---------|----------|
| `collect_interval` | `duration` | How frequently to collect information from database. | `"15s"` | no |
| `disable_query_redaction` | `bool` | Collect unredacted SQL query text (might include parameters). | `false` | no |
| `exclude_current_user` | `bool` | Do not collect query samples for current database user. | `true` | no |

### `schema_details`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ const selectPgStatActivity = `
s.query_id != 0
)
)
%s
`

const excludeCurrentUserClause = `AND s.usesysid != (select oid from pg_roles where rolname = current_user)`

type QuerySamplesInfo struct {
DatabaseName sql.NullString
DatabaseID int
Expand Down Expand Up @@ -100,13 +103,15 @@ type QuerySamplesArguments struct {
EntryHandler loki.EntryHandler
Logger log.Logger
DisableQueryRedaction bool
ExcludeCurrentUser bool
}

type QuerySamples struct {
dbConnection *sql.DB
collectInterval time.Duration
entryHandler loki.EntryHandler
disableQueryRedaction bool
excludeCurrentUser bool

logger log.Logger
running *atomic.Bool
Expand Down Expand Up @@ -204,14 +209,15 @@ func (w WaitEventIdentity) Equal(other WaitEventIdentity) bool {
}

func NewQuerySamples(args QuerySamplesArguments) (*QuerySamples, error) {
const emittedCacheSize = 1000 //pg_stat_statements default max number of statements to track
const emittedCacheSize = 1000 // pg_stat_statements default max number of statements to track
const emittedCacheTTL = 10 * time.Minute

return &QuerySamples{
dbConnection: args.DB,
collectInterval: args.CollectInterval,
entryHandler: args.EntryHandler,
disableQueryRedaction: args.DisableQueryRedaction,
excludeCurrentUser: args.ExcludeCurrentUser,
logger: log.With(args.Logger, "collector", QuerySamplesCollector),
running: &atomic.Bool{},
samples: map[SampleKey]*SampleState{},
Expand Down Expand Up @@ -275,7 +281,11 @@ func (c *QuerySamples) fetchQuerySample(ctx context.Context) error {
queryTextField = queryTextClause
}

query := fmt.Sprintf(selectPgStatActivity, queryTextField)
excludeCurrentUserClauseField := ""
if c.excludeCurrentUser {
excludeCurrentUserClauseField = excludeCurrentUserClause
}
query := fmt.Sprintf(selectPgStatActivity, queryTextField, excludeCurrentUserClauseField)
rows, err := c.dbConnection.QueryContext(ctx, query)
if err != nil {
return fmt.Errorf("failed to query pg_stat_activity: %w", err)
Expand Down
Loading
Loading