Skip to content

Commit

Permalink
ensure data is scraped at least once during startup and skip sending …
Browse files Browse the repository at this point in the history
…data when at least one database return broken results
  • Loading branch information
[email protected] committed Jun 24, 2024
1 parent 20b18ac commit c6c341e
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions pkg/cmd/spks.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,13 @@ func SpksCMD(allMetrics map[string]map[string]prometheus.Counter, ctx context.Co
startYesterdayAbsolute := time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, location).In(time.UTC)
endYesterdayAbsolute := time.Date(now.Year(), now.Month(), now.Day()-1, 23, 59, 59, 0, location).In(time.UTC)

logger.Info("Getting specific metric from thanos")

mariadbStandard, mariadbPremium, redisStandard, redisPremium := getDatabasesCounts(prometheusURL, prometheusQueryArr, logger, startOfToday, allMetrics)

odooClient := odoo.NewOdooAPIClient(c.Context, odooURL, odooOauthTokenURL, odooClientId, odooClientSecret, logger, allMetrics["odooMetrics"])

billingRecords := generateBillingRecords(salesOrder, UnitID, startYesterdayAbsolute, endYesterdayAbsolute, mariadbStandard, mariadbPremium, redisStandard, redisPremium)

ticker := time.NewTicker(24 * time.Second)

// ensure first data export after restart
// I want to avoid situation when data is not exported because of constant restarts
err = odooClient.SendData(billingRecords)
if err != nil {
logger.Error(err, "Error sending data to Odoo API")
}
firstTime := make(chan bool, 1)
firstTime <- true

for {
select {
case <-ctxx.Done():
Expand All @@ -95,6 +86,25 @@ func SpksCMD(allMetrics map[string]map[string]prometheus.Counter, ctx context.Co
// this runs every 24 hours after program start
mariadbStandard, mariadbPremium, redisStandard, redisPremium := getDatabasesCounts(prometheusURL, prometheusQueryArr, logger, startOfToday, allMetrics)

if mariadbStandard == -1 || mariadbPremium == -1 || redisStandard == -1 || redisPremium == -1 {
logger.Error(err, "At least one database query failed, skipping data export to Odoo API")
continue
}

billingRecords := generateBillingRecords(salesOrder, UnitID, startYesterdayAbsolute, endYesterdayAbsolute, mariadbStandard, mariadbPremium, redisStandard, redisPremium)

err = odooClient.SendData(billingRecords)
if err != nil {
logger.Error(err, "Error sending data to Odoo API")
}
case <-firstTime:
mariadbStandard, mariadbPremium, redisStandard, redisPremium := getDatabasesCounts(prometheusURL, prometheusQueryArr, logger, startOfToday, allMetrics)

if mariadbStandard == -1 || mariadbPremium == -1 || redisStandard == -1 || redisPremium == -1 {
logger.Error(err, "At least one database query failed, skipping data export to Odoo API")
continue
}

billingRecords := generateBillingRecords(salesOrder, UnitID, startYesterdayAbsolute, endYesterdayAbsolute, mariadbStandard, mariadbPremium, redisStandard, redisPremium)

err = odooClient.SendData(billingRecords)
Expand Down Expand Up @@ -186,7 +196,7 @@ func QueryPrometheus(ctx context.Context, v1api v1.API, query string, logger log
if err != nil {
providerMetrics["providerFailed"].Inc()
logger.Error(err, "Error querying Prometheus")
return 0
return -1
}

providerMetrics["providerSucceeded"].Inc()
Expand All @@ -200,11 +210,11 @@ func QueryPrometheus(ctx context.Context, v1api v1.API, query string, logger log
vectorVal := result.(model.Vector)
if len(vectorVal) != 1 {
logger.Error(err, "Result vector length is not 1, prometheus query failed and returned: ", "result", vectorVal)
return 0
return -1
}
default:
logger.Error(err, "result type is not Vector: ", "result", result)
return 0
return -1

}
return int(result.(model.Vector)[0].Value)
Expand Down

0 comments on commit c6c341e

Please sign in to comment.