Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"sync"
"time"

"github.com/blang/semver/v4"
Expand Down Expand Up @@ -112,6 +113,7 @@ type QuerySamples struct {
running *atomic.Bool
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup

timerBookmark float64
lastUptime float64
Expand Down Expand Up @@ -158,16 +160,17 @@ func (c *QuerySamples) Start(ctx context.Context) error {

// Start setup_consumers check goroutine if enabled
if c.autoEnableSetupConsumers {
c.wg.Add(1)
go c.runSetupConsumersCheck()
}

c.wg.Add(1)
go func() {
defer func() {
c.Stop()
c.running.Store(false)
}()
defer c.wg.Done()
defer c.running.Store(false)

ticker := time.NewTicker(c.collectInterval)
defer ticker.Stop()

for {
if err := c.fetchQuerySamples(c.ctx); err != nil {
Expand All @@ -192,11 +195,17 @@ func (c *QuerySamples) Stopped() bool {

// Stop should be kept idempotent
func (c *QuerySamples) Stop() {
c.cancel()
if c.cancel != nil {
c.cancel()
}
c.wg.Wait()
}

func (c *QuerySamples) runSetupConsumersCheck() {
defer c.wg.Done()

ticker := time.NewTicker(c.setupConsumersCheckInterval)
defer ticker.Stop()

for {
if err := c.updateSetupConsumersSettings(c.ctx); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"fmt"
"slices"
"sync"
"time"

"github.com/go-kit/log"
Expand Down Expand Up @@ -123,6 +124,7 @@ type QuerySamples struct {
running *atomic.Bool
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup

// in-memory state of running samples
samples map[SampleKey]*SampleState
Expand Down Expand Up @@ -249,13 +251,13 @@ func (c *QuerySamples) Start(ctx context.Context) error {
c.ctx = ctx
c.cancel = cancel

c.wg.Add(1)
go func() {
defer func() {
c.Stop()
c.running.Store(false)
}()
defer c.wg.Done()
defer c.running.Store(false)

ticker := time.NewTicker(c.collectInterval)
defer ticker.Stop()

for {
if err := c.fetchQuerySample(c.ctx); err != nil {
Expand All @@ -280,7 +282,10 @@ func (c *QuerySamples) Stopped() bool {

// Stop should be kept idempotent
func (c *QuerySamples) Stop() {
c.cancel()
if c.cancel != nil {
c.cancel()
}
c.wg.Wait()
}

func (c *QuerySamples) fetchQuerySample(ctx context.Context) error {
Expand Down Expand Up @@ -396,7 +401,7 @@ func (c *QuerySamples) processRow(sample QuerySamplesInfo) (SampleKey, error) {
return key, nil
}

func (c QuerySamples) validateQuerySample(sample QuerySamplesInfo) error {
func (c *QuerySamples) validateQuerySample(sample QuerySamplesInfo) error {
if c.disableQueryRedaction {
if sample.Query.Valid && sample.Query.String == "<insufficient privilege>" {
return fmt.Errorf("insufficient privilege to access query sample set: %+v", sample)
Expand Down
Loading