@@ -22,51 +22,55 @@ func getMonitoringIntervalSeconds() time.Duration {
22
22
23
23
// startGraphSizeMonitoring start a background process regularly checking the size of the graph to expose it as a prometheus metric
24
24
func startGraphSizeMonitoring (interval time.Duration , database knowledge.GraphDB , sourcesRegistry sources.Registry ) {
25
- logrus .Infof ("Monitoring of the graph size will happen every %ds" , int (interval / time .Second ))
26
- go func () {
27
- for {
28
- ctx , cancel := context .WithTimeout (context .Background (), interval )
29
- defer cancel ()
25
+ monitorForOneIteration := func () {
26
+ ctx , cancel := context .WithTimeout (context .Background (), interval )
27
+ defer cancel ()
30
28
31
- 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
- }
29
+ logrus .Debug ("Start monitoring the graph size..." )
30
+ assetsCount , err := database .CountAssets (ctx )
31
+ if err != nil {
32
+ metrics .GraphAssetsAggregatedGauge .Set (- 1 )
33
+ } else {
34
+ metrics .GraphAssetsAggregatedGauge .Set (float64 (assetsCount ))
35
+ }
36
+
37
+ relationsCount , err := database .CountRelations (ctx )
38
+ if err != nil {
39
+ metrics .GraphRelationsAggregatedGauge .Set (- 1 )
40
+ } else {
41
+ metrics .GraphRelationsAggregatedGauge .Set (float64 (relationsCount ))
42
+ }
43
+
44
+ sources , err := sourcesRegistry .ListSources (ctx )
45
+ if err != nil {
46
+ logrus .Errorf ("Unable to list sources for monitoring: %v" , err )
47
+ metrics .GraphAssetsTotalGauge .Reset ()
48
+ metrics .GraphRelationsTotalGauge .Reset ()
49
+ }
38
50
39
- relationsCount , err := database .CountRelations (ctx )
51
+ for s := range sources {
52
+ assetsCount , err := database .CountAssetsBySource (ctx , s )
40
53
if err != nil {
41
- metrics .GraphRelationsAggregatedGauge .Set (- 1 )
54
+ logrus .Errorf ("Unable to count assets of source %s for monitoring: %w" , s , err )
55
+ metrics .GraphAssetsTotalGauge .Reset ()
42
56
} else {
43
- metrics .GraphRelationsAggregatedGauge . Set (float64 (relationsCount ))
57
+ metrics .GraphAssetsTotalGauge . With (prometheus. Labels { "source" : s }). Set (float64 (assetsCount ))
44
58
}
45
59
46
- sources , err := sourcesRegistry . ListSources (ctx )
60
+ relationsCount , err := database . CountRelationsBySource (ctx , s )
47
61
if err != nil {
48
- logrus .Errorf ("Unable to list sources for monitoring: %v" , err )
49
- metrics .GraphAssetsTotalGauge .Reset ()
62
+ logrus .Errorf ("Unable to count relations of source %s for monitoring: %w" , s , err )
50
63
metrics .GraphRelationsTotalGauge .Reset ()
64
+ } else {
65
+ metrics .GraphRelationsTotalGauge .With (prometheus.Labels {"source" : s }).Set (float64 (relationsCount ))
51
66
}
67
+ }
68
+ }
52
69
53
- for s := range sources {
54
- assetsCount , err := database .CountAssetsBySource (ctx , s )
55
- if err != nil {
56
- logrus .Errorf ("Unable to count assets of source %s for monitoring: %w" , s , err )
57
- metrics .GraphAssetsTotalGauge .Reset ()
58
- } else {
59
- metrics .GraphAssetsTotalGauge .With (prometheus.Labels {"source" : s }).Set (float64 (assetsCount ))
60
- }
61
-
62
- relationsCount , err := database .CountRelationsBySource (ctx , s )
63
- if err != nil {
64
- logrus .Errorf ("Unable to count relations of source %s for monitoring: %w" , s , err )
65
- metrics .GraphRelationsTotalGauge .Reset ()
66
- } else {
67
- metrics .GraphRelationsTotalGauge .With (prometheus.Labels {"source" : s }).Set (float64 (relationsCount ))
68
- }
69
- }
70
+ logrus .Infof ("Monitoring of the graph size will happen every %ds" , int (interval / time .Second ))
71
+ go func () {
72
+ for {
73
+ monitorForOneIteration ()
70
74
time .Sleep (interval )
71
75
}
72
76
}()
0 commit comments