Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pod metrics cache lifetime #97

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ func (c *Collector) GetRawNetworkMetricsHandler(w http.ResponseWriter, req *http
m.RawNetworkMetric.RxBytes = 0
m.RawNetworkMetric.TxPackets = 0
m.RawNetworkMetric.RxPackets = 0

newLifetime := time.Now().Add(2 * time.Minute)
// reset lifetime only if current lifetime is longer than 2 minutes from now
if m.lifetime.After(newLifetime) {
m.lifetime = newLifetime
}
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ func (e *Exporter) export(ctx context.Context) error {
podMetrics, err := e.buildPodNetworkMetric(rawMetrics)
if err != nil {
if errors.Is(err, kube.ErrNotFound) {
e.log.Warnf("skipping pod metrics: %v", err)
// log warning only if metrics contain any data
if !e.emptyMetrics(rawMetrics) {
e.log.Warnf("skipping pod metrics: %v", err)
}
} else {
e.log.Errorf("init pod network metrics: %v", err)
}
Expand Down Expand Up @@ -187,6 +190,10 @@ func (e *Exporter) export(ctx context.Context) error {
return err
}

func (e *Exporter) emptyMetrics(metrics *pb.RawNetworkMetric) bool {
return metrics.TxBytes == 0 && metrics.RxBytes == 0 && metrics.TxPackets == 0 && metrics.RxPackets == 0
}

func (e *Exporter) buildPodNetworkMetric(conn *pb.RawNetworkMetric) (*pb.PodNetworkMetric, error) {
srcIP := ipFromInt32(conn.SrcIp)
pod, err := e.kubeWatcher.GetPodByIP(srcIP.String())
Expand Down
18 changes: 1 addition & 17 deletions kube/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,7 @@ func TestWatcher(t *testing.T) {
},
}

// pod exited a while ago, and should not be found
p4 := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "p4",
Namespace: "team1",
CreationTimestamp: thirtySecsAgo,
},
Spec: corev1.PodSpec{
NodeName: n1.Name,
},
Status: corev1.PodStatus{
PodIP: "10.14.7.13",
Phase: corev1.PodSucceeded,
},
}

clientset := fake.NewSimpleClientset(n1, p1, p2, p3, p4)
clientset := fake.NewSimpleClientset(n1, p1, p2, p3)

informersFactory := informers.NewSharedInformerFactoryWithOptions(clientset, 30*time.Second)
podsInformer := informersFactory.Core().V1().Pods().Informer()
Expand Down
Loading