本文整理汇总了Golang中expvar.Get函数的典型用法代码示例。如果您正苦于以下问题:Golang Get函数的具体用法?Golang Get怎么用?Golang Get使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Get函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: statsPageHandler
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")
}
示例2: GetStatus
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)
}
示例3: TestNew
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")
})
}
示例4: TestPublished
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)
}
}
}
示例5: wsServer
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
}
}
}
}
示例6: TestDerive
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")
}
}
示例7: init
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
}))
}
示例8: GetStringVar
// 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
}
示例9: NewStatistics
// 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
}
示例10: GetFloatVar
// 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
}
示例11: GetIntVar
// 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
}
示例12: TestGauge
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")
}
}
示例13: TestGauge
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())
}
示例14: TestCount
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())
}
示例15: TestCallbackGauge
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)
}
}