Skip to content

Commit

Permalink
Feat: add ocm proxy request metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Yin Da <[email protected]>
  • Loading branch information
Somefive committed Feb 17, 2023
1 parent 0e394d5 commit b87ceb9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/apis/cluster/v1alpha1/clustergateway_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func (c *ClusterGatewayProxy) New() runtime.Object {
func (in *ClusterGatewayProxy) Destroy() {}

func (c *ClusterGatewayProxy) Connect(ctx context.Context, id string, options runtime.Object, r registryrest.Responder) (http.Handler, error) {
ts := time.Now()

proxyOpts, ok := options.(*ClusterGatewayProxyOptions)
if !ok {
return nil, fmt.Errorf("invalid options object: %#v", options)
Expand Down Expand Up @@ -161,6 +163,7 @@ func (c *ClusterGatewayProxy) Connect(ctx context.Context, id string, options ru
finishFunc: func(code int) {
metrics.RecordProxiedRequestsByResource(proxyReqInfo.Resource, proxyReqInfo.Verb, code)
metrics.RecordProxiedRequestsByCluster(id, code)
metrics.RecordProxiedRequestsDuration(proxyReqInfo.Resource, proxyReqInfo.Verb, id, code, time.Since(ts))
},
}, nil
}
Expand Down Expand Up @@ -197,7 +200,21 @@ var (
apiSuffix = "/proxy"
)

func (p *proxyHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
type proxyResponseWriter struct {
http.ResponseWriter
statusCode int
}

func (in *proxyResponseWriter) WriteHeader(statusCode int) {
in.statusCode = statusCode
in.ResponseWriter.WriteHeader(statusCode)
}

func (p *proxyHandler) ServeHTTP(_writer http.ResponseWriter, request *http.Request) {
writer := &proxyResponseWriter{_writer, http.StatusOK}
defer func() {
p.finishFunc(writer.statusCode)
}()
cluster := p.clusterGateway
if cluster.Spec.Access.Credential == nil {
responsewriters.InternalError(writer, request, fmt.Errorf("proxying cluster %s not support due to lacking credentials", cluster.Name))
Expand Down
18 changes: 18 additions & 0 deletions pkg/metrics/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"strconv"
"time"

compbasemetrics "k8s.io/component-base/metrics"
)
Expand Down Expand Up @@ -45,6 +46,17 @@ var (
},
[]string{proxiedCluster, code},
)
ocmProxiedRequestsDurationHistogram = compbasemetrics.NewHistogramVec(
&compbasemetrics.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "proxied_request_duration_seconds",
Help: "Cluster proxy request time cost",
Buckets: requestDurationSecondsBuckets,
StabilityLevel: compbasemetrics.ALPHA,
},
[]string{proxiedResource, proxiedVerb, proxiedCluster, code},
)
ocmProxiedClusterEscalationRequestDurationHistogram = compbasemetrics.NewHistogramVec(
&compbasemetrics.HistogramOpts{
Namespace: namespace,
Expand All @@ -69,3 +81,9 @@ func RecordProxiedRequestsByCluster(cluster string, code int) {
WithLabelValues(cluster, strconv.Itoa(code)).
Inc()
}

func RecordProxiedRequestsDuration(resource string, verb string, cluster string, code int, ts time.Duration) {
ocmProxiedRequestsDurationHistogram.
WithLabelValues(resource, verb, cluster, strconv.Itoa(code)).
Observe(ts.Seconds())
}
1 change: 1 addition & 0 deletions pkg/metrics/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var registerMetrics sync.Once
var metrics = []compbasemetrics.Registerable{
ocmProxiedRequestsByResourceTotal,
ocmProxiedRequestsByClusterTotal,
ocmProxiedRequestsDurationHistogram,
ocmProxiedClusterEscalationRequestDurationHistogram,
}

Expand Down

0 comments on commit b87ceb9

Please sign in to comment.