Skip to content

Commit

Permalink
update metrics
Browse files Browse the repository at this point in the history
export them and only register them if
http server feature gate is enabled

Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
  • Loading branch information
everettraven committed Aug 31, 2023
1 parent 42fc3e1 commit d9a2903
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
9 changes: 7 additions & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics"

"github.com/spf13/pflag"

Expand Down Expand Up @@ -123,8 +124,12 @@ func main() {
if err := os.MkdirAll(storageDir, 0700); err != nil {
setupLog.Error(err, "unable to create storage directory for catalogs")
}
srv := server.Instance{StorageDir: storageDir}
mgr.AddMetricsExtraHandler("/catalogs/", server.AddMetricsToHandler(srv.CatalogServerHandler()))

if features.CatalogdFeatureGate.Enabled(features.HTTPServer) {
metrics.Registry.MustRegister(server.ApdexTargetMetric, server.RequestDurationMetric)
srv := server.Instance{StorageDir: storageDir}
mgr.AddMetricsExtraHandler("/catalogs/", server.AddMetricsToHandler(srv.CatalogServerHandler()))

Check failure on line 131 in cmd/manager/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `mgr.AddMetricsExtraHandler` is not checked (errcheck)
}

if err = (&corecontrollers.CatalogReconciler{
Client: mgr.GetClient(),
Expand Down
27 changes: 14 additions & 13 deletions pkg/server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,44 @@ import (

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

const (
// target response time in seconds
apdexT = 0.5
ApdexTarget = 0.5

ApdexTargetMetricName = "catalogd_http_request_apdex_target_seconds"
RequestDurationMetricName = "catalogd_http_request_duration_seconds"
)

// Sets up the necessary metrics for calculating the Apdex Score
// If using Grafana for visualization connected to a Prometheus data
// source that is scraping these metrics, you can create a panel that
// uses the following queries + expressions for calculating the Apdex Score:
// Query A: sum(http_request_catalog_server_duration_seconds_bucket{code!~"5..",le="0.5"})
// Query B: (sum(http_request_catalog_server_duration_seconds_bucket{code!~"5..",le="2"}) - on(code) sum(http_request_catalog_server_duration_seconds_bucket{code!~"5..",le="0.5"}))
// Query C: sum(http_request_catalog_server_duration_seconds_count)
// Query A: sum(catalogd_http_request_duration_seconds_bucket{code!~"5..",le="0.5"})
// Query B: (sum(catalogd_http_request_duration_seconds_bucket{code!~"5..",le="2"}) - on(code) sum(catalogd_http_request_duration_seconds_bucket{code!~"5..",le="0.5"}))
// Query C: sum(catalogd_http_request_duration_seconds_count)
// Expression for Apdex Score: ($A - ($B / 2) / $C
var (
apdexGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "http_request_catalog_server_apdex_target_seconds",
ApdexTargetMetric = prometheus.NewGauge(prometheus.GaugeOpts{
Name: ApdexTargetMetricName,
Help: "The apdex target in seconds",
})

dur = prometheus.NewHistogramVec(
RequestDurationMetric = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_catalog_server_duration_seconds",
Name: RequestDurationMetricName,
Help: "Histogram of request duration in seconds",
Buckets: []float64{apdexT, apdexT * 4, apdexT * 8},
Buckets: []float64{ApdexTarget, ApdexTarget * 4, ApdexTarget * 8},
},
[]string{"code"},
)
)

func init() {
apdexGauge.Set(apdexT)
metrics.Registry.MustRegister(apdexGauge, dur)
ApdexTargetMetric.Set(ApdexTarget)
}

func AddMetricsToHandler(handler http.Handler) http.Handler {
return promhttp.InstrumentHandlerDuration(dur, handler)
return promhttp.InstrumentHandlerDuration(RequestDurationMetric, handler)
}

0 comments on commit d9a2903

Please sign in to comment.