Skip to content

Commit ab1d3ab

Browse files
authored
Merge pull request #371 from laozc/add-prom-metrics
Set up a metrics server with data served on /metrics
2 parents 0c80fc0 + 1dae255 commit ab1d3ab

File tree

356 files changed

+73435
-26
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

356 files changed

+73435
-26
lines changed

.github/workflows/windows.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
Copy-Item -Path .\bin\csi-proxy.exe -Destination "C:\etc\kubernetes\node\bin\csi-proxy.exe"
3434
3535
# restart the csiproxy service
36-
$flags = "-v=5 -windows-service -log_file=C:\etc\kubernetes\logs\csi-proxy.log -logtostderr=false"
36+
$flags = "-v=5 -windows-service -log_file=C:\etc\kubernetes\logs\csi-proxy.log -logtostderr=false -metrics-bind-address=localhost:8888"
3737
sc.exe create csiproxy start= "auto" binPath= "C:\etc\kubernetes\node\bin\csi-proxy.exe $flags"
3838
sc.exe failure csiproxy reset= 0 actions= restart/10000
3939
sc.exe start csiproxy

cmd/csi-proxy/main.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
disksrv "github.com/kubernetes-csi/csi-proxy/pkg/server/disk"
1414
filesystemsrv "github.com/kubernetes-csi/csi-proxy/pkg/server/filesystem"
1515
iscsisrv "github.com/kubernetes-csi/csi-proxy/pkg/server/iscsi"
16+
"github.com/kubernetes-csi/csi-proxy/pkg/server/metrics"
1617
smbsrv "github.com/kubernetes-csi/csi-proxy/pkg/server/smb"
1718
syssrv "github.com/kubernetes-csi/csi-proxy/pkg/server/system"
1819
srvtypes "github.com/kubernetes-csi/csi-proxy/pkg/server/types"
@@ -34,11 +35,12 @@ func (i *workingDirFlags) Set(value string) error {
3435
}
3536

3637
var (
37-
kubeletPath = flag.String("kubelet-path", `C:\var\lib\kubelet`, "Prefix path of the kubelet directory in the host file system")
38-
windowsSvc = flag.Bool("windows-service", false, "Configure as a Windows Service")
39-
requirePrivacy = flag.Bool("require-privacy", true, "If true, New-SmbGlobalMapping will be called with -RequirePrivacy $true")
40-
service *handler
41-
workingDirs workingDirFlags
38+
kubeletPath = flag.String("kubelet-path", `C:\var\lib\kubelet`, "Prefix path of the kubelet directory in the host file system")
39+
windowsSvc = flag.Bool("windows-service", false, "Configure as a Windows Service")
40+
requirePrivacy = flag.Bool("require-privacy", true, "If true, New-SmbGlobalMapping will be called with -RequirePrivacy $true")
41+
metricsBindAddr = flag.String("metrics-bind-address", "", "The address the metric endpoint binds to. Defaults to empty in which case metrics are disabled")
42+
service *handler
43+
workingDirs workingDirFlags
4244
)
4345

