redis_svc.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 "strings"
  5. // Wraps a client, parses the Redis info command, returning a string-string map
  6. // containing all of the key value pairs returned by INFO. Takes a line delimiter
  7. // from the passed in client to support testing, because Redis uses CRLF and test
  8. // data uses LF.
  9. type redisSvc struct {
  10. client client
  11. delimiter string
  12. }
  13. // Creates a new redisSvc. Pass in a client implementation.
  14. func newRedisSvc(client client) *redisSvc {
  15. return &redisSvc{
  16. client: client,
  17. delimiter: client.delimiter(),
  18. }
  19. }
  20. // Calls the Redis INFO command on the client and returns an `info` map.
  21. func (p *redisSvc) info() (info, error) {
  22. str, err := p.client.retrieveInfo()
  23. if err != nil {
  24. return nil, err
  25. }
  26. lines := strings.Split(str, p.delimiter)
  27. attrs := make(map[string]string)
  28. for _, line := range lines {
  29. if len(line) == 0 || strings.HasPrefix(line, "#") {
  30. continue
  31. }
  32. pair := strings.Split(line, ":")
  33. if len(pair) == 2 { // defensive, should always == 2
  34. attrs[pair[0]] = pair[1]
  35. }
  36. }
  37. return attrs, nil
  38. }