diff --git a/internal/server/monitoring.go b/internal/server/monitoring.go index 1cd12f0..6faaf04 100644 --- a/internal/server/monitoring.go +++ b/internal/server/monitoring.go @@ -22,51 +22,55 @@ func getMonitoringIntervalSeconds() time.Duration { // startGraphSizeMonitoring start a background process regularly checking the size of the graph to expose it as a prometheus metric func startGraphSizeMonitoring(interval time.Duration, database knowledge.GraphDB, sourcesRegistry sources.Registry) { - logrus.Infof("Monitoring of the graph size will happen every %ds", int(interval/time.Second)) - go func() { - for { - ctx, cancel := context.WithTimeout(context.Background(), interval) - defer cancel() + monitorForOneIteration := func() { + ctx, cancel := context.WithTimeout(context.Background(), interval) + defer cancel() - logrus.Debug("Start monitoring the graph size...") - assetsCount, err := database.CountAssets(ctx) - if err != nil { - metrics.GraphAssetsAggregatedGauge.Set(-1) - } else { - metrics.GraphAssetsAggregatedGauge.Set(float64(assetsCount)) - } + logrus.Debug("Start monitoring the graph size...") + assetsCount, err := database.CountAssets(ctx) + if err != nil { + metrics.GraphAssetsAggregatedGauge.Set(-1) + } else { + metrics.GraphAssetsAggregatedGauge.Set(float64(assetsCount)) + } + + relationsCount, err := database.CountRelations(ctx) + if err != nil { + metrics.GraphRelationsAggregatedGauge.Set(-1) + } else { + metrics.GraphRelationsAggregatedGauge.Set(float64(relationsCount)) + } + + sources, err := sourcesRegistry.ListSources(ctx) + if err != nil { + logrus.Errorf("Unable to list sources for monitoring: %v", err) + metrics.GraphAssetsTotalGauge.Reset() + metrics.GraphRelationsTotalGauge.Reset() + } - relationsCount, err := database.CountRelations(ctx) + for s := range sources { + assetsCount, err := database.CountAssetsBySource(ctx, s) if err != nil { - metrics.GraphRelationsAggregatedGauge.Set(-1) + logrus.Errorf("Unable to count assets of source %s for monitoring: %w", s, err) + metrics.GraphAssetsTotalGauge.Reset() } else { - metrics.GraphRelationsAggregatedGauge.Set(float64(relationsCount)) + metrics.GraphAssetsTotalGauge.With(prometheus.Labels{"source": s}).Set(float64(assetsCount)) } - sources, err := sourcesRegistry.ListSources(ctx) + relationsCount, err := database.CountRelationsBySource(ctx, s) if err != nil { - logrus.Errorf("Unable to list sources for monitoring: %v", err) - metrics.GraphAssetsTotalGauge.Reset() + logrus.Errorf("Unable to count relations of source %s for monitoring: %w", s, err) metrics.GraphRelationsTotalGauge.Reset() + } else { + metrics.GraphRelationsTotalGauge.With(prometheus.Labels{"source": s}).Set(float64(relationsCount)) } + } + } - for s := range sources { - assetsCount, err := database.CountAssetsBySource(ctx, s) - if err != nil { - logrus.Errorf("Unable to count assets of source %s for monitoring: %w", s, err) - metrics.GraphAssetsTotalGauge.Reset() - } else { - metrics.GraphAssetsTotalGauge.With(prometheus.Labels{"source": s}).Set(float64(assetsCount)) - } - - relationsCount, err := database.CountRelationsBySource(ctx, s) - if err != nil { - logrus.Errorf("Unable to count relations of source %s for monitoring: %w", s, err) - metrics.GraphRelationsTotalGauge.Reset() - } else { - metrics.GraphRelationsTotalGauge.With(prometheus.Labels{"source": s}).Set(float64(relationsCount)) - } - } + logrus.Infof("Monitoring of the graph size will happen every %ds", int(interval/time.Second)) + go func() { + for { + monitorForOneIteration() time.Sleep(interval) } }()