diff --git a/gateway/metrics/exporter.go b/gateway/metrics/exporter.go index 8ceeb88f..2922a118 100644 --- a/gateway/metrics/exporter.go +++ b/gateway/metrics/exporter.go @@ -40,6 +40,9 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { e.metricOptions.GatewayFunctionInvocation.Describe(ch) e.metricOptions.GatewayFunctionsHistogram.Describe(ch) e.metricOptions.ServiceReplicasGauge.Describe(ch) + + e.metricOptions.ServiceMetrics.Counter.Describe(ch) + e.metricOptions.ServiceMetrics.Histogram.Describe(ch) } // Collect collects data to be consumed by prometheus @@ -54,6 +57,9 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { Set(float64(service.Replicas)) } e.metricOptions.ServiceReplicasGauge.Collect(ch) + + e.metricOptions.ServiceMetrics.Counter.Collect(ch) + e.metricOptions.ServiceMetrics.Histogram.Collect(ch) } // StartServiceWatcher starts a ticker and collects service replica counts to expose to prometheus diff --git a/gateway/metrics/metrics.go b/gateway/metrics/metrics.go index 9d0a3678..a8accd96 100644 --- a/gateway/metrics/metrics.go +++ b/gateway/metrics/metrics.go @@ -14,6 +14,13 @@ type MetricOptions struct { GatewayFunctionInvocation *prometheus.CounterVec GatewayFunctionsHistogram *prometheus.HistogramVec ServiceReplicasGauge *prometheus.GaugeVec + ServiceMetrics *ServiceMetricOptions +} + +// ServiceMetricOptions provides RED metrics +type ServiceMetricOptions struct { + Histogram *prometheus.HistogramVec + Counter *prometheus.CounterVec } // PrometheusHandler Bootstraps prometheus for metrics collection @@ -44,16 +51,40 @@ func BuildMetricsOptions() MetricOptions { []string{"function_name"}, ) + // For automatic monitoring and alerting (RED method) + histogram := prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Subsystem: "http", + Name: "request_duration_seconds", + Help: "Seconds spent serving HTTP requests.", + Buckets: prometheus.DefBuckets, + }, []string{"method", "path", "status"}) + + // Can be used Kubernetes HPA v2 + counter := prometheus.NewCounterVec( + prometheus.CounterOpts{ + Subsystem: "http", + Name: "requests_total", + Help: "The total number of HTTP requests.", + }, + []string{"status"}, + ) + + serviceMetricOptions := &ServiceMetricOptions{ + Counter: counter, + Histogram: histogram, + } + metricsOptions := MetricOptions{ GatewayFunctionsHistogram: gatewayFunctionsHistogram, GatewayFunctionInvocation: gatewayFunctionInvocation, ServiceReplicasGauge: serviceReplicas, + ServiceMetrics: serviceMetricOptions, } return metricsOptions } -//RegisterMetrics registers with Prometheus for tracking +// RegisterExporter registers with Prometheus for tracking func RegisterExporter(exporter *Exporter) { prometheus.MustRegister(exporter) }