diff --git a/agent/pkg/api/main.go b/agent/pkg/api/main.go index 6d498c769..904c9313b 100644 --- a/agent/pkg/api/main.go +++ b/agent/pkg/api/main.go @@ -117,8 +117,6 @@ func startReadingChannel(outputItems <-chan *tapApi.OutputChannelItem, extension } for item := range outputItems { - providers.EntryAdded() - extension := extensionsMap[item.Protocol.Name] resolvedSource, resolvedDestionation := resolveIP(item.ConnectionInfo) mizuEntry := extension.Dissector.Analyze(item, resolvedSource, resolvedDestionation) @@ -147,6 +145,9 @@ func startReadingChannel(outputItems <-chan *tapApi.OutputChannelItem, extension if err != nil { panic(err) } + + providers.EntryAdded(len(data)) + connection.SendText(string(data)) servicemap.GetInstance().NewTCPEntry(mizuEntry.Source, mizuEntry.Destination, &item.Protocol) diff --git a/agent/pkg/providers/stats_provider.go b/agent/pkg/providers/stats_provider.go index 0f42f0fb8..4565b7868 100644 --- a/agent/pkg/providers/stats_provider.go +++ b/agent/pkg/providers/stats_provider.go @@ -7,6 +7,7 @@ import ( type GeneralStats struct { EntriesCount int + EntriesVolumeInGB float64 FirstEntryTimestamp int LastEntryTimestamp int } @@ -21,8 +22,9 @@ func GetGeneralStats() GeneralStats { return generalStats } -func EntryAdded() { +func EntryAdded(size int) { generalStats.EntriesCount++ + generalStats.EntriesVolumeInGB += float64(size) / (1 << 30) currentTimestamp := int(time.Now().Unix()) @@ -32,5 +34,3 @@ func EntryAdded() { generalStats.LastEntryTimestamp = currentTimestamp } - - diff --git a/agent/pkg/providers/stats_provider_test.go b/agent/pkg/providers/stats_provider_test.go index 13acfece9..932ebe6a3 100644 --- a/agent/pkg/providers/stats_provider_test.go +++ b/agent/pkg/providers/stats_provider_test.go @@ -12,6 +12,10 @@ func TestNoEntryAddedCount(t *testing.T) { if entriesStats.EntriesCount != 0 { t.Errorf("unexpected result - expected: %v, actual: %v", 0, entriesStats.EntriesCount) } + + if entriesStats.EntriesVolumeInGB != 0 { + t.Errorf("unexpected result - expected: %v, actual: %v", 0, entriesStats.EntriesVolumeInGB) + } } func TestEntryAddedCount(t *testing.T) { @@ -20,7 +24,7 @@ func TestEntryAddedCount(t *testing.T) { for _, entriesCount := range tests { t.Run(fmt.Sprintf("%d", entriesCount), func(t *testing.T) { for i := 0; i < entriesCount; i++ { - providers.EntryAdded() + providers.EntryAdded(0) } entriesStats := providers.GetGeneralStats() @@ -29,7 +33,38 @@ func TestEntryAddedCount(t *testing.T) { t.Errorf("unexpected result - expected: %v, actual: %v", entriesCount, entriesStats.EntriesCount) } + if entriesStats.EntriesVolumeInGB != 0 { + t.Errorf("unexpected result - expected: %v, actual: %v", 0, entriesStats.EntriesVolumeInGB) + } + t.Cleanup(providers.ResetGeneralStats) }) } } + +func TestEntryAddedVolume(t *testing.T) { + // 6 bytes + 4 bytes + tests := [][]byte{[]byte("volume"), []byte("test")} + var expectedEntriesCount int + var expectedVolumeInGB float64 + + for _, data := range tests { + t.Run(fmt.Sprintf("%d", len(data)), func(t *testing.T) { + expectedEntriesCount++ + expectedVolumeInGB += float64(len(data)) / (1 << 30) + + providers.EntryAdded(len(data)) + + entriesStats := providers.GetGeneralStats() + + if entriesStats.EntriesCount != expectedEntriesCount { + t.Errorf("unexpected result - expected: %v, actual: %v", expectedEntriesCount, entriesStats.EntriesCount) + } + + if entriesStats.EntriesVolumeInGB != expectedVolumeInGB { + t.Errorf("unexpected result - expected: %v, actual: %v", expectedVolumeInGB, entriesStats.EntriesVolumeInGB) + } + }) + } + +} diff --git a/cli/telemetry/telemetry.go b/cli/telemetry/telemetry.go index 240422bd3..3eeab29d2 100644 --- a/cli/telemetry/telemetry.go +++ b/cli/telemetry/telemetry.go @@ -53,6 +53,7 @@ func ReportTapTelemetry(apiProvider *apiserver.Provider, args interface{}, start "args": string(argsBytes), "executionTimeInSeconds": int(time.Since(startTime).Seconds()), "apiCallsCount": generalStats["EntriesCount"], + "trafficVolumeInGB": generalStats["EntriesVolumeInGB"], } if err := sendTelemetry(argsMap); err != nil {