Skip to content

Commit

Permalink
OCPBUGS-18002: DVO metrics gatherer - use UIDs only when DVO deployed…
Browse files Browse the repository at this point in the history
… in 'openshift-deployment-validation-operator' namespace (#818)

* DVO metrics gatherer - use UIDs only in
`openshift-deployment-validation-operator` namespace

* update
  • Loading branch information
tremes committed Aug 23, 2023
1 parent 48c85cd commit 75d983e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
3 changes: 3 additions & 0 deletions docs/gathered-data.md
Expand Up @@ -714,6 +714,9 @@ None
Collects metrics from the Deployment Validation Operator's
metrics service. The metrics are fetched via the /metrics endpoint and
filtered to only include those with a `deployment_validation_operator_` prefix.
If the DVO service is deployed in a namespace other than `openshift-deployment-validation-operator',
then the names of the workloads (e.g., namespace, deployment) are collected.
Otherwise, only the UIDs of those resources are collected.

### API Reference
None
Expand Down
42 changes: 29 additions & 13 deletions pkg/gatherers/clusterconfig/gather_dvo_metrics.go
Expand Up @@ -21,9 +21,16 @@ import (
"github.com/openshift/insights-operator/pkg/utils/marshal"
)

const (
managedDVONamespaceName = "openshift-deployment-validation-operator"
)

// GatherDVOMetrics Collects metrics from the Deployment Validation Operator's
// metrics service. The metrics are fetched via the /metrics endpoint and
// filtered to only include those with a `deployment_validation_operator_` prefix.
// If the DVO service is deployed in a namespace other than `openshift-deployment-validation-operator',
// then the names of the workloads (e.g., namespace, deployment) are collected.
// Otherwise, only the UIDs of those resources are collected.
//
// ### API Reference
// None
Expand Down Expand Up @@ -66,18 +73,21 @@ func gatherDVOMetrics(
return nil, []error{err}
}

useUIDs := false
nonFatalErrors := []error{}
allDVOMetricsLines := []byte{}
for svcIdx := range serviceList.Items {
// Use pointer to make gocritic happy and avoid copying the whole Service struct.
service := &serviceList.Items[svcIdx]
useUIDs = service.Namespace == managedDVONamespaceName

for _, port := range service.Spec.Ports {
apiURL := url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s.%s.svc:%d", service.Name, service.Namespace, port.Port),
}

prefixedLines, err := gatherDVOMetricsFromEndpoint(ctx, &apiURL, rateLimiter)
prefixedLines, err := gatherDVOMetricsFromEndpoint(ctx, &apiURL, rateLimiter, useUIDs)
if err != nil {
// Log errors as warnings and don't fail the entire gatherer.
// It is possible that this service is not really the correct one
Expand Down Expand Up @@ -113,6 +123,7 @@ func gatherDVOMetricsFromEndpoint(
ctx context.Context,
apiURL *url.URL,
rateLimiter flowcontrol.RateLimiter,
useUIDs bool,
) ([]byte, error) {
metricsRESTClient, err := rest.NewRESTClient(
apiURL,
Expand Down Expand Up @@ -140,19 +151,24 @@ func gatherDVOMetricsFromEndpoint(

// only metrics with the DVO prefix should be gathered.
dvoMetricsPrefix := []byte("deployment_validation_operator_")
// precompile regex rules
regexString := `(?m)(,?%s=[^\,\}]*")`
filterProps := []*regexp.Regexp{
regexp.MustCompile(fmt.Sprintf(regexString, "name")),
regexp.MustCompile(fmt.Sprintf(regexString, "namespace")),
}
prefixedLines, err := utils.ReadAllLinesWithPrefix(dataReader, dvoMetricsPrefix, func(b []byte) []byte {
for _, re := range filterProps {
str := re.ReplaceAllString(string(b), "")
b = []byte(str)

var f func(b []byte) []byte
if useUIDs {
// precompile regex rules
regexString := `(?m)(,?%s=[^\,\}]*")`
filterProps := []*regexp.Regexp{
regexp.MustCompile(fmt.Sprintf(regexString, "name")),
regexp.MustCompile(fmt.Sprintf(regexString, "namespace")),
}
return b
})
f = func(b []byte) []byte {
for _, re := range filterProps {
str := re.ReplaceAllString(string(b), "")
b = []byte(str)
}
return b
}
}
prefixedLines, err := utils.ReadAllLinesWithPrefix(dataReader, dvoMetricsPrefix, f)
if err != io.EOF {
klog.Warningf("Unable to read metrics lines with DVO prefix: %v", err)
return prefixedLines, err
Expand Down

0 comments on commit 75d983e

Please sign in to comment.