Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
related to #124 In this pull request I implemented the metrics system in k8gb. I added one metric - counter incrementing the number of successful reconciliation loops. Adding specific metrics is part of the next PR. Like the logger, the metrics are accessed through a single singleton object and like the logger it is initialized. ```go var log = logging.Logger() var m = metrics.Metrics() ``` then anywhere in the code we can use this: ``` // increase the number of successful reconciliations m.ReconciliationIncrement() ``` The singleton is initialized in the `main()` function using the `metrics.Init(*config)` function. In case the singleton is not initialized, it will be set to the default value and all metrics will start with the prefix `k8gb_default` otherwise they will start with the prefix from the env variable `K8GB_NAMESPACE`. In addition to the singleton, I have changed the original metrics and added tests at the package level. In addition to the singleton, I significantly refactored the original metrics and added tests at the package level. The prometheus metrics implementation uses reflection. In short, I read the collectors structure during the runtime: ```go type collectors struct { HealthyRecords *prometheus.GaugeVec IngressHostsPerStatus *prometheus.GaugeVec ZoneUpdateTotal prometheus.Counter ReconciliationTotal prometheus.Counter } ``` Now I know all the types and names. From this I generate the map <name>: <instance>. I will then use the map for Register(), Unregister() and Get(). <name> is the name of the metric and is generated from the name of the structure in `collectors`. ReconciliationTotal => for example: `k8gb_gslb_healthy_records` (k8gb is `K8GB_NAMESPACE`) `Get()` is only for testing purposes - and is public only because controller_tests use metrics. I'll consider deleting tests from controller_tests and leaving only terratests and unit_tests at the package level (96% coverage now). But that will come in the next PR. Signed-off-by: kuritka <kuritka@gmail.com>
- Loading branch information
Showing
10 changed files
with
518 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Copyright 2021 The k8gb Contributors. | ||
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. | ||
Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic | ||
*/ | ||
package utils | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// SplitAfter works as the same way as strings.SplitAfter() but the separator is regexp | ||
func SplitAfter(s string, re *regexp.Regexp) (r []string) { | ||
if re == nil { | ||
return | ||
} | ||
re.ReplaceAllStringFunc(s, func(x string) string { | ||
s = strings.ReplaceAll(s, x, "::"+x) | ||
return s | ||
}) | ||
for _, x := range strings.Split(s, "::") { | ||
if x != "" { | ||
r = append(r, x) | ||
} | ||
} | ||
return | ||
} |
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,52 @@ | ||
/* | ||
Copyright 2021 The k8gb Contributors. | ||
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. | ||
Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic | ||
*/ | ||
package utils | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSplitAfterIsValid(t *testing.T) { | ||
// arrange | ||
// act | ||
result1 := SplitAfter("HElloFromTest", regexp.MustCompile("[A-Z]")) | ||
result2 := SplitAfter("hellofromtest", regexp.MustCompile("[A-Z]")) | ||
result3 := SplitAfter("1.2.3.4.5", regexp.MustCompile("[0-9]")) | ||
result4 := SplitAfter("HHHHH", regexp.MustCompile("[A-Z]")) | ||
result5 := SplitAfter("", regexp.MustCompile("[A-Z]")) | ||
result6 := SplitAfter("HEllo", regexp.MustCompile("")) | ||
// assert | ||
assert.Equal(t, "H Ello From Test", strings.Join(result1, " ")) | ||
assert.Equal(t, "hellofromtest", strings.Join(result2, " ")) | ||
assert.Equal(t, "1. 2. 3. 4. 5", strings.Join(result3, " ")) | ||
assert.Equal(t, "H H H H H", strings.Join(result4, " ")) | ||
assert.Equal(t, "", strings.Join(result5, " ")) | ||
assert.Equal(t, "H E l l o", strings.Join(result6, " ")) | ||
} | ||
|
||
func TestSplitAfterWithNilRegexp(t *testing.T) { | ||
// arrange | ||
// act | ||
result1 := SplitAfter("HElloFromTest", nil) | ||
// assert | ||
assert.Equal(t, "", strings.Join(result1, "_")) | ||
} |
Oops, something went wrong.