Skip to content

Commit

Permalink
continue running when querying the jenkins api fails
Browse files Browse the repository at this point in the history
Instead of terminating when the Jenkins Server can not be reachted, the jenkins
exporter continues to run.
This prevents that the jenkins-exporter terminates when e.g. the jenkins-server
is restarted.

A new Prometheus metric jenkins_exporter_errors is added, that counts the number
of encountered errors when trying to query the jenkins api.
  • Loading branch information
fho committed Jun 18, 2019
1 parent 15ad10b commit 04b46d9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ It provides the following Prometheus metrics:
- building_duration
- executing_time
- waiting_time

By default metrics are recorded for every finished Jenkins build.
The jobs for that builds are recorded can be limited with the
- Counter Metric: `jenkins_exporter_errors`
Counts the number of errors the jenkins-exporter encountered when fetching
informations from the Jenkins API.
- type:
- jenkins_api

By default metrics are recorded for every finished Jenkins build. The Jenkins
jobs for that builds are recorded can be limited with the
`--jenkins-job-whitelist` command-line parameter.
The duration types that are recorded can also be configured via a commandline
parameter.
Expand Down
23 changes: 23 additions & 0 deletions internal/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Collector struct {
namespace string
summaries map[string]*prometheus.SummaryVec
histograms map[string]*prometheus.HistogramVec
counters map[string]*prometheus.CounterVec
constLabels prometheus.Labels
}

Expand All @@ -20,10 +21,32 @@ func NewCollector(namespace string, constLabels map[string]string) *Collector {
namespace: namespace,
summaries: map[string]*prometheus.SummaryVec{},
histograms: map[string]*prometheus.HistogramVec{},
counters: map[string]*prometheus.CounterVec{},
constLabels: constLabels,
}
}

func (c *Collector) CounterAdd(key string, cnt float64, help string, labels map[string]string) {
s, exist := c.counters[key]
if !exist {
s = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: c.namespace,
Name: sanitize(key),
ConstLabels: c.constLabels,
Help: help,
},
labelNames(labels),
)

prometheus.MustRegister(s)

c.counters[key] = s
}

s.With(labels).Add(cnt)
}

func (c *Collector) Summary(key string, val float64, help string, labels map[string]string) {
s, exist := c.summaries[key]
if !exist {
Expand Down
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

const (
appName = "jenkins-exporter"
version = "0.1"
version = "0.2"
)

const (
Expand Down Expand Up @@ -297,6 +297,7 @@ func validateFlags() {
func main() {
envy.Parse("JE")
flag.Parse()

if *printVersion {
fmt.Printf("%s\n", version)
os.Exit(0)
Expand Down Expand Up @@ -332,13 +333,14 @@ func main() {
WithLogger(debugLogger).
WithTimeout(timeout)

// create the counter with a 0 counter
collector.CounterAdd("errors", 0, "jenkins api fetch errors", map[string]string{"type": "jenkins_api"})

for {
err := fetchAndRecord(clt, stateStore, collector)
if err != nil {
log.Printf("fetching and recording builds metrics failed: %s", err)
if _, ok := err.(*jenkins.ErrHTTPRequestFailed); !ok {
os.Exit(1)
}
collector.CounterAdd("errors", 1, "jenkins api fetch errors", map[string]string{"type": "jenkins_api"})
}

logger.Printf("fetching and recording the next build metrics in %s", pollInterval)
Expand Down

0 comments on commit 04b46d9

Please sign in to comment.