// Start the proxy. func (p *Proxy) Start() error { if p.ReplicaSet.MaxConnections == 0 { return errZeroMaxConnections } if p.ReplicaSet.MaxPerClientConnections == 0 { return errZeroMaxPerClientConnections } p.closed = make(chan struct{}) p.maxPerClientConnections = newMaxPerClientConnections(p.ReplicaSet.MaxPerClientConnections) p.serverPool = rpool.Pool{ New: p.newServerConn, CloseErrorHandler: p.serverCloseErrorHandler, Max: p.ReplicaSet.MaxConnections, MinIdle: p.ReplicaSet.MinIdleConnections, IdleTimeout: p.ReplicaSet.ServerIdleTimeout, ClosePoolSize: p.ReplicaSet.ServerClosePoolSize, } // plug stats if we can if p.ReplicaSet.Stats != nil { // Drop the default port suffix to make them pretty in production. dbName := strings.TrimSuffix(p.MongoAddr, ":27017") // We want 2 sets of keys, one specific to the proxy, and another shared // with others. p.serverPool.Stats = stats.PrefixClient( []string{ "mongoproxy.server.pool.", fmt.Sprintf("mongoproxy.%s.server.pool.", dbName), }, p.ReplicaSet.Stats, ) p.stats = stats.PrefixClient( []string{ "mongoproxy.", fmt.Sprintf("mongoproxy.%s.", dbName), }, p.ReplicaSet.Stats, ) } go p.clientAcceptLoop() return nil }
func TestPrefixClient(t *testing.T) { const ( prefix1 = "prefix1" prefix2 = "prefix2" avgKey = "avg" avgVal = float64(1) sumKey = "sum" sumVal = float64(2) histogramKey = "histogram" histogramVal = float64(3) timeKey = "time" ) var keys []string hc := &stats.HookClient{ BumpAvgHook: func(key string, val float64) { keys = append(keys, key) ensure.DeepEqual(t, val, avgVal) }, BumpSumHook: func(key string, val float64) { keys = append(keys, key) ensure.DeepEqual(t, val, sumVal) }, BumpHistogramHook: func(key string, val float64) { keys = append(keys, key) ensure.DeepEqual(t, val, histogramVal) }, BumpTimeHook: func(key string) interface { End() } { return multiEnderTest{ EndHook: func() { keys = append(keys, key) }, } }, } pc := stats.PrefixClient([]string{prefix1, prefix2}, hc) pc.BumpAvg(avgKey, avgVal) pc.BumpSum(sumKey, sumVal) pc.BumpHistogram(histogramKey, histogramVal) pc.BumpTime(timeKey).End() ensure.SameElements(t, keys, []string{ prefix1 + avgKey, prefix1 + sumKey, prefix1 + histogramKey, prefix1 + timeKey, prefix2 + avgKey, prefix2 + sumKey, prefix2 + histogramKey, prefix2 + timeKey, }) }
func TestPrefixClient() { const ( prefix1 = "prefix1-" prefix2 = "prefix2-" avgKey = "avg-john" avgVal = float64(1) sumKey = "sum-john" sumVal = float64(2) histogramKey = "histogram-john" histogramVal = float64(3) timeKey = "time-john" ) var keys []string hc := &stats.Client{ BumpAvg: func(key string, val float64) { keys = append(keys, key) fmt.Printf("BumpAvg:key = %v, val = %v, avgVal = %v\n", key, val, avgVal) //ensure.DeepEqual(t, val, avgVal) }, BumpSum: func(key string, val float64) { keys = append(keys, key) //ensure.DeepEqual(t, val, sumVal) fmt.Printf("BumpSum:key = %v, val = %v, sumVal = %v\n", key, val, sumVal) }, BumpHistogram: func(key string, val float64) { keys = append(keys, key) //ensure.DeepEqual(t, val, histogramVal) fmt.Printf("BumpHistogram:key = %v, val = %v, histogramVal = %v\n", key, val, histogramVal) }, BumpTime: func(key string) interface { End() } { return multiEnderTest{ EndHook: func() { keys = append(keys, key) fmt.Printf("EndHook:key = %v\n", key) }, } }, } pc := stats.PrefixClient([]string{prefix1, prefix2}, hc) pc.BumpAvg(avgKey, avgVal) pc.BumpSum(sumKey, sumVal) pc.BumpHistogram(histogramKey, histogramVal) pc.BumpTime(timeKey).End() fmt.Printf("%#v\n", keys) }
// NewClient creates an instance of facebookgo/stats implementation with // the given pkg name and preifx. func NewClient(pkg, prefix string) stats.Client { pkgClisLock.Lock() defer pkgClisLock.Unlock() pc, ok := pkgClis[pkg] if !ok { pc = newClient(pkg) pkgClis[pkg] = pc } if prefix == "" { return pc } if !strings.HasSuffix(prefix, ".") { prefix += "." } return stats.PrefixClient([]string{prefix}, pc) }