Skip to content

Commit 3541d35

Browse files
authored
Merge pull request #44 from parrobe/dev
Update method to generate metric description
2 parents 294f0c9 + 78c9cce commit 3541d35

File tree

2 files changed

+71
-55
lines changed

2 files changed

+71
-55
lines changed

mqmetric/discover.go

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"bufio"
3636
"fmt"
3737
"os"
38+
"regexp"
3839
"strings"
3940

4041
"github.com/ibm-messaging/mq-golang/ibmmq"
@@ -263,7 +264,7 @@ func discoverElements(ty *MonType) error {
263264
}
264265
}
265266

266-
elem.MetricName = formatDescriptionElem(elem)
267+
elem.MetricName = formatDescription(elem)
267268
ty.Elements[elementIndex] = elem
268269
}
269270
}
@@ -631,59 +632,49 @@ bytes etc), and organisation of the elements of the name (units last)
631632
While we can't change the MQ-generated descriptions for its statistics,
632633
we can reformat most of them heuristically here.
633634
*/
634-
func formatDescriptionElem(elem *MonElement) string {
635-
s := formatDescription(elem.Description)
636-
637-
unit := ""
638-
switch elem.Datatype {
639-
case ibmmq.MQIAMO_MONITOR_MICROSEC:
640-
// Although the qmgr captures in us, we convert when
641-
// pushing out to the backend, so this label needs to match
642-
unit = "_seconds"
643-
}
644-
s += unit
645-
646-
return s
647-
}
648-
649-
func formatDescription(baseName string) string {
650-
s := baseName
635+
func formatDescription(elem *MonElement) string {
636+
s := elem.Description
651637
s = strings.Replace(s, " ", "_", -1)
652638
s = strings.Replace(s, "/", "_", -1)
653639
s = strings.Replace(s, "-", "_", -1)
654640

655-
/* common pattern is "xxx - yyy" leading to 3 ugly adjacent underscores */
656-
s = strings.Replace(s, "___", "_", -1)
657-
s = strings.Replace(s, "__", "_", -1)
641+
/* Make sure we don't have multiple underscores */
642+
multiunder := regexp.MustCompile("__*")
643+
s = multiunder.ReplaceAllLiteralString(s, "_")
658644

659645
/* make it all lowercase. Not essential, but looks better */
660646
s = strings.ToLower(s)
661647

662-
// Do not use _count
648+
/* Remove all cases of bytes, seconds, count or percentage (we add them back in later) */
663649
s = strings.Replace(s, "_count", "", -1)
650+
s = strings.Replace(s, "_bytes", "", -1)
651+
s = strings.Replace(s, "_byte", "", -1)
652+
s = strings.Replace(s, "_seconds", "", -1)
653+
s = strings.Replace(s, "_second", "", -1)
654+
s = strings.Replace(s, "_percentage", "", -1)
664655

665656
// Switch round a couple of specific names
666-
s = strings.Replace(s, "bytes_written", "written_bytes", -1)
667-
s = strings.Replace(s, "bytes_max", "max_bytes", -1)
668-
s = strings.Replace(s, "bytes_in_use", "in_use_bytes", -1)
669657
s = strings.Replace(s, "messages_expired", "expired_messages", -1)
670658

671-
if strings.HasSuffix(s, "free_space") {
659+
// Add the unit at end
660+
switch elem.Datatype {
661+
case ibmmq.MQIAMO_MONITOR_PERCENT, ibmmq.MQIAMO_MONITOR_HUNDREDTHS:
672662
s = s + "_percentage"
673-
s = strings.Replace(s, "__", "_", -1)
674-
}
675-
676-
// Make "byte", "file" and "message" units plural
677-
if strings.HasSuffix(s, "byte") ||
678-
strings.HasSuffix(s, "message") ||
679-
strings.HasSuffix(s, "file") {
680-
s = s + "s"
681-
}
682-
683-
// Move % to the end
684-
if strings.Contains(s, "_percentage_") {
685-
s = strings.Replace(s, "_percentage_", "_", -1)
686-
s += "_percentage"
663+
case ibmmq.MQIAMO_MONITOR_MB, ibmmq.MQIAMO_MONITOR_GB:
664+
s = s + "_bytes"
665+
case ibmmq.MQIAMO_MONITOR_MICROSEC:
666+
s = s + "_seconds"
667+
default:
668+
if strings.Contains(s, "_total") {
669+
/* If we specify it is a total in description put that at the end */
670+
s = strings.Replace(s, "_total", "", -1)
671+
s = s + "_total"
672+
} else if strings.Contains(s, "log_") {
673+
/* Weird case where the log datatype is not MB or GB but should be bytes */
674+
s = s + "_bytes"
675+
} else {
676+
s = s + "_count"
677+
}
687678
}
688679

689680
return s

mqmetric/mqmetric_test.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,61 @@ func TestReadPatterns(t *testing.T) {
9090
}
9191
}
9292
func TestFormatDescription(t *testing.T) {
93-
give := [...]string{"hello", "no space", "no/slash", "no-dash", "single___underscore", "single__underscore", "ALLLOWER", "no_count", "this_bytes_written_switch", "this_bytes_max_switch", "this_bytes_in_use_switch", "this messages_expired_switch", "add_free_space", "suffix_byte", "suffix_message", "suffix_file", "this_percentage_move"}
94-
expected := [...]string{"hello", "no_space", "no_slash", "no_dash", "single_underscore", "single_underscore", "alllower", "no", "this_written_bytes_switch", "this_max_bytes_switch", "this_in_use_bytes_switch", "this_expired_messages_switch", "add_free_space_percentage", "suffix_bytes", "suffix_messages", "suffix_files", "this_move_percentage"}
93+
give := [...]string{"hello", "no space", "no/slash", "no-dash", "single___underscore", "single__underscore__multiplace", "ALLLOWER", "this_bytes_written_switch", "this_byte_max_switch", "this_seconds_in_use_switch", "this messages_expired_switch", "this_seconds_max_switch", "this_count_max_switch", "this_percentage_max_switch"}
94+
expected := [...]string{"hello_count", "no_space_count", "no_slash_count", "no_dash_count", "single_underscore_count", "single_underscore_multiplace_count", "alllower_count", "this_written_switch_count", "this_max_switch_count", "this_in_use_switch_count", "this_expired_messages_switch_count", "this_max_switch_count", "this_max_switch_count", "this_max_switch_count"}
9595

9696
for i, e := range give {
97-
back := formatDescription(e)
97+
elem := MonElement{
98+
Description: e,
99+
}
100+
back := formatDescription(&elem)
98101
if back != expected[i] {
99102
t.Logf("Gave %s. Expected: %s, Got: %s", e, expected[i], back)
100103
t.Fail()
101104
}
102105
}
103106
}
104-
func TestFormatDescriptionElem(t *testing.T) {
105-
test := MonElement{}
106-
test.Description = "THIS-should__be/formatted_count"
107-
expected := "this_should_be_formatted"
108107

109-
back := formatDescriptionElem(&test)
110-
if back != expected {
111-
t.Logf("Gave %s. Expected: %s, Got: %s", test.Description, expected, back)
108+
func TestSuffixes(t *testing.T) {
109+
baseDescription := "test_suffix"
110+
types := [...]int32{ibmmq.MQIAMO_MONITOR_MB, ibmmq.MQIAMO_MONITOR_GB, ibmmq.MQIAMO_MONITOR_MICROSEC, ibmmq.MQIAMO_MONITOR_PERCENT, ibmmq.MQIAMO_MONITOR_HUNDREDTHS, 0}
111+
expected := [...]string{baseDescription + "_bytes", baseDescription + "_bytes", baseDescription + "_seconds", baseDescription + "_percentage", baseDescription + "_percentage", baseDescription + "_count"}
112+
113+
for i, ty := range types {
114+
elem := MonElement{
115+
Description: baseDescription,
116+
Datatype: ty,
117+
}
118+
back := formatDescription(&elem)
119+
if back != expected[i] {
120+
t.Logf("Gave %s/%d Expected: %s, Got: %s", baseDescription, ty, expected[i], back)
121+
t.Fail()
122+
}
123+
}
124+
125+
// special case log_bytes
126+
elem := MonElement{
127+
Description: "log_test_suffix",
128+
Datatype: 0,
129+
}
130+
back := formatDescription(&elem)
131+
if back != "log_test_suffix_bytes" {
132+
t.Logf("Gave log_test_suffix/0 Expected: %s, Got: %s", "log_test_suffix_bytes", back)
112133
t.Fail()
113134
}
114135

115-
test.Datatype = ibmmq.MQIAMO_MONITOR_MICROSEC
116-
expected = "this_should_be_formatted_seconds"
117-
back = formatDescriptionElem(&test)
118-
if back != expected {
119-
t.Logf("Gave %s. Expected: %s, Got: %s", test.Description, expected, back)
136+
// special case log_total
137+
elem = MonElement{
138+
Description: "log_total_suffix",
139+
Datatype: 0,
140+
}
141+
back = formatDescription(&elem)
142+
if back != "log_suffix_total" {
143+
t.Logf("Gave log_total_suffix/0 Expected: %s, Got: %s", "log_suffix_total", back)
120144
t.Fail()
121145
}
122146
}
147+
123148
func TestParsePCFResponse(t *testing.T) {
124149
cfh := ibmmq.NewMQCFH()
125150
cfh.Type = ibmmq.MQCFT_RESPONSE

0 commit comments

Comments
 (0)