Skip to content

Comments

[feat] Network Costs Test#19

Open
Manas23601 wants to merge 14 commits intoopencost:mainfrom
Manas23601:networkcost
Open

[feat] Network Costs Test#19
Manas23601 wants to merge 14 commits intoopencost:mainfrom
Manas23601:networkcost

Conversation

@Manas23601
Copy link
Collaborator

@Manas23601 Manas23601 commented Jul 13, 2025

Description

Network Costs

  • Receive Bytes
  • Transfer Bytes

Prometheus Query

sum(increase(container_network_receive_bytes_total{pod!=""}[24h:5m])) by (pod, namespace)`
sum(increase(container_network_transmit_bytes_total{pod!="", %s}[%s:%dm])) by (pod_name, pod, namespace, %s)

Testing

Network Costs

md@Manas:/mnt/c/Manas/opencost-integration-tests$ go test -v ./test/integration/prometheus/network_costs_test.go
=== RUN   TestNetworkCosts
    network_costs_test.go:34: testCases: [{Yesterday 24h namespace false}]
=== RUN   TestNetworkCosts/Yesterday
    network_costs_test.go:180: Namespace argo
    network_costs_test.go:185:   - NetworkTransferBytes[Pass]: ~ 2113274703.39
    network_costs_test.go:190:   - NetworkReceiveBytes[Pass]: ~ 3601434433.67
    network_costs_test.go:180: Namespace gpu-operator
    network_costs_test.go:185:   - NetworkTransferBytes[Pass]: ~ 3951830782.66
    network_costs_test.go:190:   - NetworkReceiveBytes[Pass]: ~ 3303167969.23
    network_costs_test.go:180: Namespace cert-manager
    network_costs_test.go:185:   - NetworkTransferBytes[Pass]: ~ 51280593.62
    network_costs_test.go:190:   - NetworkReceiveBytes[Pass]: ~ 35189496.75
    network_costs_test.go:180: Namespace ingress-nginx
    network_costs_test.go:185:   - NetworkTransferBytes[Pass]: ~ 77230121.14
    network_costs_test.go:190:   - NetworkReceiveBytes[Pass]: ~ 69102378.82
    network_costs_test.go:180: Namespace load-generator
    network_costs_test.go:185:   - NetworkTransferBytes[Pass]: ~ 7790042667.26
    network_costs_test.go:190:   - NetworkReceiveBytes[Pass]: ~ 20252712121.09
    network_costs_test.go:180: Namespace folding-at-home
    network_costs_test.go:185:   - NetworkTransferBytes[Pass]: ~ 389527427.79
    network_costs_test.go:190:   - NetworkReceiveBytes[Pass]: ~ 591013869.49
    network_costs_test.go:180: Namespace sealed-secrets
    network_costs_test.go:185:   - NetworkTransferBytes[Pass]: ~ 9175452.21
    network_costs_test.go:190:   - NetworkReceiveBytes[Pass]: ~ 8833045.30
    network_costs_test.go:180: Namespace prometheus-system
    network_costs_test.go:185:   - NetworkTransferBytes[Pass]: ~ 29482772937.37
    network_costs_test.go:190:   - NetworkReceiveBytes[Pass]: ~ 48800928242.51
    network_costs_test.go:180: Namespace kube-system
    network_costs_test.go:185:   - NetworkTransferBytes[Pass]: ~ 104585511444.96
    network_costs_test.go:190:   - NetworkReceiveBytes[Pass]: ~ 164773304552.70
    network_costs_test.go:180: Namespace opencost
    network_costs_test.go:185:   - NetworkTransferBytes[Pass]: ~ 174612718.16
    network_costs_test.go:190:   - NetworkReceiveBytes[Pass]: ~ 463782783.89
--- PASS: TestNetworkCosts (2.09s)
    --- PASS: TestNetworkCosts/Yesterday (2.09s)
PASS
ok      command-line-arguments  2.098s

Network Internet Costs

=== RUN   TestNetworkInternetCosts
    network_internet_costs_test.go:33: testCases: [{Yesterday 24h pod false}]
=== RUN   TestNetworkInternetCosts/Yesterday
    network_internet_costs_test.go:167: Pod test-install-speedtest-tracker-56944bbf5b-9lpd4
    network_internet_costs_test.go:172:   - NetworkInternetCost[Pass]: ~ 0.52375
--- PASS: TestNetworkInternetCosts (2.78s)
    --- PASS: TestNetworkInternetCosts/Yesterday (2.78s)
PASS
ok      command-line-arguments  2.785s

Note

Adds Prometheus vs Allocation integration tests for network bytes and egress cost (internet, zone, region), and wires them into test.bats.

  • Tests (Prometheus integration):
    • Network bytes (namespace): Validates container_network_receive_bytes_total and container_network_transmit_bytes_total against Allocation /allocation by namespace in test/integration/prometheus/network_costs_test.go.
    • Network egress costs (pod):
      • Internet egress: Compares kubecost_pod_network_egress_bytes_total{internet="true"} cost vs Allocation NetworkInternetCost in network_internet_costs_test.go.
      • Cross-zone egress: Compares kubecost_pod_network_egress_bytes_total{internet="false", same_zone="false", same_region="true"} cost vs Allocation NetworkCrossZoneCost in network_zone_costs_test.go.
      • Cross-region egress: Compares kubecost_pod_network_egress_bytes_total{internet="false", same_region="false"} cost vs Allocation NetworkCrossRegionCost in network_region_costs_test.go.
    • Test runner: test/integration/prometheus/test.bats updated to execute the new network tests.

Written by Cursor Bugbot for commit 99d1524. This will update automatically on new commits. Configure here.

@mbolt35
Copy link
Collaborator

mbolt35 commented Jul 14, 2025

Just wanted to drop a quick note about network costing. When we calculate network costs, we do not use the NetworkTransferBytes or NetworkReceiveBytes -- It's not a bad test to run for comparison, but as it relates to cost, we do not use these statistics for costs because they include all sub-categories of traffic (ie: local traffic, which is free).

I'm not sure if the network-costs pods are installed on this cluster, so we should definitely ensure that gets done as soon as possible.

@mbolt35
Copy link
Collaborator

mbolt35 commented Jul 14, 2025

The actual costs and usage data we use is here: https://github.com/opencost/opencost/blob/ef740759c88732487bffa644509480b070ca77c5/modules/prometheus-source/pkg/prom/metricsquerier.go#L907-L1021

But note that this requires a network-costs daemonset be running on the cluster (not sure if this is currently running on that cluster - if not, these metrics won't show up)

@Manas23601 Manas23601 changed the title NetWork Costs [feat] Network Costs Jul 14, 2025
@Manas23601 Manas23601 changed the title [feat] Network Costs [feat] Network Costs Test Jul 14, 2025
networkCostsPod.AllocNetworkInternetGiB = allocationResponseItem.NetworkInternetCost
}

noNegligibleCosts := false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validCostsSeen := false

@Manas23601
Copy link
Collaborator Author

@ameijer , if network zone and region tests look good, I'll go ahead anc comment those out in test.bats

@ameijer
Copy link
Member

ameijer commented Jul 28, 2025

@Manas23601 looks like the tests for zone and region are failing? for those, it is ok if we don't see any valid values

// AllocNetworkInternetGiB: 0.0,
// }
// continue
// }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this commented?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted the option to drill down to a pod level if needed. do you think we need to go down further down?

Signed-off-by: Manas Sivakumar <manas23601@gmail.com>
@Manas23601
Copy link
Collaborator Author

@Manas23601 looks like the tests for zone and region are failing? for those, it is ok if we don't see any valid values

@Manas23601 Manas23601 closed this Jul 28, 2025
@Manas23601
Copy link
Collaborator Author

@Manas23601 looks like the tests for zone and region are failing? for those, it is ok if we don't see any valid values

These are meant to fail because there is no valid zone and region costs. We should either not fail the tests if there are no valid costs or not run this test at all

@Manas23601 Manas23601 reopened this Jul 28, 2025
@Manas23601 Manas23601 self-assigned this Jul 31, 2025
@Manas23601 Manas23601 requested a review from mbolt35 July 31, 2025 06:21
@Manas23601
Copy link
Collaborator Author

bugbot run

@cursor
Copy link

cursor bot commented Nov 18, 2025

Skipping Bugbot: Bugbot is disabled for this repository

Signed-off-by: Manas Sivakumar <manas23601@gmail.com>
@Manas23601 Manas23601 requested a review from a team as a code owner November 24, 2025 17:41
@ameijer
Copy link
Member

ameijer commented Nov 24, 2025

bugbot run

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is being reviewed by Cursor Bugbot

Details

You are on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

networkCostsNamespace, ok := networkCostsNamespaceMap[namespace]
if !ok {
networkCostsNamespaceMap[namespace] = &NetworkCostsAggregate{
PromNetworkReceiveBytes: networkTransferBytesPod,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Wrong field assigned in network transfer initialization

When a namespace doesn't exist in the map during Network Transfer Bytes processing, PromNetworkReceiveBytes is incorrectly set to networkTransferBytesPod instead of 0.0. This causes the receive bytes value to be initialized with transfer bytes data, leading to incorrect aggregation and comparison results.

Fix in Cursor Fix in Web

t.Errorf(" - NetworkReceiveBytes[Fail]: DifferencePercent: %0.2f, Prometheus: %0.2f, /allocation: %0.2f", diff_percent, networkCostValues.PromNetworkReceiveBytes, networkCostValues.AllocNetworkReceiveBytes)
} else {
t.Logf(" - NetworkReceiveBytes[Pass]: ~ %0.2f", networkCostValues.PromNetworkReceiveBytes)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: NetworkReceiveBytes validation uses wrong comparison result

The withinRange variable from the NetworkTransferBytes comparison on line 181 is reused for NetworkReceiveBytes validation without recalculating it. This means NetworkReceiveBytes always reports the same pass/fail status as NetworkTransferBytes, regardless of the actual NetworkReceiveBytes values. The code needs to call utils.AreWithinPercentage again for NetworkReceiveBytes before line 187.

Fix in Cursor Fix in Web

// Network Internet Cost for all Pods
// --------------------------------

networkInternetCost := promNetworkInternetCostResponse.Data.Result[0].Value.Value
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Potential panic from accessing empty result array

The code accesses Data.Result[0] without checking if the Result array is empty. If the Prometheus query returns no results, this causes a panic. This can happen when the network-costs daemonset isn't running or when there's no matching data for the query window.

Additional Locations (2)

Fix in Cursor Fix in Web

promNetworkRegionInput.Filters = map[string]string{
"internet": "false",
"same_Region": "false",
"same_region": "false",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Duplicate filter key with different casing

The filters map contains both "same_Region": "false" and "same_region": "false". Since Go map keys are case-sensitive, this creates two separate filter entries. The capital-R version appears to be a typo and likely won't match any Prometheus label, potentially causing the query to return incorrect results or no results at all.

Fix in Cursor Fix in Web

"internet": "false",
"same_Region": "false",
"same_region": "false",
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Missing same_zone filter uses wrong same_Region key

The filter map contains same_Region (capital R) instead of same_zone. According to the comment on line 56, the query should filter by same_zone="false" along with internet="false" and same_region="false". The same_Region key is a typo that won't match any Prometheus label, and the required same_zone filter is missing entirely, causing the query to return incorrect network cost data.

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants