-
Notifications
You must be signed in to change notification settings - Fork 0
Migrate to using cloudcost-exporter metrics #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,15 @@ avg by (spot) (node_cpu_hourly_cost{cluster="%s"} | |
| ) | ||
| ) | ||
| ) | ||
| ` | ||
| cloudcostExporterQueryCostPerCpu = ` | ||
| avg by (price_tier) ( | ||
| cloudcost_aws_ec2_instance_cpu_usd_per_core_hour{cluster_name="%s"} | ||
| or | ||
| cloudcost_azure_aks_instance_cpu_usd_per_core_hour{cluster_name="%s"} | ||
| or | ||
| cloudcost_gcp_gke_instance_cpu_usd_per_core_hour{cluster_name="%s"} | ||
| ) | ||
| ` | ||
|
|
||
| queryMemoryCost = ` | ||
|
|
@@ -48,6 +57,15 @@ avg by (spot) (node_ram_hourly_cost{cluster="%s"} | |
| ) | ||
| ) | ||
| ) | ||
| ` | ||
| cloudcostExporterQueryMemoryCost = ` | ||
| avg by (price_tier) ( | ||
| cloudcost_aws_ec2_instance_memory_usd_per_gib_hour{cluster_name="%s"} | ||
| or | ||
| cloudcost_azure_aks_instance_memory_usd_per_gib_hour{cluster_name="%s"} | ||
| or | ||
| cloudcost_gcp_gke_instance_memory_usd_per_gib_hour{cluster_name="%s"} | ||
| ) | ||
| ` | ||
| queryPersistentVolumeCost = "avg_over_time(avg(pv_hourly_cost{cluster=\"%s\"})[24h:1m])" | ||
|
|
||
|
|
@@ -70,7 +88,8 @@ var ( | |
|
|
||
| // Client is a client for the cost model. | ||
| type Client struct { | ||
| client api.Client | ||
| client api.Client | ||
| useCloudCostExporterMetrics bool | ||
| } | ||
|
|
||
| // Clients bundles the dev and prod client in one struct. | ||
|
|
@@ -81,10 +100,11 @@ type Clients struct { | |
|
|
||
| // ClientConfig is the configuration for the cost model client. | ||
| type ClientConfig struct { | ||
| Address string | ||
| HTTPConfigFile string | ||
| Username string | ||
| Password string | ||
| Address string | ||
| HTTPConfigFile string | ||
| Username string | ||
| Password string | ||
| UseCloudCostExporterMetrics bool | ||
| } | ||
|
|
||
| // NewClient creates a new cost model client with the given configuration. | ||
|
|
@@ -123,7 +143,10 @@ func NewClient(config *ClientConfig) (*Client, error) { | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &Client{client: client}, nil | ||
| return &Client{ | ||
| client: client, | ||
| useCloudCostExporterMetrics: config.UseCloudCostExporterMetrics, | ||
| }, nil | ||
| } | ||
|
|
||
| // NewClients creates a new cost model clients with the given configuration. | ||
|
|
@@ -143,6 +166,10 @@ func NewClients(prodConfig, devConfig *ClientConfig) (*Clients, error) { | |
| // GetCostPerCPU returns the average cost per CPU for a given cluster. | ||
| func (c *Client) GetCostPerCPU(ctx context.Context, cluster string) (Cost, error) { | ||
| query := fmt.Sprintf(queryCostPerCPU, cluster) | ||
| if c.useCloudCostExporterMetrics { | ||
| query = fmt.Sprintf(cloudcostExporterQueryCostPerCpu, cluster, cluster, cluster) | ||
| } | ||
| fmt.Printf("query: %s\n", query) | ||
| results, err := c.query(ctx, query) | ||
| if err != nil { | ||
| return Cost{}, err | ||
|
|
@@ -153,6 +180,9 @@ func (c *Client) GetCostPerCPU(ctx context.Context, cluster string) (Cost, error | |
| // GetMemoryCost returns the cost per memory for a given cluster | ||
| func (c *Client) GetMemoryCost(ctx context.Context, cluster string) (Cost, error) { | ||
| query := fmt.Sprintf(queryMemoryCost, cluster) | ||
| if c.useCloudCostExporterMetrics { | ||
| query = fmt.Sprintf(cloudcostExporterQueryMemoryCost, cluster, cluster, cluster) | ||
| } | ||
| results, err := c.query(ctx, query) | ||
| if err != nil { | ||
| return Cost{}, err | ||
|
|
@@ -206,6 +236,17 @@ func (c *Client) parseResults(results model.Value) (Cost, error) { | |
| // This is when there is no spot/non-spot label | ||
| cost.Dollars = value | ||
| } | ||
| // Handles the case for cloudcost exporter metrics where `price_tier` is the label for spot/non-spot | ||
| // TODO: Delete after removing support for OpenCost | ||
| switch sample.Metric["price_tier"] { | ||
| case "ondemand": | ||
| cost.NonSpot = value | ||
| case "spot": | ||
| cost.Spot = value | ||
| default: | ||
| // This is when there is no spot/non-spot label | ||
| cost.Dollars = value | ||
| } | ||
|
Comment on lines
+235
to
+245
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is really the heart of the change. The biggest difference between the queries is that the |
||
| } | ||
|
|
||
| return cost, nil | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.