Skip to content

Commit e84e2e2

Browse files
author
James Westby
committed
Log the number of items from each dg
This returns the number of items collected by each datagatherer so that the logs tell us a bit more about what was found in the cluster, and can help find where any items have been missed.
1 parent 8accb51 commit e84e2e2

File tree

7 files changed

+32
-18
lines changed

7 files changed

+32
-18
lines changed

pkg/agent/dummy_data_gatherer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (g *dummyDataGatherer) Delete() error {
4444
return nil
4545
}
4646

47-
func (c *dummyDataGatherer) Fetch() (interface{}, error) {
47+
func (c *dummyDataGatherer) Fetch() (interface{}, int, error) {
4848
var err error
4949
if c.attemptNumber < c.FailedAttempts {
5050
err = fmt.Errorf("First %d attempts will fail", c.FailedAttempts)
@@ -53,5 +53,5 @@ func (c *dummyDataGatherer) Fetch() (interface{}, error) {
5353
err = fmt.Errorf("This data gatherer will always fail")
5454
}
5555
c.attemptNumber++
56-
return nil, err
56+
return nil, -1, err
5757
}

pkg/agent/run.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,18 @@ func gatherData(config Config, dataGatherers map[string]datagatherer.DataGathere
362362

363363
var dgError *multierror.Error
364364
for k, dg := range dataGatherers {
365-
dgData, err := dg.Fetch()
365+
dgData, count, err := dg.Fetch()
366366
if err != nil {
367367
dgError = multierror.Append(dgError, fmt.Errorf("error in datagatherer %s: %w", k, err))
368368

369369
continue
370370
}
371371

372-
log.Printf("successfully gathered data from %q datagatherer", k)
372+
if count >= 0 {
373+
log.Printf("successfully gathered %d items from %q datagatherer", count, k)
374+
} else {
375+
log.Printf("successfully gathered data from %q datagatherer", k)
376+
}
373377
readings = append(readings, &api.DataReading{
374378
ClusterID: config.ClusterID,
375379
DataGatherer: k,

pkg/datagatherer/datagatherer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ type Config interface {
1212
// DataGatherer is the interface for Data Gatherers. Data Gatherers are in charge of fetching data from a certain cloud provider API or Kubernetes component.
1313
type DataGatherer interface {
1414
// Fetch retrieves data.
15-
Fetch() (interface{}, error)
15+
// count is the number of items that were discovered. A negative count means the number
16+
// of items was indeterminate.
17+
Fetch() (data interface{}, count int, err error)
1618
// Run starts the data gatherer's informers for resource collection.
1719
// Returns error if the data gatherer informer wasn't initialized
1820
Run(stopCh <-chan struct{}) error

pkg/datagatherer/k8s/discovery.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ func (g *DataGathererDiscovery) Delete() error {
6262
}
6363

6464
// Fetch will fetch discovery data from the apiserver, or return an error
65-
func (g *DataGathererDiscovery) Fetch() (interface{}, error) {
65+
func (g *DataGathererDiscovery) Fetch() (interface{}, int, error) {
6666
data, err := g.cl.ServerVersion()
6767
if err != nil {
68-
return nil, fmt.Errorf("failed to get server version: %v", err)
68+
return nil, -1, fmt.Errorf("failed to get server version: %v", err)
6969
}
7070

7171
response := map[string]interface{}{
7272
// data has type Info: https://godoc.org/k8s.io/apimachinery/pkg/version#Info
7373
"server_version": data,
7474
}
7575

76-
return response, nil
76+
return response, len(response), nil
7777
}

pkg/datagatherer/k8s/dynamic.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ func (g *DataGathererDynamic) Delete() error {
307307

308308
// Fetch will fetch the requested data from the apiserver, or return an error
309309
// if fetching the data fails.
310-
func (g *DataGathererDynamic) Fetch() (interface{}, error) {
310+
func (g *DataGathererDynamic) Fetch() (interface{}, int, error) {
311311
if g.groupVersionResource.String() == "" {
312-
return nil, fmt.Errorf("resource type must be specified")
312+
return nil, -1, fmt.Errorf("resource type must be specified")
313313
}
314314

315315
var list = map[string]interface{}{}
@@ -333,19 +333,19 @@ func (g *DataGathererDynamic) Fetch() (interface{}, error) {
333333
}
334334
continue
335335
}
336-
return nil, fmt.Errorf("failed to parse cached resource")
336+
return nil, -1, fmt.Errorf("failed to parse cached resource")
337337
}
338338

339339
// Redact Secret data
340340
err := redactList(items)
341341
if err != nil {
342-
return nil, errors.WithStack(err)
342+
return nil, -1, errors.WithStack(err)
343343
}
344344

345345
// add gathered resources to items
346346
list["items"] = items
347347

348-
return list, nil
348+
return list, len(items), nil
349349
}
350350

351351
func redactList(list []*api.GatheredResource) error {

pkg/datagatherer/k8s/dynamic_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
633633
if waitTimeout(&wg, 30*time.Second) {
634634
t.Fatalf("unexpected timeout")
635635
}
636-
res, err := dynamiDg.Fetch()
636+
res, count, err := dynamiDg.Fetch()
637637
if err != nil && !tc.err {
638638
t.Errorf("expected no error but got: %v", err)
639639
}
@@ -662,6 +662,10 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
662662
gotJSON, _ := json.MarshalIndent(list, "", " ")
663663
t.Fatalf("unexpected JSON: \ngot \n%s\nwant\n%s", string(gotJSON), expectedJSON)
664664
}
665+
666+
if len(list) != count {
667+
t.Errorf("wrong count of resources reported: got %d, want %d", count, len(list))
668+
}
665669
}
666670
})
667671
}
@@ -922,7 +926,7 @@ func TestDynamicGathererNativeResources_Fetch(t *testing.T) {
922926
if waitTimeout(&wg, 5*time.Second) {
923927
t.Fatalf("unexpected timeout")
924928
}
925-
res, err := dynamiDg.Fetch()
929+
res, count, err := dynamiDg.Fetch()
926930
if err != nil && !tc.err {
927931
t.Errorf("expected no error but got: %v", err)
928932
}
@@ -951,6 +955,10 @@ func TestDynamicGathererNativeResources_Fetch(t *testing.T) {
951955
gotJSON, _ := json.MarshalIndent(list, "", " ")
952956
t.Fatalf("unexpected JSON: \ngot \n%s\nwant\n%s", string(gotJSON), expectedJSON)
953957
}
958+
959+
if len(list) != count {
960+
t.Errorf("wrong count of resources reported: got %d, want %d", count, len(list))
961+
}
954962
}
955963
})
956964
}

pkg/datagatherer/local/local.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ func (g *DataGatherer) WaitForCacheSync(stopCh <-chan struct{}) error {
5454
}
5555

5656
// Fetch loads and returns the data from the LocalDatagatherer's dataPath
57-
func (g *DataGatherer) Fetch() (interface{}, error) {
57+
func (g *DataGatherer) Fetch() (interface{}, int, error) {
5858
dataBytes, err := ioutil.ReadFile(g.dataPath)
5959
if err != nil {
60-
return nil, err
60+
return nil, -1, err
6161
}
62-
return dataBytes, nil
62+
return dataBytes, -1, nil
6363
}

0 commit comments

Comments
 (0)