Skip to content

Commit 400656f

Browse files
committed
feat: export prometheus metrics
Signed-off-by: ZeroMagic <[email protected]>
1 parent 70c1077 commit 400656f

File tree

7 files changed

+1425
-4
lines changed

7 files changed

+1425
-4
lines changed

Gopkg.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,7 @@
105105
[[override]]
106106
name = "github.com/kubernetes-csi/external-snapshotter"
107107
version = "=v1.0.1"
108+
109+
[[override]]
110+
name = "github.com/prometheus/client_golang"
111+
version = "=v1.0.0"

pkg/blobfuseplugin/main.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"context"
2021
"flag"
2122
"fmt"
23+
"net"
24+
"net/http"
2225
"os"
26+
"strings"
2327

2428
"github.com/csi-driver/blobfuse-csi-driver/pkg/blobfuse"
29+
"github.com/prometheus/client_golang/prometheus/promhttp"
2530
"k8s.io/klog"
2631
)
2732

@@ -30,9 +35,10 @@ func init() {
3035
}
3136

3237
var (
33-
endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI endpoint")
34-
nodeID = flag.String("nodeid", "", "node id")
35-
version = flag.Bool("version", false, "Print the version and exit.")
38+
endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI endpoint")
39+
nodeID = flag.String("nodeid", "", "node id")
40+
version = flag.Bool("version", false, "Print the version and exit.")
41+
metricsAddress = flag.String("metrics-address", "0.0.0.0:10252", "export the metrics")
3642
)
3743

3844
func main() {
@@ -51,6 +57,7 @@ func main() {
5157
os.Exit(1)
5258
}
5359

60+
exportMetrics()
5461
handle()
5562
os.Exit(0)
5663
}
@@ -62,3 +69,39 @@ func handle() {
6269
}
6370
driver.Run(*endpoint)
6471
}
72+
73+
func exportMetrics() {
74+
l, err := net.Listen("tcp", *metricsAddress)
75+
if err != nil {
76+
klog.Warningf("failed to get listener for metrics endpoint: %v", err)
77+
return
78+
}
79+
serve(context.Background(), l, serveMetrics)
80+
}
81+
82+
func serve(ctx context.Context, l net.Listener, serveFunc func(net.Listener) error) {
83+
path := l.Addr().String()
84+
klog.V(2).Infof("set up prometheus server on %v", path)
85+
go func() {
86+
defer l.Close()
87+
if err := serveFunc(l); err != nil {
88+
klog.Fatalf("serve failure(%v), address(%v)", err, path)
89+
}
90+
}()
91+
}
92+
93+
func serveMetrics(l net.Listener) error {
94+
m := http.NewServeMux()
95+
m.Handle("/metrics", promhttp.Handler())
96+
return trapClosedConnErr(http.Serve(l, m))
97+
}
98+
99+
func trapClosedConnErr(err error) error {
100+
if err == nil {
101+
return nil
102+
}
103+
if strings.Contains(err.Error(), "use of closed network connection") {
104+
return nil
105+
}
106+
return err
107+
}

0 commit comments

Comments
 (0)