Skip to content

Commit

Permalink
Merge pull request #13 from Freakin/flush-metrics-cache
Browse files Browse the repository at this point in the history
Added flush-metrics-cache argument to prevent caching of deleted creds
  • Loading branch information
psycofdj authored Feb 19, 2020
2 parents ad206af + bca1acc commit 97216de
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ uaac client add prometheus-credhub \
| `web.auth.username`<br />`CREDHUB_EXPORTER_WEB_AUTH_USERNAME` | No | | Username for web interface basic auth |
| `web.auth.password`<br />`CREDHUB_EXPORTER_WEB_AUTH_PASSWORD` | No | | Password for web interface basic auth |
| `web.tls.cert_file`<br />`CREDHUB_EXPORTER_WEB_TLS_CERTFILE` | No | | Path to a file that contains the TLS certificate (PEM format). If the certificate is signed by a certificate authority, the file should be the concatenation of the server's certificate, any intermediates, and the CA's certificate |
| `web.tls.key_file`<br />`CREDHUB_EXPORTER_WEB_TLS_KEYFILE` | No | | Path to a file that contains the TLS private key (PEM format) |
| `web.tls.key_file`<br />`CREDHUB_EXPORTER_WEB_TLS_KEYFILE` | No | | Path to a file that contains the TLS private key (PEM format)
| `flush-metrics-cache`<br />`CREDHUB_EXPORTER_FLUSH_METRICS_CACHE` | No | `false` | Flushes the collector `credentialMetrics` and `certificateExpiresMetrics` vector to prevent deleted credentials from continuing to report.


### Metrics
Expand Down
8 changes: 8 additions & 0 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type CredhubCollector struct {
certificateExpiresMetrics *prometheus.GaugeVec
scrapeErrorMetric prometheus.Gauge
lastScrapeTimestampMetric prometheus.Gauge
flushCache bool
}

// NewCredhubCollector -
Expand Down Expand Up @@ -151,6 +152,12 @@ func (c CredhubCollector) filterCertificates(name string, cred credentials.Crede

func (c CredhubCollector) Collect(ch chan<- prometheus.Metric) {
log.Debugf("collecting credhub metrics")
if c.flushCache {
log.Debugf("flushing credhub metrics cache")
c.credentialMetrics.Reset()
c.certificateExpiresMetrics.Reset()
}

c.scrapeErrorMetric.Set(0.0)
c.lastScrapeTimestampMetric.Set(float64(time.Now().Unix()))

Expand Down Expand Up @@ -199,6 +206,7 @@ func (c CredhubCollector) Collect(ch chan<- prometheus.Metric) {
}
}


c.credentialMetrics.Collect(ch)
c.certificateExpiresMetrics.Collect(ch)
c.scrapeErrorMetric.Collect(ch)
Expand Down
5 changes: 5 additions & 0 deletions credhub_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ var (
tlsKeyFile = kingpin.Flag(
"web.tls.key_file", "Path to a file that contains the TLS private key (PEM format) ($CREDHUB_EXPORTER_WEB_TLS_KEYFILE)",
).Envar("CREDHUB_EXPORTER_WEB_TLS_KEYFILE").ExistingFile()

flushCache = kingpin.Flag(
"flush-metrics-cache", "Flush metrics cache on each collection ($CREDHUB_EXPORTER_FLUSH_METRICS_CACHE)",
).Envar("CREDHUB_EXPORTER_FLUSH_METRICS_CACHE").Default("false").Bool()
)

func init() {
Expand Down Expand Up @@ -186,6 +190,7 @@ func main() {
credhubCollector := NewCredhubCollector(*metricsDeployment, *metricsEnvironment, filters, credhubCli)
credhubCollector.filterNameLike(*filterNameLike)
credhubCollector.filterPath(*filterPath)
credhubCollector.flushCache = *flushCache
prometheus.MustRegister(credhubCollector)

handler := prometheusHandler()
Expand Down

0 comments on commit 97216de

Please sign in to comment.