|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package metrics |
| 19 | + |
| 20 | +import ( |
| 21 | + "github.com/prometheus/client_golang/prometheus" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/metrics" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + // ADC sync operation duration histogram |
| 27 | + ADCSyncDuration = prometheus.NewHistogramVec( |
| 28 | + prometheus.HistogramOpts{ |
| 29 | + Name: "apisix_ingress_adc_sync_duration_seconds", |
| 30 | + Help: "Time spent on ADC sync operations", |
| 31 | + Buckets: prometheus.DefBuckets, |
| 32 | + }, |
| 33 | + []string{"config_name", "resource_type", "status"}, |
| 34 | + ) |
| 35 | + |
| 36 | + // ADC sync operation counter |
| 37 | + ADCSyncTotal = prometheus.NewCounterVec( |
| 38 | + prometheus.CounterOpts{ |
| 39 | + Name: "apisix_ingress_adc_sync_total", |
| 40 | + Help: "Total number of ADC sync operations", |
| 41 | + }, |
| 42 | + []string{"config_name", "resource_type", "status"}, |
| 43 | + ) |
| 44 | + |
| 45 | + // ADC execution errors counter |
| 46 | + ADCExecutionErrors = prometheus.NewCounterVec( |
| 47 | + prometheus.CounterOpts{ |
| 48 | + Name: "apisix_ingress_adc_execution_errors_total", |
| 49 | + Help: "Total number of ADC execution errors", |
| 50 | + }, |
| 51 | + []string{"config_name", "error_type"}, |
| 52 | + ) |
| 53 | + |
| 54 | + // Status update channel queue length gauge |
| 55 | + StatusUpdateQueueLength = prometheus.NewGauge( |
| 56 | + prometheus.GaugeOpts{ |
| 57 | + Name: "apisix_ingress_status_update_queue_length", |
| 58 | + Help: "Current length of the status update queue", |
| 59 | + }, |
| 60 | + ) |
| 61 | + |
| 62 | + // File I/O operation duration histogram |
| 63 | + FileIODuration = prometheus.NewHistogramVec( |
| 64 | + prometheus.HistogramOpts{ |
| 65 | + Name: "apisix_ingress_file_io_duration_seconds", |
| 66 | + Help: "Time spent on file I/O operations", |
| 67 | + Buckets: prometheus.DefBuckets, |
| 68 | + }, |
| 69 | + []string{"operation", "status"}, |
| 70 | + ) |
| 71 | +) |
| 72 | + |
| 73 | +// init registers all metrics with the global prometheus registry |
| 74 | +func init() { |
| 75 | + // Register metrics with controller-runtime's metrics registry |
| 76 | + metrics.Registry.MustRegister( |
| 77 | + ADCSyncDuration, |
| 78 | + ADCSyncTotal, |
| 79 | + ADCExecutionErrors, |
| 80 | + StatusUpdateQueueLength, |
| 81 | + FileIODuration, |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +// RecordSyncDuration records the duration of an ADC sync operation |
| 86 | +func RecordSyncDuration(configName, resourceType, status string, duration float64) { |
| 87 | + ADCSyncDuration.WithLabelValues(configName, resourceType, status).Observe(duration) |
| 88 | + ADCSyncTotal.WithLabelValues(configName, resourceType, status).Inc() |
| 89 | +} |
| 90 | + |
| 91 | +// RecordExecutionError records an ADC execution error |
| 92 | +func RecordExecutionError(configName, errorType string) { |
| 93 | + ADCExecutionErrors.WithLabelValues(configName, errorType).Inc() |
| 94 | +} |
| 95 | + |
| 96 | +// UpdateStatusQueueLength updates the status update queue length gauge |
| 97 | +func UpdateStatusQueueLength(length float64) { |
| 98 | + StatusUpdateQueueLength.Set(length) |
| 99 | +} |
| 100 | + |
| 101 | +// IncStatusQueueLength increments the status update queue length gauge by 1 |
| 102 | +func IncStatusQueueLength() { |
| 103 | + StatusUpdateQueueLength.Inc() |
| 104 | +} |
| 105 | + |
| 106 | +// DecStatusQueueLength decrements the status update queue length gauge by 1 |
| 107 | +func DecStatusQueueLength() { |
| 108 | + StatusUpdateQueueLength.Dec() |
| 109 | +} |
| 110 | + |
| 111 | +// RecordFileIODuration records the duration of a file I/O operation |
| 112 | +func RecordFileIODuration(operation, status string, duration float64) { |
| 113 | + FileIODuration.WithLabelValues(operation, status).Observe(duration) |
| 114 | +} |
0 commit comments