From 0764f8824ed0dcad7692e1b29d16b3932dc4f8b8 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Thu, 5 Apr 2018 22:12:25 +0200 Subject: [PATCH] zfs: prometheus metrics refs #67 --- zfs/prometheus.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++ zfs/versions.go | 11 ++++++++++ zfs/zfs.go | 7 +++++++ 3 files changed, 71 insertions(+) create mode 100644 zfs/prometheus.go diff --git a/zfs/prometheus.go b/zfs/prometheus.go new file mode 100644 index 0000000..f27d797 --- /dev/null +++ b/zfs/prometheus.go @@ -0,0 +1,53 @@ +package zfs + +import "github.com/prometheus/client_golang/prometheus" + +var prom struct { + ZFSListFilesystemVersionDuration *prometheus.HistogramVec + ZFSDestroyFilesystemVersionDuration *prometheus.HistogramVec + ZFSSnapshotDuration *prometheus.HistogramVec + ZFSBookmarkDuration *prometheus.HistogramVec +} + +func init() { + prom.ZFSListFilesystemVersionDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Namespace: "zrepl", + Subsystem: "zfs", + Name: "list_filesystem_versions_duration", + Help: "Seconds it took for listing the versions of a given filesystem", + }, []string{"filesystem"}) + prom.ZFSDestroyFilesystemVersionDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Namespace: "zrepl", + Subsystem: "zfs", + Name: "destroy_filesystem_version_duration", + Help: "Seconds it took to destroy a version of a given filesystem", + }, []string{"filesystem", "version_type"}) + prom.ZFSSnapshotDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Namespace: "zrepl", + Subsystem: "zfs", + Name: "snapshot_duration", + Help: "Seconds it took to create a snapshot a given filesystem", + }, []string{"filesystem"}) + prom.ZFSBookmarkDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Namespace: "zrepl", + Subsystem: "zfs", + Name: "bookmark_duration", + Help: "Duration it took to bookmark a given snapshot", + }, []string{"filesystem"}) +} + +func PrometheusRegister(registry prometheus.Registerer) error { + if err := registry.Register(prom.ZFSListFilesystemVersionDuration); err != nil { + return err + } + if err := registry.Register(prom.ZFSDestroyFilesystemVersionDuration); err != nil { + return err + } + if err := registry.Register(prom.ZFSBookmarkDuration); err != nil { + return err + } + if err := registry.Register(prom.ZFSSnapshotDuration); err != nil { + return err + } + return nil +} diff --git a/zfs/versions.go b/zfs/versions.go index 643c74a..1ef9e20 100644 --- a/zfs/versions.go +++ b/zfs/versions.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + "github.com/prometheus/client_golang/prometheus" "strconv" "strings" "time" @@ -28,6 +29,10 @@ func (t VersionType) DelimiterChar() string { } } +func (t VersionType) String() string { + return string(t) +} + type FilesystemVersion struct { Type VersionType @@ -64,6 +69,9 @@ type FilesystemVersionFilter interface { func ZFSListFilesystemVersions(fs *DatasetPath, filter FilesystemVersionFilter) (res []FilesystemVersion, err error) { listResults := make(chan ZFSListResult) + promTimer := prometheus.NewTimer(prom.ZFSListFilesystemVersionDuration.WithLabelValues(fs.ToString())) + defer promTimer.ObserveDuration() + ctx, cancel := context.WithCancel(context.Background()) defer cancel() go ZFSListChan(ctx, listResults, @@ -137,6 +145,9 @@ func ZFSListFilesystemVersions(fs *DatasetPath, filter FilesystemVersionFilter) func ZFSDestroyFilesystemVersion(filesystem *DatasetPath, version FilesystemVersion) (err error) { + promTimer := prometheus.NewTimer(prom.ZFSDestroyFilesystemVersionDuration.WithLabelValues(filesystem.ToString(), version.Type.String())) + defer promTimer.ObserveDuration() + datasetPath := version.ToAbsPath(filesystem) // Sanity check... diff --git a/zfs/zfs.go b/zfs/zfs.go index f6a3b2b..29da261 100644 --- a/zfs/zfs.go +++ b/zfs/zfs.go @@ -12,6 +12,7 @@ import ( "context" "github.com/problame/go-rwccmd" + "github.com/prometheus/client_golang/prometheus" "github.com/zrepl/zrepl/util" ) @@ -386,6 +387,9 @@ func zfsBuildBookmarkName(fs *DatasetPath, name string) string { // TODO defensi func ZFSSnapshot(fs *DatasetPath, name string, recursive bool) (err error) { + promTimer := prometheus.NewTimer(prom.ZFSSnapshotDuration.WithLabelValues(fs.ToString())) + defer promTimer.ObserveDuration() + snapname := zfsBuildSnapName(fs, name) cmd := exec.Command(ZFS_BINARY, "snapshot", snapname) @@ -409,6 +413,9 @@ func ZFSSnapshot(fs *DatasetPath, name string, recursive bool) (err error) { func ZFSBookmark(fs *DatasetPath, snapshot, bookmark string) (err error) { + promTimer := prometheus.NewTimer(prom.ZFSBookmarkDuration.WithLabelValues(fs.ToString())) + defer promTimer.ObserveDuration() + snapname := zfsBuildSnapName(fs, snapshot) bookmarkname := zfsBuildBookmarkName(fs, bookmark)