Skip to content

Commit

Permalink
feat(RHTAPWATCH-570): Update exporters code with actual metrics
Browse files Browse the repository at this point in the history
Update the dsexporter code and related unit tests with
actual metrics.

Signed-off-by: Homaja Marisetty <[email protected]>
  • Loading branch information
hmariset committed Feb 5, 2024
1 parent c1b5f5e commit 3d2adc0
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 61 deletions.
85 changes: 78 additions & 7 deletions exporters/dsexporter/dsexporter.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,109 @@
package main

import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/client-go/kubernetes"
// "k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

const service = "https://console-openshift-console.apps.stone-prd-rh01.pg1f.p1.openshiftapps.com/k8s/ns/appstudio-grafana/services"
const check = "prometheus-appstudio-ds"
var getInClusterConfig = config.GetConfigOrDie
var getNewForConfig = kubernetes.NewForConfigOrDie

type CustomCollector struct {
requestCounter prometheus.Counter
konfluxDSExporter *prometheus.GaugeVec
}

// Creating a new instance of CustomCollector.
func NewCustomCollector() *CustomCollector {
return &CustomCollector{
requestCounter: prometheus.NewCounter(prometheus.CounterOpts{
Name: "request_count",
Help: "Number of requests handled by the handler",
}),
konfluxDSExporter: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "konflux_dsexporter",
Help: "Availability of konflux service/component",
},
[]string{"service", "check"}),
}
}

// Describe method sends descriptions of the metrics to Prometheus.
// When Prometheus scrapes the /metrics endpoint of the exporter,
// it first calls the Describe method to get a description of all the metrics.
func (e *CustomCollector) Describe(ch chan<- *prometheus.Desc) {
e.requestCounter.Describe(ch)
e.konfluxDSExporter.Describe(ch)
}

// Collect method sends the current values of the metrics to Prometheus.
// After Prometheus understands what metrics are available (using the `Describe` method),
// it then calls the `Collect` method to actually get the values of those metrics.
func (e *CustomCollector) Collect(ch chan<- prometheus.Metric) {
e.requestCounter.Collect(ch)
var availability float64

availability = CheckDataSourceExist(GetDataSources(GetGrafanaResource(NewKubeClient())), check)
e.konfluxDSExporter.WithLabelValues(service, check).Set(availability)
e.konfluxDSExporter.Collect(ch)
}

// get the grafna resource as a map
func GetGrafanaResource(clientset *kubernetes.Clientset) map[string]interface{} {
data, err := clientset.RESTClient().
Get().
AbsPath("/apis/grafana.integreatly.org/v1beta1").
Namespace("appstudio-grafana").
Resource("grafanas").
Name("grafana-oauth").
DoRaw(context.TODO())
var grafanaResource map[string]interface{}
err = json.Unmarshal(data, &grafanaResource)
if err != nil {
fmt.Printf("Error getting resource: %v\n", err)
os.Exit(1)
}

return grafanaResource
}

// get datasources from grafana resource
func GetDataSources(grafanaResource map[string]interface{}) []string {
// return empty string slice if datasources are not defined
if grafanaResource["status"].(map[string]any)["datasources"] == nil {
return make([]string, 0)
}
datasourcesIfc := grafanaResource["status"].(map[string]any)["datasources"].([]interface{})
datasources := make([]string, len(datasourcesIfc))
for i, v := range datasourcesIfc {
datasources[i] = v.(string)
}
return datasources
}

// check if datasource exists, return 1 if yes, 0 if not
func CheckDataSourceExist(datasources []string, dsToCheck string) float64 {
for _, datasource := range datasources {
if strings.Contains(datasource, dsToCheck) {
fmt.Println("Datasource", datasource, "exists")
return 1
}
}
fmt.Println("Datasource", dsToCheck, "does not exist")
return 0
}

func NewKubeClient() *kubernetes.Clientset {
// creates the in-cluster config
config := getInClusterConfig()
// creates the clientset
clientset := getNewForConfig(config)
return clientset
}

// Using a separate pedantic registry ensures that only your custom metric is exposed on the "/metrics" endpoint,
Expand All @@ -50,6 +120,7 @@ func main() {
Registry: reg,
},
))

fmt.Println("Server is listening on http://localhost:8090/metrics")
http.ListenAndServe(":8090", nil)
}
95 changes: 66 additions & 29 deletions exporters/dsexporter/dsexporter_test.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,83 @@
package main

import (
"fmt"
"testing"
"net/http"
"net/http/httptest"

"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func TestCustomCollector(t *testing.T) {
exporter := NewCustomCollector()
prometheus.MustRegister(exporter)

// Simulate collecting metrics and check the exported metric value.
metrics := prometheus.DefaultGatherer
metricFamilies, err := metrics.Gather()
assert.NoError(t, err)

var RequestCountValue float64
for _, mf := range metricFamilies {
if mf.GetName() == "request_count" {
RequestCountValue = mf.GetMetric()[0].GetCounter().GetValue()
break
}
func TestAvailabilityHandler(t *testing.T) {
mockExporter := NewCustomCollector()

req, err := http.NewRequest("GET", "/?service=test.com&check=test", nil)
if err != nil {
t.Fatal(err)
}

rec := httptest.NewRecorder()
availabilityHandler(rec, req, mockExporter)

if status := rec.Code; status != http.StatusOK {
t.Errorf("Service unavailable")
}

// Check whether the exported metric value is initially 0.
assert.Equal(t, float64(0), RequestCountValue)
expected := fmt.Sprintf("Service: test.com, Check Name: test, Availability:",)
assert.Contains(t, rec.Body.String(), expected)
}

func TestcheckIfServiceUpPass(t *testing.T) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer mockServer.Close()

serviceUrl := mockServer.URL

// Increment the requestCounter by calling the Inc method.
exporter.requestCounter.Inc()
available := checkIfServiceUp(serviceUrl)

if available == 0 {
t.Errorf("Service not accessible")
}
}

// Collecting metrics again
metricFamilies, err = metrics.Gather()
assert.NoError(t, err)
func TestcheckIfServiceUpFail(t *testing.T) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
}))
defer mockServer.Close()

for _, mf := range metricFamilies {
if mf.GetName() == "request_count" {
RequestCountValue = mf.GetMetric()[0].GetCounter().GetValue()
break
serviceUrl := mockServer.URL

available := checkIfServiceUp(serviceUrl)

if available == 0 {
t.Errorf("Service not accessible")
}
}

func TestCustomCollector(t *testing.T) {

mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

if r.URL.Path == "/metrics" {
promhttp.Handler().ServeHTTP(w, r)
return
}

}))

defer mockServer.Close()

resp, err := http.Get(mockServer.URL + "/metrics")
if err != nil {
t.Fatal(err)
}

// Check whether the exported metric value is now 1 after incrementing.
assert.Equal(t, float64(1), RequestCountValue)
if resp.StatusCode != http.StatusOK {
t.Errorf("Service unavailable")
}
}
58 changes: 50 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,63 @@
module github.com/redhat-appstudio/o11y.git

go 1.20
go 1.21

require github.com/prometheus/client_golang v1.17.0
toolchain go1.21.5

require github.com/prometheus/client_golang v1.18.0

require (
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.29.0 // indirect
k8s.io/apimachinery v0.29.0 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/sys v0.11.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/stretchr/testify v1.8.4
golang.org/x/sys v0.16.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/client-go v0.29.0
sigs.k8s.io/controller-runtime v0.17.0
)
Loading

0 comments on commit 3d2adc0

Please sign in to comment.