Skip to content

Commit 16f479e

Browse files
committed
Fix resource overflow issue with defer.
1 parent 92be9e3 commit 16f479e

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

internal/server/monitoring.go

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,55 @@ func getMonitoringIntervalSeconds() time.Duration {
2222

2323
// startGraphSizeMonitoring start a background process regularly checking the size of the graph to expose it as a prometheus metric
2424
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()
3028

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+
}
3850

39-
relationsCount, err := database.CountRelations(ctx)
51+
for s := range sources {
52+
assetsCount, err := database.CountAssetsBySource(ctx, s)
4053
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()
4256
} else {
43-
metrics.GraphRelationsAggregatedGauge.Set(float64(relationsCount))
57+
metrics.GraphAssetsTotalGauge.With(prometheus.Labels{"source": s}).Set(float64(assetsCount))
4458
}
4559

46-
sources, err := sourcesRegistry.ListSources(ctx)
60+
relationsCount, err := database.CountRelationsBySource(ctx, s)
4761
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)
5063
metrics.GraphRelationsTotalGauge.Reset()
64+
} else {
65+
metrics.GraphRelationsTotalGauge.With(prometheus.Labels{"source": s}).Set(float64(relationsCount))
5166
}
67+
}
68+
}
5269

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()
7074
time.Sleep(interval)
7175
}
7276
}()

0 commit comments

Comments
 (0)