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.5KB

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