|
| 1 | +/* |
| 2 | +Copyright 2022 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package repository |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/prometheus/client_golang/prometheus" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/metrics" |
| 22 | + "time" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + ChartRepoTypeHelm = "helm" |
| 27 | + //ChartRepoTypeOCI = "oci" |
| 28 | + ChartRepoEventTypeDownloadIndex = "chart_repository_download" |
| 29 | + ChartRepoEventTypeDownloadChart = "chart_download" |
| 30 | +) |
| 31 | + |
| 32 | +// Recorder is a recorder for chart repository events. |
| 33 | +type Recorder struct { |
| 34 | + // TODO: type up the metrics and talk to aryan9600 |
| 35 | + // TODO: split this counter?? |
| 36 | + chartRepoEventsCounter *prometheus.CounterVec |
| 37 | + durationHistogram *prometheus.HistogramVec |
| 38 | +} |
| 39 | + |
| 40 | +// NewRepositoryRecorder returns a new Recorder. |
| 41 | +// The configured labels are: event_type, name, namespace. |
| 42 | +// The event_type is one of: |
| 43 | +// - "chart_repository_download" |
| 44 | +// - "chart_download" |
| 45 | +// The url is the url of the helm chart repository |
| 46 | +func NewRepositoryRecorder() *Recorder { |
| 47 | + return &Recorder{ |
| 48 | + chartRepoEventsCounter: prometheus.NewCounterVec( |
| 49 | + prometheus.CounterOpts{ |
| 50 | + Name: "gotk_chart_repository_events_total", |
| 51 | + Help: "Total number of events for a Helm Chart Repository.", |
| 52 | + }, |
| 53 | + []string{"name", "repo_type", "namespace", "url", "checksum"}, |
| 54 | + ), |
| 55 | + durationHistogram: prometheus.NewHistogramVec( |
| 56 | + prometheus.HistogramOpts{ |
| 57 | + Name: "gotk_chart_repository_event_duration_seconds", |
| 58 | + Help: "The duration in seconds of an event for a Helm Chart Repository.", |
| 59 | + Buckets: prometheus.ExponentialBuckets(10e-9, 10, 10), |
| 60 | + }, |
| 61 | + []string{"name", "repo_type", "namespace", "url", "checksum"}, |
| 62 | + ), |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// ChartRepoCollectors returns the metrics.Collector objects for the Recorder. |
| 67 | +func (r *Recorder) ChartRepoCollectors() []prometheus.Collector { |
| 68 | + return []prometheus.Collector{ |
| 69 | + r.chartRepoEventsCounter, |
| 70 | + r.durationHistogram, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// IncChartRepoEvents increment by 1 the chart repo event count for the given event type, url and checksum. |
| 75 | +func (r *Recorder) IncChartRepoEvents(event, repoType, url, checksum, namespace string) { |
| 76 | + r.chartRepoEventsCounter.WithLabelValues(event, repoType, url, checksum, namespace).Inc() |
| 77 | +} |
| 78 | + |
| 79 | +// RecordChartRepoEventDuration records the duration since start for the given ref. |
| 80 | +func (r *Recorder) RecordChartRepoEventDuration(event, repoType, namespace, url string, start time.Time) { |
| 81 | + r.durationHistogram.WithLabelValues(event, repoType, namespace, url).Observe(time.Since(start).Seconds()) |
| 82 | +} |
| 83 | + |
| 84 | +// MustMakeMetrics creates a new Recorder, and registers the metrics collectors in the controller-runtime metrics registry. |
| 85 | +func MustMakeMetrics() *Recorder { |
| 86 | + r := NewRepositoryRecorder() |
| 87 | + metrics.Registry.MustRegister(r.ChartRepoCollectors()...) |
| 88 | + |
| 89 | + return r |
| 90 | +} |
0 commit comments