-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from janboll/add-metrics-cache
Implementing metrics caching
- Loading branch information
Showing
11 changed files
with
516 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package pkg | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
dto "github.com/prometheus/client_model/go" | ||
) | ||
|
||
type MetricsCache struct { | ||
cacheMutex *sync.Mutex | ||
entries map[string]cacheEntry | ||
ttl time.Duration | ||
} | ||
|
||
func NewMetricsCache(ttl time.Duration) *MetricsCache { | ||
return &MetricsCache{ | ||
cacheMutex: &sync.Mutex{}, | ||
entries: map[string]cacheEntry{}, | ||
ttl: ttl, | ||
} | ||
} | ||
|
||
func getMetricHash(metric prometheus.Metric) string { | ||
var dto dto.Metric | ||
metric.Write(&dto) | ||
labelString := metric.Desc().String() | ||
|
||
for _, labelPair := range dto.GetLabel() { | ||
labelString = fmt.Sprintf("%s,%s,%s", labelString, labelPair.GetName(), labelPair.GetValue()) | ||
} | ||
|
||
checksum := sha256.Sum256([]byte(labelString)) | ||
return fmt.Sprintf("%x", checksum[:]) | ||
} | ||
|
||
// AddMetric adds a metric to the cache | ||
func (mc *MetricsCache) AddMetric(metric prometheus.Metric) { | ||
mc.cacheMutex.Lock() | ||
mc.entries[getMetricHash(metric)] = cacheEntry{ | ||
creation: time.Now(), | ||
metric: metric, | ||
} | ||
mc.cacheMutex.Unlock() | ||
} | ||
|
||
// GetAllMetrics Iterates over all cached metrics and discards expired ones. | ||
func (mc *MetricsCache) GetAllMetrics() []prometheus.Metric { | ||
mc.cacheMutex.Lock() | ||
returnArr := make([]prometheus.Metric, 0) | ||
for k, v := range mc.entries { | ||
if time.Since(v.creation).Seconds() > mc.ttl.Seconds() { | ||
delete(mc.entries, k) | ||
} else { | ||
returnArr = append(returnArr, v.metric) | ||
} | ||
} | ||
mc.cacheMutex.Unlock() | ||
return returnArr | ||
} | ||
|
||
type cacheEntry struct { | ||
creation time.Time | ||
metric prometheus.Metric | ||
} |
Oops, something went wrong.