-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate prometheus metrics handler and expose basic metrics (#…
…226) * feat: integrate prometheus metrics handler and expose basic metrics * fix obsolete metric record * fix lint * improve prometheus metrics recording * fix lint * small optimization
- Loading branch information
Showing
9 changed files
with
146 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
const ( | ||
prometheusMetricsPath = "/metrics" | ||
prometheusMetricsPort = 8000 | ||
) | ||
|
||
func init() { | ||
go initPrometheusMetrics() | ||
} | ||
|
||
func initPrometheusMetrics() { | ||
http.Handle(prometheusMetricsPath, promhttp.Handler()) | ||
|
||
if err := http.ListenAndServe(fmt.Sprintf(":%d", prometheusMetricsPort), nil); err != nil { | ||
klog.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
var ( | ||
recorder = (&prometheusRecorder{}).init() | ||
) | ||
|
||
type prometheusRecorder struct { | ||
discoveryAPICacheRefreshCounter prometheus.Counter | ||
discoveryAPICacheRefreshErrorCounter prometheus.Counter | ||
serviceReconciliationCounter *prometheus.CounterVec | ||
serviceReconciliationErrorCounter *prometheus.CounterVec | ||
stackRunJobsCreatedCounter prometheus.Counter | ||
} | ||
|
||
func (in *prometheusRecorder) DiscoveryAPICacheRefresh(err error) { | ||
if err != nil { | ||
in.discoveryAPICacheRefreshErrorCounter.Inc() | ||
return | ||
} | ||
|
||
in.discoveryAPICacheRefreshCounter.Inc() | ||
} | ||
|
||
func (in *prometheusRecorder) ServiceReconciliation(serviceID, serviceName string, err error) { | ||
if err != nil { | ||
in.serviceReconciliationErrorCounter.WithLabelValues(serviceID, serviceName).Inc() | ||
return | ||
} | ||
|
||
in.serviceReconciliationCounter.WithLabelValues(serviceID, serviceName).Inc() | ||
} | ||
|
||
func (in *prometheusRecorder) StackRunJobCreation() { | ||
in.stackRunJobsCreatedCounter.Inc() | ||
} | ||
|
||
func (in *prometheusRecorder) init() Recorder { | ||
in.discoveryAPICacheRefreshCounter = promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: DiscoveryAPICacheRefreshMetricName, | ||
Help: DiscoveryAPICacheRefreshMetricDescription, | ||
}) | ||
|
||
in.discoveryAPICacheRefreshErrorCounter = promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: DiscoveryAPICacheRefreshErrorMetricName, | ||
Help: DiscoveryAPICacheRefreshErrorMetricDescription, | ||
}) | ||
|
||
in.serviceReconciliationCounter = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Name: ServiceReconciliationMetricName, | ||
Help: ServiceReconciliationMetricDescription, | ||
}, []string{ServiceReconciliationMetricLabelServiceID, ServiceReconciliationMetricLabelServiceName}) | ||
|
||
in.serviceReconciliationErrorCounter = promauto.NewCounterVec(prometheus.CounterOpts{ | ||
Name: ServiceReconciliationErrorMetricName, | ||
Help: ServiceReconciliationErrorMetricDescription, | ||
}, []string{ServiceReconciliationMetricLabelServiceID, ServiceReconciliationMetricLabelServiceName}) | ||
|
||
in.stackRunJobsCreatedCounter = promauto.NewCounter(prometheus.CounterOpts{ | ||
Name: StackRunJobsCreatedMetricName, | ||
Help: StackRunJobsCreatedMetricDescription, | ||
}) | ||
|
||
return in | ||
} | ||
|
||
func Record() Recorder { | ||
return recorder | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package metrics | ||
|
||
const ( | ||
DiscoveryAPICacheRefreshMetricName = "agent_discoveryapi_cache_refresh_total" | ||
DiscoveryAPICacheRefreshMetricDescription = "The total number of Discovery API cache refresh attempts" | ||
|
||
DiscoveryAPICacheRefreshErrorMetricName = "agent_discoveryapi_cache_refresh_error_total" | ||
DiscoveryAPICacheRefreshErrorMetricDescription = "The total number of Discovery API cache refresh errors" | ||
|
||
ServiceReconciliationMetricName = "agent_service_reconciliations_total" | ||
ServiceReconciliationMetricDescription = "The total number of service reconciliations" | ||
|
||
ServiceReconciliationErrorMetricName = "agent_service_reconciliation_errors_total" | ||
ServiceReconciliationErrorMetricDescription = "The total number of service reconciliation errors" | ||
|
||
ServiceReconciliationMetricLabelServiceID = "service_id" | ||
ServiceReconciliationMetricLabelServiceName = "service_name" | ||
|
||
StackRunJobsCreatedMetricName = "agent_stack_runs_created_total" | ||
StackRunJobsCreatedMetricDescription = "The total number of created stack runs" | ||
) | ||
|
||
type Recorder interface { | ||
DiscoveryAPICacheRefresh(err error) | ||
ServiceReconciliation(serviceID, serviceName string, err error) | ||
StackRunJobCreation() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters