Golang expvar.Get类(方法)实例源码

下面列出了Golang expvar.Get 类(方法)源码代码实例,从而了解它的用法。

作者:KarolBedkowsk    项目:secprox   
func statsPageHandler(w http.ResponseWriter, r *http.Request, bctx *BasePageContext) {
	ctx := &struct {
		*BasePageContext
		Stats []*stat
	}{
		BasePageContext: bctx,
		Stats:           []*stat{},
	}

	var stats *expvar.Map
	stats = expvar.Get("counters").(*expvar.Map)
	servStat := expvar.Get("states").(*expvar.Map)
	errors := expvar.Get("errors").(*expvar.Map)

	for _, ep := range bctx.Globals.GetEndpoints() {
		epname := ep.Name
		all := stats.Get(epname)
		success := stats.Get(epname + "|pass")
		unauth := stats.Get(epname + "|401")
		fail := stats.Get(epname + "|403")
		status := servStat.Get(epname)
		statusSSL := servStat.Get(epname + "|ssl")
		err := errors.Get(epname)
		errSSL := errors.Get(epname + "|ssl")
		ctx.Stats = append(ctx.Stats, &stat{epname, fail, success, unauth, all, status, statusSSL,
			err, errSSL})
	}

	RenderTemplateStd(w, ctx, "stats.tmpl")
}

作者:yongleho    项目:hook   
func GetStatus(w rest.ResponseWriter, r *rest.Request) {
	// b := GetBase(r)
	status := make(map[string]string)
	status["status"] = "ok"
	status["attemptsError"] = expvar.Get("attemptsError").String()
	status["attemptsSuccess"] = expvar.Get("attemptsSuccess").String()
	w.WriteJson(status)
}

作者:patrickToc    项目:xstat   
func TestNew(t *testing.T) {
	// Publishes prefix in expvar, panics the second time
	assert.Nil(t, expvar.Get("name"))
	New("name")
	assert.NotNil(t, expvar.Get("name"))
	assert.IsType(t, expvar.Get("name"), &expvar.Map{})
	assert.Panics(t, func() {
		New("name")
	})
}

作者:CowLe    项目:vites   
func TestPublished(t *testing.T) {
	l, err := Listen("")
	if err != nil {
		t.Fatal(err)
	}
	opened := make(chan struct{})
	closed := make(chan struct{})
	go func() {
		for {
			conn, err := l.Accept()
			opened <- struct{}{}
			if err != nil {
				t.Fatal(err)
			}
			go func() {
				b := make([]byte, 100)
				for {
					_, err := conn.Read(b)
					if err != nil {
						conn.Close()
						closed <- struct{}{}
						return
					}
				}
			}()
		}
	}()

	addr := l.Addr().String()
	for i := 1; i <= 3; i++ {
		conn1, err := net.Dial("tcp", addr)
		if err != nil {
			t.Fatal(err)
		}
		<-opened
		if v := expvar.Get("ConnCount").String(); v != "1" {
			t.Errorf("ConnCount: %v, want 1", v)
		}
		conn1.Close()
		<-closed
		if v := expvar.Get("ConnCount").String(); v != "0" {
			t.Errorf("ConnCount: %v, want 1", v)
		}
		if v := expvar.Get("ConnAccepted").String(); v != fmt.Sprintf("%d", i) {
			t.Errorf("ConnAccepted: %v, want %d", v, i)
		}
	}
}

作者:millke    项目:kama   
func wsServer(ws *websocket.Conn) {
	var buf string
	defer func() {
		if err := ws.Close(); err != nil {
			log.Println("Websocket could not be closed", err.Error())
		} else {
			log.Println("Websocket closed")
		}
	}()
	//q := ws.Request().URL.Query()
	//name := q.Get("name")
	stopped := false
	ticker := time.Tick(time.Duration(1) * time.Second)
	for !stopped {
		select {
		case <-ticker:
			val := expvar.Get(metricsVar)
			if val == nil {
				buf = ""
			} else {
				buf = val.String()
			}
			_, err := ws.Write([]byte(buf))
			if err != nil {
				log.Printf("Websocket error: %s\n", err.Error())
				stopped = true
			}

		}
	}
}

