Skip to content

Commit

Permalink
Fix resource overflow issue with defer.
Browse files Browse the repository at this point in the history
  • Loading branch information
clems4ever committed Feb 26, 2021
1 parent 92be9e3 commit 16f479e
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions internal/server/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()
Expand Down

0 comments on commit 16f479e

Please sign in to comment.