Skip to content
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

Metric Stream cost calculation #990

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions providers/aws/cloudwatch/alarms.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/pricing"
"github.com/aws/aws-sdk-go-v2/service/pricing/types"
"strconv"
"time"

"github.com/aws/aws-sdk-go-v2/service/pricing"
"github.com/aws/aws-sdk-go-v2/service/pricing/types"

log "github.com/sirupsen/logrus"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -18,7 +19,6 @@ import (
)

const (
RateCode = "JRTCKXETXF.6YS6EN2CT7"
AverageHoursPerMonth = 730
)

Expand Down Expand Up @@ -127,16 +127,14 @@ func calculateCostPerMonth(pricingOutput *pricing.GetProductsOutput) (float64, e
for _, details := range onDemand.(map[string]interface{}) {
if priceDetails, ok := details.(map[string]interface{})["priceDimensions"].(map[string]interface{}); ok {
for _, price := range priceDetails {
rateCode := price.(map[string]interface{})["rateCode"].(string)
if rateCode == RateCode {
usdPrice := price.(map[string]interface{})["pricePerUnit"].(map[string]interface{})["USD"].(string)
cost, err := strconv.ParseFloat(usdPrice, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse cost per month: %w", err)
}
costPerMonth = cost * AverageHoursPerMonth
break
usdPrice := price.(map[string]interface{})["pricePerUnit"].(map[string]interface{})["USD"].(string)
cost, err := strconv.ParseFloat(usdPrice, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse cost per month: %w", err)
}
costPerMonth = cost * AverageHoursPerMonth
break

}
}
}
Expand Down
84 changes: 83 additions & 1 deletion providers/aws/cloudwatch/log_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,85 @@ package cloudwatch

import (
"context"
"encoding/json"
"fmt"
"strconv"
"time"

log "github.com/sirupsen/logrus"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
cwTypes "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"

"github.com/aws/aws-sdk-go-v2/service/pricing"
"github.com/aws/aws-sdk-go-v2/service/pricing/types"

"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
)

func getRate(pricingOutput *pricing.GetProductsOutput) (float64, error) {
costPerMonth := 0.0

if pricingOutput != nil && len(pricingOutput.PriceList) > 0 {
var priceList interface{}
err := json.Unmarshal([]byte(pricingOutput.PriceList[0]), &priceList)
if err != nil {
return 0, fmt.Errorf("failed to unmarshal JSON: %w", err)
}

priceListMap := priceList.(map[string]interface{})
if onDemand, ok := priceListMap["terms"].(map[string]interface{})["OnDemand"]; ok {
for _, details := range onDemand.(map[string]interface{}) {
if priceDetails, ok := details.(map[string]interface{})["priceDimensions"].(map[string]interface{}); ok {
for _, price := range priceDetails {
usdPrice := price.(map[string]interface{})["pricePerUnit"].(map[string]interface{})["USD"].(string)
costPerMonth, err = strconv.ParseFloat(usdPrice, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse cost per month: %w", err)
}
break
}
}
}
}
}

return costPerMonth, nil
}

func MetricStreams(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
resources := make([]models.Resource, 0)
cloudWatchMetricsClient := cloudwatch.NewFromConfig(*client.AWSClient)
pricingClient := pricing.NewFromConfig(*client.AWSClient)

pricingOutput, err := pricingClient.GetProducts(ctx, &pricing.GetProductsInput{
ServiceCode: aws.String("AmazonCloudWatch"),
Filters: []types.Filter{
{
Field: aws.String("regionCode"),
Value: aws.String(client.AWSClient.Region),
Type: types.FilterTypeTermMatch,
},
{
Field: aws.String("operation"),
Value: aws.String("MetricUpdate"),
Type: types.FilterTypeTermMatch,
},
},
MaxResults: aws.Int32(1),
})
if err != nil {
log.Printf("ERROR: Couldn't fetch pricing info for Metric Streams: %v", err)
return resources, err
}

costPerUpdate, err := getRate(pricingOutput)
if err != nil {
log.Printf("ERROR: Failed to calculate cost per month: %v", err)
return resources, err
}

input := &cloudwatch.ListMetricStreamsInput{}
for {
Expand All @@ -42,14 +107,31 @@ func MetricStreams(ctx context.Context, client providers.ProviderClient) ([]mode
}
}

statisticsOutput, err := cloudWatchMetricsClient.GetMetricStatistics(ctx, &cloudwatch.GetMetricStatisticsInput{
MetricName: aws.String("MetricUpdate"),
Namespace: aws.String("AWS/CloudWatch/MetricStreams"),
Dimensions: []cwTypes.Dimension{
{
Name: aws.String("MetricStreamName"),
Value: stream.Name,
},
},
})
if err != nil {
return resources, err
}

updateCount := *statisticsOutput.Datapoints[0].Sum
monthlyCost := costPerUpdate * updateCount

resources = append(resources, models.Resource{
Provider: "AWS",
Account: client.Name,
Service: "CloudWatch Metric Stream",
ResourceId: streamArn,
Region: client.AWSClient.Region,
Name: aws.ToString(stream.Name),
Cost: 0,
Cost: monthlyCost,
Tags: tags,
FetchedAt: time.Now(),
Link: fmt.Sprintf("https://%s.console.aws.amazon.com/cloudwatch/home?region=%s#metric-streams:streamsList/%s", client.AWSClient.Region, client.AWSClient.Region, aws.ToString(stream.Name)),
Expand Down