Skip to content

Commit

Permalink
Introduced cached and interval for retrieval of data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Whyeasy committed Sep 18, 2020
1 parent b9121bc commit 2f5e085
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Change listening port of the exporter; `--listenAddress <string>` or as env vari

Change listening path of the exporter; `--listenPath <string>` or as env variable `LISTEN_PATH`. Default = `/metrics`

Change the interval of retrieving data in the background; `--interval <string>` or as env variable `INTERVAL`. Default is `60`

## Helm

You can find a helm chart to install the exporter [here](https://github.com/Whyeasy/helm-charts/tree/master/charts/gitlab-extra-exporter).
8 changes: 7 additions & 1 deletion cmd/gitlab-extra-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func init() {
flag.StringVar(&config.ListenPath, "listenPath", os.Getenv("LISTEN_PATH"), "Path where metrics will be exposed")
flag.StringVar(&config.GitlabURI, "gitlabURI", os.Getenv("GITLAB_URI"), "URI to Gitlab instance to monitor")
flag.StringVar(&config.GitlabAPIKey, "gitlabAPIKey", os.Getenv("GITLAB_API_KEY"), "API Key to access the Gitlab instance")
flag.StringVar(&config.Interval, "interval", os.Getenv("INTERVAL"), "Provide a interval on what rate the Jira Service Desk API should be scraped.")
}

func main() {
Expand Down Expand Up @@ -80,7 +81,12 @@ func parseConfig() error {
log.Error(err)
}
}

if f.Name == "interval" && (f.Value.String() == "" || f.Value.String() == "0") {
err = f.Value.Set("60")
if err != nil {
log.Error(err)
}
}
})
return err
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/hashicorp/go-retryablehttp v0.6.7 // indirect
github.com/prometheus/client_golang v1.7.1
github.com/sirupsen/logrus v1.6.0
github.com/xanzy/go-gitlab v0.37.0
github.com/xanzy/go-gitlab v0.38.1
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/xanzy/go-gitlab v0.37.0 h1:Z/CQkjj5VwbWVYVL7S70kS/TFj5H/pJumV7xbJ0YUQ8=
github.com/xanzy/go-gitlab v0.37.0/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfDUseX11Ug=
github.com/xanzy/go-gitlab v0.38.1 h1:st5/Ag4h8CqVfp3LpOWW0Jd4jYHTGETwu0KksYDPnYE=
github.com/xanzy/go-gitlab v0.38.1/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfDUseX11Ug=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down
1 change: 1 addition & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type Config struct {
ListenPath string
GitlabURI string
GitlabAPIKey string
Interval string
}
78 changes: 69 additions & 9 deletions lib/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package client

import (
"net/http"
"strconv"
"time"

log "github.com/sirupsen/logrus"
"github.com/whyeasy/gitlab-extra-exporter/internal"
gitlab "github.com/xanzy/go-gitlab"
)
Expand All @@ -25,57 +27,115 @@ type ExporterClient struct {
gitlabURI string
gitlabAPIKey string
httpClient *http.Client
interval time.Duration
}

//New returns a new Client connection to Gitlab.
func New(c internal.Config) *ExporterClient {
return &ExporterClient{

convertedTime, _ := strconv.ParseInt(c.Interval, 10, 64)

exporter := &ExporterClient{
gitlabAPIKey: c.GitlabAPIKey,
gitlabURI: c.GitlabURI,
httpClient: &http.Client{Timeout: 10 * time.Second},
interval: time.Duration(convertedTime),
}

exporter.startFetchData()

return exporter
}

// CachedStats is to store scraped data for caching purposes.
var CachedStats *Stats = &Stats{
Projects: &[]ProjectStats{},
MergeRequests: &[]MergeRequestStats{},
MergeRequestsOpen: &[]MergeRequestStats{},
MergeRequestsClosed: &[]MergeClosedStats{},
MergeRequestsMerged: &[]MergeMergedStats{},
Approvals: &[]ApprovalStats{},
Changes: &[]ChangeStats{},
}

//GetStats retrieves data from API to create metrics from.
func (c *ExporterClient) GetStats() (*Stats, error) {

return CachedStats, nil
}

func (c *ExporterClient) getData() error {

glc, err := gitlab.NewClient(c.gitlabAPIKey, gitlab.WithBaseURL(c.gitlabURI), gitlab.WithHTTPClient(c.httpClient))
if err != nil {
return nil, err
return err
}

projects, err := getProjects(glc)
if err != nil {
return nil, err
return err
}

mrs, err := getMergeRequest(glc)
if err != nil {
return nil, err
return err
}

mrOpen, mrMerged, mrClosed, err := getMergeRequestsDetails(glc, *mrs)
if err != nil {
return nil, err
return err
}

approvals, err := getApprovals(glc, *mrOpen)
if err != nil {
return nil, err
return err
}

changes, err := getChanges(glc, *mrOpen)
if err != nil {
return nil, err
return err
}

return &Stats{
CachedStats = &Stats{
Projects: projects,
MergeRequests: mrs,
MergeRequestsOpen: mrOpen,
MergeRequestsClosed: mrClosed,
MergeRequestsMerged: mrMerged,
Approvals: approvals,
Changes: changes,
}, nil
}

log.Info("New data retrieved.")

return nil
}

func (c *ExporterClient) startFetchData() {

// Do initial call to have data from the start.
go func() {
err := c.getData()
if err != nil {
log.Error("Scraping failed.")
}
}()

ticker := time.NewTicker(c.interval * time.Second)
quit := make(chan struct{})

go func() {
for {
select {
case <-ticker.C:
err := c.getData()
if err != nil {
log.Error("Scraping failed.")
}
case <-quit:
ticker.Stop()
return
}
}
}()
}
1 change: 1 addition & 0 deletions lib/client/mergeRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ApprovalStats struct {
ProjectID string
}

//ChangeStats is the struct for the total amount of changes within a MR.
type ChangeStats struct {
ProjectID string
ID string
Expand Down

0 comments on commit 2f5e085

Please sign in to comment.