-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add etcd-manager metrics, add openstack API metrics
- Loading branch information
Showing
171 changed files
with
31,841 additions
and
32 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "metrics", | ||
srcs = ["metrics.go"], | ||
importpath = "sigs.k8s.io/etcdadm/etcd-manager/pkg/metrics", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/volumes/openstack", | ||
"//vendor/github.com/prometheus/client_golang/prometheus", | ||
"//vendor/github.com/prometheus/client_golang/prometheus/promhttp", | ||
"//vendor/k8s.io/klog/v2:klog", | ||
], | ||
) |
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,44 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package metrics | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/etcdadm/etcd-manager/pkg/volumes/openstack" | ||
) | ||
|
||
func RegisterMetrics(port int, provider string) { | ||
if provider == "openstack" { | ||
openstack.RegisterMetrics() | ||
} | ||
http.Handle("/metrics", promhttp.HandlerFor( | ||
prometheus.DefaultGatherer, | ||
promhttp.HandlerOpts{ | ||
EnableOpenMetrics: true, | ||
}, | ||
)) | ||
klog.Infof("Listening etcd-manager metrics in port %d", port) | ||
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil) | ||
if err != nil { | ||
klog.Fatalf("Unable to start etcd-manager metrics: %v", 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,64 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package openstack | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
type OpenstackMetrics struct { | ||
Duration *prometheus.HistogramVec | ||
Total *prometheus.CounterVec | ||
Errors *prometheus.CounterVec | ||
} | ||
|
||
// MetricContext indicates the context for OpenStack metrics. | ||
type MetricContext struct { | ||
Start time.Time | ||
Attributes []string | ||
Metrics *OpenstackMetrics | ||
} | ||
|
||
// NewMetricContext creates a new MetricContext. | ||
func NewMetricContext(resource string, request string) *MetricContext { | ||
return &MetricContext{ | ||
Start: time.Now(), | ||
Attributes: []string{resource + "_" + request}, | ||
} | ||
} | ||
|
||
// ObserveRequest records the request latency and counts the errors. | ||
func (mc *MetricContext) Observe(om *OpenstackMetrics, err error) error { | ||
if om == nil { | ||
// mc.RequestMetrics not set, ignore this request | ||
return nil | ||
} | ||
|
||
om.Duration.WithLabelValues(mc.Attributes...).Observe( | ||
time.Since(mc.Start).Seconds()) | ||
om.Total.WithLabelValues(mc.Attributes...).Inc() | ||
if err != nil { | ||
om.Errors.WithLabelValues(mc.Attributes...).Inc() | ||
} | ||
return err | ||
} | ||
|
||
func RegisterMetrics() { | ||
doRegisterAPIMetrics() | ||
} |
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,61 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package openstack | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
var ( | ||
APIRequestMetrics = &OpenstackMetrics{ | ||
Duration: prometheus.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Name: "openstack_api_request_duration_seconds", | ||
Help: "Latency of an OpenStack API call", | ||
}, []string{"request"}), | ||
Total: prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "openstack_api_requests_total", | ||
Help: "Total number of OpenStack API calls", | ||
}, []string{"request"}), | ||
Errors: prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "openstack_api_request_errors_total", | ||
Help: "Total number of errors for an OpenStack API call", | ||
}, []string{"request"}), | ||
} | ||
) | ||
|
||
// ObserveRequest records the request latency and counts the errors. | ||
func (mc *MetricContext) ObserveRequest(err error) error { | ||
return mc.Observe(APIRequestMetrics, err) | ||
} | ||
|
||
var registerAPIMetrics sync.Once | ||
|
||
// RegisterMetrics registers OpenStack metrics. | ||
func doRegisterAPIMetrics() { | ||
registerAPIMetrics.Do(func() { | ||
prometheus.MustRegister( | ||
APIRequestMetrics.Duration, | ||
APIRequestMetrics.Total, | ||
APIRequestMetrics.Errors, | ||
) | ||
}) | ||
} |
Oops, something went wrong.