athenaintegration.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. package aws
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/aws/aws-sdk-go-v2/service/athena/types"
  9. "github.com/opencost/opencost/core/pkg/log"
  10. "github.com/opencost/opencost/core/pkg/opencost"
  11. "github.com/opencost/opencost/pkg/cloud"
  12. )
  13. const LabelColumnPrefix = "resource_tags_user_"
  14. const AWSLabelColumnPrefix = "resource_tags_aws_"
  15. const AthenaResourceTagPrefix = "resource_tags_"
  16. // athenaDateLayout is the default AWS date format
  17. const AthenaDateLayout = "2006-01-02 15:04:05.000"
  18. // Cost Columns
  19. const AthenaPricingColumn = "line_item_unblended_cost"
  20. // Amortized Cost Columns
  21. const AthenaRIPricingColumn = "reservation_effective_cost"
  22. const AthenaSPPricingColumn = "savings_plan_savings_plan_effective_cost"
  23. // Net Cost Columns
  24. const AthenaNetPricingColumn = "line_item_net_unblended_cost"
  25. var AthenaNetPricingCoalesce = fmt.Sprintf("COALESCE(%s, %s, 0)", AthenaNetPricingColumn, AthenaPricingColumn)
  26. // Amortized Net Cost Columns
  27. const AthenaNetRIPricingColumn = "reservation_net_effective_cost"
  28. var AthenaNetRIPricingCoalesce = fmt.Sprintf("COALESCE(%s, %s, 0)", AthenaNetRIPricingColumn, AthenaRIPricingColumn)
  29. const AthenaNetSPPricingColumn = "savings_plan_net_savings_plan_effective_cost"
  30. var AthenaNetSPPricingCoalesce = fmt.Sprintf("COALESCE(%s, %s, 0)", AthenaNetSPPricingColumn, AthenaSPPricingColumn)
  31. // athenaDateTruncColumn Aggregates line items from the hourly level to daily. "line_item_usage_start_date" is used because at
  32. // all time values 00:00-23:00 it will truncate to the correct date.
  33. const AthenaDateColumn = "line_item_usage_start_date"
  34. const AthenaDateTruncColumn = "DATE_TRUNC('day'," + AthenaDateColumn + ") as usage_date"
  35. const AthenaWhereDateFmt = `line_item_usage_start_date >= date '%s' AND line_item_usage_start_date < date '%s'`
  36. const AthenaWhereUsage = "(line_item_line_item_type = 'Usage' OR line_item_line_item_type = 'DiscountedUsage' OR line_item_line_item_type = 'SavingsPlanCoveredUsage' OR line_item_line_item_type = 'EdpDiscount' OR line_item_line_item_type = 'PrivateRateDiscount')"
  37. // AthenaQueryIndexes is a struct for holding the context of a query
  38. type AthenaQueryIndexes struct {
  39. Query string
  40. ColumnIndexes map[string]int
  41. TagColumns []string
  42. AWSTagColumns []string
  43. ListCostColumn string
  44. NetCostColumn string
  45. AmortizedNetCostColumn string
  46. AmortizedCostColumn string
  47. IsK8sColumn string
  48. }
  49. type AthenaIntegration struct {
  50. AthenaQuerier
  51. }
  52. // Query Athena for CUR data and build a new CloudCostSetRange containing the info
  53. func (ai *AthenaIntegration) GetCloudCost(start, end time.Time) (*opencost.CloudCostSetRange, error) {
  54. log.Infof("AthenaIntegration[%s]: GetCloudCost: %s", ai.Key(), opencost.NewWindow(&start, &end).String())
  55. // Query for all column names
  56. allColumns, err := ai.GetColumns()
  57. if err != nil {
  58. return nil, fmt.Errorf("GetCloudCost: error getting Athena columns: %w", err)
  59. }
  60. // List known, hard-coded columns to query
  61. groupByColumns := []string{
  62. AthenaDateTruncColumn,
  63. "line_item_resource_id",
  64. "bill_payer_account_id",
  65. "line_item_usage_account_id",
  66. "line_item_product_code",
  67. "line_item_usage_type",
  68. "product_region_code",
  69. "line_item_availability_zone",
  70. }
  71. // Create query indices
  72. aqi := AthenaQueryIndexes{}
  73. // Add is k8s column
  74. isK8sColumn := ai.GetIsKubernetesColumn(allColumns)
  75. groupByColumns = append(groupByColumns, isK8sColumn)
  76. aqi.IsK8sColumn = isK8sColumn
  77. // Determine which columns are user-defined tags and add those to the list
  78. // of columns to query.
  79. for column := range allColumns {
  80. if strings.HasPrefix(column, LabelColumnPrefix) {
  81. quotedTag := fmt.Sprintf(`"%s"`, column)
  82. groupByColumns = append(groupByColumns, quotedTag)
  83. aqi.TagColumns = append(aqi.TagColumns, quotedTag)
  84. }
  85. if strings.HasPrefix(column, AWSLabelColumnPrefix) {
  86. groupByColumns = append(groupByColumns, column)
  87. aqi.AWSTagColumns = append(aqi.AWSTagColumns, column)
  88. }
  89. }
  90. var selectColumns []string
  91. // Duplicate GroupBy Columns into select columns
  92. selectColumns = append(selectColumns, groupByColumns...)
  93. // Clean Up group by columns
  94. ai.RemoveColumnAliases(groupByColumns)
  95. // Build list cost column and add it to the select columns
  96. listCostColumn := ai.GetListCostColumn()
  97. selectColumns = append(selectColumns, listCostColumn)
  98. aqi.ListCostColumn = listCostColumn
  99. // Build net cost column and add it to select columns
  100. netCostColumn := ai.GetNetCostColumn(allColumns)
  101. selectColumns = append(selectColumns, netCostColumn)
  102. aqi.NetCostColumn = netCostColumn
  103. // Build amortized net cost column and add it to select columns
  104. amortizedNetCostColumn := ai.GetAmortizedNetCostColumn(allColumns)
  105. selectColumns = append(selectColumns, amortizedNetCostColumn)
  106. aqi.AmortizedNetCostColumn = amortizedNetCostColumn
  107. // Build Amortized cost column and add it to select columns
  108. amortizedCostColumn := ai.GetAmortizedCostColumn(allColumns)
  109. selectColumns = append(selectColumns, amortizedCostColumn)
  110. aqi.AmortizedCostColumn = amortizedCostColumn
  111. // Build map of query columns to use for parsing query
  112. aqi.ColumnIndexes = map[string]int{}
  113. for i, column := range selectColumns {
  114. aqi.ColumnIndexes[column] = i
  115. }
  116. whereDate := fmt.Sprintf(AthenaWhereDateFmt, start.Format("2006-01-02"), end.Format("2006-01-02"))
  117. wherePartitions := ai.GetPartitionWhere(start, end)
  118. // Query for all line items with a resource_id or from AWS Marketplace, which did not end before
  119. // the range or start after it. This captures all costs with any amount of
  120. // overlap with the range, for which we will only extract the relevant costs
  121. whereConjuncts := []string{
  122. wherePartitions,
  123. whereDate,
  124. AthenaWhereUsage,
  125. }
  126. columnStr := strings.Join(selectColumns, ", ")
  127. whereClause := strings.Join(whereConjuncts, " AND ")
  128. groupByStr := strings.Join(groupByColumns, ", ")
  129. queryStr := `
  130. SELECT %s
  131. FROM "%s"
  132. WHERE %s
  133. GROUP BY %s
  134. `
  135. aqi.Query = fmt.Sprintf(queryStr, columnStr, ai.Table, whereClause, groupByStr)
  136. ccsr, err := opencost.NewCloudCostSetRange(start, end, opencost.AccumulateOptionDay, ai.Key())
  137. if err != nil {
  138. return nil, err
  139. }
  140. // Generate row handling function.
  141. rowHandler := func(row types.Row) {
  142. err2 := ai.RowToCloudCost(row, aqi, ccsr)
  143. if err2 != nil {
  144. log.Errorf("AthenaIntegration: GetCloudCost: error while parsing row: %s", err2.Error())
  145. }
  146. }
  147. log.Debugf("AthenaIntegration[%s]: GetCloudCost: querying: %s", ai.Key(), aqi.Query)
  148. // Query CUR data and fill out CCSR
  149. err = ai.Query(context.TODO(), aqi.Query, GetAthenaQueryFunc(rowHandler))
  150. if err != nil {
  151. return nil, err
  152. }
  153. ai.ConnectionStatus = ai.GetConnectionStatusFromResult(ccsr, ai.ConnectionStatus)
  154. return ccsr, nil
  155. }
  156. func (ai *AthenaIntegration) GetListCostColumn() string {
  157. var listCostBuilder strings.Builder
  158. listCostBuilder.WriteString("CASE line_item_line_item_type")
  159. listCostBuilder.WriteString(" WHEN 'EdpDiscount' THEN 0")
  160. listCostBuilder.WriteString(" WHEN 'PrivateRateDiscount' THEN 0")
  161. listCostBuilder.WriteString(" ELSE ")
  162. listCostBuilder.WriteString(AthenaPricingColumn)
  163. listCostBuilder.WriteString(" END")
  164. return fmt.Sprintf("SUM(%s) as list_cost", listCostBuilder.String())
  165. }
  166. func (ai *AthenaIntegration) GetNetCostColumn(allColumns map[string]bool) string {
  167. netCostColumn := ""
  168. if allColumns[AthenaNetPricingColumn] { // if Net pricing exists
  169. netCostColumn = AthenaNetPricingCoalesce
  170. } else { // Non-net for if there's no net pricing.
  171. netCostColumn = AthenaPricingColumn
  172. }
  173. return fmt.Sprintf("SUM(%s) as net_cost", netCostColumn)
  174. }
  175. func (ai *AthenaIntegration) GetAmortizedCostColumn(allColumns map[string]bool) string {
  176. amortizedCostCase := ai.GetAmortizedCostCase(allColumns)
  177. return fmt.Sprintf("SUM(%s) as amortized_cost", amortizedCostCase)
  178. }
  179. func (ai *AthenaIntegration) GetAmortizedNetCostColumn(allColumns map[string]bool) string {
  180. amortizedNetCostCase := ""
  181. if allColumns[AthenaNetPricingColumn] { // if Net pricing exists
  182. amortizedNetCostCase = ai.GetAmortizedNetCostCase(allColumns)
  183. } else { // Non-net for if there's no net pricing.
  184. amortizedNetCostCase = ai.GetAmortizedCostCase(allColumns)
  185. }
  186. return fmt.Sprintf("SUM(%s) as amortized_net_cost", amortizedNetCostCase)
  187. }
  188. func (ai *AthenaIntegration) GetAmortizedCostCase(allColumns map[string]bool) string {
  189. // Use unblended costs if Reserved Instances/Savings Plans aren't in use
  190. if !allColumns[AthenaRIPricingColumn] && !allColumns[AthenaSPPricingColumn] {
  191. return AthenaPricingColumn
  192. }
  193. var costBuilder strings.Builder
  194. costBuilder.WriteString("CASE line_item_line_item_type")
  195. if allColumns[AthenaRIPricingColumn] {
  196. costBuilder.WriteString(" WHEN 'DiscountedUsage' THEN ")
  197. costBuilder.WriteString(AthenaRIPricingColumn)
  198. }
  199. if allColumns[AthenaSPPricingColumn] {
  200. costBuilder.WriteString(" WHEN 'SavingsPlanCoveredUsage' THEN ")
  201. costBuilder.WriteString(AthenaSPPricingColumn)
  202. }
  203. costBuilder.WriteString(" ELSE ")
  204. costBuilder.WriteString(AthenaPricingColumn)
  205. costBuilder.WriteString(" END")
  206. return costBuilder.String()
  207. }
  208. func (ai *AthenaIntegration) GetAmortizedNetCostCase(allColumns map[string]bool) string {
  209. // Use net unblended costs if Reserved Instances/Savings Plans aren't in use
  210. if !allColumns[AthenaNetRIPricingColumn] && !allColumns[AthenaNetSPPricingColumn] {
  211. return AthenaNetPricingCoalesce
  212. }
  213. var costBuilder strings.Builder
  214. costBuilder.WriteString("CASE line_item_line_item_type")
  215. if allColumns[AthenaNetRIPricingColumn] {
  216. costBuilder.WriteString(" WHEN 'DiscountedUsage' THEN ")
  217. costBuilder.WriteString(AthenaNetRIPricingCoalesce)
  218. }
  219. if allColumns[AthenaNetSPPricingColumn] {
  220. costBuilder.WriteString(" WHEN 'SavingsPlanCoveredUsage' THEN ")
  221. costBuilder.WriteString(AthenaNetSPPricingCoalesce)
  222. }
  223. costBuilder.WriteString(" ELSE ")
  224. costBuilder.WriteString(AthenaNetPricingCoalesce)
  225. costBuilder.WriteString(" END")
  226. return costBuilder.String()
  227. }
  228. func (ai *AthenaIntegration) RemoveColumnAliases(columns []string) {
  229. for i, column := range columns {
  230. if strings.Contains(column, " as ") {
  231. columnValues := strings.Split(column, " as ")
  232. columns[i] = columnValues[0]
  233. }
  234. }
  235. }
  236. func (ai *AthenaIntegration) ConvertLabelToAWSTag(label string) string {
  237. // if the label already has the column prefix assume that it is in the correct format
  238. if strings.HasPrefix(label, LabelColumnPrefix) {
  239. return label
  240. }
  241. // replace characters with underscore
  242. tag := label
  243. tag = strings.ReplaceAll(tag, ".", "_")
  244. tag = strings.ReplaceAll(tag, "/", "_")
  245. tag = strings.ReplaceAll(tag, ":", "_")
  246. tag = strings.ReplaceAll(tag, "-", "_")
  247. // add prefix and return
  248. return LabelColumnPrefix + tag
  249. }
  250. // GetIsKubernetesColumn builds a column that determines if a row represents kubernetes spend
  251. func (ai *AthenaIntegration) GetIsKubernetesColumn(allColumns map[string]bool) string {
  252. disjuncts := []string{
  253. "line_item_product_code = 'AmazonEKS'", // EKS is always kubernetes
  254. }
  255. // tagColumns is a list of columns where the presence of a value indicates that a resource is part of a kubernetes cluster
  256. tagColumns := []string{
  257. "resource_tags_aws_eks_cluster_name",
  258. "resource_tags_user_eks_cluster_name",
  259. "resource_tags_user_alpha_eksctl_io_cluster_name",
  260. "resource_tags_user_kubernetes_io_service_name",
  261. "resource_tags_user_kubernetes_io_created_for_pvc_name",
  262. "resource_tags_user_kubernetes_io_created_for_pv_name",
  263. }
  264. for _, tagColumn := range tagColumns {
  265. // if tag column is present in the CUR check for it
  266. if _, ok := allColumns[tagColumn]; ok {
  267. disjunctStr := fmt.Sprintf("%s <> ''", tagColumn)
  268. disjuncts = append(disjuncts, disjunctStr)
  269. }
  270. }
  271. return fmt.Sprintf("(%s) as is_kubernetes", strings.Join(disjuncts, " OR "))
  272. }
  273. func (ai *AthenaIntegration) GetPartitionWhere(start, end time.Time) string {
  274. month := time.Date(start.Year(), start.Month(), 1, 0, 0, 0, 0, time.UTC)
  275. endMonth := time.Date(end.Year(), end.Month(), 1, 0, 0, 0, 0, time.UTC)
  276. var disjuncts []string
  277. for !month.After(endMonth) {
  278. disjuncts = append(disjuncts, fmt.Sprintf("(year = '%d' AND month = '%d')", month.Year(), month.Month()))
  279. month = month.AddDate(0, 1, 0)
  280. }
  281. str := fmt.Sprintf("(%s)", strings.Join(disjuncts, " OR "))
  282. return str
  283. }
  284. func (ai *AthenaIntegration) RowToCloudCost(row types.Row, aqi AthenaQueryIndexes, ccsr *opencost.CloudCostSetRange) error {
  285. if len(row.Data) < len(aqi.ColumnIndexes) {
  286. return fmt.Errorf("rowToCloudCost: row with fewer than %d columns (has only %d)", len(aqi.ColumnIndexes), len(row.Data))
  287. }
  288. // Iterate through the slice of tag columns, assigning
  289. // values to the column names, minus the tag prefix.
  290. labels := opencost.CloudCostLabels{}
  291. for _, tagColumnName := range aqi.TagColumns {
  292. // remove quotes
  293. labelName := strings.TrimPrefix(tagColumnName, `"`)
  294. labelName = strings.TrimSuffix(labelName, `"`)
  295. // remove prefix
  296. labelName = strings.TrimPrefix(labelName, LabelColumnPrefix)
  297. value := GetAthenaRowValue(row, aqi.ColumnIndexes, tagColumnName)
  298. if value != "" {
  299. labels[labelName] = value
  300. }
  301. }
  302. for _, awsColumnName := range aqi.AWSTagColumns {
  303. // partially remove prefix leaving "aws_"
  304. labelName := strings.TrimPrefix(awsColumnName, AthenaResourceTagPrefix)
  305. value := GetAthenaRowValue(row, aqi.ColumnIndexes, awsColumnName)
  306. if value != "" {
  307. labels[labelName] = value
  308. }
  309. }
  310. invoiceEntityID := GetAthenaRowValue(row, aqi.ColumnIndexes, "bill_payer_account_id")
  311. accountID := GetAthenaRowValue(row, aqi.ColumnIndexes, "line_item_usage_account_id")
  312. startStr := GetAthenaRowValue(row, aqi.ColumnIndexes, AthenaDateTruncColumn)
  313. providerID := GetAthenaRowValue(row, aqi.ColumnIndexes, "line_item_resource_id")
  314. productCode := GetAthenaRowValue(row, aqi.ColumnIndexes, "line_item_product_code")
  315. usageType := GetAthenaRowValue(row, aqi.ColumnIndexes, "line_item_usage_type")
  316. regionCode := GetAthenaRowValue(row, aqi.ColumnIndexes, "product_region_code")
  317. availabilityZone := GetAthenaRowValue(row, aqi.ColumnIndexes, "line_item_availability_zone")
  318. isK8s, _ := strconv.ParseBool(GetAthenaRowValue(row, aqi.ColumnIndexes, aqi.IsK8sColumn))
  319. k8sPct := 0.0
  320. if isK8s {
  321. k8sPct = 1.0
  322. }
  323. listCost, err := GetAthenaRowValueFloat(row, aqi.ColumnIndexes, aqi.ListCostColumn)
  324. if err != nil {
  325. return err
  326. }
  327. netCost, err := GetAthenaRowValueFloat(row, aqi.ColumnIndexes, aqi.NetCostColumn)
  328. if err != nil {
  329. return err
  330. }
  331. amortizedNetCost, err := GetAthenaRowValueFloat(row, aqi.ColumnIndexes, aqi.AmortizedNetCostColumn)
  332. if err != nil {
  333. return err
  334. }
  335. amortizedCost, err := GetAthenaRowValueFloat(row, aqi.ColumnIndexes, aqi.AmortizedCostColumn)
  336. if err != nil {
  337. return err
  338. }
  339. // Identify resource category in the CUR
  340. category := SelectAWSCategory(providerID, usageType, productCode)
  341. // Retrieve final stanza of product code for ProviderID
  342. if productCode == "AWSELB" || productCode == "AmazonFSx" {
  343. providerID = ParseARN(providerID)
  344. }
  345. if productCode == "AmazonEKS" && category == opencost.ComputeCategory {
  346. if strings.Contains(usageType, "CPU") {
  347. providerID = fmt.Sprintf("%s/CPU", providerID)
  348. } else if strings.Contains(usageType, "GB") {
  349. providerID = fmt.Sprintf("%s/RAM", providerID)
  350. }
  351. }
  352. properties := opencost.CloudCostProperties{
  353. ProviderID: providerID,
  354. Provider: opencost.AWSProvider,
  355. AccountID: accountID,
  356. AccountName: accountID,
  357. InvoiceEntityID: invoiceEntityID,
  358. InvoiceEntityName: invoiceEntityID,
  359. RegionID: regionCode,
  360. AvailabilityZone: availabilityZone,
  361. Service: productCode,
  362. Category: category,
  363. Labels: labels,
  364. }
  365. start, err := time.Parse(AthenaDateLayout, startStr)
  366. if err != nil {
  367. return fmt.Errorf("unable to parse %s: '%s'", AthenaDateTruncColumn, err.Error())
  368. }
  369. end := start.AddDate(0, 0, 1)
  370. cc := &opencost.CloudCost{
  371. Properties: &properties,
  372. Window: opencost.NewWindow(&start, &end),
  373. ListCost: opencost.CostMetric{
  374. Cost: listCost,
  375. KubernetesPercent: k8sPct,
  376. },
  377. NetCost: opencost.CostMetric{
  378. Cost: netCost,
  379. KubernetesPercent: k8sPct,
  380. },
  381. AmortizedNetCost: opencost.CostMetric{
  382. Cost: amortizedNetCost,
  383. KubernetesPercent: k8sPct,
  384. },
  385. AmortizedCost: opencost.CostMetric{
  386. Cost: amortizedCost,
  387. KubernetesPercent: k8sPct,
  388. },
  389. InvoicedCost: opencost.CostMetric{
  390. Cost: netCost, // We are using Net Cost for Invoiced Cost for now as it is the closest approximation
  391. KubernetesPercent: k8sPct,
  392. },
  393. }
  394. ccsr.LoadCloudCost(cc)
  395. return nil
  396. }
  397. func (ai *AthenaIntegration) GetConnectionStatusFromResult(result cloud.EmptyChecker, currentStatus cloud.ConnectionStatus) cloud.ConnectionStatus {
  398. if result.IsEmpty() && currentStatus != cloud.SuccessfulConnection {
  399. return cloud.MissingData
  400. }
  401. return cloud.SuccessfulConnection
  402. }