Skip to content

Commit

Permalink
update metrics for clusternet hub (#566)
Browse files Browse the repository at this point in the history
Signed-off-by: Yiwei Chen <larrywei1024@gmail.com>
  • Loading branch information
yiwei-C committed Jan 10, 2023
1 parent 2fdc052 commit a4b9ac3
Show file tree
Hide file tree
Showing 19 changed files with 119 additions and 21 deletions.
5 changes: 2 additions & 3 deletions go.mod
Expand Up @@ -124,9 +124,9 @@ require (
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.1 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.30.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rubenv/sql-migrate v0.0.0-20210614095031-55d5740dbbcc // indirect
Expand Down Expand Up @@ -187,7 +187,6 @@ require (

replace (
github.com/opencontainers/image-spec => github.com/opencontainers/image-spec v1.0.2
github.com/spf13/afero => github.com/spf13/afero v1.5.1
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc => go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.20.0
go.opentelemetry.io/otel => go.opentelemetry.io/otel v0.20.0
go.opentelemetry.io/otel/oteltest => go.opentelemetry.io/otel/oteltest v0.20.0
Expand Down
14 changes: 10 additions & 4 deletions go.sum
Expand Up @@ -1073,8 +1073,9 @@ github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQ
github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_golang v1.11.1 h1:+4eQaD7vAZ6DsfsxB15hbE0odUjGI5ARs9yskGu1v4s=
github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_golang v1.12.2 h1:51L9cDoUHVrXx4zWYlcLQIZ+d+VXHgqnYKkIuq4g/34=
github.com/prometheus/client_golang v1.12.2/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
github.com/prometheus/client_model v0.0.0-20171117100541-99fa1f4be8e5/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand All @@ -1090,8 +1091,9 @@ github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8b
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
github.com/prometheus/common v0.28.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
github.com/prometheus/common v0.30.0 h1:JEkYlQnpzrzQFxi6gnukFPdQ+ac82oRhzMcIduJu/Ug=
github.com/prometheus/common v0.30.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4=
github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
github.com/prometheus/procfs v0.0.0-20180125133057-cb4147076ac7/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
Expand Down Expand Up @@ -1159,8 +1161,11 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.5.1 h1:VHu76Lk0LSP1x254maIu2bplkWpfBWI+B+6fdoZprcg=
github.com/spf13/afero v1.5.1/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
Expand Down Expand Up @@ -1629,6 +1634,7 @@ golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/apps/aggregatestatus/aggregatestatus.go
Expand Up @@ -80,7 +80,7 @@ func NewController(clusternetClient clusternetClientSet.Interface,

c := &Controller{
clusternetClient: clusternetClient,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "aggregatestatus"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "aggregating_status"),
subsLister: subsInformer.Lister(),
subsSynced: subsInformer.Informer().HasSynced,
descLister: descInformer.Lister(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/apps/base/base.go
Expand Up @@ -76,7 +76,7 @@ func NewController(clusternetClient clusternetclientset.Interface,

c := &Controller{
clusternetClient: clusternetClient,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "base"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Base"),
baseLister: baseInformer.Lister(),
baseSynced: baseInformer.Informer().HasSynced,
recorder: recorder,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/apps/description/description.go
Expand Up @@ -79,7 +79,7 @@ func NewController(clusternetClient clusternetClientSet.Interface,

c := &Controller{
clusternetClient: clusternetClient,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "description"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Description"),
descLister: descInformer.Lister(),
descSynced: descInformer.Informer().HasSynced,
hrSynced: hrInformer.Informer().HasSynced,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/apps/feedinventory/feedinventory.go
Expand Up @@ -85,7 +85,7 @@ func NewController(clusternetClient clusternetclientset.Interface,
reservedNamespace string, customSyncHandlerFunc SyncHandlerFunc) (*Controller, error) {
c := &Controller{
clusternetClient: clusternetClient,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "feedInventory"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "FeedInventory"),
subLister: subsInformer.Lister(),
subSynced: subsInformer.Informer().HasSynced,
finvLister: finvInformer.Lister(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/apps/globalization/globalization.go
Expand Up @@ -88,7 +88,7 @@ func NewController(clusternetClient clusternetclientset.Interface,

c := &Controller{
clusternetClient: clusternetClient,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "globalization"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Globalization"),
globLister: globInformer.Lister(),
globSynced: globInformer.Informer().HasSynced,
chartLister: chartInformer.Lister(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/apps/helmchart/helmchart.go
Expand Up @@ -78,7 +78,7 @@ func NewController(clusternetClient clusternetclientset.Interface,

c := &Controller{
clusternetClient: clusternetClient,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "helmChart"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "HelmChart"),
helmChartLister: helmChartInformer.Lister(),
helmChartSynced: helmChartInformer.Informer().HasSynced,
baseLister: baseInformer.Lister(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/apps/helmrelease/helmrelease.go
Expand Up @@ -71,7 +71,7 @@ func NewController(clusternetClient clusternetclientset.Interface, hrInformer ap

c := &Controller{
clusternetClient: clusternetClient,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "helmRelease"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "HelmRelease"),
hrLister: hrInformer.Lister(),
hrSynced: hrInformer.Informer().HasSynced,
recorder: recorder,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/apps/localization/localization.go
Expand Up @@ -88,7 +88,7 @@ func NewController(clusternetClient clusternetclientset.Interface,

c := &Controller{
clusternetClient: clusternetClient,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "localization"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Localization"),
locLister: locInformer.Lister(),
locSynced: locInformer.Informer().HasSynced,
chartLister: chartInformer.Lister(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/apps/manifest/manifest.go
Expand Up @@ -81,7 +81,7 @@ func NewController(clusternetClient clusternetclientset.Interface,

c := &Controller{
clusternetClient: clusternetClient,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "manifest"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Manifest"),
manifestLister: manifestInformer.Lister(),
manifestSynced: manifestInformer.Informer().HasSynced,
baseLister: baseInformer.Lister(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/apps/subscription/subscription.go
Expand Up @@ -80,7 +80,7 @@ func NewController(clusternetClient clusternetclientset.Interface,

c := &Controller{
clusternetClient: clusternetClient,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "subscription"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Subscription"),
subsLister: subsInformer.Lister(),
subsSynced: subsInformer.Informer().HasSynced,
baseSynced: baseInformer.Informer().HasSynced,
Expand Down
Expand Up @@ -66,7 +66,7 @@ func NewController(clusternetClient clusternetClientSet.Interface,
clusterInformer clusterinformers.ManagedClusterInformer, recorder record.EventRecorder) *Controller {
c := &Controller{
clusternetClient: clusternetClient,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "managedCluster"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ManagedCluster"),
clusterLister: clusterInformer.Lister(),
clusterSynced: clusterInformer.Informer().HasSynced,
recorder: recorder,
Expand Down
Expand Up @@ -76,7 +76,7 @@ func NewController(clusternetClient clusternetClientSet.Interface,
clusternetClient: clusternetClient,
crrsLister: crrsInformer.Lister(),
crrsSynced: crrsInformer.Informer().HasSynced,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "cluster-registration-requests"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ClusterRegistrationRequest"),
SyncHandler: syncHandler,
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/misc/secret/secret.go
Expand Up @@ -70,7 +70,7 @@ func NewController(kubeclient kubernetes.Interface, secretInformer coreInformers

c := &Controller{
kubeclient: kubeclient,
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "secret"),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Secret"),
secretLister: secretInformer.Lister(),
secretSynced: secretInformer.Informer().HasSynced,
recorder: recorder,
Expand Down
1 change: 1 addition & 0 deletions pkg/hub/hub.go
Expand Up @@ -69,6 +69,7 @@ import (
shadowapiserver "github.com/clusternet/clusternet/pkg/hub/apiserver/shadow"
"github.com/clusternet/clusternet/pkg/hub/approver"
"github.com/clusternet/clusternet/pkg/hub/deployer"
_ "github.com/clusternet/clusternet/pkg/hub/metrics"
"github.com/clusternet/clusternet/pkg/hub/options"
"github.com/clusternet/clusternet/pkg/known"
"github.com/clusternet/clusternet/pkg/utils"
Expand Down
31 changes: 31 additions & 0 deletions pkg/hub/metrics/metrics.go
@@ -0,0 +1,31 @@
/*
Copyright 2023 The Clusternet Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metrics

import (
"k8s.io/component-base/metrics/legacyregistry"

"github.com/clusternet/clusternet/pkg/registry/proxies/socket"
)

func init() {
// register clusternet metrics
legacyregistry.MustRegister(
socket.ConnectionCount,
socket.ConnectionDurationSeconds,
)
}
46 changes: 46 additions & 0 deletions pkg/registry/proxies/socket/metrics.go
@@ -0,0 +1,46 @@
/*
Copyright 2023 The Clusternet Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package socket

import (
compbasemetrics "k8s.io/component-base/metrics"
)

const (
metricSubsystem = "socket_connections"
)

var (
ConnectionCount = compbasemetrics.NewGauge(&compbasemetrics.GaugeOpts{
Namespace: category,
Subsystem: metricSubsystem,
Name: "count",
Help: "Number of socket connections",
StabilityLevel: compbasemetrics.ALPHA,
})

ConnectionDurationSeconds = compbasemetrics.NewHistogram(
&compbasemetrics.HistogramOpts{
Namespace: category,
Subsystem: metricSubsystem,
Name: "duration_seconds",
Help: "Active socket connection duration in seconds",
Buckets: compbasemetrics.ExponentialBuckets(60, 4, 8),
StabilityLevel: compbasemetrics.ALPHA,
},
)
)
17 changes: 16 additions & 1 deletion pkg/registry/proxies/socket/rest.go
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"net/http"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -79,7 +80,21 @@ func (r *REST) Connect(ctx context.Context, id string, opts runtime.Object, resp
return nil, fmt.Errorf("invalid options object: %#v", opts)
}

return r.Exchanger.Connect(ctx, id, socket, responder)
handler, err := r.Exchanger.Connect(ctx, id, socket, responder)
if err != nil {
return nil, err
}
// wrap the handler func only to better add metrics
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
startTime := time.Now()
defer func() {
defer ConnectionCount.Dec()
ConnectionDurationSeconds.Observe(time.Since(startTime).Seconds())
}()
ConnectionCount.Inc()

handler.ServeHTTP(writer, request)
}), nil
}

// NewREST returns a RESTStorage object that will work against API services.
Expand Down

0 comments on commit a4b9ac3

Please sign in to comment.