forked from konflux-ci/internal-services
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(HACBS-1728): implement metrics for the Internal Service controller
- adds metrics for internal-services-controller - update go.mod - adds tests for /metrics Signed-off-by: Leandro Mendes <[email protected]>
- Loading branch information
1 parent
739e4af
commit be96277
Showing
10 changed files
with
348 additions
and
8 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
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,7 @@ | ||
kind: Kustomization | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
namespace: grafana-operator-system | ||
generatorOptions: | ||
disableNameSuffixHash: true | ||
resources: | ||
- prometheus/monitor.yaml |
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,20 @@ | ||
|
||
# Prometheus Monitor Service (Metrics) | ||
apiVersion: monitoring.coreos.com/v1 | ||
kind: ServiceMonitor | ||
metadata: | ||
labels: | ||
control-plane: controller-manager | ||
name: controller-manager-metrics-monitor | ||
namespace: system | ||
spec: | ||
endpoints: | ||
- path: /metrics | ||
port: https | ||
scheme: https | ||
bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token | ||
tlsConfig: | ||
insecureSkipVerify: true | ||
selector: | ||
matchLabels: | ||
control-plane: controller-manager |
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,79 @@ | ||
/* | ||
Copyright 2022. | ||
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 metrics | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/metrics" | ||
) | ||
|
||
var ( | ||
InternalRequestAttemptConcurrentTotal = prometheus.NewGauge( | ||
prometheus.GaugeOpts{ | ||
Name: "internal_request_attempt_concurrent_requests", | ||
Help: "Total number of concurrent InternalRequest attempts", | ||
}, | ||
) | ||
|
||
InternalRequestAttemptDurationSeconds = prometheus.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Name: "internal_request_attempt_duration_seconds", | ||
Help: "Time from the moment the InternalRequest starts being processed until it completes", | ||
Buckets: []float64{10, 20, 40, 60, 150, 300, 450, 900, 1800, 3600}, | ||
}, | ||
[]string{"request", "namespace", "reason", "succeeded"}, | ||
) | ||
|
||
InternalRequestAttemptTotal = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "internal_request_attempt_total", | ||
Help: "Total number of InternalRequests processed by the operator", | ||
}, | ||
[]string{"request", "namespace", "reason", "succeeded"}, | ||
) | ||
) | ||
|
||
// RegisterCompletedInternalRequest decrements the 'internal_request_attempt_concurrent_total' metric, increments `internal_request_attempt_total` | ||
// and registers a new observation for 'internal_request_attempt_duration_seconds' with the elapsed time from the moment the | ||
// InternalRequest attempt started (InternalRequest marked as 'Running'). | ||
func RegisterCompletedInternalRequest(request, namespace, reason string, startTime, completionTime *metav1.Time, succeeded bool) { | ||
labels := prometheus.Labels{ | ||
"request": request, | ||
"namespace": namespace, | ||
"reason": reason, | ||
"succeeded": strconv.FormatBool(succeeded), | ||
} | ||
InternalRequestAttemptConcurrentTotal.Dec() | ||
InternalRequestAttemptDurationSeconds.With(labels).Observe(completionTime.Sub(startTime.Time).Seconds()) | ||
InternalRequestAttemptTotal.With(labels).Inc() | ||
} | ||
|
||
// RegisterNewInternalRequest increments the number of the 'internal_request_attempt_concurrent_total' metric which represents the number of concurrent running InternalRequests. | ||
func RegisterNewInternalRequest(creationTime metav1.Time, startTime *metav1.Time) { | ||
InternalRequestAttemptConcurrentTotal.Inc() | ||
} | ||
|
||
func init() { | ||
metrics.Registry.MustRegister( | ||
InternalRequestAttemptConcurrentTotal, | ||
InternalRequestAttemptDurationSeconds, | ||
InternalRequestAttemptTotal, | ||
) | ||
} |
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,102 @@ | ||
/* | ||
Copyright 2022. | ||
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 metrics | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
|
||
ctrl "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
type inputHeader struct { | ||
Name string | ||
Help string | ||
} | ||
|
||
func TestMetricsRelease(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Metrics Release Test Suite") | ||
} | ||
|
||
var ( | ||
testEnv *envtest.Environment | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
) | ||
|
||
var _ = BeforeSuite(func() { | ||
ctx, cancel = context.WithCancel(context.TODO()) | ||
testEnv = &envtest.Environment{} | ||
|
||
cfg, err := testEnv.Start() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(cfg).NotTo(BeNil()) | ||
|
||
k8sManager, _ := ctrl.NewManager(cfg, ctrl.Options{ | ||
MetricsBindAddress: ":8081", | ||
LeaderElection: false, | ||
}) | ||
|
||
go func() { | ||
defer GinkgoRecover() | ||
Expect(k8sManager.Start(ctx)).To(Succeed()) | ||
}() | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
cancel() | ||
By("tearing down the test environment") | ||
err := testEnv.Stop() | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
// createCounterReader creates a prometheus counter type string with the given parameters to be used as input data | ||
// for 'strings.NewReader' in Prometheus 'client_golang' function 'testutil.CollectAndCompare'. | ||
func createCounterReader(header inputHeader, labels string, renderHeader bool, count int) string { | ||
readerData := "" | ||
if renderHeader { | ||
readerData = fmt.Sprintf("# HELP %s %s\n# TYPE %s counter\n", header.Name, header.Help, header.Name) | ||
} | ||
readerData += fmt.Sprintf("%s{%s} %d\n", header.Name, labels, count) | ||
|
||
return readerData | ||
} | ||
|
||
// createHistogramReader creates a prometheus histogram type string with the given parameters to be used as input data | ||
// for 'strings.NewReader' in Prometheus 'client_golang' function 'testutil.CollectAndCompare'. | ||
func createHistogramReader(header inputHeader, timeBuckets []string, bucketsData []int, labels string, sum float64, count int) string { | ||
readerData := fmt.Sprintf("# HELP %s %s\n# TYPE %s histogram\n", header.Name, header.Help, header.Name) | ||
for i, bucket := range timeBuckets { | ||
readerData += fmt.Sprintf("%s_bucket{%sle=\"%s\"} %d\n", header.Name, labels, bucket, bucketsData[i]) | ||
} | ||
|
||
if labels != "" { | ||
readerData += fmt.Sprintf("%s_sum{%s} %f\n\n", header.Name, labels, sum) | ||
readerData += fmt.Sprintf("%s_count{%s} %d\n", header.Name, labels, count) | ||
} else { | ||
readerData += fmt.Sprintf("%s_sum %f\n%s_count %d\n", header.Name, sum, header.Name, count) | ||
} | ||
|
||
return readerData | ||
} |
Oops, something went wrong.