Language fixes in README, reduce map lookups in saveLabelValues.

Signed-off-by: Ivan Mikheykin <ivan.mikheykin@flant.com>
This commit is contained in:
Ivan Mikheykin 2018-12-18 15:16:05 +03:00
parent e550f061f6
commit 331d2a56d0
2 changed files with 18 additions and 16 deletions

View file

@ -268,7 +268,7 @@ defaults:
buckets: [.005, .01, .025, .05, .1, .25, .5, 1, 2.5 ] buckets: [.005, .01, .025, .05, .1, .25, .5, 1, 2.5 ]
match_type: glob match_type: glob
glob_disable_ordering: false glob_disable_ordering: false
ttl: 0 # metrics not expired ttl: 0 # metrics do not expire
mappings: mappings:
# This will be a histogram using the buckets set in `defaults`. # This will be a histogram using the buckets set in `defaults`.
- match: test.timing.*.*.* - match: test.timing.*.*.*
@ -353,15 +353,15 @@ Possible values for `match_metric_type` are `gauge`, `counter` and `timer`.
### Time series expiration ### Time series expiration
`ttl` parameter can be used to define expiration time for stale metrics. The `ttl` parameter can be used to define the expiration time for stale metrics.
Value is a time duration with valid time units: "ns", "us" (or "µs"), The value is a time duration with valid time units: "ns", "us" (or "µs"),
"ms", "s", "m", "h". For example, `ttl: 1m20s`. `0` value is used to indicate "ms", "s", "m", "h". For example, `ttl: 1m20s`. `0` value is used to indicate
not expired metrics. metrics that do not expire.
Expiration is useful to gather metrics with changing label values from TTLs are applied to each mapped metric name/labels combination whenever
dynamic environments like Kubernetes. For example, metric new samples are received. This means that you cannot immediately expire a
`ingress_nginx_upstream_retries_count` with label `pod` and metric only by changing the mapping configuration. At least one sample must
changing pod name as a value for this label. be received for updated mappings to take effect.
## Using Docker ## Using Docker

View file

@ -149,7 +149,7 @@ func NewSummaryContainer(mapper *mapper.MetricMapper) *SummaryContainer {
} }
} }
func (c *SummaryContainer) Get(metricName string, labels prometheus.Labels, help string, mapping *mapper.MetricMapping) (prometheus.Summary, error) { func (c *SummaryContainer) Get(metricName string, labels prometheus.Labels, help string, mapping *mapper.MetricMapping) (prometheus.Observer, error) {
summaryVec, ok := c.Elements[metricName] summaryVec, ok := c.Elements[metricName]
if !ok { if !ok {
quantiles := c.mapper.Defaults.Quantiles quantiles := c.mapper.Defaults.Quantiles
@ -192,7 +192,7 @@ func NewHistogramContainer(mapper *mapper.MetricMapper) *HistogramContainer {
} }
} }
func (c *HistogramContainer) Get(metricName string, labels prometheus.Labels, help string, mapping *mapper.MetricMapping) (prometheus.Histogram, error) { func (c *HistogramContainer) Get(metricName string, labels prometheus.Labels, help string, mapping *mapper.MetricMapping) (prometheus.Observer, error) {
histogramVec, ok := c.Elements[metricName] histogramVec, ok := c.Elements[metricName]
if !ok { if !ok {
buckets := c.mapper.Defaults.Buckets buckets := c.mapper.Defaults.Buckets
@ -459,22 +459,24 @@ func (b *Exporter) removeStaleMetrics() {
// saveLabelValues stores label values set to labelValues and update lastRegisteredAt time and ttl value // saveLabelValues stores label values set to labelValues and update lastRegisteredAt time and ttl value
func (b *Exporter) saveLabelValues(metricName string, labels prometheus.Labels, ttl time.Duration) { func (b *Exporter) saveLabelValues(metricName string, labels prometheus.Labels, ttl time.Duration) {
_, hasMetric := b.labelValues[metricName] metric, hasMetric := b.labelValues[metricName]
if !hasMetric { if !hasMetric {
b.labelValues[metricName] = make(map[uint64]*LabelValues) metric = make(map[uint64]*LabelValues)
b.labelValues[metricName] = metric
} }
hash := hashNameAndLabels(metricName, labels) hash := hashNameAndLabels(metricName, labels)
_, ok := b.labelValues[metricName][hash] metricLabelValues, ok := metric[hash]
if !ok { if !ok {
b.labelValues[metricName][hash] = &LabelValues{ metricLabelValues = &LabelValues{
labels: labels, labels: labels,
ttl: ttl, ttl: ttl,
} }
b.labelValues[metricName][hash] = metricLabelValues
} }
now := time.Now() now := time.Now()
b.labelValues[metricName][hash].lastRegisteredAt = now metricLabelValues.lastRegisteredAt = now
// Update ttl from mapping // Update ttl from mapping
b.labelValues[metricName][hash].ttl = ttl metricLabelValues.ttl = ttl
} }
func NewExporter(mapper *mapper.MetricMapper) *Exporter { func NewExporter(mapper *mapper.MetricMapper) *Exporter {