Utilize metrics summary instead of dumping metrics through syscalls
This commit is contained in:
parent
569dbf18ce
commit
753acffec7
@ -1,8 +1,10 @@
|
|||||||
package events
|
package events
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@ -173,8 +175,8 @@ const APICall m = "api_call"
|
|||||||
|
|
||||||
// configurations
|
// configurations
|
||||||
const (
|
const (
|
||||||
reportInterval = 1 * time.Minute
|
reportInterval = 100 * time.Millisecond
|
||||||
retentionDuration = 2 * time.Minute
|
retentionDuration = 200 * time.Millisecond
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewMetrics embeds a metrics bus into the provided context. The bus can be
|
// NewMetrics embeds a metrics bus into the provided context. The bus can be
|
||||||
@ -186,28 +188,28 @@ func NewMetrics(ctx context.Context, w io.Writer) (context.Context, func()) {
|
|||||||
// logging rates; that's handled by dumpMetrics().
|
// logging rates; that's handled by dumpMetrics().
|
||||||
sink = metrics.NewInmemSink(reportInterval, retentionDuration)
|
sink = metrics.NewInmemSink(reportInterval, retentionDuration)
|
||||||
cfg = metrics.DefaultConfig("corso")
|
cfg = metrics.DefaultConfig("corso")
|
||||||
sig = metrics.NewInmemSignal(sink, sentSignal, w)
|
//sig = metrics.NewInmemSignal(sink, sentSignal, w)
|
||||||
)
|
)
|
||||||
|
|
||||||
cfg.EnableHostname = false
|
cfg.EnableHostname = false
|
||||||
cfg.EnableRuntimeMetrics = false
|
cfg.EnableRuntimeMetrics = true
|
||||||
|
|
||||||
gm, err := metrics.NewGlobal(cfg, sink)
|
gm, err := metrics.NewGlobal(cfg, sink)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.CtxErr(ctx, err).Error("metrics bus constructor")
|
logger.CtxErr(ctx, err).Error("metrics bus constructor")
|
||||||
sig.Stop()
|
//sig.Stop()
|
||||||
|
|
||||||
return ctx, func() {}
|
return ctx, func() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
stop := make(chan struct{})
|
stop := make(chan struct{})
|
||||||
go dumpMetrics(ctx, stop, sig)
|
go dumpMetrics(ctx, stop, sink)
|
||||||
|
|
||||||
flush := func() {
|
flush := func() {
|
||||||
signalDump(ctx)
|
signalDump(ctx)
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
close(stop)
|
close(stop)
|
||||||
sig.Stop()
|
//sig.Stop()
|
||||||
gm.Shutdown()
|
gm.Shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,19 +222,32 @@ func NewMetrics(ctx context.Context, w io.Writer) (context.Context, func()) {
|
|||||||
// it to dump metrics to the provided writer (which should be the logger).
|
// it to dump metrics to the provided writer (which should be the logger).
|
||||||
// Expectation is for users to call this in a goroutine. Any signal or close() on the stop chan
|
// Expectation is for users to call this in a goroutine. Any signal or close() on the stop chan
|
||||||
// will exit the loop.
|
// will exit the loop.
|
||||||
func dumpMetrics(ctx context.Context, stop <-chan struct{}, sig *metrics.InmemSignal) {
|
func dumpMetrics(ctx context.Context, stop <-chan struct{}, sink *metrics.InmemSink) {
|
||||||
tock := time.NewTicker(reportInterval)
|
tock := time.NewTicker(reportInterval)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-tock.C:
|
case <-tock.C:
|
||||||
signalDump(ctx)
|
//signalDump(ctx)
|
||||||
|
raw, _ := sink.DisplayMetrics(nil, nil)
|
||||||
|
result := raw.(metrics.MetricsSummary)
|
||||||
|
summary, _ := dumpMetricsSummary(result)
|
||||||
|
fmt.Fprintln(os.Stderr, string(summary))
|
||||||
|
|
||||||
case <-stop:
|
case <-stop:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dumpMetricsSummary(summary metrics.MetricsSummary) ([]byte, error) {
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
if err := json.NewEncoder(buf).Encode(summary); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Inc increments the given category by 1.
|
// Inc increments the given category by 1.
|
||||||
func Inc(cat m, keys ...string) {
|
func Inc(cat m, keys ...string) {
|
||||||
cats := append([]string{string(cat)}, keys...)
|
cats := append([]string{string(cat)}, keys...)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user