-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cac8fd
commit 0593ec1
Showing
2 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"net/http" | ||
"sort" | ||
"strings" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ( | ||
metrics string | ||
metricsMutex sync.RWMutex | ||
) | ||
|
||
func generateMetrics() { | ||
now := time.Now() | ||
metricsSlice := make([]string, 0, 10) | ||
|
||
for i := 0; i < 5; i++ { | ||
timestamp := now.Add(time.Duration(-i)*time.Minute).UnixNano() / int64(time.Millisecond) | ||
value1 := rand.Intn(500) + 100 // Random value between 100 and 599 | ||
//value2 := rand.Intn(500) + 100 // Random value between 100 and 599 | ||
metricsSlice = append(metricsSlice, fmt.Sprintf("http_requests_total{code=\"200\",service=\"user\",index=\"%d\"} %d %d", i, value1, timestamp)) | ||
//newMetrics += fmt.Sprintf("http_requests_total{code=\"500\",service=\"user%d\"} %d %d\n", i+1, value2, timestamp) | ||
|
||
} | ||
sort.Strings(metricsSlice) | ||
newMetrics := strings.Join(metricsSlice, "\n") + "\n" | ||
|
||
metricsMutex.Lock() | ||
metrics = newMetrics | ||
metricsMutex.Unlock() | ||
} | ||
|
||
func metricsHandler(w http.ResponseWriter, r *http.Request) { | ||
metricsMutex.RLock() | ||
defer metricsMutex.RUnlock() | ||
fmt.Fprint(w, metrics) | ||
} | ||
|
||
func main() { | ||
rand.Seed(time.Now().UnixNano()) // Seed the random number generator | ||
Check failure on line 45 in main/main.go GitHub Actions / lint
|
||
|
||
// Generate initial metrics | ||
generateMetrics() | ||
|
||
// Set up a ticker to generate new metrics every 5 minutes | ||
ticker := time.NewTicker(5 * time.Minute) | ||
go func() { | ||
for range ticker.C { | ||
fmt.Printf("generate") | ||
generateMetrics() | ||
} | ||
}() | ||
|
||
http.HandleFunc("/metrics", metricsHandler) | ||
|
||
fmt.Println("Starting server on :12990") | ||
http.ListenAndServe(":12990", nil) | ||
} |
This file contains 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