From b9a34f356b6f49570e75253cc94cb25cf0a7e156 Mon Sep 17 00:00:00 2001 From: Anirudh Date: Mon, 2 Dec 2024 12:11:12 -0500 Subject: [PATCH] feat: add kernelVersion to telemetry traces Signed-off-by: Anirudh --- pkg/telemetry/telemetry.go | 18 +++++++++++++----- pkg/telemetry/telemetry_test.go | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/pkg/telemetry/telemetry.go b/pkg/telemetry/telemetry.go index 4984df42db5..086a5f3dd1f 100644 --- a/pkg/telemetry/telemetry.go +++ b/pkg/telemetry/telemetry.go @@ -19,10 +19,10 @@ import ( ) var ( - client appinsights.TelemetryClient - version string - mbShift uint64 = 20 - + client appinsights.TelemetryClient + version string + mbShift uint64 = 20 + kernelVersionFunc = KernelVersion // property keys kernelversion = "kernelversion" sysmem = "sysmem" @@ -161,7 +161,7 @@ func (t *TelemetryClient) trackWarning(err error, msg string) { } func (t *TelemetryClient) heartbeat(ctx context.Context) { - kernelVersion, err := KernelVersion(ctx) + kernelVersion, err := kernelVersionFunc(ctx) if err != nil { t.trackWarning(err, "failed to get kernel version") } @@ -224,6 +224,14 @@ func (t *TelemetryClient) TrackMetric(metricname string, value float64, properti func (t *TelemetryClient) TrackTrace(name string, severity contracts.SeverityLevel, properties map[string]string) { trace := appinsights.NewTraceTelemetry(name, severity) + + kernelVersion, err := kernelVersionFunc(context.Background()) + if err == nil { + t.trackWarning(err, "failed to get kernel version for trace") + } else { + properties[kernelversion] = kernelVersion + } + if t.properties != nil { t.RLock() for k, v := range t.properties { diff --git a/pkg/telemetry/telemetry_test.go b/pkg/telemetry/telemetry_test.go index ae8c050dd91..c3455ec2a48 100644 --- a/pkg/telemetry/telemetry_test.go +++ b/pkg/telemetry/telemetry_test.go @@ -12,6 +12,8 @@ import ( "testing" "github.com/stretchr/testify/require" + "github.com/microsoft/ApplicationInsights-Go/appinsights" + ) func init() { @@ -22,6 +24,27 @@ func TestNewAppInsightsTelemetryClient(t *testing.T) { require.NotPanics(t, func() { NewAppInsightsTelemetryClient("test", map[string]string{}) }) } +func TestTrackTraceIncludesKernelVersion(t *testing.T) { + mockKernelVersion := "5.15.0-101-generic" + originalKernelVersionFunc := kernelVersionFunc + kernelVersionFunc = func(ctx context.Context) (string, error) { + return mockKernelVersion, nil + } + defer func() { kernelVersionFunc = originalKernelVersionFunc }() + + client := &TelemetryClient{ + properties: map[string]string{ + "test_key": "test_value", + }, + } + + traceProperties := map[string]string{} + + client.TrackTrace("test_trace_event", appinsights.Information, traceProperties) + + require.Equal(t, mockKernelVersion, traceProperties[kernelversion], "kernelVersion should be included in trace properties") +} + func TestGetEnvironmentProerties(t *testing.T) { properties := GetEnvironmentProperties()