-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from yandex/expvar
publish some metrics to expvar (close #13)
- Loading branch information
Showing
5 changed files
with
145 additions
and
5 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,55 @@ | ||
package ammo | ||
|
||
import ( | ||
"expvar" | ||
"log" | ||
"strconv" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
type Counter struct { | ||
i int64 | ||
} | ||
|
||
func (c *Counter) String() string { | ||
return strconv.FormatInt(atomic.LoadInt64(&c.i), 10) | ||
} | ||
|
||
func (c *Counter) Add(delta int64) { | ||
atomic.AddInt64(&c.i, delta) | ||
} | ||
|
||
func (c *Counter) Set(value int64) { | ||
atomic.StoreInt64(&c.i, value) | ||
} | ||
|
||
func (c *Counter) Get() int64 { | ||
return atomic.LoadInt64(&c.i) | ||
} | ||
|
||
func NewCounter(name string) *Counter { | ||
v := &Counter{} | ||
expvar.Publish(name, v) | ||
return v | ||
} | ||
|
||
var ( | ||
evPassesLeft = NewCounter("ammo_PassesLeft") | ||
) | ||
|
||
func init() { | ||
go func() { | ||
passesLeft := evPassesLeft.Get() | ||
for _ = range time.NewTicker(1 * time.Second).C { | ||
if passesLeft < 0 { | ||
return // infinite number of passes | ||
} | ||
newPassesLeft := evPassesLeft.Get() | ||
if newPassesLeft != passesLeft { | ||
log.Printf("[AMMO] passes left: %d", newPassesLeft) | ||
passesLeft = newPassesLeft | ||
} | ||
} | ||
}() | ||
} |
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
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
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,73 @@ | ||
package engine | ||
|
||
import ( | ||
"expvar" | ||
"log" | ||
"strconv" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
type Counter struct { | ||
i int64 | ||
} | ||
|
||
func (c *Counter) String() string { | ||
return strconv.FormatInt(atomic.LoadInt64(&c.i), 10) | ||
} | ||
|
||
func (c *Counter) Add(delta int64) { | ||
atomic.AddInt64(&c.i, delta) | ||
} | ||
|
||
func (c *Counter) Set(value int64) { | ||
atomic.StoreInt64(&c.i, value) | ||
} | ||
|
||
func (c *Counter) Get() int64 { | ||
return atomic.LoadInt64(&c.i) | ||
} | ||
|
||
func NewCounter(name string) *Counter { | ||
v := &Counter{} | ||
expvar.Publish(name, v) | ||
return v | ||
} | ||
|
||
var ( | ||
evRequests = NewCounter("engine_Requests") | ||
evResponses = NewCounter("engine_Responses") | ||
evUsersStarted = NewCounter("engine_UsersStarted") | ||
evUsersFinished = NewCounter("engine_UsersFinished") | ||
) | ||
|
||
func init() { | ||
evReqPS := NewCounter("engine_ReqPS") | ||
evResPS := NewCounter("engine_ResPS") | ||
evActiveUsers := NewCounter("engine_ActiveUsers") | ||
evActiveRequests := NewCounter("engine_ActiveRequests") | ||
requests := evRequests.Get() | ||
responses := evResponses.Get() | ||
go func() { | ||
var requestsNew, responsesNew int64 | ||
for _ = range time.NewTicker(1 * time.Second).C { | ||
requestsNew = evRequests.Get() | ||
responsesNew = evResponses.Get() | ||
rps := responsesNew - responses | ||
reqps := requestsNew - requests | ||
activeUsers := evUsersStarted.Get() - evUsersFinished.Get() | ||
activeRequests := requestsNew - responsesNew | ||
log.Printf( | ||
"[ENGINE] %d resp/s; %d req/s; %d users; %d active\n", | ||
rps, reqps, activeUsers, activeRequests) | ||
|
||
requests = requestsNew | ||
responses = responsesNew | ||
|
||
evActiveUsers.Set(activeUsers) | ||
evActiveRequests.Set(activeRequests) | ||
evReqPS.Set(reqps) | ||
evResPS.Set(rps) | ||
} | ||
}() | ||
} |
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