client.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package redisreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver"
  4. import (
  5. "github.com/go-redis/redis/v7"
  6. )
  7. // Interface for a Redis client. Implementation can be faked for testing.
  8. type client interface {
  9. // retrieves a string of key/value pairs of redis metadata
  10. retrieveInfo() (string, error)
  11. // line delimiter
  12. // redis lines are delimited by \r\n, files (for testing) by \n
  13. delimiter() string
  14. // close release redis client connection pool
  15. close() error
  16. }
  17. // Wraps a real Redis client, implements `client` interface.
  18. type redisClient struct {
  19. client *redis.Client
  20. }
  21. var _ client = (*redisClient)(nil)
  22. // Creates a new real Redis client from the passed-in redis.Options.
  23. func newRedisClient(options *redis.Options) client {
  24. return &redisClient{
  25. client: redis.NewClient(options),
  26. }
  27. }
  28. // Redis strings are CRLF delimited.
  29. func (c *redisClient) delimiter() string {
  30. return "\r\n"
  31. }
  32. // Retrieve Redis INFO. We retrieve all of the 'sections'.
  33. func (c *redisClient) retrieveInfo() (string, error) {
  34. return c.client.Info("all").Result()
  35. }
  36. // close client to release connention pool.
  37. func (c *redisClient) close() error {
  38. return c.client.Close()
  39. }