ids.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package prom
  2. import (
  3. "strings"
  4. prometheus "github.com/prometheus/client_golang/api"
  5. )
  6. const (
  7. // PrometheusClientID is the identifier used when creating the client that
  8. // targets prometheus. This can be used to check a specific client instance
  9. // by calling prom.IsClientID(client, prom.PrometheusClientID)
  10. PrometheusClientID string = "Prometheus"
  11. // ThanosClientID is the identifier used when creating the client that
  12. // targets thanos. This can be used to check a specific client instance
  13. // by calling prom.IsClientID(client, prom.ThanosClientID)
  14. ThanosClientID string = "Thanos"
  15. )
  16. // identityClient provides an interface for extracting an indentifer from the client objects
  17. type identityClient interface {
  18. ID() string
  19. }
  20. // IsClientID returns true if the client has an identifier of the specific type.
  21. func IsClientID(cli prometheus.Client, id string) bool {
  22. if cli == nil {
  23. return false
  24. }
  25. if idClient, ok := cli.(identityClient); ok {
  26. return strings.EqualFold(idClient.ID(), id)
  27. }
  28. return false
  29. }
  30. // IsPrometheus returns true if the client provided is used to target prometheus
  31. func IsPrometheus(cli prometheus.Client) bool {
  32. return IsClientID(cli, PrometheusClientID)
  33. }
  34. // IsThanos returns true if the client provided is used to target thanos
  35. func IsThanos(cli prometheus.Client) bool {
  36. return IsClientID(cli, ThanosClientID)
  37. }