From a1cc0e2edd5e4748bfe3c73fec630a731b61f89d Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 7 Nov 2023 17:36:36 +0800 Subject: [PATCH] fix: telemetry recorder.Done() may panic if telemetry is disabled --- pkg/telemetry/recorder.go | 3 +++ pkg/telemetry/recorder_test.go | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/telemetry/recorder.go b/pkg/telemetry/recorder.go index 20d0e8c09..567b894f8 100644 --- a/pkg/telemetry/recorder.go +++ b/pkg/telemetry/recorder.go @@ -97,6 +97,9 @@ func (t *recorder) GenerateTelemetryRecord(object any, objectType, eventType, re } func (t *recorder) Done() { + if t.telemetryDisabled { + return + } t.throttler.close() } diff --git a/pkg/telemetry/recorder_test.go b/pkg/telemetry/recorder_test.go index 2564bc5ca..00db0aa49 100644 --- a/pkg/telemetry/recorder_test.go +++ b/pkg/telemetry/recorder_test.go @@ -27,10 +27,13 @@ import ( type fakeEventRecorder struct{} func (f *fakeEventRecorder) Event(object runtime.Object, eventtype, reason, message string) { + getLogger().Printf("Event: %+v, %s, %s, %s\n", object, eventtype, reason, message) } func (f *fakeEventRecorder) Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...any) { + getLogger().Printf("Eventf: %+v, %s, %s, %s, %v\n", object, eventtype, reason, messageFmt, args) } func (f *fakeEventRecorder) AnnotatedEventf(object runtime.Object, annotations map[string]string, eventtype, reason, messageFmt string, args ...any) { + getLogger().Printf("AnnotatedEventf: %+v, %+v, %s, %s, %s, %v\n", object, annotations, eventtype, reason, messageFmt, args) } var _ = Describe("Telemetry", Label("telemetry"), Ordered, func() { @@ -60,7 +63,11 @@ var _ = Describe("Telemetry", Label("telemetry"), Ordered, func() { It("GetHostMetrics", func() { metrics := telemetry.GetHostMetrics() - Expect(metrics).ShouldNot(BeNil()) + if TelemetryDisabled { + Expect(metrics).Should(BeNil()) + } else { + Expect(metrics).ShouldNot(BeNil()) + } }) It("Event", func() {