github.com/argoproj/argo-events@v1.9.1/metrics/metrics.go (about)

     1  package metrics
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/prometheus/client_golang/prometheus/collectors"
     8  
     9  	"github.com/prometheus/client_golang/prometheus"
    10  	"github.com/prometheus/client_golang/prometheus/promhttp"
    11  	"go.uber.org/zap"
    12  
    13  	"github.com/argoproj/argo-events/common/logging"
    14  )
    15  
    16  const (
    17  	prefix = "argo_events"
    18  
    19  	labelNamespace       = "namespace"
    20  	labelEventSourceName = "eventsource_name"
    21  	labelEventName       = "event_name"
    22  	labelSensorName      = "sensor_name"
    23  	labelTriggerName     = "trigger_name"
    24  )
    25  
    26  // Metrics represents EventSource metrics information
    27  type Metrics struct {
    28  	namespace               string
    29  	runningEventServices    *prometheus.GaugeVec
    30  	eventsSent              *prometheus.CounterVec
    31  	eventsSentFailed        *prometheus.CounterVec
    32  	eventsProcessingFailed  *prometheus.CounterVec
    33  	eventProcessingDuration *prometheus.SummaryVec
    34  	actionTriggered         *prometheus.CounterVec
    35  	actionFailed            *prometheus.CounterVec
    36  	actionDuration          *prometheus.SummaryVec
    37  }
    38  
    39  // NewMetrics returns a Metrics instance
    40  func NewMetrics(namespace string) *Metrics {
    41  	return &Metrics{
    42  		namespace: namespace,
    43  		runningEventServices: prometheus.NewGaugeVec(prometheus.GaugeOpts{
    44  			Namespace: prefix,
    45  			Name:      "event_service_running_total",
    46  			Help:      "How many configured events in the EventSource object are actively running. https://argoproj.github.io/argo-events/metrics/#argo_events_event_service_running_total",
    47  			ConstLabels: prometheus.Labels{
    48  				labelNamespace: namespace,
    49  			},
    50  		}, []string{labelEventSourceName}),
    51  		eventsSent: prometheus.NewCounterVec(prometheus.CounterOpts{
    52  			Namespace: prefix,
    53  			Name:      "events_sent_total",
    54  			Help:      "How many events have been sent successfully. https://argoproj.github.io/argo-events/metrics/#argo_events_events_sent_total",
    55  			ConstLabels: prometheus.Labels{
    56  				labelNamespace: namespace,
    57  			},
    58  		}, []string{labelEventSourceName, labelEventName}),
    59  		eventsSentFailed: prometheus.NewCounterVec(prometheus.CounterOpts{
    60  			Namespace: prefix,
    61  			Name:      "events_sent_failed_total",
    62  			Help:      "How many events failed to send to EventBus. https://argoproj.github.io/argo-events/metrics/#argo_events_events_sent_failed_total",
    63  			ConstLabels: prometheus.Labels{
    64  				labelNamespace: namespace,
    65  			},
    66  		}, []string{labelEventSourceName, labelEventName}),
    67  		eventsProcessingFailed: prometheus.NewCounterVec(prometheus.CounterOpts{
    68  			Namespace: prefix,
    69  			Name:      "events_processing_failed_total",
    70  			Help:      "Number of events failed to process, it includes argo_events_events_sent_failed_total. https://argoproj.github.io/argo-events/metrics/#argo_events_events_processing_failed_total",
    71  			ConstLabels: prometheus.Labels{
    72  				labelNamespace: namespace,
    73  			},
    74  		}, []string{labelEventSourceName, labelEventName}),
    75  		eventProcessingDuration: prometheus.NewSummaryVec(prometheus.SummaryOpts{
    76  			Namespace: prefix,
    77  			Name:      "event_processing_duration_milliseconds",
    78  			Help:      "Summary of durations of event processing. https://argoproj.github.io/argo-events/metrics/#argo_events_event_processing_duration_milliseconds",
    79  			ConstLabels: prometheus.Labels{
    80  				labelNamespace: namespace,
    81  			},
    82  		}, []string{labelEventSourceName, labelEventName}),
    83  		actionTriggered: prometheus.NewCounterVec(prometheus.CounterOpts{
    84  			Namespace: prefix,
    85  			Name:      "action_triggered_total",
    86  			Help:      "How many actions have been triggered successfully. https://argoproj.github.io/argo-events/metrics/#argo_events_action_triggered_total",
    87  			ConstLabels: prometheus.Labels{
    88  				labelNamespace: namespace,
    89  			},
    90  		}, []string{labelSensorName, labelTriggerName}),
    91  		actionFailed: prometheus.NewCounterVec(prometheus.CounterOpts{
    92  			Namespace: prefix,
    93  			Name:      "action_failed_total",
    94  			Help:      "How many actions failed. https://argoproj.github.io/argo-events/metrics/#argo_events_action_failed_total",
    95  			ConstLabels: prometheus.Labels{
    96  				labelNamespace: namespace,
    97  			},
    98  		}, []string{labelSensorName, labelTriggerName}),
    99  		actionDuration: prometheus.NewSummaryVec(prometheus.SummaryOpts{
   100  			Namespace: prefix,
   101  			Name:      "action_duration_milliseconds",
   102  			Help:      "Summary of durations of trigging actions. https://argoproj.github.io/argo-events/metrics/#argo_events_action_duration_milliseconds",
   103  			ConstLabels: prometheus.Labels{
   104  				labelNamespace: namespace,
   105  			},
   106  		}, []string{labelSensorName, labelTriggerName}),
   107  	}
   108  }
   109  
   110  func (m *Metrics) Collect(ch chan<- prometheus.Metric) {
   111  	m.runningEventServices.Collect(ch)
   112  	m.eventsSent.Collect(ch)
   113  	m.eventsSentFailed.Collect(ch)
   114  	m.eventsProcessingFailed.Collect(ch)
   115  	m.eventProcessingDuration.Collect(ch)
   116  	m.actionTriggered.Collect(ch)
   117  	m.actionFailed.Collect(ch)
   118  	m.actionDuration.Collect(ch)
   119  }
   120  
   121  func (m *Metrics) Describe(ch chan<- *prometheus.Desc) {
   122  	m.runningEventServices.Describe(ch)
   123  	m.eventsSent.Describe(ch)
   124  	m.eventsSentFailed.Describe(ch)
   125  	m.eventsProcessingFailed.Describe(ch)
   126  	m.eventProcessingDuration.Describe(ch)
   127  	m.actionTriggered.Describe(ch)
   128  	m.actionFailed.Describe(ch)
   129  	m.actionDuration.Describe(ch)
   130  }
   131  
   132  func (m *Metrics) IncRunningServices(eventSourceName string) {
   133  	m.runningEventServices.WithLabelValues(eventSourceName).Inc()
   134  }
   135  
   136  func (m *Metrics) DecRunningServices(eventSourceName string) {
   137  	m.runningEventServices.WithLabelValues(eventSourceName).Dec()
   138  }
   139  
   140  func (m *Metrics) EventSent(eventSourceName, eventName string) {
   141  	m.eventsSent.WithLabelValues(eventSourceName, eventName).Inc()
   142  }
   143  
   144  func (m *Metrics) EventSentFailed(eventSourceName, eventName string) {
   145  	m.eventsSentFailed.WithLabelValues(eventSourceName, eventName).Inc()
   146  }
   147  
   148  func (m *Metrics) EventProcessingFailed(eventSourceName, eventName string) {
   149  	m.eventsProcessingFailed.WithLabelValues(eventSourceName, eventName).Inc()
   150  }
   151  
   152  func (m *Metrics) EventProcessingDuration(eventSourceName, eventName string, num float64) {
   153  	m.eventProcessingDuration.WithLabelValues(eventSourceName, eventName).Observe(num)
   154  }
   155  
   156  func (m *Metrics) ActionTriggered(sensorName, triggerName string) {
   157  	m.actionTriggered.WithLabelValues(sensorName, triggerName).Inc()
   158  }
   159  
   160  func (m *Metrics) ActionFailed(sensorName, triggerName string) {
   161  	m.actionFailed.WithLabelValues(sensorName, triggerName).Inc()
   162  }
   163  
   164  func (m *Metrics) ActionDuration(sensorName, triggerName string, num float64) {
   165  	m.actionDuration.WithLabelValues(sensorName, triggerName).Observe(num)
   166  }
   167  
   168  // Run starts a metrics server
   169  func (m *Metrics) Run(ctx context.Context, addr string) {
   170  	log := logging.FromContext(ctx)
   171  	metricsRegistry := prometheus.NewRegistry()
   172  	metricsRegistry.MustRegister(collectors.NewGoCollector(), m)
   173  	http.Handle("/metrics", promhttp.HandlerFor(metricsRegistry, promhttp.HandlerOpts{}))
   174  
   175  	log.Info("starting metrics server")
   176  	if err := http.ListenAndServe(addr, nil); err != nil {
   177  		log.Fatalw("failed to start metrics server", zap.Error(err))
   178  	}
   179  }