-
Notifications
You must be signed in to change notification settings - Fork 750
/
Copy pathcni_metrics.go
228 lines (213 loc) · 6.85 KB
/
cni_metrics.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 handles the processing of all metrics. This file handles metrics for ipamd
package metrics
import (
"github.com/aws/amazon-vpc-cni-k8s/pkg/utils/logger"
"golang.org/x/net/context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/aws/amazon-vpc-cni-k8s/pkg/publisher"
)
// Port where prometheus metrics are published.
const metricsPort = 61678
// InterestingCNIMetrics defines metrics parsing definition for kube-state-metrics
var InterestingCNIMetrics = map[string]metricsConvert{
"awscni_assigned_ip_addresses": {
actions: []metricsAction{
{cwMetricName: "assignIPAddresses",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{}}}},
"awscni_total_ip_addresses": {
actions: []metricsAction{
{cwMetricName: "totalIPAddresses",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{}}}},
"awscni_total_ipv4_prefixes": {
actions: []metricsAction{
{cwMetricName: "totalIPv4Prefixes",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{}}}},
"awscni_assigned_ip_per_cidr": {
actions: []metricsAction{
{cwMetricName: "totalAssignedIPv4sPerCidr",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{}}}},
"awscni_eni_allocated": {
actions: []metricsAction{
{cwMetricName: "eniAllocated",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{}}}},
"awscni_eni_max": {
actions: []metricsAction{
{cwMetricName: "eniMaxAvailable",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{}}}},
"awscni_ip_max": {
actions: []metricsAction{
{cwMetricName: "maxIPAddresses",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{}}}},
"awscni_aws_api_latency_ms": {
actions: []metricsAction{
{cwMetricName: "awsAPILatency",
matchFunc: matchAny,
actionFunc: metricsMax,
data: &dataPoints{},
logToFile: true}}},
"awscni_aws_api_error_count": {
actions: []metricsAction{
{cwMetricName: "awsAPIErr",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{},
logToFile: true}}},
"awscni_aws_utils_error_count": {
actions: []metricsAction{
{cwMetricName: "awsUtilErr",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{},
logToFile: true}}},
"awscni_ipamd_error_count": {
actions: []metricsAction{
{cwMetricName: "ipamdErr",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{},
logToFile: true}}},
"awscni_force_removed_enis": {
actions: []metricsAction{
{cwMetricName: "forceRemoveENI",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{},
logToFile: true}}},
"awscni_force_removed_ips": {
actions: []metricsAction{
{cwMetricName: "forceRemoveIPs",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{},
logToFile: true}}},
"awscni_ipamd_action_inprogress": {
actions: []metricsAction{
{cwMetricName: "ipamdActionInProgress",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{},
logToFile: true}}},
"awscni_reconcile_count": {
actions: []metricsAction{
{cwMetricName: "reconcileCount",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{},
logToFile: true}}},
"awscni_add_ip_req_count": {
actions: []metricsAction{
{cwMetricName: "addReqCount",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{},
logToFile: true}}},
"awscni_del_ip_req_count": {
actions: []metricsAction{
{cwMetricName: "delReqCount",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{},
logToFile: true}}},
"awscni_pod_eni_error_count": {
actions: []metricsAction{
{cwMetricName: "podENIErr",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{},
logToFile: true}}},
"awscni_ec2api_req_count": {
actions: []metricsAction{
{cwMetricName: "ec2ApiReqCount",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{},
logToFile: true}}},
"awscni_ec2api_error_count": {
actions: []metricsAction{
{cwMetricName: "ec2ApiErrCount",
matchFunc: matchAny,
actionFunc: metricsAdd,
data: &dataPoints{},
logToFile: true}}},
}
// CNIMetricsTarget defines data structure for kube-state-metric target
type CNIMetricsTarget struct {
interestingMetrics map[string]metricsConvert
cwMetricsPublisher publisher.Publisher
kubeClient kubernetes.Interface
podWatcher *defaultPodWatcher
submitCW bool
submitPrometheusMetrics bool
log logger.Logger
}
// CNIMetricsNew creates a new metricsTarget
func CNIMetricsNew(k8sClient kubernetes.Interface, cw publisher.Publisher, submitCW bool, submitPrometheus bool, l logger.Logger,
watcher *defaultPodWatcher) *CNIMetricsTarget {
return &CNIMetricsTarget{
interestingMetrics: InterestingCNIMetrics,
cwMetricsPublisher: cw,
kubeClient: k8sClient,
podWatcher: watcher,
submitCW: submitCW,
submitPrometheusMetrics: submitPrometheus,
log: l,
}
}
func (t *CNIMetricsTarget) grabMetricsFromTarget(ctx context.Context, cniPod string) ([]byte, error) {
output, err := getMetricsFromPod(ctx, t.kubeClient, cniPod, metav1.NamespaceSystem, metricsPort)
if err != nil {
t.log.Errorf("grabMetricsFromTarget: Failed to grab CNI endpoint: %v", err)
return nil, err
}
t.log.Debugf("cni-metrics text output: %s", string(output))
return output, nil
}
func (t *CNIMetricsTarget) getInterestingMetrics() map[string]metricsConvert {
return InterestingCNIMetrics
}
func (t *CNIMetricsTarget) getCWMetricsPublisher() publisher.Publisher {
return t.cwMetricsPublisher
}
func (t *CNIMetricsTarget) getTargetList(ctx context.Context) ([]string, error) {
pods, err := t.podWatcher.GetCNIPods(ctx)
if err != nil {
return pods, err
}
return pods, nil
}
func (t *CNIMetricsTarget) submitCloudWatch() bool {
return t.submitCW
}
func (t *CNIMetricsTarget) getLogger() logger.Logger {
return t.log
}
func (t *CNIMetricsTarget) submitPrometheus() bool {
return t.submitPrometheusMetrics
}