This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(exporter): dothill_csi_rpc_call metric
- Loading branch information
1 parent
d9e37b6
commit 32ae764
Showing
6 changed files
with
455 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package exporter | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
type Collector struct { | ||
csiRPCCallCounters map[string]prometheus.Counter | ||
} | ||
|
||
var ( | ||
csiRPCCallMetric = "dothill_csi_rpc_call" | ||
csiRPCCallHelp = "How many CSI RPC calls have been executed" | ||
) | ||
|
||
func NewCollector() *Collector { | ||
return &Collector{ | ||
csiRPCCallCounters: map[string]prometheus.Counter{}, | ||
} | ||
} | ||
|
||
func (collector *Collector) Describe(ch chan<- *prometheus.Desc) { | ||
for _, counter := range collector.csiRPCCallCounters { | ||
ch <- counter.Desc() | ||
} | ||
} | ||
|
||
func (collector *Collector) Collect(ch chan<- prometheus.Metric) { | ||
for _, counter := range collector.csiRPCCallCounters { | ||
ch <- counter | ||
} | ||
} | ||
|
||
func (collector *Collector) getCSIRPCCallCounter(method string, success bool) prometheus.Counter { | ||
ID := method + ":" + fmt.Sprintf("%t", success) | ||
|
||
if counter, ok := collector.csiRPCCallCounters[ID]; ok { | ||
return counter | ||
} | ||
|
||
counter := prometheus.NewCounter(prometheus.CounterOpts{ | ||
Name: csiRPCCallMetric, | ||
Help: csiRPCCallHelp, | ||
ConstLabels: prometheus.Labels{ | ||
"method": method, | ||
"success": fmt.Sprintf("%t", success), | ||
}, | ||
}) | ||
collector.csiRPCCallCounters[ID] = counter | ||
|
||
return counter | ||
} | ||
|
||
func (collector *Collector) IncCSIRPCCall(method string, success bool) { | ||
collector.getCSIRPCCallCounter(method, success).Inc() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package exporter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"k8s.io/klog" | ||
) | ||
|
||
// Exporter : Configuration (from command-line) | ||
type Exporter struct { | ||
Port int | ||
Collector *Collector | ||
|
||
listener net.Listener | ||
server *http.Server | ||
} | ||
|
||
func New(port int) *Exporter { | ||
return &Exporter{ | ||
Port: port, | ||
Collector: NewCollector(), | ||
} | ||
} | ||
|
||
// ListenAndServe : Convenience function to start exporter | ||
func (exporter *Exporter) ListenAndServe() error { | ||
if err := exporter.Listen(); err != nil { | ||
return err | ||
} | ||
|
||
return exporter.Serve() | ||
} | ||
|
||
// Listen : Listen for requests | ||
func (exporter *Exporter) Listen() error { | ||
err := prometheus.Register(exporter.Collector) | ||
if err != nil { | ||
if registered, ok := err.(prometheus.AlreadyRegisteredError); ok { | ||
prometheus.Unregister(registered.ExistingCollector) | ||
prometheus.MustRegister(exporter.Collector) | ||
} | ||
} | ||
|
||
listen := fmt.Sprintf(":%d", exporter.Port) | ||
klog.Infof("listening on %s", listen) | ||
|
||
listener, err := net.Listen("tcp", listen) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
exporter.listener = listener | ||
return nil | ||
} | ||
|
||
// Serve : Actually reply to requests | ||
func (exporter *Exporter) Serve() error { | ||
mux := http.NewServeMux() | ||
mux.Handle("/metrics", promhttp.Handler()) | ||
|
||
exporter.server = &http.Server{ | ||
Handler: mux, | ||
} | ||
|
||
return exporter.server.Serve(exporter.listener) | ||
} | ||
|
||
// Shutdown : Properly tear down server | ||
func (exporter *Exporter) Shutdown() error { | ||
return exporter.server.Shutdown(context.Background()) | ||
} |