...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/bpm/metrics.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/bpm

     1  // Copyright 2021 Chaos Mesh 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  
    16  package bpm
    17  
    18  import (
    19  	"github.com/prometheus/client_golang/prometheus"
    20  )
    21  
    22  type metricsCollector struct {
    23  	backgroundProcessManager  *BackgroundProcessManager
    24  	bpmControlledProcesses    prometheus.Gauge
    25  	bpmControlledProcessTotal prometheus.Counter
    26  }
    27  
    28  // newMetricsCollector initializes metrics for each chaos daemon
    29  func newMetricsCollector(backgroundProcessManager *BackgroundProcessManager, register prometheus.Registerer) *metricsCollector {
    30  	collector := &metricsCollector{
    31  		backgroundProcessManager: backgroundProcessManager,
    32  		bpmControlledProcesses: prometheus.NewGauge(prometheus.GaugeOpts{
    33  			Name: "chaos_daemon_bpm_controlled_processes",
    34  			Help: "Current number of bpm controlled processes",
    35  		}),
    36  		bpmControlledProcessTotal: prometheus.NewCounter(prometheus.CounterOpts{
    37  			Name: "chaos_daemon_bpm_controlled_process_total",
    38  			Help: "Total count of bpm controlled processes",
    39  		}),
    40  	}
    41  	register.MustRegister(collector)
    42  	return collector
    43  }
    44  
    45  func (collector *metricsCollector) Describe(ch chan<- *prometheus.Desc) {
    46  	collector.bpmControlledProcesses.Describe(ch)
    47  	collector.bpmControlledProcessTotal.Describe(ch)
    48  }
    49  
    50  func (collector *metricsCollector) Collect(ch chan<- prometheus.Metric) {
    51  	collector.collectBpmControlledProcesses()
    52  	collector.bpmControlledProcesses.Collect(ch)
    53  	collector.bpmControlledProcessTotal.Collect(ch)
    54  }
    55  
    56  func (collector *metricsCollector) collectBpmControlledProcesses() {
    57  	ids := collector.backgroundProcessManager.GetIdentifiers()
    58  	collector.bpmControlledProcesses.Set(float64(len(ids)))
    59  }
    60