作者:zj848    项目:go-collect   
func TestDerive(t *testing.T) {
	d := NewDerive(api.Identifier{
		Host:   "example.com",
		Plugin: "golang",
		Type:   "derive",
	})

	for i := 0; i < 10; i++ {
		d.Add(i)
	}

	want := api.ValueList{
		Identifier: api.Identifier{
			Host:   "example.com",
			Plugin: "golang",
			Type:   "derive",
		},
		Values: []api.Value{api.Derive(45)},
	}
	got := d.ValueList()

	if !reflect.DeepEqual(got, want) {
		t.Errorf("got %#v, want %#v", got, want)
	}

	s := expvar.Get("example.com/golang/derive").String()
	if s != "45" {
		t.Errorf("got %q, want %q", s, "45")
	}
}

作者:emerald-c    项目:test-runne   
func init() {
	registry := expvar.Get("registry")
	if registry == nil {
		registry = expvar.NewMap("registry")
	}

	cache := registry.(*expvar.Map).Get("cache")
	if cache == nil {
		cache = &expvar.Map{}
		cache.(*expvar.Map).Init()
		registry.(*expvar.Map).Set("cache", cache)
	}

	storage := cache.(*expvar.Map).Get("storage")
	if storage == nil {
		storage = &expvar.Map{}
		storage.(*expvar.Map).Init()
		cache.(*expvar.Map).Set("storage", storage)
	}

	storage.(*expvar.Map).Set("blobdescriptor", expvar.Func(func() interface{} {
		// no need for synchronous access: the increments are atomic and
		// during reading, we don't care if the data is up to date. The
		// numbers will always *eventually* be reported correctly.
		return blobStatterCacheMetrics
	}))
}

作者:md1445    项目:kapacito   
// Gets an exported var and returns its unquoted string contents
func GetStringVar(name string) string {
	s, err := strconv.Unquote(expvar.Get(name).String())
	if err != nil {
		panic(err)
	}
	return s
}

作者:rhyoligh    项目:influxd   
// NewStatistics returns an expvar-based map with the given key. Within that map
// is another map. Within there "name" is the Measurement name, "tags" are the tags,
// and values are placed at the key "values".
func NewStatistics(key, name string, tags map[string]string) *expvar.Map {
	expvarMu.Lock()
	defer expvarMu.Unlock()

	// Add expvar for this service.
	var v expvar.Var
	if v = expvar.Get(key); v == nil {
		v = expvar.NewMap(key)
	}
	m := v.(*expvar.Map)

	// Set the name
	nameVar := &expvar.String{}
	nameVar.Set(name)
	m.Set("name", nameVar)

	// Set the tags
	tagsVar := &expvar.Map{}
	tagsVar.Init()
	for k, v := range tags {
		value := &expvar.String{}
		value.Set(v)
		tagsVar.Set(k, value)
	}
	m.Set("tags", tagsVar)

	// Create and set the values entry used for actual stats.
	statMap := &expvar.Map{}
	statMap.Init()
	m.Set("values", statMap)

	return statMap
}

作者:md1445    项目:kapacito   
// Gets an exported var and returns its float value
func GetFloatVar(name string) float64 {
	f, err := strconv.ParseFloat(expvar.Get(name).String(), 64)
	if err != nil {
		panic(err)
	}
	return f
}

