GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

monitoring.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Monitoring middlewares for registry interfaces
  2. package registry
  3. import (
  4. "strconv"
  5. "time"
  6. "github.com/go-kit/kit/metrics/prometheus"
  7. stdprometheus "github.com/prometheus/client_golang/prometheus"
  8. "github.com/weaveworks/flux"
  9. fluxmetrics "github.com/weaveworks/flux/metrics"
  10. )
  11. const (
  12. LabelRequestKind = "kind"
  13. RequestKindTags = "tags"
  14. RequestKindMetadata = "metadata"
  15. )
  16. var (
  17. fetchDuration = prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
  18. Namespace: "flux",
  19. Subsystem: "registry",
  20. Name: "fetch_duration_seconds",
  21. Help: "Duration of Image metadata fetches, in seconds.",
  22. Buckets: stdprometheus.DefBuckets,
  23. }, []string{fluxmetrics.LabelSuccess})
  24. requestDuration = prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
  25. Namespace: "flux",
  26. Subsystem: "registry",
  27. Name: "request_duration_seconds",
  28. Help: "Duration of HTTP requests made in the course of fetching Image metadata",
  29. }, []string{LabelRequestKind, fluxmetrics.LabelSuccess})
  30. )
  31. type InstrumentedRegistry Registry
  32. type instrumentedRegistry struct {
  33. next Registry
  34. }
  35. func NewInstrumentedRegistry(next Registry) InstrumentedRegistry {
  36. return &instrumentedRegistry{
  37. next: next,
  38. }
  39. }
  40. func (m *instrumentedRegistry) GetRepository(repository Repository) (res []flux.Image, err error) {
  41. start := time.Now()
  42. res, err = m.next.GetRepository(repository)
  43. fetchDuration.With(
  44. fluxmetrics.LabelSuccess, strconv.FormatBool(err == nil),
  45. ).Observe(time.Since(start).Seconds())
  46. return
  47. }
  48. func (m *instrumentedRegistry) GetImage(repository Repository, tag string) (res flux.Image, err error) {
  49. start := time.Now()
  50. res, err = m.next.GetImage(repository, tag)
  51. fetchDuration.With(
  52. fluxmetrics.LabelSuccess, strconv.FormatBool(err == nil),
  53. ).Observe(time.Since(start).Seconds())
  54. return
  55. }
  56. type InstrumentedClient Client
  57. type instrumentedClient struct {
  58. next Client
  59. }
  60. func NewInstrumentedClient(next Client) Client {
  61. return &instrumentedClient{
  62. next: next,
  63. }
  64. }
  65. func (m *instrumentedClient) Manifest(repository Repository, tag string) (res flux.Image, err error) {
  66. start := time.Now()
  67. res, err = m.next.Manifest(repository, tag)
  68. requestDuration.With(
  69. LabelRequestKind, RequestKindMetadata,
  70. fluxmetrics.LabelSuccess, strconv.FormatBool(err == nil),
  71. ).Observe(time.Since(start).Seconds())
  72. return
  73. }
  74. func (m *instrumentedClient) Tags(repository Repository) (res []string, err error) {
  75. start := time.Now()
  76. res, err = m.next.Tags(repository)
  77. requestDuration.With(
  78. LabelRequestKind, RequestKindTags,
  79. fluxmetrics.LabelSuccess, strconv.FormatBool(err == nil),
  80. ).Observe(time.Since(start).Seconds())
  81. return
  82. }
  83. func (m *instrumentedClient) Cancel() {
  84. m.next.Cancel()
  85. }