Last active
May 22, 2024 16:40
-
-
Save nickzelei/43486b05a7c58133a7aecd23f67de54d to your computer and use it in GitHub Desktop.
Prometheus Daily Counts
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
| package main | |
| import ( | |
| "context" | |
| "fmt" | |
| "log" | |
| "sort" | |
| "time" | |
| "github.com/prometheus/client_golang/api" | |
| v1 "github.com/prometheus/client_golang/api/prometheus/v1" | |
| "github.com/prometheus/common/model" | |
| ) | |
| func main() { | |
| // Create a new Prometheus API client | |
| client, err := api.NewClient(api.Config{ | |
| Address: "http://localhost:9090", | |
| }) | |
| if err != nil { | |
| log.Fatalf("Error creating Prometheus client: %v", err) | |
| } | |
| // Create a new v1 API instance | |
| v1api := v1.NewAPI(client) | |
| // Define the time range for the current month | |
| now := time.Now() | |
| start := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC) | |
| end := now | |
| // Define the context and the time range for the query | |
| ctx, cancel := context.WithTimeout(context.Background(), 300*time.Second) | |
| defer cancel() | |
| // Define the range for the query | |
| queryRange := v1.Range{ | |
| Start: start, | |
| End: end, | |
| Step: time.Minute * 5, // Query every 5 minutes | |
| } | |
| // Define the query to fetch raw values | |
| // query := `input_received_total{runId="683df805-5537-454a-8cc7-f6129ed6bb02-2024-05-21T22:23:42Z"}` | |
| // query := `input_received_total{jobId="41606319-5eee-4667-8dfd-b8b954b65bf9"}` | |
| query := `input_received_total{accountId="561b59fc-f582-4bdd-9e03-c6eb6ae27c5f"}` | |
| // Query Prometheus | |
| result, warnings, err := v1api.QueryRange(ctx, query, queryRange) | |
| if err != nil { | |
| log.Fatalf("Error querying Prometheus: %v", err) | |
| } | |
| if len(warnings) > 0 { | |
| log.Printf("Warnings: %v\n", warnings) | |
| } | |
| // Print the result | |
| // fmt.Printf("Result:\n%v\n", result) | |
| // Calculate the daily total count by summing increments manually | |
| dailyTotals := calculateDailyTotals(result) | |
| // Sort the dates and print the totals in ascending order | |
| printDailyTotals(dailyTotals) | |
| } | |
| func calculateDailyTotals(result model.Value) map[string]float64 { | |
| matrix, ok := result.(model.Matrix) | |
| if !ok || len(matrix) == 0 { | |
| return nil | |
| } | |
| dailyTotals := make(map[string]float64) | |
| for _, sampleStream := range matrix { | |
| if len(sampleStream.Values) == 1 { | |
| // If there's only one value, add it directly to the total count for that day | |
| timestamp := time.Unix(int64(sampleStream.Values[0].Timestamp)/1000, 0) | |
| day := timestamp.Format("2006-01-02") | |
| dailyTotals[day] += float64(sampleStream.Values[0].Value) | |
| } else { | |
| prevValue := float64(sampleStream.Values[0].Value) | |
| prevTimestamp := time.Unix(int64(sampleStream.Values[0].Timestamp)/1000, 0) | |
| prevDay := prevTimestamp.Format("2006-01-02") | |
| for i := 1; i < len(sampleStream.Values); i++ { | |
| currentValue := float64(sampleStream.Values[i].Value) | |
| currentTimestamp := time.Unix(int64(sampleStream.Values[i].Timestamp)/1000, 0) | |
| currentDay := currentTimestamp.Format("2006-01-02") | |
| if currentDay == prevDay { | |
| dailyTotals[currentDay] += currentValue - prevValue | |
| } else { | |
| dailyTotals[currentDay] += currentValue | |
| } | |
| prevValue = currentValue | |
| prevDay = currentDay | |
| } | |
| } | |
| } | |
| return dailyTotals | |
| } | |
| func printDailyTotals(dailyTotals map[string]float64) { | |
| // Collect the dates | |
| var dates []string | |
| for day := range dailyTotals { | |
| dates = append(dates, day) | |
| } | |
| // Sort the dates | |
| sort.Strings(dates) | |
| // Print the totals in ascending order | |
| fmt.Printf("Daily totals for the current month:\n") | |
| for _, day := range dates { | |
| fmt.Printf("%v: %v\n", day, dailyTotals[day]) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment