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

[receiver/redisreceiver] add scraper shutdown #17491

Merged
merged 4 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions receiver/redisreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type client interface {
// line delimiter
// redis lines are delimited by \r\n, files (for testing) by \n
delimiter() string
// close
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid comments that don't provide any value

close() error
}

// Wraps a real Redis client, implements `client` interface.
Expand All @@ -50,3 +52,8 @@ func (c *redisClient) delimiter() string {
func (c *redisClient) retrieveInfo() (string, error) {
return c.client.Info("all").Result()
}

// close.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// close.

func (c *redisClient) close() error {
return c.client.Close()
}
4 changes: 4 additions & 0 deletions receiver/redisreceiver/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (fakeClient) retrieveInfo() (string, error) {
return readFile("info")
}

func (fakeClient) close() error {
return nil
}

func readFile(fname string) (string, error) {
file, err := os.ReadFile(filepath.Join("testdata", fname+".txt"))
if err != nil {
Expand Down
15 changes: 14 additions & 1 deletion receiver/redisreceiver/redis_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
// Runs intermittently, fetching info from Redis, creating metrics/datapoints,
// and feeding them to a metricsConsumer.
type redisScraper struct {
client client
redisSvc *redisSvc
settings component.TelemetrySettings
mb *metadata.MetricsBuilder
Expand All @@ -58,11 +59,23 @@ func newRedisScraper(cfg *Config, settings receiver.CreateSettings) (scraperhelp

func newRedisScraperWithClient(client client, settings receiver.CreateSettings, cfg *Config) (scraperhelper.Scraper, error) {
rs := &redisScraper{
client: client,
redisSvc: newRedisSvc(client),
settings: settings.TelemetrySettings,
mb: metadata.NewMetricsBuilder(cfg.Metrics, settings),
}
return scraperhelper.NewScraper(typeStr, rs.Scrape)
return scraperhelper.NewScraper(
typeStr,
rs.Scrape,
scraperhelper.WithShutdown(rs.shutdown),
)
}

func (rs *redisScraper) shutdown(context.Context) error {
if rs.client != nil {
return rs.client.close()
}
return nil
}

// Scrape is called periodically, querying Redis and building Metrics to send to
Expand Down