作者:md1445    项目:kapacito   
// Gets an exported var and returns its int value
func GetIntVar(name string) int64 {
	i, err := strconv.ParseInt(expvar.Get(name).String(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

作者:zj848    项目:go-collect   
func TestGauge(t *testing.T) {
	g := NewGauge(api.Identifier{
		Host:   "example.com",
		Plugin: "golang",
		Type:   "gauge",
	})

	g.Set(42.0)

	want := api.ValueList{
		Identifier: api.Identifier{
			Host:   "example.com",
			Plugin: "golang",
			Type:   "gauge",
		},
		Values: []api.Value{api.Gauge(42)},
	}
	got := g.ValueList()

	if !reflect.DeepEqual(got, want) {
		t.Errorf("got %#v, want %#v", got, want)
	}

	s := expvar.Get("example.com/golang/gauge").String()
	if s != "42" {
		t.Errorf("got %q, want %q", s, "42")
	}
}

作者:patrickToc    项目:xstat   
func TestGauge(t *testing.T) {
	s := New("gauge")
	v := expvar.Get("gauge").(*expvar.Map)
	s.Gauge("test", 1)
	assert.Equal(t, "1", v.Get("test").String())
	s.Gauge("test", -1)
	assert.Equal(t, "-1", v.Get("test").String())
}

作者:patrickToc    项目:xstat   
func TestCount(t *testing.T) {
	s := New("count")
	v := expvar.Get("count").(*expvar.Map)
	s.Count("test", 1)
	assert.Equal(t, "1", v.Get("test").String())
	s.Count("test", -1)
	assert.Equal(t, "0", v.Get("test").String())
}

作者:jllopi    项目:ki   
func TestCallbackGauge(t *testing.T) {
	value := 42.43
	metricName := "foo"
	expvar.PublishCallbackGauge(metricName, func() float64 { return value })
	if want, have := fmt.Sprint(value), stdexpvar.Get(metricName).String(); want != have {
		t.Errorf("want %q, have %q", want, have)
	}
}

作者:pranjal521    项目:vites   
func checkMemcacheExpvar(t *testing.T, name string, expectedVal string) {
	val := expvar.Get(name)
	if val == nil {
		t.Fatalf("cannot find exported variable: %s", name)
	}
	if val.String() != expectedVal {
		t.Fatalf("name: %s, expect to get %s, but got: %s", name, expectedVal, val.String())
	}
}

作者:yanghongkjx    项目:mtai   
func TestWatcherErrors(t *testing.T) {
	orig, err := strconv.ParseInt(expvar.Get("log_watcher_error_count").String(), 10, 64)
	if err != nil {
		t.Fatalf("couldn't convert expvar %q", expvar.Get("log_watcher_error_count").String())
	}
	w, err := NewLogWatcher()
	if err != nil {
		t.Fatalf("couldn't create a watcher")
	}
	w.Errors <- errors.New("just a test, not really an error")
	if err := w.Close(); err != nil {
		t.Fatalf("watcher close failed: %q", err)
	}
	diff := pretty.Compare(strconv.FormatInt(orig+1, 10), expvar.Get("log_watcher_error_count").String())
	if len(diff) > 0 {
		t.Errorf("log watcher error count doens't match:\n%s", diff)
	}
}

作者:cnicolo    项目:ki   
func TestCounter(t *testing.T) {
	var (
		name  = "m"
		value = 123
	)
	expvar.NewCounter(name).With(metrics.Field{Key: "ignored", Value: "field"}).Add(uint64(value))
	if want, have := fmt.Sprint(value), stdexpvar.Get(name).String(); want != have {
		t.Errorf("want %q, have %q", want, have)
	}
}

作者:ninneman    项目:goengin   
func GetStatement(key string) (stmt mysql.Stmt, err error) {
	stmt, ok := Statements[key]
	if !ok {
		qry := expvar.Get(key)
		if qry == nil {
			err = errors.New("Invalid query reference")
		}
	}
	return

}

作者:nulleins013    项目:mtai   
func TestWatcherErrors(t *testing.T) {
	w, err := NewLogWatcher()
	if err != nil {
		t.Fatalf("couldn't create a watcher")
	}
	w.Errors <- errors.New("test error")
	w.Close()
	diff := pretty.Compare(expvar.Get("log_watcher_error_count").String(), "1")
	if len(diff) > 0 {
		t.Errorf("log watcher error count doens't match:\n%s", diff)
	}
}


问题


面经


文章

微信
公众号

扫码关注公众号