-
Notifications
You must be signed in to change notification settings - Fork 185
/
mqmetric_test_util.go
259 lines (237 loc) · 7.66 KB
/
mqmetric_test_util.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
© Copyright IBM Corporation 2018, 2023
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 main
import (
"bufio"
"fmt"
"io"
"net/http"
"strings"
"testing"
"time"
ce "github.com/ibm-messaging/mq-container/test/container/containerengine"
)
type mqmetric struct {
Key string
Value string
Labels map[string]string
}
const defaultMetricURL = "/metrics"
const defaultMetricPort = 9157
const defaultMQNamespace = "ibmmq"
const defaultMetricQMName = "qm1"
func getMetrics(t *testing.T, port string) []mqmetric {
returned := []mqmetric{}
urlToUse := fmt.Sprintf("http://localhost:%s%s", port, defaultMetricURL)
resp, err := http.Get(urlToUse)
if err != nil {
t.Fatalf("Error from HTTP GET for metrics: %v", err)
return returned
}
defer resp.Body.Close()
metricsRaw, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error reading metrics data: %v", err)
return returned
}
return convertRawMetricToMap(t, string(metricsRaw))
}
// Also filters out all non "ibmmq" metrics
func convertRawMetricToMap(t *testing.T, input string) []mqmetric {
returnList := []mqmetric{}
scanner := bufio.NewScanner(strings.NewReader(input))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
// Comment line of HELP or TYPE. Ignore
continue
}
if !strings.HasPrefix(line, defaultMQNamespace) {
// Not an ibmmq_ metric. Ignore
continue
}
//It's an IBM MQ metric!
key, value, labelMap, err := convertMetricLineToMetric(line)
if err != nil {
t.Fatalf("ibmmq_ metric could not be deciphered - %v", err)
}
toAdd := mqmetric{
Key: key,
Value: value,
Labels: labelMap,
}
returnList = append(returnList, toAdd)
}
return returnList
}
func convertMetricLineToMetric(input string) (string, string, map[string]string, error) {
// Lines are in the form "<key>{<labels>}<value>" or "<key> <value>"
// Get the key and value while skipping the label
var key, value string
labelMap := make(map[string]string)
if strings.Contains(input, "{") {
// Get key
splitted := strings.Split(input, "{")
if len(splitted) != 2 {
return "", "", labelMap, fmt.Errorf("Could not split by { Expected 2 but got %d - %s", len(splitted), input)
}
key = strings.TrimSpace(splitted[0])
// Get value
splitted = strings.Split(splitted[1], "}")
if len(splitted) != 2 {
return "", "", labelMap, fmt.Errorf("Could not split by } Expected 2 but got %d - %s", len(splitted), input)
}
value = strings.TrimSpace(splitted[1])
// Get labels
allLabels := strings.Split(splitted[0], ",")
for _, e := range allLabels {
labelPair := strings.Split(e, "=")
if len(labelPair) != 2 {
return "", "", labelMap, fmt.Errorf("Could not split label by '=' Expected 2 but got %d - %s", len(labelPair), e)
}
lkey := strings.TrimSpace(labelPair[0])
lvalue := strings.TrimSpace(labelPair[1])
lvalue = strings.Trim(lvalue, "\"")
labelMap[lkey] = lvalue
}
} else {
splitted := strings.Split(input, " ")
if len(splitted) != 2 {
return "", "", labelMap, fmt.Errorf("Could not split by ' ' Expected 2 but got %d - %s", len(splitted), input)
}
key = strings.TrimSpace(splitted[0])
value = strings.TrimSpace(splitted[1])
}
return key, value, labelMap, nil
}
func waitForMetricReady(t *testing.T, port string) {
timeout := 12 // 12 * 5 = 1 minute
for i := 0; i < timeout; i++ {
urlToUse := fmt.Sprintf("http://localhost:%s", port)
resp, err := http.Get(urlToUse)
if err == nil {
resp.Body.Close()
return
}
time.Sleep(time.Second * 10)
}
t.Fatalf("Metric endpoint failed to startup in timely manner")
}
func metricsContainerConfig() *ce.ContainerConfig {
return &ce.ContainerConfig{
Env: []string{
"LICENSE=accept",
"MQ_QMGR_NAME=" + defaultMetricQMName,
"MQ_ENABLE_METRICS=true",
},
}
}
func metricNames() []string {
// NB: There are currently a total of 93 metrics, but the following 3 do not generate values (based on the queue manager configuration)
// - log_occupied_by_reusable_extents_bytes
// - log_occupied_by_extents_waiting_to_be_archived_bytes
// - log_required_for_media_recovery_bytes
names := []string{
"cpu_load_one_minute_average_percentage",
"cpu_load_five_minute_average_percentage",
"cpu_load_fifteen_minute_average_percentage",
"system_cpu_time_percentage",
"user_cpu_time_percentage",
"ram_free_percentage",
// disabled : "system_ram_size_bytes",
"system_cpu_time_estimate_for_queue_manager_percentage",
"user_cpu_time_estimate_for_queue_manager_percentage",
"ram_usage_estimate_for_queue_manager_bytes",
"trace_file_system_free_space_percentage",
"trace_file_system_in_use_bytes",
"errors_file_system_free_space_percentage",
"errors_file_system_in_use_bytes",
"fdc_files",
"queue_manager_file_system_free_space_percentage",
"queue_manager_file_system_in_use_bytes",
"log_logical_written_bytes_total",
"log_physical_written_bytes_total",
"log_primary_space_in_use_percentage",
"log_workload_primary_space_utilization_percentage",
"log_write_latency_seconds",
"log_max_bytes",
"log_write_size_bytes",
"log_in_use_bytes",
"log_file_system_max_bytes",
"log_file_system_in_use_bytes",
"durable_subscription_create_total",
"durable_subscription_alter_total",
"durable_subscription_resume_total",
"durable_subscription_delete_total",
"non_durable_subscription_create_total",
"non_durable_subscription_delete_total",
"failed_subscription_create_alter_resume_total",
"failed_subscription_delete_total",
"mqsubrq_total",
"failed_mqsubrq_total",
// disabled : "durable_subscriber_high_water_mark",
// disabled : "durable_subscriber_low_water_mark",
// disabled : "non_durable_subscriber_high_water_mark",
// disabled : "non_durable_subscriber_low_water_mark",
"topic_mqput_mqput1_total",
"topic_put_bytes_total",
"failed_topic_mqput_mqput1_total",
"persistent_topic_mqput_mqput1_total",
"non_persistent_topic_mqput_mqput1_total",
"published_to_subscribers_message_total",
"published_to_subscribers_bytes_total",
"mqconn_mqconnx_total",
"failed_mqconn_mqconnx_total",
"mqdisc_total",
// disabled : "concurrent_connections_high_water_mark",
"mqopen_total",
"failed_mqopen_total",
"mqclose_total",
"failed_mqclose_total",
"mqinq_total",
"failed_mqinq_total",
"mqset_total",
"failed_mqset_total",
"persistent_message_mqput_total",
"persistent_message_mqput1_total",
"persistent_message_put_bytes_total",
"non_persistent_message_mqput_total",
"non_persistent_message_mqput1_total",
"non_persistent_message_put_bytes_total",
"mqput_mqput1_total",
"mqput_mqput1_bytes_total",
"failed_mqput_total",
"failed_mqput1_total",
"mqstat_total",
"persistent_message_destructive_get_total",
"persistent_message_browse_total",
"persistent_message_get_bytes_total",
"persistent_message_browse_bytes_total",
"non_persistent_message_destructive_get_total",
"non_persistent_message_browse_total",
"non_persistent_message_get_bytes_total",
"non_persistent_message_browse_bytes_total",
"destructive_get_total",
"destructive_get_bytes_total",
"failed_mqget_total",
"failed_browse_total",
"mqctl_total",
"expired_message_total",
"purged_queue_total",
"mqcb_total",
"failed_mqcb_total",
"commit_total",
"rollback_total",
}
return names
}