forked from rancher/steve
/
metrics_client.go
83 lines (71 loc) · 3.67 KB
/
metrics_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package metrics
import (
"time"
"github.com/oneblock-ai/apiserver/v2/pkg/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"github.com/oneblock-ai/steve/v2/pkg/metrics"
)
type ResourceClientWithMetrics struct {
dynamic.ResourceInterface
}
func Wrap(resourceInterface dynamic.ResourceInterface, err error) (ResourceClientWithMetrics, error) {
client := ResourceClientWithMetrics{}
if err != nil {
return client, err
}
client.ResourceInterface = resourceInterface
return client, err
}
func (r ResourceClientWithMetrics) Create(apiOp *types.APIRequest, obj *unstructured.Unstructured, options metav1.CreateOptions, subresources ...string) (*unstructured.Unstructured, error) {
m := metrics.MetricLogger{Resource: apiOp.Schema.ID, Method: apiOp.Method}
start := time.Now()
obj, err := r.ResourceInterface.Create(apiOp.Context(), obj, options, subresources...)
m.RecordK8sClientResponseTime(err, float64(time.Since(start).Milliseconds()))
return obj, err
}
func (r ResourceClientWithMetrics) Update(apiOp *types.APIRequest, obj *unstructured.Unstructured, options metav1.UpdateOptions, subresources ...string) (*unstructured.Unstructured, error) {
m := metrics.MetricLogger{Resource: apiOp.Schema.ID, Method: apiOp.Method}
start := time.Now()
obj, err := r.ResourceInterface.Update(apiOp.Context(), obj, options, subresources...)
m.RecordK8sClientResponseTime(err, float64(time.Since(start).Milliseconds()))
return obj, err
}
func (r ResourceClientWithMetrics) Delete(apiOp *types.APIRequest, name string, options metav1.DeleteOptions, subresources ...string) error {
m := metrics.MetricLogger{Resource: apiOp.Schema.ID, Method: apiOp.Method}
start := time.Now()
err := r.ResourceInterface.Delete(apiOp.Context(), name, options, subresources...)
m.RecordK8sClientResponseTime(err, float64(time.Since(start).Milliseconds()))
return err
}
func (r ResourceClientWithMetrics) Get(apiOp *types.APIRequest, name string, options metav1.GetOptions, subresources ...string) (*unstructured.Unstructured, error) {
m := metrics.MetricLogger{Resource: apiOp.Schema.ID, Method: apiOp.Method}
start := time.Now()
obj, err := r.ResourceInterface.Get(apiOp.Context(), name, options, subresources...)
m.RecordK8sClientResponseTime(err, float64(time.Since(start).Milliseconds()))
return obj, err
}
func (r ResourceClientWithMetrics) List(apiOp *types.APIRequest, opts metav1.ListOptions) (*unstructured.UnstructuredList, error) {
m := metrics.MetricLogger{Resource: apiOp.Schema.ID, Method: apiOp.Method}
start := time.Now()
obj, err := r.ResourceInterface.List(apiOp.Context(), opts)
m.RecordK8sClientResponseTime(err, float64(time.Since(start).Milliseconds()))
return obj, err
}
func (r ResourceClientWithMetrics) Watch(apiOp *types.APIRequest, opts metav1.ListOptions) (watch.Interface, error) {
m := metrics.MetricLogger{Resource: apiOp.Schema.ID, Method: apiOp.Method}
start := time.Now()
watchInterface, err := r.ResourceInterface.Watch(apiOp.Context(), opts)
m.RecordK8sClientResponseTime(err, float64(time.Since(start).Milliseconds()))
return watchInterface, err
}
func (r ResourceClientWithMetrics) Patch(apiOp *types.APIRequest, name string, pt k8stypes.PatchType, data []byte, options metav1.PatchOptions, subresources ...string) (*unstructured.Unstructured, error) {
m := metrics.MetricLogger{Resource: apiOp.Schema.ID, Method: apiOp.Method}
start := time.Now()
obj, err := r.ResourceInterface.Patch(apiOp.Context(), name, pt, data, options, subresources...)
m.RecordK8sClientResponseTime(err, float64(time.Since(start).Milliseconds()))
return obj, err
}