This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement prometheus metrics for tracing path.
Signed-off-by: Harkishen-Singh <harkishensingh@hotmail.com> This commit implements Prometheus metrics for tracing module that includes: 1. Ingest 2. Query 3. Cache This commit also puts all shared metrics into pgmodel/metrics so that ingestor and querier can easily access them and utilize their labels for 'type'.
- Loading branch information
1 parent
8cd208b
commit 53cc24e
Showing
6 changed files
with
281 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// This file and its contents are licensed under the Apache License 2.0. | ||
// Please see the included NOTICE for copyright information and | ||
// LICENSE for a copy of the license. | ||
|
||
package cache | ||
|
||
import ( | ||
"fmt" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/timescale/promscale/pkg/util" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
var ( | ||
Enabled = prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Namespace: util.PromNamespace, | ||
Subsystem: "cache", | ||
Name: "enabled", | ||
Help: "Cache is enalbed or not.", | ||
}, | ||
[]string{"subsystem", "name"}, // type => ["trace" or "metric"] and name => name of the cache i.e., metric cache, series cache, etc. | ||
) | ||
capacity = prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Namespace: util.PromNamespace, | ||
Subsystem: "cache", | ||
Name: "capacity", | ||
Help: "Cache is enabled or not.", | ||
}, | ||
[]string{"subsystem", "name"}, | ||
) | ||
sizeBytes = prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Namespace: util.PromNamespace, | ||
Subsystem: "cache", | ||
Name: "size_bytes", | ||
Help: "Cache size in bytes.", | ||
}, | ||
[]string{"subsystem", "name"}, | ||
) | ||
evictionsTotal = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Namespace: util.PromNamespace, | ||
Subsystem: "cache", | ||
Name: "evictions_total", | ||
Help: "Total evictions in a clockcache.", | ||
}, | ||
[]string{"subsystem", "name"}, | ||
) | ||
) | ||
|
||
func init() { | ||
prometheus.MustRegister( | ||
Enabled, | ||
capacity, | ||
sizeBytes, | ||
evictionsTotal, | ||
) | ||
funcs.Store([]updateFunc{}) | ||
go metricsUpdater() | ||
} | ||
|
||
const ( | ||
Cap = iota | ||
Size | ||
Evict | ||
) | ||
|
||
type MetricKind uint8 | ||
|
||
type updateFunc struct { | ||
typ MetricKind | ||
update func(metric prometheus.Collector) | ||
} | ||
|
||
var funcs atomic.Value | ||
|
||
// RegisterUpdateFunc updates some metrics like SizeBytes and Capacity every 30 secs. | ||
// Earlier these were done via supplying a func to NewGaugeFunc that called that func | ||
// when prometheus scraped. But now we have labels, and we have to use NewGaugeVec | ||
// which does not allow to implement a func. Hence, we have to choose the routine way | ||
// in order to update these metrics. | ||
func RegisterUpdateFunc(kind MetricKind, update func(metric prometheus.Collector)) { | ||
l := funcs.Load().([]updateFunc) | ||
switch kind { | ||
case Cap: | ||
l = append(l, updateFunc{kind, update}) | ||
case Size: | ||
l = append(l, updateFunc{kind, update}) | ||
case Evict: | ||
l = append(l, updateFunc{kind, update}) | ||
default: | ||
panic(fmt.Sprintf("invalid kind %d", kind)) | ||
} | ||
funcs.Store(l) | ||
} | ||
|
||
func metricsUpdater() { | ||
update := time.NewTicker(time.Second * 10) | ||
defer update.Stop() | ||
for range update.C { | ||
if len(funcs.Load().([]updateFunc)) == 0 { | ||
continue | ||
} | ||
needUpdate := funcs.Load().([]updateFunc) | ||
for _, f := range needUpdate { | ||
switch f.typ { | ||
case Cap: | ||
f.update(capacity) | ||
case Size: | ||
f.update(sizeBytes) | ||
case Evict: | ||
f.update(evictionsTotal) | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.