ids.go 966 B

12345678910111213141516171819202122232425262728293031323334353637
  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. )
  12. // identityClient provides an interface for extracting an indentifer from the client objects
  13. type identityClient interface {
  14. ID() string
  15. }
  16. // IsClientID returns true if the client has an identifier of the specific type.
  17. func IsClientID(cli prometheus.Client, id string) bool {
  18. if cli == nil {
  19. return false
  20. }
  21. if idClient, ok := cli.(identityClient); ok {
  22. return strings.EqualFold(idClient.ID(), id)
  23. }
  24. return false
  25. }
  26. // IsPrometheus returns true if the client provided is used to target prometheus
  27. func IsPrometheus(cli prometheus.Client) bool {
  28. return IsClientID(cli, PrometheusClientID)
  29. }