Skip to content

Commit

Permalink
exporter: allow to specify the path to ccache
Browse files Browse the repository at this point in the history
Signed-off-by: VirtualTam <[email protected]>
  • Loading branch information
virtualtam committed Oct 26, 2019
1 parent 7c6cc9d commit 40e62a1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
8 changes: 7 additions & 1 deletion cmd/ccache_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ const (

func main() {
listenAddr := flag.String("listenAddr", DefaultListenAddr, "Listen on this address")
ccacheBinaryPath := flag.String("ccacheBinaryPath", ccache.DefaultBinaryPath, "Path to the ccache binary")
flag.Parse()

collector := ccache.NewCollector()
wrapper, err := ccache.NewBinaryWrapper(*ccacheBinaryPath)
if err != nil {
log.Fatal(err)
}
collector := ccache.NewCollector(wrapper)

prometheus.MustRegister(collector)

http.Handle("/metrics", promhttp.Handler())
Expand Down
10 changes: 6 additions & 4 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package ccache

import (
"log"
"os/exec"

"github.com/prometheus/client_golang/prometheus"
)
Expand All @@ -16,6 +15,8 @@ const (
)

type collector struct {
wrapper Wrapper

call *prometheus.Desc
callHit *prometheus.Desc
cacheHitRatio *prometheus.Desc
Expand All @@ -31,8 +32,9 @@ type collector struct {

// NewCollector initializes and returns a Prometheus collector for ccache
// metrics.
func NewCollector() *collector {
func NewCollector(w Wrapper) *collector {
return &collector{
wrapper: w,
call: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "call_total"),
"Cache calls (total)",
Expand Down Expand Up @@ -120,13 +122,13 @@ func (c *collector) Describe(ch chan<- *prometheus.Desc) {

// Collect gathers metrics from ccache.
func (c *collector) Collect(ch chan<- prometheus.Metric) {
out, err := exec.Command("ccache", "-s").Output()
out, err := c.wrapper.ShowStats()
if err != nil {
log.Fatal(err)
}

stats := Statistics{}
stats.Parse(string(out[:]))
stats.Parse(string(out))

// counters
ch <- prometheus.MustNewConstMetric(
Expand Down
53 changes: 53 additions & 0 deletions wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2018 VirtualTam.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.

package ccache

import "os/exec"

const (
DefaultBinaryPath = "/usr/bin/ccache"
)

// Wrapper exposes supported ccache commands.
type Wrapper interface {
ShowStats() (string, error)
Version() (string, error)
}

// BinaryWrapper runs ccache commands locally.
type BinaryWrapper struct {
path string
}

func (w *BinaryWrapper) exec(option string) (string, error) {
out, err := exec.Command(w.path, option).Output()

if err != nil {
return "", err
}

return string(out[:]), nil
}

// ShowStats returns the result of ``ccache --show-stats''.
func (w *BinaryWrapper) ShowStats() (string, error) {
return w.exec("--show-stats")
}

// Version returns the result of ``ccache --version''.
func (w *BinaryWrapper) Version() (string, error) {
return w.exec("--version")
}

// NewBinaryWrapper ensures the ccache executable exists and can be invoked, and
// returns an initialized BinaryWrapper.
func NewBinaryWrapper(path string) (*BinaryWrapper, error) {
err := exec.Command(path, "-s").Run()
if err != nil {
return &BinaryWrapper{}, err
}

return &BinaryWrapper{path: path}, nil
}

0 comments on commit 40e62a1

Please sign in to comment.