|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/prometheus/client_golang/prometheus" |
| 8 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 9 | + log "github.com/sirupsen/logrus" |
| 10 | + "github.com/utilitywarehouse/go-operational/op" |
| 11 | +) |
| 12 | + |
| 13 | +type Prometheus struct { |
| 14 | + createSnapshotSuccess *prometheus.CounterVec |
| 15 | + deleteSnapshotSuccess *prometheus.CounterVec |
| 16 | + operationSuccess *prometheus.CounterVec |
| 17 | +} |
| 18 | + |
| 19 | +// PrometheusInterface allows for mocking out the functionality of Prometheus when testing the full process of an apply run. |
| 20 | +type PrometheusInterface interface { |
| 21 | + Init() |
| 22 | + UpdateCreateSnapshotStatus(disk string, success bool) |
| 23 | + UpdateDeleteSnapshotStatus(disk string, success bool) |
| 24 | + UpdateOperationStatus(operation_type string, success bool) |
| 25 | +} |
| 26 | + |
| 27 | +func (p *Prometheus) Init() { |
| 28 | + p.createSnapshotSuccess = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 29 | + Name: "gcp_disk_snapshotter_create_api_call_count", |
| 30 | + Help: "Success metric for snapshots created per disk", |
| 31 | + }, |
| 32 | + []string{ |
| 33 | + // Path of the file that was applied |
| 34 | + "disk", |
| 35 | + // Result: true if creation api call was successful, false otherwise |
| 36 | + "success", |
| 37 | + }, |
| 38 | + ) |
| 39 | + p.deleteSnapshotSuccess = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 40 | + Name: "gcp_disk_snapshotter_delete_api_call_count", |
| 41 | + Help: "Success metric for snapshots deletion per disk", |
| 42 | + }, |
| 43 | + []string{ |
| 44 | + // Path of the file that was applied |
| 45 | + "disk", |
| 46 | + // Result: true if deletion api call was successful, false otherwise |
| 47 | + "success", |
| 48 | + }, |
| 49 | + ) |
| 50 | + p.operationSuccess = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 51 | + Name: "gcp_disk_snapshotter_operation_count", |
| 52 | + Help: "Success metric for operations initiated by disk snapshotter", |
| 53 | + }, |
| 54 | + []string{ |
| 55 | + // Global or Zonal |
| 56 | + "operation_type", |
| 57 | + // Result: true if the operation was successful, false otherwise |
| 58 | + "success", |
| 59 | + }, |
| 60 | + ) |
| 61 | + prometheus.MustRegister(p.createSnapshotSuccess) |
| 62 | + prometheus.MustRegister(p.deleteSnapshotSuccess) |
| 63 | + prometheus.MustRegister(p.operationSuccess) |
| 64 | + |
| 65 | + go startServer() |
| 66 | +} |
| 67 | + |
| 68 | +func startServer() { |
| 69 | + log.Info("starting HTTP endpoints ...") |
| 70 | + |
| 71 | + mux := http.NewServeMux() |
| 72 | + mux.Handle("/__/", op.NewHandler( |
| 73 | + op.NewStatus("gcp-disk-snapshotter", "gcp-disk-snapshotter handles snapshot creation/deletion on gcp for a given set of disks"). |
| 74 | + AddOwner("infrastructure", "#infra"). |
| 75 | + AddLink("github", "https://github.com/utilitywarehouse/gcp-disk-snapshotter"). |
| 76 | + SetRevision("master"). |
| 77 | + AddChecker("running", func(cr *op.CheckResponse) { cr.Healthy("service is running") }). |
| 78 | + ReadyAlways(), |
| 79 | + )) |
| 80 | + mux.Handle("/metrics", promhttp.Handler()) |
| 81 | + |
| 82 | + if err := http.ListenAndServe(":5000", mux); err != nil { |
| 83 | + log.Fatal("could not start HTTP router: ", err) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// UpdateCreateSnapshotStatus increments the given disk's Counter for either successful create attempts or failed apply attempts. |
| 88 | +func (p *Prometheus) UpdateCreateSnapshotStatus(disk string, success bool) { |
| 89 | + p.createSnapshotSuccess.With(prometheus.Labels{ |
| 90 | + "disk": disk, "success": strconv.FormatBool(success), |
| 91 | + }).Inc() |
| 92 | +} |
| 93 | + |
| 94 | +// UpdateDeleteSnapshotStatus increments the given disk's Counter for either successful delete attempts or failed apply attempts. |
| 95 | +func (p *Prometheus) UpdateDeleteSnapshotStatus(disk string, success bool) { |
| 96 | + p.deleteSnapshotSuccess.With(prometheus.Labels{ |
| 97 | + "disk": disk, "success": strconv.FormatBool(success), |
| 98 | + }).Inc() |
| 99 | +} |
| 100 | + |
| 101 | +// UpdateDeleteSnapshotStatus increments the given disk's Counter for either successful delete attempts or failed apply attempts. |
| 102 | +func (p *Prometheus) UpdateOperationStatus(operation_type string, success bool) { |
| 103 | + p.operationSuccess.With(prometheus.Labels{ |
| 104 | + "operation_type": operation_type, "success": strconv.FormatBool(success), |
| 105 | + }).Inc() |
| 106 | +} |
0 commit comments