Skip to content

Commit 974e701

Browse files
committed
Expose gRPC prometheus metrics
1 parent 154e6db commit 974e701

File tree

433 files changed

+101860
-24
lines changed

Some content is hidden

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

433 files changed

+101860
-24
lines changed

cmd/csi-proxy/main.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"flag"
5+
"net/http"
56

67
diskapi "github.com/kubernetes-csi/csi-proxy/pkg/os/disk"
78
filesystemapi "github.com/kubernetes-csi/csi-proxy/pkg/os/filesystem"
@@ -17,6 +18,8 @@ import (
1718
syssrv "github.com/kubernetes-csi/csi-proxy/pkg/server/system"
1819
srvtypes "github.com/kubernetes-csi/csi-proxy/pkg/server/types"
1920
volumesrv "github.com/kubernetes-csi/csi-proxy/pkg/server/volume"
21+
"github.com/prometheus/client_golang/prometheus"
22+
"github.com/prometheus/client_golang/prometheus/promhttp"
2023
"golang.org/x/sys/windows"
2124
"golang.org/x/sys/windows/svc"
2225
"k8s.io/klog/v2"
@@ -34,11 +37,12 @@ func (i *workingDirFlags) Set(value string) error {
3437
}
3538

3639
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
40+
kubeletPath = flag.String("kubelet-path", `C:\var\lib\kubelet`, "Prefix path of the kubelet directory in the host file system")
41+
windowsSvc = flag.Bool("windows-service", false, "Configure as a Windows Service")
42+
requirePrivacy = flag.Bool("require-privacy", true, "If true, New-SmbGlobalMapping will be called with -RequirePrivacy $true")
43+
metricsBindAddr = flag.String("metrics-bind-addr", "localhost:10010", "The address the metric endpoint binds to.")
44+
service *handler
45+
workingDirs workingDirFlags
4246
)
4347

