|
17 | 17 | package telemetry |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "bytes" |
20 | 21 | "context" |
21 | 22 | "net" |
22 | 23 | "reflect" |
@@ -306,7 +307,7 @@ func TestBuildMetricsToPrometheus(t *testing.T) { |
306 | 307 | workloadMetricCache: map[workloadMetricLabels]*workloadMetricInfo{}, |
307 | 308 | serviceMetricCache: map[serviceMetricLabels]*serviceMetricInfo{}, |
308 | 309 | } |
309 | | - m.updateWorkloadMetricCache(tt.args.data, tt.args.labels, tt.args.tcpConns) |
| 310 | + m.updateWorkloadMetricCache(tt.args.data, tt.args.labels, tt.args.tcpConns[tt.args.data.conSrcDstInfo]) |
310 | 311 | assert.Equal(t, m.workloadMetricCache[tt.args.labels].WorkloadConnClosed, tt.want[0]) |
311 | 312 | assert.Equal(t, m.workloadMetricCache[tt.args.labels].WorkloadConnOpened, tt.want[1]) |
312 | 313 | assert.Equal(t, m.workloadMetricCache[tt.args.labels].WorkloadConnReceivedBytes, tt.want[2]) |
@@ -440,7 +441,7 @@ func TestBuildServiceMetricsToPrometheus(t *testing.T) { |
440 | 441 | workloadMetricCache: map[workloadMetricLabels]*workloadMetricInfo{}, |
441 | 442 | serviceMetricCache: map[serviceMetricLabels]*serviceMetricInfo{}, |
442 | 443 | } |
443 | | - m.updateServiceMetricCache(tt.args.data, tt.args.labels, tt.args.tcpConns) |
| 444 | + m.updateServiceMetricCache(tt.args.data, tt.args.labels, tt.args.tcpConns[tt.args.data.conSrcDstInfo]) |
444 | 445 | assert.Equal(t, m.serviceMetricCache[tt.args.labels].ServiceConnClosed, tt.want[0]) |
445 | 446 | assert.Equal(t, m.serviceMetricCache[tt.args.labels].ServiceConnOpened, tt.want[1]) |
446 | 447 | assert.Equal(t, m.serviceMetricCache[tt.args.labels].ServiceConnReceivedBytes, tt.want[2]) |
@@ -1677,3 +1678,96 @@ func TestMetricController_updatePrometheusMetric(t *testing.T) { |
1677 | 1678 | }) |
1678 | 1679 | } |
1679 | 1680 | } |
| 1681 | + |
| 1682 | +func TestBuildV4Metric(t *testing.T) { |
| 1683 | + buff := bytes.NewBuffer([]byte{10, 244, 1, 13, 10, 244, 1, 12, 34, 208, 144, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1684 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 96, 46, 224, 144, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1685 | + 0, 3, 0, 0, 0, 147, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 167, 122, 203, 84, 2, 0, 0, 0, 153, 163, |
| 1686 | + 210, 202, 232, 184, 0, 0, 64, 30, 158, 31, 235, 184, 0, 0, 0, 0, 0, 0, 150, 158, 0, 0, 19, 0, 0, 0, 0, 0, |
| 1687 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) |
| 1688 | + data := requestMetric{ |
| 1689 | + conSrcDstInfo: connectionSrcDst{ |
| 1690 | + src: [4]uint32{218231818, 0, 0, 0}, |
| 1691 | + dst: [4]uint32{201454602, 0, 0, 0}, |
| 1692 | + srcPort: 53282, |
| 1693 | + dstPort: 8080, |
| 1694 | + }, |
| 1695 | + origDstAddr: [4]uint32{3761135626, 0, 0, 0}, |
| 1696 | + origDstPort: 8080, |
| 1697 | + direction: 2, |
| 1698 | + receivedBytes: 147, |
| 1699 | + sentBytes: 3, |
| 1700 | + state: 1, |
| 1701 | + success: 1, |
| 1702 | + duration: 10012555943, |
| 1703 | + startTime: 203309974725529, |
| 1704 | + lastReportTime: 203319987281472, |
| 1705 | + srtt: 40598, |
| 1706 | + minRtt: 19, |
| 1707 | + totalRetrans: 0, |
| 1708 | + packetLost: 0, |
| 1709 | + } |
| 1710 | + |
| 1711 | + tests := []struct { |
| 1712 | + name string |
| 1713 | + tcpConns map[connectionSrcDst]connMetric |
| 1714 | + newConnMetric connMetric |
| 1715 | + }{ |
| 1716 | + { |
| 1717 | + name: "v4 metric test, tcpConn is empty", |
| 1718 | + tcpConns: map[connectionSrcDst]connMetric{}, |
| 1719 | + newConnMetric: connMetric{ |
| 1720 | + receivedBytes: 147, |
| 1721 | + sentBytes: 3, |
| 1722 | + packetLost: 0, |
| 1723 | + totalRetrans: 0, |
| 1724 | + totalReports: 1, |
| 1725 | + }, |
| 1726 | + }, |
| 1727 | + { |
| 1728 | + name: "v4 metric test, tcpConns in not empty", |
| 1729 | + tcpConns: map[connectionSrcDst]connMetric{ |
| 1730 | + data.conSrcDstInfo: { |
| 1731 | + receivedBytes: 1, |
| 1732 | + sentBytes: 1, |
| 1733 | + packetLost: 0, |
| 1734 | + totalRetrans: 0, |
| 1735 | + totalReports: 1, |
| 1736 | + }, |
| 1737 | + }, |
| 1738 | + newConnMetric: connMetric{ |
| 1739 | + receivedBytes: 147, |
| 1740 | + sentBytes: 3, |
| 1741 | + packetLost: 0, |
| 1742 | + totalRetrans: 0, |
| 1743 | + totalReports: 2, |
| 1744 | + }, |
| 1745 | + }, |
| 1746 | + } |
| 1747 | + |
| 1748 | + for _, tt := range tests { |
| 1749 | + t.Run(tt.name, func(t *testing.T) { |
| 1750 | + copiedBytes := make([]byte, len(buff.Bytes())) |
| 1751 | + copy(copiedBytes, buff.Bytes()) |
| 1752 | + |
| 1753 | + copiedBuff := bytes.NewBuffer(copiedBytes) |
| 1754 | + prevTcpConns := make(map[connectionSrcDst]connMetric) |
| 1755 | + for k, v := range tt.tcpConns { |
| 1756 | + prevTcpConns[k] = v |
| 1757 | + } |
| 1758 | + |
| 1759 | + got, err := buildV4Metric(copiedBuff, tt.tcpConns) |
| 1760 | + if err != nil { |
| 1761 | + t.Errorf("buildV4Metric() error = %v", err) |
| 1762 | + } |
| 1763 | + assert.Equal(t, tt.newConnMetric, tt.tcpConns[data.conSrcDstInfo]) |
| 1764 | + |
| 1765 | + temp := data |
| 1766 | + temp.sentBytes = temp.sentBytes - prevTcpConns[temp.conSrcDstInfo].sentBytes |
| 1767 | + temp.receivedBytes = temp.receivedBytes - prevTcpConns[temp.conSrcDstInfo].receivedBytes |
| 1768 | + temp.packetLost = temp.packetLost - prevTcpConns[temp.conSrcDstInfo].packetLost |
| 1769 | + temp.totalRetrans = temp.totalRetrans - prevTcpConns[temp.conSrcDstInfo].totalRetrans |
| 1770 | + assert.Equal(t, temp, got) |
| 1771 | + }) |
| 1772 | + } |
| 1773 | +} |
0 commit comments