|
| 1 | +// Copyright 2019-2023 The Liqo Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package metrics |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + "os" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/prometheus/client_golang/prometheus" |
| 23 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 24 | + "k8s.io/klog/v2" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + // MetricsPort is the metrics port constant. |
| 29 | + MetricsPort = ":9090" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + // ErrorsCounter is the counter of the errors occurred during the reflection. |
| 34 | + ErrorsCounter *prometheus.CounterVec |
| 35 | + // ItemsCounter is the counter of the reflected resources. |
| 36 | + // A fast increase of this metric can indicate a race condition between local and remote operators. |
| 37 | + ItemsCounter *prometheus.CounterVec |
| 38 | +) |
| 39 | + |
| 40 | +// Init initializes the metrics. If no error occurs or no item is processed, the corresponding metric is not exported. |
| 41 | +func init() { |
| 42 | + var MetricsLabels = []string{"namespace", "reflector_resource"} |
| 43 | + |
| 44 | + ErrorsCounter = prometheus.NewCounterVec( |
| 45 | + prometheus.CounterOpts{ |
| 46 | + Name: "liqo_virtual_kubelet_reflection_error_counter", |
| 47 | + Help: "The counter of the transient errors.", |
| 48 | + }, |
| 49 | + MetricsLabels, |
| 50 | + ) |
| 51 | + |
| 52 | + ItemsCounter = prometheus.NewCounterVec( |
| 53 | + prometheus.CounterOpts{ |
| 54 | + Name: "liqo_virtual_kubelet_reflection_item_counter", |
| 55 | + Help: "The counter of the reflected resources. A fast increase of this metric can indicate a race condition between local and remote operators.", |
| 56 | + }, |
| 57 | + MetricsLabels, |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +// SetupMetricHandler sets up the metric handler. |
| 62 | +func SetupMetricHandler() { |
| 63 | + // Register the metrics to the prometheus registry. |
| 64 | + prometheus.MustRegister(ErrorsCounter) |
| 65 | + // Register the metrics to the prometheus registry. |
| 66 | + prometheus.MustRegister(ItemsCounter) |
| 67 | + |
| 68 | + http.Handle("/metrics", promhttp.Handler()) |
| 69 | + |
| 70 | + go func() { |
| 71 | + klog.Infof("Starting the virtual kubelet Metric Handler listening on %q", MetricsPort) |
| 72 | + |
| 73 | + server := &http.Server{ |
| 74 | + Addr: ":1234", |
| 75 | + ReadHeaderTimeout: 10 * time.Second, |
| 76 | + } |
| 77 | + |
| 78 | + // Key and certificate paths are not specified, since already configured as part of the TLSConfig. |
| 79 | + if err := server.ListenAndServe(); err != nil { |
| 80 | + klog.Errorf("Failed to start the Metric Handler: %v", err) |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | + }() |
| 84 | +} |
0 commit comments