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 7, 2024
1 parent c1b5f5e commit 5f722f9
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 60 deletions.
84 changes: 77 additions & 7 deletions exporters/dsexporter/dsexporter.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,108 @@
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"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

const service = "grafana"
const check = "prometheus-appstudio-ds"
var getInClusterConfig = config.GetConfigOrDie
var getNewForConfig = kubernetes.NewForConfigOrDie

type CustomCollector struct {
requestCounter prometheus.Counter
konfluxUp *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",
}),
konfluxUp: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "konflux_up",
Help: "Availability of the Konflux default grafana datasource",
},
[]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.konfluxUp.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.konfluxUp.WithLabelValues(service, check).Set(availability)
e.konfluxUp.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 +119,7 @@ func main() {
Registry: reg,
},
))

fmt.Println("Server is listening on http://localhost:8090/metrics")
http.ListenAndServe(":8090", nil)
}
113 changes: 85 additions & 28 deletions exporters/dsexporter/dsexporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,102 @@ package main

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

"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

func TestCustomCollector(t *testing.T) {
exporter := NewCustomCollector()
prometheus.MustRegister(exporter)
func MockInclusterConfig() (*rest.Config) {
return nil
}

// Simulate collecting metrics and check the exported metric value.
metrics := prometheus.DefaultGatherer
metricFamilies, err := metrics.Gather()
assert.NoError(t, err)
func MockNewForConfig(*rest.Config) (*kubernetes.Clientset) {
return nil
}

var RequestCountValue float64
for _, mf := range metricFamilies {
if mf.GetName() == "request_count" {
RequestCountValue = mf.GetMetric()[0].GetCounter().GetValue()
break
}
}
func TestNewKubeClient(t *testing.T) {
actualInclusterConfig := getInClusterConfig
getInClusterConfig = MockInclusterConfig
actualNewForConfig := getNewForConfig
getNewForConfig = MockNewForConfig

defer func() {
getInClusterConfig = actualInclusterConfig
getNewForConfig = actualNewForConfig
}()

// Check whether the exported metric value is initially 0.
assert.Equal(t, float64(0), RequestCountValue)
client := NewKubeClient()
assert.Nil(t, client, "Client is not nil")
}

// Increment the requestCounter by calling the Inc method.
exporter.requestCounter.Inc()
func TestCheckDataSourceExist(t *testing.T) {
datasources := []string{"prometheus-appstudio-ds", "prometheus-appstudio", "appstudio-ds"}
check := "prometheus-appstudio-ds"
result := CheckDataSourceExist(datasources, check)
if result != 1 {
t.Errorf("Expected result 1, but got %f", result)
}

// Collecting metrics again
metricFamilies, err = metrics.Gather()
assert.NoError(t, err)
check = "prometheus-appstudio-dsexporter"
result = CheckDataSourceExist(datasources, check)
if result != 0 {
t.Errorf("Expected result 0, but got %f", result)
}
}

for _, mf := range metricFamilies {
if mf.GetName() == "request_count" {
RequestCountValue = mf.GetMetric()[0].GetCounter().GetValue()
break
func TestGetGrafanaResource(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/apis/grafana.integreatly.org/v1beta1/namespaces/appstudio-grafana/grafanas/grafana-oauth" {
t.Errorf("Unexpected request path: %s", r.URL.Path)
}
response := `{"test": "data"}`
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(response))
}))
defer server.Close()

config := &rest.Config{
Host: server.URL,
}
clientset, err := kubernetes.NewForConfig(config)
if(err != nil) {
t.Fatalf("Error: %v", err)
}
result := GetGrafanaResource(clientset)
expectedResult := map[string]interface{}{"test": "data"}
if !reflect.DeepEqual(result, expectedResult) {
t.Errorf("Expected %v, but got %v", expectedResult, result)
}
}

func TestGetDataSources(t *testing.T) {
grafanaRes := map[string]interface{}{
"status": map[string]interface{}{
"datasources": []interface{}{"appstudio-ds1", "appstudio-ds2"},
},
}

// Check whether the exported metric value is now 1 after incrementing.
assert.Equal(t, float64(1), RequestCountValue)
expectedResult := []string{"appstudio-ds1", "appstudio-ds2"}
result := GetDataSources(grafanaRes)
if !reflect.DeepEqual(result, expectedResult) {
t.Errorf("Test-1 failed, Expected %v, but got %v", expectedResult, result)
}

// empty datasources
grafanaRes = map[string]interface{}{
"status": map[string]interface{}{
"datasources": []interface{}{},
},
}

expectedResult = []string{}
result = GetDataSources(grafanaRes)
if !reflect.DeepEqual(result, expectedResult) {
t.Errorf("Test-2 failed, Expected %v, but got %v", expectedResult, result)
}
}
57 changes: 49 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,62 @@
module github.com/redhat-appstudio/o11y.git

go 1.20
go 1.21

require github.com/prometheus/client_golang v1.17.0
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
github.com/stretchr/objx v0.5.0 // 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 // indirect
sigs.k8s.io/controller-runtime v0.17.0 // indirect
)
Loading

0 comments on commit 5f722f9

Please sign in to comment.