Skip to content

Commit

Permalink
Apply metrics name linter
Browse files Browse the repository at this point in the history
Signed-off-by: assaf-admi <[email protected]>
  • Loading branch information
assafad authored and akrejcir committed Oct 9, 2023
1 parent 12b9ba6 commit ba88b80
Show file tree
Hide file tree
Showing 32 changed files with 2,667 additions and 1,555 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,7 @@ GOLANGCI_LINT_VERSION ?= v1.51.1
lint:
test -s $(GOLANGCI_LINT) || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LOCALBIN) $(GOLANGCI_LINT_VERSION)
$(GOLANGCI_LINT) run --timeout 5m

.PHONY: lint-metrics
lint-metrics:
./hack/prom_metric_linter.sh --operator-name="kubevirt" --sub-operator-name="ssp"
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/blang/semver/v4 v4.0.0
github.com/fsnotify/fsnotify v1.6.0
github.com/go-logr/logr v1.2.4
github.com/kubevirt/monitoring/pkg/metrics/parser v0.0.0-20230706095033-373a95665d5a
github.com/machadovilaca/operator-observability v0.0.5
github.com/onsi/ginkgo/v2 v2.9.2
github.com/onsi/gomega v1.27.6
Expand All @@ -16,7 +17,7 @@ require (
github.com/operator-framework/operator-lib v0.11.0
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.64.1
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/client_model v0.3.0
github.com/prometheus/client_model v0.4.0
github.com/prometheus/common v0.41.0
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -108,7 +109,7 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221014213838-99cd37c6964a // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubevirt/monitoring/pkg/metrics/parser v0.0.0-20230706095033-373a95665d5a h1:7YL/LNARjQWuXihwJ4b/nVzddGvoFRI7JqxAKISyJkg=
github.com/kubevirt/monitoring/pkg/metrics/parser v0.0.0-20230706095033-373a95665d5a/go.mod h1:qGj2agzgwQ27nYhP3xhLs+IBzE5+ALNUg8bDfMcwPqo=
github.com/machadovilaca/operator-observability v0.0.5 h1:zhalgvVjoJYmiObjPYmdf0Q3mlt5pr/9SVy4fKoxbVo=
github.com/machadovilaca/operator-observability v0.0.5/go.mod h1:B1d+efJGa36WWcLYH2ZhqbjiH/OGv/b7nhe9Vb0WL+8=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand Down Expand Up @@ -359,8 +361,8 @@ github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY=
github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU=
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
Expand Down Expand Up @@ -794,8 +796,9 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
72 changes: 72 additions & 0 deletions hack/prom_metric_linter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env bash
#
# Copyright 2022 Red Hat, Inc.
#
# 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.

set -e

linter_image_tag="v0.0.2"

PROJECT_ROOT="$(readlink -e "$(dirname "${BASH_SOURCE[0]}")"/../)"
export METRICS_COLLECTOR_PATH="${METRICS_COLLECTOR_PATH:-${PROJECT_ROOT}/tools/prom-metrics-collector}"

if [[ ! -d "$METRICS_COLLECTOR_PATH" ]]; then
echo "Invalid METRICS_COLLECTOR_PATH: $METRICS_COLLECTOR_PATH is not a valid directory path"
exit 1
fi

# Parse command-line arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--operator-name=*)
operator_name="${1#*=}"
shift
;;
--sub-operator-name=*)
sub_operator_name="${1#*=}"
shift
;;
*)
echo "Invalid argument: $1"
exit 1
;;
esac
done

# Get the metrics list
go build -o _out/prom-metrics-collector "$METRICS_COLLECTOR_PATH/..."
json_output=$(_out/prom-metrics-collector 2>/dev/null)

# Select container runtime
source "${PROJECT_ROOT}"/hack/config.sh

# Check if runs as part of Openshift-ci. If so, run directly
if [[ "${OPENSHIFT_CI}" == "true" ]]; then
errors=$(/usr/bin/prom-metrics-linter \
--metric-families="$json_output" \
--operator-name="$operator_name" \
--sub-operator-name="$sub_operator_name" 2>/dev/null)
# Else, run in a container, using the linter image
else
errors=$($KUBEVIRT_CRI run -i "quay.io/kubevirt/prom-metrics-linter:$linter_image_tag" \
--metric-families="$json_output" \
--operator-name="$operator_name" \
--sub-operator-name="$sub_operator_name" 2>/dev/null)
fi

# Check if there were any errors, if yes print and fail
if [[ $errors != "" ]]; then
echo "$errors"
exit 1
fi
37 changes: 37 additions & 0 deletions tools/prom-metrics-collector/metrics_collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
parser "github.com/kubevirt/monitoring/pkg/metrics/parser"
"kubevirt.io/ssp-operator/internal/operands/metrics"

dto "github.com/prometheus/client_model/go"
)

// excludedMetrics defines the metrics to ignore.
// open bug: https://bugzilla.redhat.com/show_bug.cgi?id=2219763
// Do not add metrics to this list!
var excludedMetrics = map[string]struct{}{
"kubevirt_ssp_operator_up_total": {},
"kubevirt_ssp_template_validator_up_total": {},
"ssp_operator_reconciling_properly": {},
"total_rejected_vms": {},
"total_restored_common_templates": {},
}

func readMetrics() []*dto.MetricFamily {
var metricFamilies []*dto.MetricFamily
sspMetrics := metrics.RecordRulesDescList

for _, metric := range sspMetrics {
if _, isExcludedMetric := excludedMetrics[metric.Name]; !isExcludedMetric {
mf := parser.CreateMetricFamily(parser.Metric{
Name: metric.Name,
Help: metric.Description,
Type: metric.Type,
})
metricFamilies = append(metricFamilies, mf)
}
}

return metricFamilies
}
19 changes: 19 additions & 0 deletions tools/prom-metrics-collector/metrics_json_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"encoding/json"
"fmt"
"os"
)

func main() {
metricFamilies := readMetrics()

jsonBytes, err := json.Marshal(metricFamilies)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println(string(jsonBytes))
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ba88b80

Please sign in to comment.