Skip to content

Commit

Permalink
implement rancher api resource limit config
Browse files Browse the repository at this point in the history
  • Loading branch information
sookloeckner committed Dec 14, 2018
1 parent 8162d7e commit e9411ee
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
30 changes: 16 additions & 14 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ import (

// Exporter Sets up all the runtime and metrics
type Exporter struct {
labelsFilter *regexp.Regexp
rancherURL string
accessKey string
secretKey string
hideSys bool
mutex sync.RWMutex
gaugeVecs map[string]*prometheus.GaugeVec
labelsFilter *regexp.Regexp
rancherURL string
accessKey string
secretKey string
hideSys bool
resourceLimit string
mutex sync.RWMutex
gaugeVecs map[string]*prometheus.GaugeVec
}

// NewExporter creates the metrics we wish to monitor
func newExporter(rancherURL, accessKey, secretKey string, labelsFilter *regexp.Regexp, hideSys bool) *Exporter {
func newExporter(rancherURL, accessKey, secretKey string, labelsFilter *regexp.Regexp, hideSys bool, resourceLimit string) *Exporter {
gaugeVecs := addMetrics()
return &Exporter{
labelsFilter: labelsFilter,
gaugeVecs: gaugeVecs,
rancherURL: rancherURL,
accessKey: accessKey,
secretKey: secretKey,
hideSys: hideSys,
labelsFilter: labelsFilter,
gaugeVecs: gaugeVecs,
rancherURL: rancherURL,
accessKey: accessKey,
secretKey: secretKey,
hideSys: hideSys,
resourceLimit: resourceLimit,
}
}
8 changes: 4 additions & 4 deletions gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func (e *Exporter) processMetrics(data *Data, endpoint string, hideSys bool, ch
}

// gatherData - Collects the data from thw API, invokes functions to transform that data into metrics
func (e *Exporter) gatherData(rancherURL string, accessKey string, secretKey string, endpoint string, ch chan<- prometheus.Metric) (*Data, error) {
func (e *Exporter) gatherData(rancherURL string, resourceLimit string, accessKey string, secretKey string, endpoint string, ch chan<- prometheus.Metric) (*Data, error) {
// Return the correct URL path
url := setEndpoint(rancherURL, endpoint)
url := setEndpoint(rancherURL, endpoint, resourceLimit)

// Create new data slice from Struct
var data = new(Data)
Expand Down Expand Up @@ -174,10 +174,10 @@ func getJSON(url string, accessKey string, secretKey string, target interface{})
}

// setEndpoint - Determines the correct URL endpoint to use, gives us backwards compatibility
func setEndpoint(rancherURL string, component string) string {
func setEndpoint(rancherURL string, component string, resourceLimit string) string {
var endpoint string

endpoint = (rancherURL + "/" + component + "/")
endpoint = (rancherURL + "/" + component + "/" + "?limit=" + resourceLimit)
endpoint = strings.Replace(endpoint, "v1", "v2-beta", 1)

return endpoint
Expand Down
2 changes: 1 addition & 1 deletion prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
// Range over the pre-configured endpoints array
for _, p := range endpoints {

var data, err = e.gatherData(e.rancherURL, e.accessKey, e.secretKey, p, ch)
var data, err = e.gatherData(e.rancherURL, e.resourceLimit, e.accessKey, e.secretKey, p, ch)

if err != nil {
log.Error("Error getting JSON from URL ", p)
Expand Down
3 changes: 2 additions & 1 deletion rancher_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
secretKey = os.Getenv("CATTLE_SECRET_KEY") // Optional - Secret Key for Rancher API
labelsFilter = os.Getenv("LABELS_FILTER") // Optional - Filter for Rancher label names
logLevel = getEnv("LOG_LEVEL", "info") // Optional - Set the logging level
resourceLimit = getEnv("API_LIMIT", "100") // Optional - Rancher API resource limit (default: 100)
hideSys, _ = strconv.ParseBool(getEnv("HIDE_SYS", "true")) // hideSys - Optional - Flag that indicates if the environment variable `HIDE_SYS` is set to a boolean true value
)

Expand Down Expand Up @@ -88,7 +89,7 @@ func main() {
measure.Init()

// Register a new Exporter
exporter := newExporter(rancherURL, accessKey, secretKey, labelsFilterRegexp, hideSys)
exporter := newExporter(rancherURL, accessKey, secretKey, labelsFilterRegexp, hideSys, resourceLimit)

// Register Metrics from each of the endpoints
// This invokes the Collect method through the prometheus client libraries.
Expand Down

0 comments on commit e9411ee

Please sign in to comment.