Skip to content

Commit

Permalink
fix: do not accumulate if pod is not there anymore #333 (#348)
Browse files Browse the repository at this point in the history
* do not accumulate if pod is not there anymore

* write simple test and return nil, for better checking

* Update metrics/accumulator.go

---------

Co-authored-by: Tyler Helmuth <[email protected]>
  • Loading branch information
enc and TylerHelmuth authored Mar 13, 2023
1 parent fef6119 commit 4660da8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions metrics/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func (a *MetricDataAccumulator) podStats(podResource *Resource, s stats.PodStats
if !a.metricGroupsToCollect[PodMetricGroup] {
return
}

// Metatdata can be nil if pod is terminated before metadata is fetched.
// no metrics are needed.
if podResource.PodMetadata == nil {
return
}

a.accumulate(
podResource,
Expand Down
2 changes: 1 addition & 1 deletion metrics/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (m *Metadata) GetPodMetadataByUid(uid types.UID) (*PodMetadata, error) {
logrus.WithFields(logrus.Fields{
"podUid": uid,
}).Error("Metadata: Pod not found")
return &PodMetadata{}, errors.New("pod not found")
return nil, errors.New("pod not found")
}

type NodeMetadata struct {
Expand Down
14 changes: 14 additions & 0 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,20 @@ func TestNodeStats(t *testing.T) {
assert.Equal(t, 1, len(data.Resource.Labels))
}

func TestPodStatsOnDeadPod(t *testing.T) {
summary, metadata := createMockSourceAssets(true, true, nil, true)
acc := createMockAccumulator(metadata, ValidMetricGroups)

podStats := getPodStatsByUID(summary.Pods, "5997ad9b-1d2a-43cf-ab57-a98d8796dc34")
node := getNodeResource(summary.Node, metadata)
pod := getPodResource(node, podStats, metadata)
pod.PodMetadata = nil

acc.podStats(pod, podStats)
assert.Equal(t, 0, len(acc.Data))

}

func TestPodStats(t *testing.T) {
summary, metadata := createMockSourceAssets(true, true, nil, true)
acc := createMockAccumulator(metadata, ValidMetricGroups)
Expand Down

0 comments on commit 4660da8

Please sign in to comment.