@@ -51,6 +51,7 @@ func (r *Reaper) DetectSprocChanges(ctx context.Context, md *sources.SourceConn)
5151 detectedChanges := make (metrics.Measurements , 0 )
5252 var firstRun bool
5353 l := log .GetLogger (ctx )
54+ changeCounts .Target = "functions"
5455 l .Debug ("checking for sproc changes..." )
5556 if _ , ok := md .ChangeState ["sproc_hashes" ]; ! ok {
5657 firstRun = true
@@ -131,6 +132,7 @@ func (r *Reaper) DetectTableChanges(ctx context.Context, md *sources.SourceConn)
131132 var firstRun bool
132133 var changeCounts ChangeDetectionResults
133134 l := log .GetLogger (ctx )
135+ changeCounts .Target = "tables"
134136 l .Debug ("checking for table changes..." )
135137 if _ , ok := md .ChangeState ["table_hashes" ]; ! ok {
136138 firstRun = true
@@ -214,6 +216,7 @@ func (r *Reaper) DetectIndexChanges(ctx context.Context, md *sources.SourceConn)
214216 var firstRun bool
215217 var changeCounts ChangeDetectionResults
216218 l := log .GetLogger (ctx )
219+ changeCounts .Target = "indexes"
217220 l .Debug ("checking for index changes..." )
218221 if _ , ok := md .ChangeState ["index_hashes" ]; ! ok {
219222 firstRun = true
@@ -296,6 +299,7 @@ func (r *Reaper) DetectPrivilegeChanges(ctx context.Context, md *sources.SourceC
296299 var firstRun bool
297300 var changeCounts ChangeDetectionResults
298301 l := log .GetLogger (ctx )
302+ changeCounts .Target = "privileges"
299303 l .Debug ("checking object privilege changes..." )
300304 if _ , ok := md .ChangeState ["object_privileges" ]; ! ok {
301305 firstRun = true
@@ -377,6 +381,7 @@ func (r *Reaper) DetectConfigurationChanges(ctx context.Context, md *sources.Sou
377381 var firstRun bool
378382 var changeCounts ChangeDetectionResults
379383 l := log .GetLogger (ctx )
384+ changeCounts .Target = "settings"
380385 l .Debug ("checking for configuration changes..." )
381386 if _ , ok := md .ChangeState ["configuration_hashes" ]; ! ok {
382387 firstRun = true
@@ -460,48 +465,23 @@ func (r *Reaper) GetInstanceUpMeasurement(ctx context.Context, md *sources.Sourc
460465 }, err
461466}
462467
463- func (r * Reaper ) CheckForPGObjectChangesAndStore (ctx context.Context , md * sources.SourceConn ) {
468+ func (r * Reaper ) GetObjectChangesMeasurement (ctx context.Context , md * sources.SourceConn ) (metrics. Measurements , error ) {
464469 md .Lock ()
465470 defer md .Unlock ()
466- var err error
467- l := log .GetLogger (ctx ).WithField ("source" , md .Name ).WithField ("metric" , specialMetricChangeEvents )
468- ctx = log .WithLogger (ctx , l )
469- sprocCounts := r .DetectSprocChanges (ctx , md ) // TODO some of Detect*() code could be unified...
470- tableCounts := r .DetectTableChanges (ctx , md )
471- indexCounts := r .DetectIndexChanges (ctx , md )
472- confCounts := r .DetectConfigurationChanges (ctx , md )
473- privChangeCounts := r .DetectPrivilegeChanges (ctx , md )
474-
475- // need to send info on all object changes as one message as Grafana applies "last wins" for annotations with similar timestamp
476- if sprocCounts .Total () > 0 {
477- l = l .WithField ("functions" , sprocCounts .String ())
478- }
479- if tableCounts .Total () > 0 {
480- l = l .WithField ("relations" , tableCounts .String ())
481- }
482- if indexCounts .Total () > 0 {
483- l = l .WithField ("indexes" , indexCounts .String ())
484- }
485- if confCounts .Total () > 0 {
486- l = l .WithField ("settings" , confCounts .String ())
487- }
488- if privChangeCounts .Total () > 0 {
489- l = l .WithField ("privileges" , privChangeCounts .String ())
490- }
491- if len (l .Data ) > 2 { // source and metric are always there
492- m := metrics .NewMeasurement (time .Now ().UnixNano ())
493- if m ["details" ], err = l .String (); err != nil {
494- l .Error (err )
495- return
496- }
497- r .measurementCh <- metrics.MeasurementEnvelope {
498- DBName : md .Name ,
499- MetricName : "object_changes" ,
500- Data : metrics.Measurements {m },
501- CustomTags : md .CustomTags ,
502- }
503- l .Info ("detected changes [created/altered/dropped]" )
471+
472+ spN := r .DetectSprocChanges (ctx , md )
473+ tblN := r .DetectTableChanges (ctx , md )
474+ idxN := r .DetectIndexChanges (ctx , md )
475+ cnfN := r .DetectConfigurationChanges (ctx , md )
476+ privN := r .DetectPrivilegeChanges (ctx , md )
477+
478+ if spN .Total ()+ tblN .Total ()+ idxN .Total ()+ cnfN .Total ()+ privN .Total () == 0 {
479+ return nil , nil
504480 }
481+
482+ m := metrics .NewMeasurement (time .Now ().UnixNano ())
483+ m ["details" ] = strings .Join ([]string {spN .String (), tblN .String (), idxN .String (), cnfN .String (), privN .String ()}, " " )
484+ return metrics.Measurements {m }, nil
505485}
506486
507487// Called once on daemon startup if some commonly wanted extension (most notably pg_stat_statements) is missing.
0 commit comments