Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix metrics value type #284

Merged
merged 4 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/jobs/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ func singleRequestJob(ctx context.Context, globalConfig GlobalConfig, args Args,
dataSize += len(key) + len(value)
}

metrics.Default.Write(metrics.Traffic, uuid.New().String(), dataSize)
metrics.Default.Write(metrics.Traffic, uuid.New().String(), uint64(dataSize))
err = sendFastHTTPRequest(client, req, resp, debug)
if err == nil {
metrics.Default.Write(metrics.ProcessedTraffic, uuid.New().String(), dataSize)
metrics.Default.Write(metrics.ProcessedTraffic, uuid.New().String(), uint64(dataSize))
}
headers := make(map[string]string)
resp.Header.VisitAll(func(key []byte, value []byte) {
Expand Down Expand Up @@ -156,13 +156,13 @@ func fastHTTPJob(ctx context.Context, globalConfig GlobalConfig, args Args, debu
dataSize += len(key) + len(value)
}

trafficMonitor.Add(dataSize)
trafficMonitor.Add(uint64(dataSize))
if err := sendFastHTTPRequest(client, req, nil, debug); err != nil {
if debug {
log.Printf("Error sending request %v: %v", req, err)
}
} else {
processedTrafficMonitor.Add(dataSize)
processedTrafficMonitor.Add(uint64(dataSize))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/jobs/packetgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func packetgenJob(ctx context.Context, globalConfig GlobalConfig, args Args, deb
protocolLabelValue,
metrics.StatusSuccess)

trafficMonitor.Add(len)
trafficMonitor.Add(uint64(len))
}

return nil, nil
Expand Down
6 changes: 3 additions & 3 deletions src/jobs/rawnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func tcpJob(ctx context.Context, globalConfig GlobalConfig, args Args, debug boo

body := []byte(templates.Execute(bodyTpl, nil))
_, err = conn.Write(body)
trafficMonitor.Add(len(body))
trafficMonitor.Add(uint64(len(body)))

if err != nil {
metrics.IncRawnetTCP(tcpAddr.String(), metrics.StatusFail)
Expand All @@ -81,7 +81,7 @@ func tcpJob(ctx context.Context, globalConfig GlobalConfig, args Args, debug boo
if debug {
log.Printf("%s finished at %d", jobConfig.Address, time.Now().Unix())
}
processedTrafficMonitor.Add(len(body))
processedTrafficMonitor.Add(uint64(len(body)))
metrics.IncRawnetTCP(tcpAddr.String(), metrics.StatusSuccess)
}
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func udpJob(ctx context.Context, globalConfig GlobalConfig, args Args, debug boo
log.Printf("%s failed at %d with err: %s", jobConfig.Address, time.Now().Unix(), err.Error())
}
} else {
trafficMonitor.Add(len(body))
trafficMonitor.Add(uint64(len(body)))
metrics.IncRawnetUDP(udpAddr.String(), metrics.StatusSuccess)

if debug {
Expand Down
20 changes: 10 additions & 10 deletions src/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ func init() {
Default.trackers[ProcessedTraffic] = &metricTracker{}
}

func (ms *Storage) Write(name, jobID string, value int) {
func (ms *Storage) Write(name, jobID string, value uint64) {
if tracker, ok := ms.trackers[name]; ok {
tracker.metrics.Store(jobID, value)
}
}

func (ms *Storage) Read(name string) int {
sum := 0
func (ms *Storage) Read(name string) uint64 {
sum := uint64(0)
if tracker, ok := ms.trackers[name]; ok {
tracker.metrics.Range(func(k, v interface{}) bool {
if value, ok := v.(int); ok {
if value, ok := v.(uint64); ok {
sum = sum + value
}
return true
Expand All @@ -84,16 +84,16 @@ type Writer struct {
ms *Storage
jobID string
name string
value int
value uint64
}

// Add used to increase metric value by a specific amount
func (w *Writer) Add(value int) {
func (w *Writer) Add(value uint64) {
w.value = w.value + value
}

// Set used to set metric to a specific value
func (w *Writer) Set(value int) {
func (w *Writer) Set(value uint64) {
w.value = value
}

Expand All @@ -102,9 +102,9 @@ func (w *Writer) Flush() {
w.ms.Write(w.name, w.jobID, w.value)
}

// Update updates writer with a set interval
func (w *Writer) Update(ctx context.Context, interval time.Duration) {
ticker := time.NewTicker(interval)
// Update updates writer with a set uint64erval
func (w *Writer) Update(ctx context.Context, uint64erval time.Duration) {
ticker := time.NewTicker(uint64erval)
defer ticker.Stop()
for {
select {
Expand Down