Skip to content

Commit

Permalink
feat: add kernelVersion to telemetry traces
Browse files Browse the repository at this point in the history
Signed-off-by: Anirudh <[email protected]>
  • Loading branch information
Anirudh2112 committed Dec 5, 2024
1 parent 2e7b979 commit f514bbb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
24 changes: 19 additions & 5 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -224,6 +224,20 @@ 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 {
if client != nil {
errtrace := appinsights.NewTraceTelemetry(fmt.Sprintf("failed to get kernel version for trace: %v", err), contracts.Error)
for k, v := range t.properties {
errtrace.Properties[k] = v
}
client.Track(errtrace)
}
} else {
properties[kernelversion] = kernelVersion
}

if t.properties != nil {
t.RLock()
for k, v := range t.properties {
Expand Down
23 changes: 23 additions & 0 deletions pkg/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/microsoft/ApplicationInsights-Go/appinsights"

)

func init() {
Expand All @@ -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(_ 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()

Expand Down

0 comments on commit f514bbb

Please sign in to comment.