-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from sjtug/prometheus
Prometheus Integration
- Loading branch information
Showing
11 changed files
with
1,598 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Package exporter provides definition of exporter | ||
package exporter | ||
|
||
import ( | ||
log "github.com/Sirupsen/logrus" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/sjtug/lug/helper" | ||
"net/http" | ||
) | ||
|
||
// Exporter exports lug metrics to Prometheus | ||
type Exporter struct { | ||
successCounter *prometheus.CounterVec | ||
failCounter *prometheus.CounterVec | ||
diskUsage *prometheus.GaugeVec | ||
} | ||
|
||
var instance *Exporter | ||
|
||
// newExporter creates a new exporter | ||
func newExporter() *Exporter { | ||
newExporter := Exporter{ | ||
successCounter: prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "success_sync", | ||
Help: "How many successful synchronizations processed, partitioned by workers.", | ||
}, | ||
[]string{"worker"}, | ||
), | ||
failCounter: prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "fail_sync", | ||
Help: "How many failed synchronizations processed, partitioned by workers.", | ||
}, | ||
[]string{"worker"}, | ||
), | ||
diskUsage: prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Namespace: "lug", | ||
Subsystem: "storage", | ||
Name: "disk_usage", | ||
Help: "Disk usage in bytes, partitioned by workers.", | ||
}, | ||
[]string{"worker"}, | ||
), | ||
} | ||
prometheus.MustRegister(newExporter.successCounter) | ||
prometheus.MustRegister(newExporter.failCounter) | ||
prometheus.MustRegister(newExporter.diskUsage) | ||
log.Info("Exporter initialized") | ||
return &newExporter | ||
} | ||
|
||
// GetInstance gets the exporter | ||
func GetInstance() *Exporter { | ||
if instance == nil { | ||
instance = newExporter() | ||
} | ||
return instance | ||
} | ||
|
||
// Expose the registered metrics via HTTP. | ||
func Expose(addr string) { | ||
GetInstance() // ensure init | ||
http.Handle("/metrics", promhttp.Handler()) | ||
log.Info("Metrics exposed at " + addr + "/metrics") | ||
log.Fatal(http.ListenAndServe(addr, nil)) | ||
} | ||
|
||
// SyncSuccess will report a successful synchronization | ||
func (e *Exporter) SyncSuccess(worker string) { | ||
e.successCounter.With(prometheus.Labels{"worker": worker}).Inc() | ||
} | ||
|
||
// SyncFail will report a failed synchronization | ||
func (e *Exporter) SyncFail(worker string) { | ||
e.failCounter.With(prometheus.Labels{"worker": worker}).Inc() | ||
} | ||
|
||
// UpdateDiskUsage will update the disk usage of a directory | ||
func (e *Exporter) UpdateDiskUsage(worker string, path string) { | ||
size, err := helper.DiskUsage(path) | ||
if err == nil { | ||
e.diskUsage.With(prometheus.Labels{"worker": worker}).Set(float64(size)) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,35 @@ | ||
package helper | ||
|
||
import ( | ||
"os" | ||
"path" | ||
) | ||
|
||
// DiskUsage counts the disk usage of a directory | ||
func DiskUsage(curPath string) (int64, error) { | ||
var size int64 | ||
|
||
dir, err := os.Open(curPath) | ||
if err != nil { | ||
return size, err | ||
} | ||
defer dir.Close() | ||
|
||
files, err := dir.Readdir(-1) | ||
if err != nil { | ||
return size, err | ||
} | ||
|
||
for _, file := range files { | ||
if file.IsDir() { | ||
s, err := DiskUsage(path.Join(curPath, file.Name())) | ||
if err != nil { | ||
return size, err | ||
} | ||
size += s | ||
} else { | ||
size += file.Size() | ||
} | ||
} | ||
return size, nil | ||
} |
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
Oops, something went wrong.