Skip to content

Commit

Permalink
Merge pull request #11 from sandhyadalavi/add-sample-metrics
Browse files Browse the repository at this point in the history
Added few sample metrics
  • Loading branch information
openshift-merge-robot authored Apr 29, 2022
2 parents 0411475 + 02f7b87 commit e66947b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
4 changes: 1 addition & 3 deletions apis/reference/v1alpha1/zz_generated.deepcopy.go

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

5 changes: 5 additions & 0 deletions cmd/reference-addon-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

refapis "github.com/openshift/reference-addon/apis"
"github.com/openshift/reference-addon/internal/controllers"
"github.com/openshift/reference-addon/internal/metrics"
)

var (
Expand Down Expand Up @@ -104,6 +105,10 @@ func main() {
os.Exit(1)
}

// register and fill metrics
metrics.RegisterMetrics()
metrics.RequestSampleResponseData()

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/go-logr/logr v0.4.0
github.com/prometheus/client_golang v1.7.1 // indirect
github.com/stretchr/testify v1.6.1
k8s.io/api v0.20.2
k8s.io/apiextensions-apiserver v0.20.1
Expand Down
4 changes: 4 additions & 0 deletions internal/controllers/reference_addon_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

refapisv1alpha1 "github.com/openshift/reference-addon/apis/reference/v1alpha1"
"github.com/openshift/reference-addon/internal/metrics"
)

type ReferenceAddonReconciler struct {
Expand All @@ -31,6 +32,9 @@ func (r *ReferenceAddonReconciler) Reconcile(
log.Info("reconciling for a reference addon object not prefixed by redhat- or named reference-addon")
}

// add sample metrics
metrics.RequestSampleResponseData()

return ctrl.Result{}, nil
}

Expand Down
57 changes: 57 additions & 0 deletions internal/metrics/url_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package metrics

import (
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
)

var (
externalURLs = []string{"https://httpstat.us/503", "https://httpstat.us/200"}

availability = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "reference_addon_sample_availability",
Help: "external url availability 0-not available and 1-available.",
},
[]string{"url"},
)
responseTime = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "reference_addon_sample_response_time",
Help: "external url response time taken.",
},
[]string{"url"},
)
)

// RegisterMetrics must register metrics in given registry collector
func RegisterMetrics() {
ctrlmetrics.Registry.MustRegister(availability)
ctrlmetrics.Registry.MustRegister(responseTime)
}

func RequestSampleResponseData() {
for _, externalURL := range externalURLs {
status, timeTaken := callExternalURL(externalURL)
availability.WithLabelValues(externalURL).Set(status)
responseTime.WithLabelValues(externalURL).Set(timeTaken)
}
}

func callExternalURL(externalURL string) (float64, float64) {
start := time.Now()
response, err := http.Get(externalURL)
if err != nil {
return 0, 0
}
defer response.Body.Close()
timeTaken := time.Since(start).Milliseconds()
status := 0
if response.StatusCode == 200 {
status = 1
}
return float64(status), float64(timeTaken)
}

0 comments on commit e66947b

Please sign in to comment.