Skip to content

Commit

Permalink
feat: address lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulguptajss committed Aug 22, 2023
1 parent 6cac8fd commit 0593ec1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
63 changes: 63 additions & 0 deletions main/main.go
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

Check failure on line 24 in main/main.go

View workflow job for this annotation

GitHub Actions / lint

G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec)
//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) {

Check warning on line 38 in main/main.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'r' seems to be unused, consider removing or renaming it as _ (revive)
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

View workflow job for this annotation

GitHub Actions / lint

SA1019: rand.Seed has been deprecated since Go 1.20 and an alternative has been available since Go 1.0: As of Go 1.20 there is no reason to call Seed with a random value. Programs that call Seed with a known value to get a specific sequence of results should use New(NewSource(seed)) to obtain a local random generator. (staticcheck)

// 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)

Check failure on line 62 in main/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `http.ListenAndServe` is not checked (errcheck)
}
4 changes: 2 additions & 2 deletions pkg/dict/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package dict

import (
"github.com/netapp/harvest/v2/pkg/util"
"reflect"
"slices"
"strings"
)

Expand Down Expand Up @@ -129,7 +129,7 @@ func (d *Dict) CompareLabels(prev *Dict, labels []string) (*Dict, *Dict) {
old := New()
for key, val1 := range d.dict {
val2, ok := prev.dict[key]
if util.Contains(labels, key) && (!ok || !reflect.DeepEqual(val1, val2)) {
if slices.Contains(labels, key) && (!ok || !reflect.DeepEqual(val1, val2)) {
cur.dict[key] = val1
old.dict[key] = val2
}
Expand Down

0 comments on commit 0593ec1

Please sign in to comment.