Skip to content

Commit 92be9e3

Browse files
committed
Move context in the loop and add aggregated graph size.
1 parent a56490c commit 92be9e3

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

internal/metrics/metrics.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,30 @@ var StartTimeGauge = promauto.NewGauge(prometheus.GaugeOpts{
1313

1414
// ********************* GRAPH METRICS ******************
1515

16-
// GraphAssetsTotalGauge reports the number of nodes in the graph
16+
// GraphAssetsTotalGauge reports the number of nodes in the graph of a given source
1717
var GraphAssetsTotalGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
1818
Name: "go_graphkb_graph_assets_total_gauge",
19-
Help: "The number of nodes in the graph",
19+
Help: "The number of nodes in the graph for a given source",
2020
}, []string{"source"})
2121

22-
// GraphRelationsTotalGauge reports the number of nodes in the graph
22+
// GraphRelationsTotalGauge reports the number of nodes in the graph of a given source
2323
var GraphRelationsTotalGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
2424
Name: "go_graphkb_graph_relations_total_gauge",
25-
Help: "The number of edges in the graph",
25+
Help: "The number of edges in the graph for a given source",
2626
}, []string{"source"})
2727

28+
// GraphAssetsAggregatedGauge reports the number of nodes in the graph with the various sources merged
29+
var GraphAssetsAggregatedGauge = promauto.NewGauge(prometheus.GaugeOpts{
30+
Name: "go_graphkb_graph_assets_aggregated_gauge",
31+
Help: "The number of nodes in the graph with the various datasource graphs merged",
32+
})
33+
34+
// GraphRelationsAggregatedGauge reports the number of nodes in the graph with the various sources merged
35+
var GraphRelationsAggregatedGauge = promauto.NewGauge(prometheus.GaugeOpts{
36+
Name: "go_graphkb_graph_relations_aggregated_gauge",
37+
Help: "The number of edges in the graph with the various datasource graphs merged",
38+
})
39+
2840
// ********************* GRAPH UPDATE REQUESTS ******************
2941

3042
// GraphUpdateRequestsReceivedCounter reports the number of authorized and not rate limited updates requests received by the webserver

internal/server/monitoring.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,32 @@ func getMonitoringIntervalSeconds() time.Duration {
2424
func startGraphSizeMonitoring(interval time.Duration, database knowledge.GraphDB, sourcesRegistry sources.Registry) {
2525
logrus.Infof("Monitoring of the graph size will happen every %ds", int(interval/time.Second))
2626
go func() {
27-
ctx, cancel := context.WithTimeout(context.Background(), interval)
28-
defer cancel()
29-
3027
for {
28+
ctx, cancel := context.WithTimeout(context.Background(), interval)
29+
defer cancel()
30+
3131
logrus.Debug("Start monitoring the graph size...")
32+
assetsCount, err := database.CountAssets(ctx)
33+
if err != nil {
34+
metrics.GraphAssetsAggregatedGauge.Set(-1)
35+
} else {
36+
metrics.GraphAssetsAggregatedGauge.Set(float64(assetsCount))
37+
}
38+
39+
relationsCount, err := database.CountRelations(ctx)
40+
if err != nil {
41+
metrics.GraphRelationsAggregatedGauge.Set(-1)
42+
} else {
43+
metrics.GraphRelationsAggregatedGauge.Set(float64(relationsCount))
44+
}
45+
3246
sources, err := sourcesRegistry.ListSources(ctx)
3347
if err != nil {
3448
logrus.Errorf("Unable to list sources for monitoring: %v", err)
35-
continue
49+
metrics.GraphAssetsTotalGauge.Reset()
50+
metrics.GraphRelationsTotalGauge.Reset()
3651
}
52+
3753
for s := range sources {
3854
assetsCount, err := database.CountAssetsBySource(ctx, s)
3955
if err != nil {

0 commit comments

Comments
 (0)