Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jenkins_exporter_errors{type=http_requests_total} + jenkins_http_requests_total metric mix up #28

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,16 @@ It provides the following Prometheus metrics:
- Counter:
- `jenkins_exporter_errors`
Counts the number of errors the jenkins-exporter encountered when fetching
informations from the Jenkins API.
information from the Jenkins API.
- Labels:
- type:
- jenkins_api
- jenkins_wfapi
- `jenkins_http_requests_total`
Counts the number of http-requests sent successfully to Jenkins.

By default job 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
The duration types that are recorded can also be configured via a command-line
parameter.

The exporter can also record durations of individual pipeline stages. This
Expand Down Expand Up @@ -82,7 +80,7 @@ make

Then copy the jenkins-exporter into your `$PATH`.

### As systemd Service ###
### As Systemd Service ###

1. Install `jenkins-exporter` to `/usr/local/bin`
2. `cp dist/etc/default/jenkins-exporter /etc/default/`
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/buildstagemapflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "strings"
// "<JOB-NAME>:<STAGE-NAME>,..."
// into a map[JOB-NAME]map[STAGE-NAME]struct{} datastructure.
// If an elem ends with a "," it is interpreted as being on the Job-Name.
// Later elemens overwrite earlier elements.
// Later element overwrite earlier elements.
type BuildStageMapFlag map[string]map[string]struct{}

func (b BuildStageMapFlag) Set(v string) error {
Expand Down
24 changes: 6 additions & 18 deletions internal/jenkins/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus"
"golang.org/x/time/rate"
)

Expand All @@ -23,12 +22,11 @@ type auth struct {
}

type Client struct {
client http.Client
auth *auth
serverURL string
logger *log.Logger
errorMetricCounter *prometheus.CounterVec
limiter *rate.Limiter
client http.Client
auth *auth
serverURL string
logger *log.Logger
limiter *rate.Limiter
}

func (c *Client) WithAuth(username, password string) *Client {
Expand All @@ -49,12 +47,6 @@ func (c *Client) WithLogger(l *log.Logger) *Client {
return c
}

func (c *Client) WithErrorMetrics(counter *prometheus.CounterVec) *Client {
c.errorMetricCounter = counter

return c
}

// WithRatelimit limits the number of http-request that are sent out per
// interval. An interval of 0 means unlimited.
func (c *Client) WithRatelimit(interval time.Duration) *Client {
Expand Down Expand Up @@ -103,15 +95,11 @@ func (c *Client) do(method, url string, result interface{}) error {
if err != nil {
return err
}
if c.errorMetricCounter != nil {
c.errorMetricCounter.With(prometheus.Labels{"type": "http_requests_total"}).Inc()
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
// body has always be read until EOF and closed
_, _ = io.ReadAll(resp.Body)
_, _ = io.Copy(io.Discard, resp.Body)
return &ErrHTTPRequestFailed{Code: resp.StatusCode}
}

Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
appName = "jenkins-exporter"
)

// Version is set during compiliation via -ldflags.
// Version is set during compilation via -ldflags.
var Version = "unknown"

const stateStoreCleanupInterval = 10 * 60 * time.Second
Expand Down Expand Up @@ -515,7 +515,6 @@ func main() {
WithAuth(*jenkinsUsername, *jenkinsPassword).
WithLogger(debugLogger).
WithTimeout(*httpTimeout).
WithErrorMetrics(metrics.Errors).
WithRatelimit(*httpRequestRateEvery)

nextStateStoreCleanup := time.Now()
Expand Down
Loading