本文整理汇总了Golang中expvar.Do函数的典型用法代码示例。如果您正苦于以下问题:Golang Do函数的具体用法?Golang Do怎么用?Golang Do使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Do函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Cleanup
func (eb *Winlogbeat) Cleanup(b *beat.Beat) error {
logp.Debug("winlogbeat", "Dumping runtime metrics...")
expvar.Do(func(kv expvar.KeyValue) {
logp.Debug("winlogbeat", "%s=%s", kv.Key, kv.Value.String())
})
return nil
}
示例2: TestSchemaInfoQueryCache
func TestSchemaInfoQueryCache(t *testing.T) {
db := fakesqldb.Register()
for query, result := range getSchemaInfoTestSupportedQueries() {
db.AddQuery(query, result)
}
firstQuery := "select * from test_table_01"
secondQuery := "select * from test_table_02"
db.AddQuery("select * from test_table_01 where 1 != 1", &sqltypes.Result{})
db.AddQuery("select * from test_table_02 where 1 != 1", &sqltypes.Result{})
schemaInfo := newTestSchemaInfo(10, 10*time.Second, 10*time.Second, true)
dbaParams := sqldb.ConnParams{Engine: db.Name}
schemaInfo.Open(&dbaParams, true)
defer schemaInfo.Close()
ctx := context.Background()
logStats := newLogStats("GetPlanStats", ctx)
schemaInfo.SetQueryCacheCap(1)
firstPlan := schemaInfo.GetPlan(ctx, logStats, firstQuery)
if firstPlan == nil {
t.Fatalf("plan should not be nil")
}
secondPlan := schemaInfo.GetPlan(ctx, logStats, secondQuery)
if secondPlan == nil {
t.Fatalf("plan should not be nil")
}
expvar.Do(func(kv expvar.KeyValue) {
_ = kv.Value.String()
})
schemaInfo.ClearQueryPlanCache()
}
示例3: ExportVariablesWithTimestamp
// Export expvars to Cube right now. Use the provided timestamp for the
// submitted event. This function sends variables once and returns.
//
// You shouldn't need this function under normal circumstances. Use Run()
// instead.
func ExportVariablesWithTimestamp(collectionType string, putUrl string, timestamp time.Time) error {
variables := make([]string, 0)
expvar.Do(func(entry expvar.KeyValue) {
variables = append(variables, fmt.Sprintf("%q: %s", entry.Key, entry.Value))
})
request := fmt.Sprintf(
`[
{
"type": "%s",
"time": "%s",
"data": { %s }
}
]`,
collectionType,
timestamp.Format(time.ANSIC),
strings.Join(variables, ","))
response, err := http.Post(putUrl, "application/json", bytes.NewBufferString(request))
if err != nil {
log.Printf("Error POSTing events to Cube collector: %v", err)
log.Printf("The request we tried to post: %v", request)
return err
}
defer response.Body.Close()
return nil
}
示例4: StatsLogInterval
// By using StatsLog, you can print stats on stdout every second, which is sometimes handy to check the state
// of the server. The stats themselves are declared using the "expvar" package
// to use this function, just before starting your listeners, create a goroutine like this
// go logging.StatsLog()
func StatsLogInterval(seconds int) {
// If we are running in debug mode, do not clog the screen
if IsDebug() {
log.Println("disabling logger in debug mode")
return
}
log.Println("starting logger")
info := log.New(os.Stdout, "s:", log.Ldate|log.Ltime)
sleepTime := time.Duration(seconds) * time.Second
for _ = range time.Tick(sleepTime) {
var buffer bytes.Buffer
expvar.Do(func(k expvar.KeyValue) {
if strings.HasPrefix(k.Key, StatsPrefix) {
buffer.WriteString(fmt.Sprintf("[%s %s] ", strings.TrimLeft(k.Key, StatsPrefix), k.Value))
// reset stats every nseconds
if v, ok := (k.Value).(*expvar.Int); ok {
v.Set(0)
}
}
})
info.Println(buffer.String())
}
}
示例5: RegisterDebug
// RegisterDebug adds handlers for /debug/vars (expvar) and /debug/pprof (net/http/pprof) support
func (this *HttpWeb) RegisterDebug(m *martini.ClassicMartini) {
m.Get("/debug/vars", func(w http.ResponseWriter, r *http.Request) {
// from expvar.go, since the expvarHandler isn't exported :(
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "{\n")
first := true
expvar.Do(func(kv expvar.KeyValue) {
if !first {
fmt.Fprintf(w, ",\n")
}
first = false
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
})
fmt.Fprintf(w, "\n}\n")
})
// list all the /debug/ endpoints we want
m.Get("/debug/pprof", pprof.Index)
m.Get("/debug/pprof/cmdline", pprof.Cmdline)
m.Get("/debug/pprof/profile", pprof.Profile)
m.Get("/debug/pprof/symbol", pprof.Symbol)
m.Post("/debug/pprof/symbol", pprof.Symbol)
m.Get("/debug/pprof/block", pprof.Handler("block").ServeHTTP)
m.Get("/debug/pprof/heap", pprof.Handler("heap").ServeHTTP)
m.Get("/debug/pprof/goroutine", pprof.Handler("goroutine").ServeHTTP)
m.Get("/debug/pprof/threadcreate", pprof.Handler("threadcreate").ServeHTTP)
}
示例6: ExpvarHandler
// ExpvarHandler dumps json representation of expvars to http response.
//
// Expvars may be filtered by regexp provided via 'r' query argument.
//
// See https://golang.org/pkg/expvar/ for details.
func ExpvarHandler(ctx *fasthttp.RequestCtx) {
expvarHandlerCalls.Add(1)
ctx.Response.Reset()
r, err := getExpvarRegexp(ctx)
if err != nil {
expvarRegexpErrors.Add(1)
fmt.Fprintf(ctx, "Error when obtaining expvar regexp: %s", err)
ctx.SetStatusCode(fasthttp.StatusBadRequest)
return
}
fmt.Fprintf(ctx, "{\n")
first := true
expvar.Do(func(kv expvar.KeyValue) {
if r.MatchString(kv.Key) {
if !first {
fmt.Fprintf(ctx, ",\n")
}
first = false
fmt.Fprintf(ctx, "\t%q: %s", kv.Key, kv.Value)
}
})
fmt.Fprintf(ctx, "\n}\n")
ctx.SetContentType("application/json; charset=utf-8")
}
示例7: Status
func (daemon *SSEDaemon) Status(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "{\"status\":\"OK\"")
expvar.Do(func(kv expvar.KeyValue) {
fmt.Fprintf(w, ",%q:%s", kv.Key, kv.Value)
})
fmt.Fprintf(w, "}")
}
示例8: dumpMetrics
// dumpMetrics is used to log metrics on shutdown.
func dumpMetrics() {
logp.Info("Dumping runtime metrics...")
expvar.Do(func(kv expvar.KeyValue) {
if kv.Key != "memstats" {
logp.Info("%s=%s", kv.Key, kv.Value.String())
}
})
}
示例9: LogOnDebugLevel
// LogOnDebugLevel logs all the current metrics, if logging is on Debug level.
func LogOnDebugLevel() {
if log.GetLevel() == log.DebugLevel {
fields := log.Fields{}
expvar.Do(func(kv expvar.KeyValue) {
fields[kv.Key] = kv.Value
})
logger.WithFields(fields).Debug("current values of metrics")
}
}
示例10: Cleanup
// Cleanup performs clean-up after Run completes.
func (bt *Metricbeat) Cleanup(b *beat.Beat) error {
logp.Info("Dumping runtime metrics...")
expvar.Do(func(kv expvar.KeyValue) {
if kv.Key != "memstats" {
logp.Info("%s=%s", kv.Key, kv.Value.String())
}
})
return nil
}
示例11: snapshotExpvars
// snapshotExpvars iterates through all the defined expvars, and for the vars
// that are integers it snapshots the name and value in a separate (flat) map.
func snapshotExpvars(varsMap map[string]int64) {
expvar.Do(func(kv expvar.KeyValue) {
switch kv.Value.(type) {
case *expvar.Int:
varsMap[kv.Key], _ = strconv.ParseInt(kv.Value.String(), 10, 64)
case *expvar.Map:
snapshotMap(varsMap, kv.Key, kv.Value.(*expvar.Map))
}
})
}
示例12: printVars
func printVars() {
log.Println("expvars:")
expvar.Do(func(kv expvar.KeyValue) {
switch kv.Key {
case "memstats":
// Do nothing, this is a big output.
default:
log.Printf("\t%s -> %s", kv.Key, kv.Value)
}
})
}
示例13: cleanup
// cleanup attempts to remove any files or data it may have created which should
// not be persisted.
func (eb *Winlogbeat) cleanup(b *beat.Beat) {
logp.Info("Dumping runtime metrics...")
expvar.Do(func(kv expvar.KeyValue) {
logf := logp.Info
if kv.Key == "memstats" {
logf = memstatsf
}
logf("%s=%s", kv.Key, kv.Value.String())
})
}
示例14: writeMetrics
func writeMetrics(w io.Writer) {
fmt.Fprintf(w, "{\n")
first := true
expvar.Do(func(kv expvar.KeyValue) {
if !first {
fmt.Fprintf(w, ",\n")
}
first = false
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
})
fmt.Fprintf(w, "\n}\n")
}
示例15: do
func do(f func(expvar.KeyValue)) {
expvar.Do(func(kv expvar.KeyValue) {
switch kv.Key {
case "cmdline",
"memstats":
return
}
if _, ok := kv.Value.(*expvar.Int); !ok {
return
}
f(kv)
})
}