metricsynthesizer_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. package synthetic
  2. import (
  3. "maps"
  4. "math"
  5. "testing"
  6. "time"
  7. "github.com/opencost/opencost/core/pkg/source"
  8. "github.com/opencost/opencost/core/pkg/util"
  9. "github.com/opencost/opencost/modules/collector-source/pkg/metric"
  10. )
  11. var _ metric.Updater = (*FuncUpdater)(nil)
  12. type FuncUpdater struct {
  13. f func(*metric.UpdateSet)
  14. }
  15. func NewFuncUpdater(f func(*metric.UpdateSet)) *FuncUpdater {
  16. return &FuncUpdater{f}
  17. }
  18. func (fu *FuncUpdater) Update(set *metric.UpdateSet) {
  19. fu.f(set)
  20. }
  21. func toMemoryResource(m map[string]string) map[string]string {
  22. mm := maps.Clone(m)
  23. mm[source.ResourceLabel] = "memory"
  24. mm[source.UnitLabel] = "byte"
  25. return mm
  26. }
  27. func toCpuResource(m map[string]string) map[string]string {
  28. mm := maps.Clone(m)
  29. mm[source.ResourceLabel] = "cpu"
  30. mm[source.UnitLabel] = "core"
  31. return mm
  32. }
  33. func findMetric(t *testing.T, set *metric.UpdateSet, name string, container string) *metric.Update {
  34. t.Helper()
  35. var metric *metric.Update
  36. for _, update := range set.Updates {
  37. if update.Name == name && update.Labels[source.ContainerLabel] == container {
  38. metric = &update
  39. break
  40. }
  41. }
  42. return metric
  43. }
  44. func assertMetricValue(t *testing.T, set *metric.UpdateSet, name string, container string, value float64) {
  45. t.Helper()
  46. metric := findMetric(t, set, name, container)
  47. if metric == nil {
  48. t.Fatalf("Failed to Locate a %s Metric for Container: %s\n", name, container)
  49. return
  50. }
  51. if !util.IsApproximately(metric.Value, value) {
  52. t.Fatalf("Expected %f for %s [Container: %s], got: %f\n", value, name, container, metric.Value)
  53. return
  54. }
  55. }
  56. func assertMetricExists(t *testing.T, set *metric.UpdateSet, name string, container string) {
  57. t.Helper()
  58. metric := findMetric(t, set, name, container)
  59. if metric == nil {
  60. t.Fatalf("Failed to Locate a %s Metric for Container: %s\n", name, container)
  61. return
  62. }
  63. }
  64. func assertNoMetricExists(t *testing.T, set *metric.UpdateSet, name string, container string) {
  65. t.Helper()
  66. metric := findMetric(t, set, name, container)
  67. if metric != nil {
  68. t.Fatalf("Expected metric to not exist: %s Metric for Container: %s\n", name, container)
  69. return
  70. }
  71. }
  72. func TestMetricSynthesizerRAMAllocation(t *testing.T) {
  73. pod1Info := map[string]string{
  74. source.NamespaceLabel: "namespace1",
  75. source.NodeLabel: "node1",
  76. source.InstanceLabel: "node1",
  77. source.PodLabel: "pod1",
  78. source.UIDLabel: "pod-uuid1",
  79. }
  80. container1Info := map[string]string{
  81. source.NamespaceLabel: "namespace1",
  82. source.NodeLabel: "node1",
  83. source.InstanceLabel: "node1",
  84. source.PodLabel: "pod1",
  85. source.UIDLabel: "pod-uuid1",
  86. source.ContainerLabel: "container1",
  87. }
  88. container2Info := map[string]string{
  89. source.NamespaceLabel: "kube-system",
  90. source.NodeLabel: "node1",
  91. source.InstanceLabel: "node1",
  92. source.PodLabel: "pod2",
  93. source.UIDLabel: "pod-uuid2",
  94. source.ContainerLabel: "container2",
  95. }
  96. const startingCPUSeconds float64 = 506000.0
  97. updateSet1 := &metric.UpdateSet{
  98. Timestamp: time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC),
  99. Updates: []metric.Update{
  100. // container1 has both requests and usage
  101. {
  102. Name: metric.KubePodContainerResourceRequests,
  103. Labels: toMemoryResource(container1Info),
  104. Value: 4.0 * 1024 * 1024 * 1024,
  105. },
  106. {
  107. Name: metric.ContainerMemoryWorkingSetBytes,
  108. Labels: maps.Clone(container1Info),
  109. Value: 5.5 * 1024 * 1024 * 1024,
  110. },
  111. // container2 only has usage
  112. {
  113. Name: metric.ContainerMemoryWorkingSetBytes,
  114. Labels: maps.Clone(container2Info),
  115. Value: 1.5 * 1024 * 1024 * 1024,
  116. },
  117. // add some additional metrics to test filtering
  118. {
  119. Name: metric.KubeNamespaceLabels,
  120. Labels: maps.Clone(pod1Info),
  121. Value: 0,
  122. },
  123. {
  124. Name: metric.KubePodContainerResourceRequests,
  125. Labels: toCpuResource(container1Info),
  126. Value: 20,
  127. },
  128. },
  129. }
  130. updateSet2 := &metric.UpdateSet{
  131. Timestamp: time.Date(2026, time.January, 1, 0, 0, 30, 0, time.UTC),
  132. Updates: []metric.Update{
  133. // container1 has both requests and usage
  134. {
  135. Name: metric.KubePodContainerResourceRequests,
  136. Labels: toMemoryResource(container1Info),
  137. Value: 4.0 * 1024 * 1024 * 1024,
  138. },
  139. {
  140. Name: metric.ContainerMemoryWorkingSetBytes,
  141. Labels: maps.Clone(container1Info),
  142. Value: 3.0 * 1024 * 1024 * 1024,
  143. },
  144. // container2 only has usage
  145. {
  146. Name: metric.ContainerMemoryWorkingSetBytes,
  147. Labels: maps.Clone(container2Info),
  148. Value: 2.5 * 1024 * 1024 * 1024,
  149. },
  150. // add some additional metrics to test filtering
  151. {
  152. Name: metric.KubeNamespaceLabels,
  153. Labels: maps.Clone(pod1Info),
  154. Value: 0,
  155. },
  156. {
  157. Name: metric.KubePodContainerResourceRequests,
  158. Labels: toCpuResource(container1Info),
  159. Value: 75,
  160. },
  161. },
  162. }
  163. updateSet3 := &metric.UpdateSet{
  164. Timestamp: time.Date(2026, time.January, 1, 0, 1, 0, 0, time.UTC),
  165. Updates: []metric.Update{
  166. // container1 has both requests and usage
  167. {
  168. Name: metric.KubePodContainerResourceRequests,
  169. Labels: toMemoryResource(container1Info),
  170. Value: 4.0 * 1024 * 1024 * 1024,
  171. },
  172. {
  173. Name: metric.ContainerMemoryWorkingSetBytes,
  174. Labels: maps.Clone(container1Info),
  175. Value: 6.0 * 1024 * 1024 * 1024,
  176. },
  177. // container2 only has usage
  178. {
  179. Name: metric.ContainerMemoryWorkingSetBytes,
  180. Labels: maps.Clone(container2Info),
  181. Value: 1.75 * 1024 * 1024 * 1024,
  182. },
  183. // add some additional metrics to test filtering
  184. {
  185. Name: metric.KubeNamespaceLabels,
  186. Labels: maps.Clone(pod1Info),
  187. Value: 0,
  188. },
  189. {
  190. Name: metric.KubePodContainerResourceRequests,
  191. Labels: toCpuResource(container1Info),
  192. Value: 135,
  193. },
  194. },
  195. }
  196. scrape := 0
  197. updater := NewFuncUpdater(func(us *metric.UpdateSet) {
  198. // first scrape:
  199. // - container1: max(4.0gb, 5.5gb)
  200. // - container2: 1.5gb
  201. if scrape == 0 {
  202. assertMetricValue(t, us, metric.ContainerMemoryAllocationBytes, "container1", 5.5*1024*1024*1024)
  203. assertMetricValue(t, us, metric.ContainerMemoryAllocationBytes, "container2", 1.5*1024*1024*1024)
  204. }
  205. // second scrape
  206. // - container1: max(4.0gb, 3.5gb)
  207. // - container2: 2.5gb
  208. if scrape == 1 {
  209. assertMetricValue(t, us, metric.ContainerMemoryAllocationBytes, "container1", 4.0*1024*1024*1024)
  210. assertMetricValue(t, us, metric.ContainerMemoryAllocationBytes, "container2", 2.5*1024*1024*1024)
  211. }
  212. // third scrape
  213. // - container1: max(4.0gb, 6.0gb)
  214. // - container2: 1.75gb
  215. if scrape == 2 {
  216. assertMetricValue(t, us, metric.ContainerMemoryAllocationBytes, "container1", 6.0*1024*1024*1024)
  217. assertMetricValue(t, us, metric.ContainerMemoryAllocationBytes, "container2", 1.75*1024*1024*1024)
  218. }
  219. scrape += 1
  220. })
  221. metricSynth := NewMetricSynthesizers(updater, NewContainerCpuAllocationSynthesizer(), NewContainerMemoryAllocationSynthesizer())
  222. metricSynth.Update(updateSet1)
  223. metricSynth.Update(updateSet2)
  224. metricSynth.Update(updateSet3)
  225. }
  226. func TestMetricSynthesizerCPUAllocation(t *testing.T) {
  227. pod1Info := map[string]string{
  228. source.NamespaceLabel: "namespace1",
  229. source.NodeLabel: "node1",
  230. source.InstanceLabel: "node1",
  231. source.PodLabel: "pod1",
  232. source.UIDLabel: "pod-uuid1",
  233. }
  234. container1Info := map[string]string{
  235. source.NamespaceLabel: "namespace1",
  236. source.NodeLabel: "node1",
  237. source.InstanceLabel: "node1",
  238. source.PodLabel: "pod1",
  239. source.UIDLabel: "pod-uuid1",
  240. source.ContainerLabel: "container1",
  241. }
  242. container2Info := map[string]string{
  243. source.NamespaceLabel: "kube-system",
  244. source.NodeLabel: "node1",
  245. source.InstanceLabel: "node1",
  246. source.PodLabel: "pod2",
  247. source.UIDLabel: "pod-uuid2",
  248. source.ContainerLabel: "container2",
  249. }
  250. const startingCPUSeconds float64 = 506000.0
  251. updateSet1 := &metric.UpdateSet{
  252. Timestamp: time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC),
  253. Updates: []metric.Update{
  254. // container1 has both requests and usage
  255. {
  256. Name: metric.KubePodContainerResourceRequests,
  257. Labels: toCpuResource(container1Info),
  258. Value: 0.2,
  259. },
  260. {
  261. Name: metric.ContainerCPUUsageSecondsTotal,
  262. Labels: maps.Clone(container1Info),
  263. Value: startingCPUSeconds,
  264. },
  265. // container2 only has usage
  266. {
  267. Name: metric.ContainerCPUUsageSecondsTotal,
  268. Labels: maps.Clone(container2Info),
  269. Value: startingCPUSeconds,
  270. },
  271. // add some additional metrics to test filtering
  272. {
  273. Name: metric.KubeNamespaceLabels,
  274. Labels: maps.Clone(pod1Info),
  275. Value: 0,
  276. },
  277. {
  278. Name: metric.KubePodContainerResourceRequests,
  279. Labels: toMemoryResource(container1Info),
  280. Value: 2.5 * 1024.0 * 1024.0 * 1024.0,
  281. },
  282. },
  283. }
  284. updateSet2 := &metric.UpdateSet{
  285. Timestamp: time.Date(2026, time.January, 1, 0, 0, 30, 0, time.UTC),
  286. Updates: []metric.Update{
  287. // container1 has both requests and usage
  288. {
  289. Name: metric.KubePodContainerResourceRequests,
  290. Labels: toCpuResource(container1Info),
  291. Value: 0.2,
  292. },
  293. {
  294. Name: metric.ContainerCPUUsageSecondsTotal,
  295. Labels: maps.Clone(container1Info),
  296. Value: startingCPUSeconds + 40.0,
  297. },
  298. // container2 only has usage
  299. {
  300. Name: metric.ContainerCPUUsageSecondsTotal,
  301. Labels: maps.Clone(container2Info),
  302. Value: startingCPUSeconds + 30.0,
  303. },
  304. // add some additional metrics to test filtering
  305. {
  306. Name: metric.KubeNamespaceLabels,
  307. Labels: maps.Clone(pod1Info),
  308. Value: 0,
  309. },
  310. {
  311. Name: metric.KubePodContainerResourceRequests,
  312. Labels: toMemoryResource(container1Info),
  313. Value: 2.5 * 1024.0 * 1024.0 * 1024.0,
  314. },
  315. },
  316. }
  317. updateSet3 := &metric.UpdateSet{
  318. Timestamp: time.Date(2026, time.January, 1, 0, 1, 0, 0, time.UTC),
  319. Updates: []metric.Update{
  320. // container1 has both requests and usage
  321. {
  322. Name: metric.KubePodContainerResourceRequests,
  323. Labels: toCpuResource(container1Info),
  324. Value: 0.2,
  325. },
  326. {
  327. Name: metric.ContainerCPUUsageSecondsTotal,
  328. Labels: maps.Clone(container1Info),
  329. Value: startingCPUSeconds + 40.0 + 5.0,
  330. },
  331. // container2 only has usage
  332. {
  333. Name: metric.ContainerCPUUsageSecondsTotal,
  334. Labels: maps.Clone(container2Info),
  335. Value: startingCPUSeconds + 30.0 + 30.0,
  336. },
  337. // add some additional metrics to test filtering
  338. {
  339. Name: metric.KubeNamespaceLabels,
  340. Labels: maps.Clone(pod1Info),
  341. Value: 0,
  342. },
  343. {
  344. Name: metric.KubePodContainerResourceRequests,
  345. Labels: toMemoryResource(container1Info),
  346. Value: 2.5 * 1024.0 * 1024.0 * 1024.0,
  347. },
  348. },
  349. }
  350. scrape := 0
  351. updater := NewFuncUpdater(func(us *metric.UpdateSet) {
  352. // first scrape:
  353. // - container1: alloc = request
  354. // - container2: no metric
  355. if scrape == 0 {
  356. assertMetricValue(t, us, metric.ContainerCPUAllocation, "container1", 0.2)
  357. assertNoMetricExists(t, us, metric.ContainerCPUAllocation, "container2")
  358. }
  359. // second scrape
  360. // - container1: alloc = 40s/30s = 1.33
  361. // - container2: alloc = 30s/30s = 1.0
  362. if scrape == 1 {
  363. assertMetricValue(t, us, metric.ContainerCPUAllocation, "container1", 1.33333333)
  364. assertMetricValue(t, us, metric.ContainerCPUAllocation, "container2", 1.0)
  365. }
  366. // third scrape
  367. // - container1: alloc = 5.0/30.0s = 0.13, so alloc = request again (0.2)
  368. // - container2: alloc = 30s/30s = 1.0
  369. if scrape == 2 {
  370. assertMetricValue(t, us, metric.ContainerCPUAllocation, "container1", 0.2)
  371. assertMetricValue(t, us, metric.ContainerCPUAllocation, "container2", 1.0)
  372. }
  373. scrape += 1
  374. })
  375. metricSynth := NewMetricSynthesizers(updater, NewContainerCpuAllocationSynthesizer(), NewContainerMemoryAllocationSynthesizer())
  376. metricSynth.Update(updateSet1)
  377. metricSynth.Update(updateSet2)
  378. metricSynth.Update(updateSet3)
  379. }
  380. func TestMetricSynthesizerCPUAllocation_UsageOverflow(t *testing.T) {
  381. container1Info := map[string]string{
  382. source.NamespaceLabel: "namespace1",
  383. source.NodeLabel: "node1",
  384. source.InstanceLabel: "node1",
  385. source.PodLabel: "pod1",
  386. source.UIDLabel: "pod-uuid1",
  387. source.ContainerLabel: "container1",
  388. }
  389. // start a max uint64 nanoseconds -> seconds
  390. // since the source metrics use nanoseconds, that's where overflow would occur.
  391. var startingCPUNanoSeconds uint64 = math.MaxUint64
  392. const nanosIncrement uint64 = 40 * 1e9
  393. toSeconds := func(nanos uint64) float64 {
  394. return float64(nanos) * 1e-9
  395. }
  396. updateSet1 := &metric.UpdateSet{
  397. Timestamp: time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC),
  398. Updates: []metric.Update{
  399. // First Update has requests AND 1 usage sample
  400. {
  401. Name: metric.KubePodContainerResourceRequests,
  402. Labels: toCpuResource(container1Info),
  403. Value: 0.2,
  404. },
  405. {
  406. Name: metric.ContainerCPUUsageSecondsTotal,
  407. Labels: maps.Clone(container1Info),
  408. Value: toSeconds(startingCPUNanoSeconds),
  409. },
  410. },
  411. }
  412. updateSet2 := &metric.UpdateSet{
  413. Timestamp: time.Date(2026, time.January, 1, 0, 0, 30, 0, time.UTC),
  414. Updates: []metric.Update{
  415. // Second Update doesn't have request, and has the second usage sample
  416. {
  417. Name: metric.ContainerCPUUsageSecondsTotal,
  418. Labels: maps.Clone(container1Info),
  419. Value: toSeconds(startingCPUNanoSeconds + nanosIncrement),
  420. },
  421. },
  422. }
  423. updateSet3 := &metric.UpdateSet{
  424. Timestamp: time.Date(2026, time.January, 1, 0, 1, 0, 0, time.UTC),
  425. Updates: []metric.Update{
  426. {
  427. Name: metric.ContainerCPUUsageSecondsTotal,
  428. Labels: maps.Clone(container1Info),
  429. Value: toSeconds(startingCPUNanoSeconds + nanosIncrement + nanosIncrement),
  430. },
  431. },
  432. }
  433. scrape := 0
  434. updater := NewFuncUpdater(func(us *metric.UpdateSet) {
  435. // first scrape:
  436. // - container1: alloc = request
  437. if scrape == 0 {
  438. assertMetricValue(t, us, metric.ContainerCPUAllocation, "container1", 0.2)
  439. }
  440. // second scrape
  441. // - container1: alloc = overflow, reset to current sample
  442. if scrape == 1 {
  443. assertMetricValue(t, us, metric.ContainerCPUAllocation, "container1", 0.0)
  444. }
  445. // third scrape
  446. // - container1: alloc = 40.0/30s = 1.33333
  447. if scrape == 2 {
  448. assertMetricValue(t, us, metric.ContainerCPUAllocation, "container1", 1.33333333)
  449. }
  450. scrape += 1
  451. })
  452. metricSynth := NewMetricSynthesizers(updater, NewContainerCpuAllocationSynthesizer(), NewContainerMemoryAllocationSynthesizer())
  453. metricSynth.Update(updateSet1)
  454. metricSynth.Update(updateSet2)
  455. metricSynth.Update(updateSet3)
  456. }
  457. func TestMetricSynthesizerCPUAllocation_UsageCounterReset(t *testing.T) {
  458. const nanosIncrement uint64 = 40 * 1e9
  459. container1Info := map[string]string{
  460. source.NamespaceLabel: "namespace1",
  461. source.NodeLabel: "node1",
  462. source.InstanceLabel: "node1",
  463. source.PodLabel: "pod1",
  464. source.UIDLabel: "pod-uuid1",
  465. source.ContainerLabel: "container1",
  466. }
  467. // Starting CPU Total Seconds
  468. const startingCPUSeconds float64 = 506000.0
  469. updateSet1 := &metric.UpdateSet{
  470. Timestamp: time.Date(2026, time.January, 1, 0, 0, 0, 0, time.UTC),
  471. Updates: []metric.Update{
  472. // First Update has requests AND 1 usage sample
  473. {
  474. Name: metric.KubePodContainerResourceRequests,
  475. Labels: toCpuResource(container1Info),
  476. Value: 0.2,
  477. },
  478. {
  479. Name: metric.ContainerCPUUsageSecondsTotal,
  480. Labels: maps.Clone(container1Info),
  481. Value: startingCPUSeconds,
  482. },
  483. },
  484. }
  485. updateSet2 := &metric.UpdateSet{
  486. Timestamp: time.Date(2026, time.January, 1, 0, 0, 30, 0, time.UTC),
  487. Updates: []metric.Update{
  488. // Second Update doesn't have request, and has the second usage sample
  489. {
  490. Name: metric.ContainerCPUUsageSecondsTotal,
  491. Labels: maps.Clone(container1Info),
  492. Value: startingCPUSeconds - 1000.0,
  493. },
  494. },
  495. }
  496. updateSet3 := &metric.UpdateSet{
  497. Timestamp: time.Date(2026, time.January, 1, 0, 1, 0, 0, time.UTC),
  498. Updates: []metric.Update{
  499. {
  500. Name: metric.ContainerCPUUsageSecondsTotal,
  501. Labels: maps.Clone(container1Info),
  502. Value: (startingCPUSeconds - 1000.0) + 40.0,
  503. },
  504. },
  505. }
  506. scrape := 0
  507. updater := NewFuncUpdater(func(us *metric.UpdateSet) {
  508. // first scrape:
  509. // - container1: alloc = request
  510. if scrape == 0 {
  511. assertMetricValue(t, us, metric.ContainerCPUAllocation, "container1", 0.2)
  512. }
  513. // second scrape
  514. // - container1: alloc = (subtract 1000s - usage sample is less than last recorded), reset to 0.0
  515. if scrape == 1 {
  516. assertMetricValue(t, us, metric.ContainerCPUAllocation, "container1", 0.0)
  517. }
  518. // third scrape
  519. // - container1: alloc = 40.0/30s = 1.33333
  520. if scrape == 2 {
  521. assertMetricValue(t, us, metric.ContainerCPUAllocation, "container1", 1.33333333)
  522. }
  523. scrape += 1
  524. })
  525. metricSynth := NewMetricSynthesizers(updater, NewContainerCpuAllocationSynthesizer(), NewContainerMemoryAllocationSynthesizer())
  526. metricSynth.Update(updateSet1)
  527. metricSynth.Update(updateSet2)
  528. metricSynth.Update(updateSet3)
  529. }