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 Jan 19, 2024
1 parent 696a09c commit 4bf255e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
16 changes: 9 additions & 7 deletions exporters/dsexporter/dsexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
)

type CustomCollector struct {
requestCounter prometheus.Counter
appstudioProbeSuccess *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",
}),
appstudioProbeSuccess: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "appstudio_probe_success",
Help: "Availability of appstudio 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.appstudioProbeSuccess.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)
e.appstudioProbeSuccess.Collect(ch)
}

// Using a separate pedantic registry ensures that only your custom metric is exposed on the "/metrics" endpoint,
Expand All @@ -42,6 +43,7 @@ func main() {
reg := prometheus.NewPedanticRegistry()
exporter := NewCustomCollector()
reg.MustRegister(exporter)
exporter.appstudioProbeSuccess.With(prometheus.Labels{"service":"test_service", "check":"github"}).Inc()

fmt.Println("Server is listening on http://localhost:8090/metrics")

Expand Down
18 changes: 9 additions & 9 deletions exporters/dsexporter/dsexporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@ func TestCustomCollector(t *testing.T) {
metricFamilies, err := metrics.Gather()
assert.NoError(t, err)

var RequestCountValue float64
var appstudioProbeSuccessValue float64
for _, mf := range metricFamilies {
if mf.GetName() == "request_count" {
RequestCountValue = mf.GetMetric()[0].GetCounter().GetValue()
if mf.GetName() == "appstudio_probe_success" {
appstudioProbeSuccessValue = mf.GetMetric()[0].GetCounter().GetValue()
break
}
}

// Check whether the exported metric value is initially 0.
assert.Equal(t, float64(0), RequestCountValue)
assert.Equal(t, float64(0), appstudioProbeSuccessValue)

// Increment the requestCounter by calling the Inc method.
exporter.requestCounter.Inc()
// Increment the appstudioProbeSuccess by calling the Inc method.
exporter.appstudioProbeSuccess.With(prometheus.Labels{"service":"test_service", "check":"github"}).Inc()

// Collecting metrics again
metricFamilies, err = metrics.Gather()
assert.NoError(t, err)

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

// Check whether the exported metric value is now 1 after incrementing.
assert.Equal(t, float64(1), RequestCountValue)
assert.Equal(t, float64(1), appstudioProbeSuccessValue)
}

0 comments on commit 4bf255e

Please sign in to comment.