athenaintegration.go 16 KB

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