@@ -146,6 +146,7 @@ type App struct {
146
146
HarvestTrigger HarvestTriggerFunc
147
147
LastActivity time.Time
148
148
Rules MetricRules
149
+ PhpPackages map [PhpPackagesKey ]struct {}
149
150
}
150
151
151
152
func (app * App ) String () string {
@@ -180,6 +181,7 @@ func NewApp(info *AppInfo) *App {
180
181
info : info ,
181
182
HarvestTrigger : nil ,
182
183
LastActivity : now ,
184
+ PhpPackages : make (map [PhpPackagesKey ]struct {}),
183
185
}
184
186
}
185
187
@@ -303,10 +305,10 @@ func (app *App) NeedsConnectAttempt(now time.Time, backoff time.Duration) bool {
303
305
return false
304
306
}
305
307
306
- //Since span events are not included in Faster Event Harvest due to concerns
307
- //about downsampling within a distributed trace, the report period and harvest
308
- //limit are reported separately in span_event_harvest_config instead of
309
- //event_harvest_config. Combine them both into EventHarvestConfig here.
308
+ // Since span events are not included in Faster Event Harvest due to concerns
309
+ // about downsampling within a distributed trace, the report period and harvest
310
+ // limit are reported separately in span_event_harvest_config instead of
311
+ // event_harvest_config. Combine them both into EventHarvestConfig here.
310
312
func combineEventConfig (ehc collector.EventHarvestConfig , sehc collector.SpanEventHarvestConfig ) collector.EventHarvestConfig {
311
313
ehc .EventConfigs .SpanEventConfig .Limit = sehc .SpanEventConfig .Limit
312
314
ehc .EventConfigs .SpanEventConfig .ReportPeriod = sehc .SpanEventConfig .ReportPeriod
@@ -338,3 +340,78 @@ func (app *App) Inactive(threshold time.Duration) bool {
338
340
}
339
341
return time .Since (app .LastActivity ) > threshold
340
342
}
343
+
344
+ // filter seen php packages data to avoid sending duplicates
345
+ //
346
+ // the `App` structure contains a map of PHP Packages the reporting
347
+ // application has encountered.
348
+ //
349
+ // the map of packages should persist for the duration of the
350
+ // current connection
351
+ //
352
+ // takes the `PhpPackages.data` byte array as input and unmarshals
353
+ // into an anonymous interface array
354
+ //
355
+ // the JSON format received from the agent is:
356
+ //
357
+ // [["package_name","version",{}],...]
358
+ //
359
+ // for each entry, assign the package name and version to the `PhpPackagesKey`
360
+ // struct and use the key to verify data does not exist in the map. If the
361
+ // key does not exist, add it to the map and the array of 'new' packages.
362
+ //
363
+ // convert the array of 'new' packages into a byte array representing
364
+ // the expected data that should match input, minus the duplicates.
365
+ func (app * App ) filterPhpPackages (data []byte ) []byte {
366
+ if data == nil {
367
+ return nil
368
+ }
369
+
370
+ var pkgKey PhpPackagesKey
371
+ var newPkgs []PhpPackagesKey
372
+ var x []interface {}
373
+
374
+ err := json .Unmarshal (data , & x )
375
+ if nil != err {
376
+ log .Errorf ("failed to unmarshal php package json: %s" , err )
377
+ return nil
378
+ }
379
+
380
+ for _ , pkgJson := range x {
381
+ pkg , _ := pkgJson .([]interface {})
382
+ if len (pkg ) != 3 {
383
+ log .Errorf ("invalid php package json structure: %+v" , pkg )
384
+ return nil
385
+ }
386
+ name , ok := pkg [0 ].(string )
387
+ version , ok := pkg [1 ].(string )
388
+ pkgKey = PhpPackagesKey {name , version }
389
+ _ , ok = app .PhpPackages [pkgKey ]
390
+ if ! ok {
391
+ app .PhpPackages [pkgKey ] = struct {}{}
392
+ newPkgs = append (newPkgs , pkgKey )
393
+ }
394
+ }
395
+
396
+ if newPkgs == nil {
397
+ return nil
398
+ }
399
+
400
+ buf := & bytes.Buffer {}
401
+ buf .WriteString (`[` )
402
+ for _ , pkg := range newPkgs {
403
+ buf .WriteString (`["` )
404
+ buf .WriteString (pkg .Name )
405
+ buf .WriteString (`","` )
406
+ buf .WriteString (pkg .Version )
407
+ buf .WriteString (`",{}],` )
408
+ }
409
+
410
+ resJson := buf .Bytes ()
411
+
412
+ // swap last ',' character with ']'
413
+ resJson = resJson [:len (resJson )- 1 ]
414
+ resJson = append (resJson , ']' )
415
+
416
+ return resJson
417
+ }
0 commit comments