4446
type handler struct {
@@ -68,7 +70,15 @@ func main() {
6870
if err != nil {
6971
panic(err)
7072
}
71-
s := server.NewServer(apiGroups...)
73+
74+
enableMetrics := *metricsBindAddr != ""
75+
if enableMetrics {
76+
err := metrics.SetupMetricsServer(*metricsBindAddr)
77+
if err != nil {
78+
panic(err)
79+
}
80+
}
81+
s := server.NewServer(enableMetrics, apiGroups...)
7282

7383
if err := s.Start(nil); err != nil {
7484
panic(err)

go.mod

+17-2
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,46 @@ toolchain go1.22.3
1010
require (
1111
github.com/Microsoft/go-winio v0.6.2
1212
github.com/google/go-cmp v0.6.0
13+
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1
1314
github.com/iancoleman/strcase v0.3.0
1415
github.com/kubernetes-csi/csi-proxy/client v1.1.3
1516
github.com/pkg/errors v0.9.1
17+
github.com/prometheus/client_golang v1.20.5
1618
github.com/sergi/go-diff v1.3.1
1719
github.com/spf13/pflag v1.0.5
1820
github.com/stretchr/testify v1.10.0
1921
golang.org/x/sys v0.28.0
2022
google.golang.org/grpc v1.69.2
2123
google.golang.org/protobuf v1.36.0
24+
k8s.io/component-base v0.28.4
2225
k8s.io/gengo v0.0.0-20240911193312-2b36238f13e9
2326
k8s.io/klog/v2 v2.130.1
2427
)
2528

2629
require (
27-
github.com/davecgh/go-spew v1.1.1 // indirect
30+
github.com/beorn7/perks v1.0.1 // indirect
31+
github.com/blang/semver/v4 v4.0.0 // indirect
32+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
33+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2834
github.com/go-logr/logr v1.4.2 // indirect
2935
github.com/golang/protobuf v1.5.4 // indirect
30-
github.com/pmezard/go-difflib v1.0.0 // indirect
36+
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
37+
github.com/klauspost/compress v1.17.9 // indirect
38+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
39+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
40+
github.com/prometheus/client_model v0.6.1 // indirect
41+
github.com/prometheus/common v0.55.0 // indirect
42+
github.com/prometheus/procfs v0.15.1 // indirect
43+
github.com/rogpeppe/go-internal v1.13.1 // indirect
44+
go.opentelemetry.io/otel v1.33.0 // indirect
3145
golang.org/x/mod v0.22.0 // indirect
3246
golang.org/x/net v0.33.0 // indirect
3347
golang.org/x/sync v0.10.0 // indirect
3448
golang.org/x/text v0.21.0 // indirect
3549
golang.org/x/tools v0.28.0 // indirect
3650
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 // indirect
3751
gopkg.in/yaml.v3 v3.0.1 // indirect
52+
k8s.io/apimachinery v0.28.4 // indirect
3853
k8s.io/klog v1.0.0 // indirect
3954
)
4055

go.sum

+50-11
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
33
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
44
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
55
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
6+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
7+
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
8+
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
9+
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
610
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
11+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
12+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
713
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
814
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
915
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1116
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
17+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
18+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1219
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
1320
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
1421
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
@@ -42,21 +49,44 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
4249
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4350
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4451
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
52+
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 h1:qnpSQwGEnkcRpTqNOIR6bJbR0gAorgP9CSALpRcKoAA=
53+
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1/go.mod h1:lXGCsh6c22WGtjr+qGHj1otzZpV/1kwTMAqkwZsnWRU=
54+
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 h1:pRhl55Yx1eC7BZ1N+BBWwnKaMyD8uC+34TLdndZMAKk=
55+
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0/go.mod h1:XKMd7iuf/RGPSMJ/U4HP0zS2Z9Fh8Ps9a+6X26m/tmI=
4556
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
4657
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
58+
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
59+
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
4760
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
48-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
4961
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
62+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
63+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
5064
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
51-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
5265
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
66+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
67+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
68+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
69+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
5370
github.com/mauriciopoppe/gengo v0.0.0-20210525224835-9c78f58f3486 h1:+l047vEi0SyAzdVToIaAcfoY5DwwGW+OyqTdH/P3TTg=
5471
github.com/mauriciopoppe/gengo v0.0.0-20210525224835-9c78f58f3486/go.mod h1:xXv3T4UXTLta31wMhVezwVkc26OLei4hMbLeBJbPmxc=
72+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
73+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
5574
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
5675
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
57-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5876
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
77+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
78+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
79+
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
80+
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
5981
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
82+
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
83+
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
84+
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
85+
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
86+
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
87+
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
88+
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
89+
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
6090
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
6191
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
6292
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
@@ -69,16 +99,18 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
6999
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
70100
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
71101
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
72-
go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY=
73-
go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE=
74-
go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE=
75-
go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY=
102+
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
103+
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
104+
go.opentelemetry.io/otel v1.33.0 h1:/FerN9bax5LoK51X/sI0SVYrjSE0/yUL7DpxW4K3FWw=
105+
go.opentelemetry.io/otel v1.33.0/go.mod h1:SUUkR6csvUQl+yjReHu5uM3EtVV7MBm5FHKRlNx4I8I=
106+
go.opentelemetry.io/otel/metric v1.33.0 h1:r+JOocAyeRVXD8lZpjdQjzMadVZp2M4WmQ+5WtEnklQ=
107+
go.opentelemetry.io/otel/metric v1.33.0/go.mod h1:L9+Fyctbp6HFTddIxClbQkjtubW6O9QS3Ann/M82u6M=
76108
go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk=
77109
go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0=
78110
go.opentelemetry.io/otel/sdk/metric v1.31.0 h1:i9hxxLJF/9kkvfHppyLL55aW7iIJz4JjxTeYusH7zMc=
79111
go.opentelemetry.io/otel/sdk/metric v1.31.0/go.mod h1:CRInTMVvNhUKgSAMbKyTMxqOBC0zgyxzW55lZzX43Y8=
80-
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
81-
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
112+
go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s=
113+
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
82114
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
83115
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
84116
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -140,15 +172,22 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
140172
google.golang.org/protobuf v1.36.0 h1:mjIs9gYtt56AzC4ZaffQuh88TZurBGhIJMBZGSxNerQ=
141173
google.golang.org/protobuf v1.36.0/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
142174
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
143-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
144175
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
176+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
177+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
145178
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
146179
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
147180
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
148181
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
149182
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
150183
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
184+
k8s.io/apimachinery v0.28.4 h1:zOSJe1mc+GxuMnFzD4Z/U1wst50X28ZNsn5bhgIIao8=
185+
k8s.io/apimachinery v0.28.4/go.mod h1:wI37ncBvfAoswfq626yPTe6Bz1c22L7uaJ8dho83mgg=
186+
k8s.io/component-base v0.28.4 h1:c/iQLWPdUgI90O+T9TeECg8o7N3YJTiuz2sKxILYcYo=
187+
k8s.io/component-base v0.28.4/go.mod h1:m9hR0uvqXDybiGL2nf/3Lf0MerAfQXzkfWhUY58JUbU=
151188
k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8=
152189
k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
153190
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
154191
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
192+
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
193+
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=

integrationtests/filesystem_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package integrationtests
22

33
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"net/http"
48
"os"
59
"testing"
610
)
@@ -19,17 +23,42 @@ func pathExists(path string) (bool, error) {
1923
func TestFilesystemAPIGroup(t *testing.T) {
2024
t.Run("v2alpha1FilesystemTests", func(t *testing.T) {
2125
v2alpha1FilesystemTests(t)
26+
testMetrics(t)
2227
})
2328
t.Run("v1FilesystemTests", func(t *testing.T) {
2429
v1FilesystemTests(t)
30+
testMetrics(t)
2531
})
2632
t.Run("v1beta2FilesystemTests", func(t *testing.T) {
2733
v1beta2FilesystemTests(t)
34+
testMetrics(t)
2835
})
2936
t.Run("v1beta1FilesystemTests", func(t *testing.T) {
3037
v1beta1FilesystemTests(t)
38+
testMetrics(t)
3139
})
3240
t.Run("v1alpha1FilesystemTests", func(t *testing.T) {
3341
v1alpha1FilesystemTests(t)
42+
testMetrics(t)
3443
})
3544
}
45+
46+
func testMetrics(t *testing.T) {
47+
metricsAddress := "localhost:8888"
48+
resp, err := http.Get(fmt.Sprintf("http://%s/metrics", metricsAddress))
49+
if err != nil {
50+
t.Fatalf("Failed to send GET request: %v", err)
51+
}
52+
defer resp.Body.Close()
53+
54+
// Read the response body
55+
body, err := io.ReadAll(resp.Body)
56+
if err != nil {
57+
t.Fatalf("Failed to read response body: %v", err)
58+
}
59+
60+
// Check if the response body contains 'grpc_server_handling_seconds_bucket' metrics for CreateSymlink
61+
if !bytes.Contains(body, []byte("grpc_server_handling_seconds_bucket{grpc_method=\"CreateSymlink\"")) {
62+
t.Fatalf("Response did not contain 'grpc_server_handling_seconds_bucket' metrics. Response: %s", body)
63+
}
64+
}

integrationtests/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
// startServer starts the proxy's GRPC servers, and returns a function to shut them down when done with testing
2929
func startServer(t *testing.T, apiGroups ...srvtypes.APIGroup) func() {
30-
s := server.NewServer(apiGroups...)
30+
s := server.NewServer(true, apiGroups...)
3131

3232
listeningChan := make(chan interface{})
3333
go func() {

pkg/server/metrics/metrics.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package metrics
2+
3+
import (
4+
"net"
5+
"net/http"
6+
"strings"
7+
8+
grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
9+
"github.com/prometheus/client_golang/prometheus"
10+
"google.golang.org/grpc"
11+
"k8s.io/component-base/metrics/legacyregistry"
12+
"k8s.io/klog/v2"
13+
)
14+
15+
var (
16+
// metricsList is a list of all raw metrics that should be registered always, regardless of any feature gate's value.
17+
metricsList []prometheus.Collector
18+
grpcServerMetrics *grpcprom.ServerMetrics
19+
)
20+
21+
// Register registers a list of metrics.
22+
func Register() {
23+
grpcServerMetrics = grpcprom.NewServerMetrics(
24+
grpcprom.WithServerHandlingTimeHistogram(
25+
grpcprom.WithHistogramBuckets([]float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120}),
26+
),
27+
)
28+
29+
metricsList = []prometheus.Collector{
30+
grpcServerMetrics,
31+
}
32+
33+
for _, metric := range metricsList {
34+
legacyregistry.RawMustRegister(metric)
35+
}
36+
}
37+
38+
// SetupMetricsServer creates an HTTP server to expose the gRPC server metrics
39+
// at the`endpoint at `metricsAddress`
40+
func SetupMetricsServer(metricsAddress string) error {
41+
if metricsAddress == "" {
42+
return nil
43+
}
44+
l, err := net.Listen("tcp", metricsAddress)
45+
if err != nil {
46+
klog.Warningf("failed to get listener for metrics endpoint: %v", err)
47+
return err
48+
}
49+
path := l.Addr().String()
50+
klog.V(2).Infof("set up prometheus server on %v", path)
51+
go func() {
52+
defer l.Close()
53+
54+
m := http.NewServeMux()
55+
m.Handle("/metrics", legacyregistry.Handler())
56+
if err := trapClosedConnErr(http.Serve(l, m)); err != nil {
57+
klog.Fatalf("serve failure(%v), address(%v)", err, path)
58+
}
59+
}()
60+
return nil
61+
}
62+
63+
func trapClosedConnErr(err error) error {
64+
if err == nil {
65+
return nil
66+
}
67+
if strings.Contains(err.Error(), "use of closed network connection") {
68+
return nil
69+
}
70+
return err
71+
}
72+
73+
// GRPCServerMetricsOptions returns the ServerOption applying on gRPC server
74+
// to collect server metrics
75+
func GRPCServerMetricsOptions() []grpc.ServerOption {
76+
return []grpc.ServerOption{
77+
grpc.ChainUnaryInterceptor(
78+
grpcServerMetrics.UnaryServerInterceptor(),
79+
),
80+
grpc.ChainStreamInterceptor(
81+
grpcServerMetrics.StreamServerInterceptor(),
82+
),
83+
}
84+
}

0 commit comments

Comments
 (0)