4448
type handler struct {
@@ -68,7 +72,22 @@ func main() {
6872
if err != nil {
6973
panic(err)
7074
}
71-
s := server.NewServer(apiGroups...)
75+
76+
reg := prometheus.NewRegistry()
77+
go func() error {
78+
m := http.NewServeMux()
79+
// Create HTTP handler for Prometheus metrics.
80+
m.Handle("/metrics", promhttp.HandlerFor(
81+
reg,
82+
promhttp.HandlerOpts{
83+
// Opt into OpenMetrics e.g. to support exemplars.
84+
EnableOpenMetrics: true,
85+
},
86+
))
87+
88+
return http.ListenAndServe(*metricsBindAddr, m)
89+
}()
90+
s := server.NewServer(reg, apiGroups...)
7291

7392
if err := s.Start(nil); err != nil {
7493
panic(err)

go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ godebug winsymlink=0
77
require (
88
github.com/Microsoft/go-winio v0.6.2
99
github.com/google/go-cmp v0.6.0
10+
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1
1011
github.com/iancoleman/strcase v0.3.0
1112
github.com/kubernetes-csi/csi-proxy/client v1.1.3
1213
github.com/pkg/errors v0.9.1
14+
github.com/prometheus/client_golang v1.20.5
1315
github.com/sergi/go-diff v1.3.1
1416
github.com/spf13/pflag v1.0.5
1517
github.com/stretchr/testify v1.10.0
18+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.58.0
1619
golang.org/x/sys v0.28.0
1720
google.golang.org/grpc v1.69.2
1821
google.golang.org/protobuf v1.36.0
@@ -21,10 +24,23 @@ require (
2124
)
2225

2326
require (
27+
github.com/beorn7/perks v1.0.1 // indirect
28+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2429
github.com/davecgh/go-spew v1.1.1 // indirect
2530
github.com/go-logr/logr v1.4.2 // indirect
31+
github.com/go-logr/stdr v1.2.2 // indirect
2632
github.com/golang/protobuf v1.5.4 // indirect
33+
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
34+
github.com/klauspost/compress v1.17.9 // indirect
35+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
2736
github.com/pmezard/go-difflib v1.0.0 // indirect
37+
github.com/prometheus/client_model v0.6.1 // indirect
38+
github.com/prometheus/common v0.55.0 // indirect
39+
github.com/prometheus/procfs v0.15.1 // indirect
40+
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
41+
go.opentelemetry.io/otel v1.33.0 // indirect
42+
go.opentelemetry.io/otel/metric v1.33.0 // indirect
43+
go.opentelemetry.io/otel/trace v1.33.0 // indirect
2844
golang.org/x/mod v0.22.0 // indirect
2945
golang.org/x/net v0.33.0 // indirect
3046
golang.org/x/sync v0.10.0 // indirect

go.sum

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ 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=
68
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
9+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
10+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
711
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
812
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
913
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -14,6 +18,7 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
1418
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
1519
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
1620
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
21+
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
1722
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
1823
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
1924
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
@@ -42,21 +47,43 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
4247
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4348
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4449
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
50+
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 h1:qnpSQwGEnkcRpTqNOIR6bJbR0gAorgP9CSALpRcKoAA=
51+
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1/go.mod h1:lXGCsh6c22WGtjr+qGHj1otzZpV/1kwTMAqkwZsnWRU=
52+
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 h1:pRhl55Yx1eC7BZ1N+BBWwnKaMyD8uC+34TLdndZMAKk=
53+
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0/go.mod h1:XKMd7iuf/RGPSMJ/U4HP0zS2Z9Fh8Ps9a+6X26m/tmI=
4554
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
4655
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
56+
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
57+
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
4758
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=
4959
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
60+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
61+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
5062
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
51-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
5263
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
64+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
65+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
66+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
67+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
5368
github.com/mauriciopoppe/gengo v0.0.0-20210525224835-9c78f58f3486 h1:+l047vEi0SyAzdVToIaAcfoY5DwwGW+OyqTdH/P3TTg=
5469
github.com/mauriciopoppe/gengo v0.0.0-20210525224835-9c78f58f3486/go.mod h1:xXv3T4UXTLta31wMhVezwVkc26OLei4hMbLeBJbPmxc=
70+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
71+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
5572
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
5673
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
5774
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5875
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
76+
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
77+
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
5978
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
79+
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
80+
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
81+
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
82+
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
83+
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
84+
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
85+
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
86+
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
6087
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
6188
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
6289
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
@@ -69,16 +96,20 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
6996
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
7097
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
7198
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=
99+
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
100+
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
101+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.58.0 h1:PS8wXpbyaDJQ2VDHHncMe9Vct0Zn1fEjpsjrLxGJoSc=
102+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.58.0/go.mod h1:HDBUsEjOuRC0EzKZ1bSaRGZWUBAzo+MhAcUUORSr4D0=
103+
go.opentelemetry.io/otel v1.33.0 h1:/FerN9bax5LoK51X/sI0SVYrjSE0/yUL7DpxW4K3FWw=
104+
go.opentelemetry.io/otel v1.33.0/go.mod h1:SUUkR6csvUQl+yjReHu5uM3EtVV7MBm5FHKRlNx4I8I=
105+
go.opentelemetry.io/otel/metric v1.33.0 h1:r+JOocAyeRVXD8lZpjdQjzMadVZp2M4WmQ+5WtEnklQ=
106+
go.opentelemetry.io/otel/metric v1.33.0/go.mod h1:L9+Fyctbp6HFTddIxClbQkjtubW6O9QS3Ann/M82u6M=
76107
go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk=
77108
go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0=
78109
go.opentelemetry.io/otel/sdk/metric v1.31.0 h1:i9hxxLJF/9kkvfHppyLL55aW7iIJz4JjxTeYusH7zMc=
79110
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=
111+
go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s=
112+
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
82113
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
83114
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
84115
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -140,8 +171,9 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
140171
google.golang.org/protobuf v1.36.0 h1:mjIs9gYtt56AzC4ZaffQuh88TZurBGhIJMBZGSxNerQ=
141172
google.golang.org/protobuf v1.36.0/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
142173
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=
144174
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
175+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
176+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
145177
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
146178
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
147179
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

integrationtests/utils.go

Lines changed: 1 addition & 1 deletion
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(nil, apiGroups...)
3131

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

pkg/server/server.go

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,45 @@ import (
66
"sync"
77

88
"github.com/Microsoft/go-winio"
9+
grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
910
"github.com/kubernetes-csi/csi-proxy/client"
1011
srvtypes "github.com/kubernetes-csi/csi-proxy/pkg/server/types"
1112
"github.com/pkg/errors"
13+
"github.com/prometheus/client_golang/prometheus"
14+
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
1215
"google.golang.org/grpc"
1316
)
1417

1518
// Server aggregates a number of API groups and versions,
1619
// and serves requests for all of them.
1720
type Server struct {
18-
versionedAPIs []*srvtypes.VersionedAPI
19-
started bool
20-
mutex *sync.Mutex
21-
grpcServers []*grpc.Server
21+
versionedAPIs []*srvtypes.VersionedAPI
22+
started bool
23+
mutex *sync.Mutex
24+
grpcServers []*grpc.Server
25+
prometheusRegistry *prometheus.Registry
26+
prometheusMetrics *grpcprom.ServerMetrics
2227
}
2328

2429
// NewServer creates a new Server for the given API groups.
25-
func NewServer(apiGroups ...srvtypes.APIGroup) *Server {
30+
func NewServer(reg *prometheus.Registry, apiGroups ...srvtypes.APIGroup) *Server {
2631
versionedAPIs := make([]*srvtypes.VersionedAPI, 0, len(apiGroups))
2732
for _, apiGroup := range apiGroups {
2833
versionedAPIs = append(versionedAPIs, apiGroup.VersionedAPIs()...)
2934
}
3035

36+
srvMetrics := grpcprom.NewServerMetrics(
37+
grpcprom.WithServerHandlingTimeHistogram(
38+
grpcprom.WithHistogramBuckets([]float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120}),
39+
),
40+
)
41+
reg.MustRegister(srvMetrics)
42+
3143
return &Server{
32-
versionedAPIs: versionedAPIs,
33-
mutex: &sync.Mutex{},
44+
versionedAPIs: versionedAPIs,
45+
mutex: &sync.Mutex{},
46+
prometheusRegistry: reg,
47+
prometheusMetrics: srvMetrics,
3448
}
3549
}
3650

@@ -70,6 +84,24 @@ func (s *Server) startListening() (chan *versionedAPIDone, []error) {
7084
return s.createAndStartGRPCServers(listeners), nil
7185
}
7286

87+
//
88+
//func (s *Server) createOtelExporter(ctx context.Context) {
89+
// exporter, err := otlptracegrpc.New(ctx,
90+
// otlptracegrpc.WithInsecure(),
91+
// )
92+
// if err != nil {
93+
// log.Fatalf("failed to create exporter: %v", err)
94+
// }
95+
//
96+
// tp := sdktrace.NewTracerProvider(
97+
// sdktrace.WithSampler(sdktrace.AlwaysSample()),
98+
// sdktrace.WithBatcher(exporter),
99+
// )
100+
// otel.SetTracerProvider(tp)
101+
// otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
102+
// //defer func() { _ = exporter.Shutdown(context.Background()) }()
103+
//}
104+
73105
// createListeners creates the named pipes.
74106
func (s *Server) createListeners() (listeners []net.Listener, errors []error) {
75107
listeners = make([]net.Listener, len(s.versionedAPIs))
@@ -107,8 +139,25 @@ func (s *Server) createAndStartGRPCServers(listeners []net.Listener) chan *versi
107139
doneChan := make(chan *versionedAPIDone, len(s.versionedAPIs))
108140
s.grpcServers = make([]*grpc.Server, len(s.versionedAPIs))
109141

142+
//s.createOtelExporter(context.Background())
143+
110144
for i, versionedAPI := range s.versionedAPIs {
111-
grpcServer := grpc.NewServer()
145+
opts := []grpc.ServerOption{
146+
grpc.StatsHandler(otelgrpc.NewServerHandler()),
147+
grpc.ChainUnaryInterceptor(
148+
s.prometheusMetrics.UnaryServerInterceptor(), //grpcprom.WithExemplarFromContext(exemplarFromContext)),
149+
// logging.UnaryServerInterceptor(interceptorLogger(rpcLogger), logging.WithFieldsFromContext(logTraceID)),
150+
// selector.UnaryServerInterceptor(auth.UnaryServerInterceptor(authFn), selector.MatchFunc(allButHealthZ)),
151+
// recovery.UnaryServerInterceptor(recovery.WithRecoveryHandler(grpcPanicRecoveryHandler)),
152+
),
153+
grpc.ChainStreamInterceptor(
154+
s.prometheusMetrics.StreamServerInterceptor(), //grpcprom.WithExemplarFromContext(exemplarFromContext)),
155+
// logging.StreamServerInterceptor(interceptorLogger(rpcLogger), logging.WithFieldsFromContext(logTraceID)),
156+
// selector.StreamServerInterceptor(auth.StreamServerInterceptor(authFn), selector.MatchFunc(allButHealthZ)),
157+
// recovery.StreamServerInterceptor(recovery.WithRecoveryHandler(grpcPanicRecoveryHandler)),
158+
),
159+
}
160+
grpcServer := grpc.NewServer(opts...)
112161
s.grpcServers[i] = grpcServer
113162

114163
versionedAPI.Registrant(grpcServer)

vendor/github.com/beorn7/perks/LICENSE

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)