func NewSubjectPrefixMatchFilter(next Filter, subjectPrefixes []string) *SubjectPrefixMatchFilter { total := metrics.NewCounter() matched := metrics.NewCounter() metrics.Register("SubjectPrefixMatchFilter-Total", total) metrics.Register("SubjectPrefixMatchFilter-Matched", matched) return &SubjectPrefixMatchFilter{next, subjectPrefixes, total, matched} }
func NewRelayOnlyFilter(next Filter, localDomain string) *RelayOnlyFilter { total := metrics.NewCounter() numOfRelay := metrics.NewCounter() metrics.Register("RelayOnlyFilter-Total", total) metrics.Register("RelayOnlyFilter-Relay", numOfRelay) return &RelayOnlyFilter{next, localDomain, total, numOfRelay} }
func NewContentInspectionUpdater(anlz analyzer.Analyzer, class string) *ContentInspectionUpdater { total := metrics.NewCounter() malformed := metrics.NewCounter() metrics.Register("ContentInspectionUpdater-Total", total) metrics.Register("ContentInspectionUpdater-Malformed", malformed) return &ContentInspectionUpdater{anlz, class, total, malformed} }
func NewClientMetrics() *ClientMetrics { return &ClientMetrics{ connGauge: metrics.NewGauge(), connMeter: metrics.NewMeter(), connTimer: metrics.NewTimer(), proxySetupTimer: metrics.NewTimer(), bytesIn: metrics.NewHistogram(metrics.NewExpDecaySample(sampleSize, sampleAlpha)), bytesOut: metrics.NewHistogram(metrics.NewExpDecaySample(sampleSize, sampleAlpha)), bytesInCount: metrics.NewCounter(), bytesOutCount: metrics.NewCounter(), } }
func NewDeliverFilter(next Filter, paths map[Result]string) *DeliverFilter { total := metrics.NewCounter() metrics.Register("DeliverFilter-Total", total) counters := make(map[Result]metrics.Counter) for result, path := range paths { counter := metrics.NewCounter() counters[result] = counter metrics.Register("DeliverFilter-"+path, counter) } return &DeliverFilter{next, paths, total, counters} }
func NewContentInspectionFilter(next Filter, allPass bool, anlz analyzer.Analyzer) *ContentInspectionFilter { total := metrics.NewCounter() metrics.Register("ContentInspectionFilter-Total", total) counters := make(map[string]metrics.Counter) for _, class := range []string{analyzer.Good, analyzer.Bad, analyzer.Neutral} { counter := metrics.NewCounter() counters[class] = counter metrics.Register("ContentInspectionFilter-"+class, counter) } malformed := metrics.NewCounter() metrics.Register("ContentInspectionFilter-Malformed", malformed) return &ContentInspectionFilter{next, allPass, anlz, total, malformed, counters} }
func NewFileHandlerAdapter(updater Updater, factory mailfile.MailFileFactory) *FileHandlerAdapter { total := metrics.NewCounter() meter := metrics.NewMeter() metrics.Register("FileHandlerAdapter-Total", total) metrics.Register("FileHandlerAdapter-Size", meter) return &FileHandlerAdapter{updater, factory, total, meter} }
func TestCounter(t *testing.T) { c := mt.NewCounter() if c.Count() != 0 { t.Errorf("counter.count: expected %d, got %d", 0, c.Count()) } c.Inc(1) if c.Count() != 1 { t.Errorf("counter.Inc: expected %d, got %d", 1, c.Count()) } c.Inc(-2) if c.Count() != -1 { t.Errorf("counter.Inc: expected %d, got %d", -1, c.Count()) } c.Dec(1) if c.Count() != -2 { t.Errorf("counter.Dec: expected %d, got %d", -2, c.Count()) } c.Dec(-3) if c.Count() != 1 { t.Errorf("counter.Dec: expected %d, got %d", 1, c.Count()) } c.Clear() if c.Count() != 0 { t.Errorf("counter.Clear: expected %d, got %d", 0, c.Count()) } }
func NewPointInPolygonMetrics() *WOFPointInPolygonMetrics { registry := metrics.NewRegistry() cnt_lookups := metrics.NewCounter() cnt_unmarshal := metrics.NewCounter() cnt_cache_hit := metrics.NewCounter() cnt_cache_miss := metrics.NewCounter() cnt_cache_set := metrics.NewCounter() tm_unmarshal := metrics.NewTimer() tm_intersect := metrics.NewTimer() tm_inflate := metrics.NewTimer() tm_contain := metrics.NewTimer() tm_process := metrics.NewTimer() registry.Register("pip.reversegeo.lookups", cnt_lookups) registry.Register("pip.geojson.unmarshaled", cnt_unmarshal) registry.Register("pip.cache.hit", cnt_cache_hit) registry.Register("pip.cache.miss", cnt_cache_miss) registry.Register("pip.cache.set", cnt_cache_set) registry.Register("pip.timer.reversegeo", tm_process) registry.Register("pip.timer.unmarshal", tm_unmarshal) // registry.Register("time-to-intersect", tm_intersect) // registry.Register("time-to-inflate", tm_inflate) registry.Register("pip.timer.containment", tm_contain) m := WOFPointInPolygonMetrics{ Registry: ®istry, CountLookups: &cnt_lookups, CountUnmarshal: &cnt_unmarshal, CountCacheHit: &cnt_cache_hit, CountCacheMiss: &cnt_cache_miss, CountCacheSet: &cnt_cache_set, TimeToUnmarshal: &tm_unmarshal, TimeToIntersect: &tm_intersect, TimeToInflate: &tm_inflate, TimeToContain: &tm_contain, TimeToProcess: &tm_process, } metrics.RegisterRuntimeMemStats(registry) go metrics.CaptureRuntimeMemStats(registry, 10e9) return &m }
func NewCachingProxy(target Filter, size int) *CachingProxy { _, ok := target.(CacheKey) if !ok { log.Fatalf("%s should implement CacheKey interface\n", target) return nil } total := metrics.NewCounter() hit := metrics.NewCounter() cached := metrics.NewGauge() targetName := fmt.Sprintf("%s", target) metrics.Register("CachingProxy("+targetName+")-Total", total) metrics.Register("CachingProxy("+targetName+")-Hit", hit) metrics.Register("CachingProxy("+targetName+")-Cached", cached) return &CachingProxy{target, &sync.RWMutex{}, list.New(), size, total, hit, cached} }
func NewMetricsHttp() *MetricsHttp { return &MetricsHttp{ clients: mt.NewHistogram(mt.NewExpDecaySample(1028, 0.015)), clientsCounter: mt.NewCounter(), requests: mt.NewTimer(), status2xx: mt.NewMeter(), status3xx: mt.NewMeter(), status4xx: mt.NewMeter(), status5xx: mt.NewMeter(), } }
func NewLocalMetrics(reportInterval time.Duration) *LocalMetrics { metrics := LocalMetrics{ Logger: log.NewPrefixLogger("metrics"), reportInterval: reportInterval, windowsCounter: gometrics.NewCounter(), linuxCounter: gometrics.NewCounter(), osxCounter: gometrics.NewCounter(), otherCounter: gometrics.NewCounter(), tunnelMeter: gometrics.NewMeter(), tcpTunnelMeter: gometrics.NewMeter(), httpTunnelMeter: gometrics.NewMeter(), connMeter: gometrics.NewMeter(), lostHeartbeatMeter: gometrics.NewMeter(), connTimer: gometrics.NewTimer(), bytesInCount: gometrics.NewCounter(), bytesOutCount: gometrics.NewCounter(), /* metrics.tunnelGauge = gometrics.NewGauge(), metrics.tcpTunnelGauge = gometrics.NewGauge(), metrics.connGauge = gometrics.NewGauge(), */ } go metrics.Report() return &metrics }
// initializeFieldTagPath traverses the given struct trying to initialize // metric values. The "metric" struct tag is used to determine the name of the // metrics for each struct field. If there is no "metric" struct tag, the // lowercased struct field name is used for the metric name. The name is // prefixed with tags from previous struct fields if any, separated by a dot. // For example: // // Messages struct { // Smtp struct { // Latency metrics.Timer `metric:"latency"` // } `metric:"smtp"` // Http struct { // Latency metrics.Timer `metric:"latency"` // } `metric:"http"` // } `metric:"messages"` // // yields timers with names "messages.smtp.latency" and "messages.http.latency" // respectively. // // If there is no metric tag for a field it is skipped and assumed it is used // for other purposes such as configuration. func (m *MetricTags) initializeFieldTagPath(fieldType reflect.Value, prefix string) { for i := 0; i < fieldType.NumField(); i++ { val := fieldType.Field(i) field := fieldType.Type().Field(i) tag := field.Tag.Get("metric") if tag == "" { // If tag isn't found, derive tag from the lower case name of // the field. tag = strings.ToLower(field.Name) } if prefix != "" { tag = prefix + m.separator + tag } if field.Type.Kind() == reflect.Struct { // Recursively traverse an embedded struct m.initializeFieldTagPath(val, tag) } else if field.Type.Kind() == reflect.Map && field.Type.Key().Kind() == reflect.String { // If this is a map[string]Something, then use the string key as bucket name and recursively generate the metrics below for _, k := range val.MapKeys() { m.initializeFieldTagPath(val.MapIndex(k).Elem(), tag+m.separator+k.String()) } } else { // Found a field, initialize switch field.Type.String() { case "metrics.Counter": c := metrics.NewCounter() metrics.Register(tag, c) val.Set(reflect.ValueOf(c)) case "metrics.Timer": t := metrics.NewTimer() metrics.Register(tag, t) val.Set(reflect.ValueOf(t)) case "metrics.Meter": m := metrics.NewMeter() metrics.Register(tag, m) val.Set(reflect.ValueOf(m)) case "metrics.Gauge": g := metrics.NewGauge() metrics.Register(tag, g) val.Set(reflect.ValueOf(g)) case "metrics.Histogram": s := metrics.NewUniformSample(1028) h := metrics.NewHistogram(s) metrics.Register(tag, h) val.Set(reflect.ValueOf(h)) } } } }
// SaveCounter indicates that we want to store an internal representation of the counts in this bucket so that we can // query it (via GetCounter). func (i *Instrumentation) SaveCounter(bucket string) error { i.mtx.Lock() defer i.mtx.Unlock() if _, exists := i.savedCounters[bucket]; exists { return fmt.Errorf("Counter bucket %s already registered", bucket) } c := metrics.NewCounter() i.registry.Register(bucket, c) i.savedCounters[bucket] = c return nil }
func (m *Metrics) GetCounter(counterName string) gometrics.Counter { m.Lock() defer m.Unlock() prefixedCounterName := m.Prefix + "_" + counterName counter, exists := m.Counters[prefixedCounterName] if !exists { counter = gometrics.NewCounter() m.Registry.Register(prefixedCounterName, counter) m.Counters[prefixedCounterName] = counter } return m.Counters[prefixedCounterName] }
func NewHttpMetric() *HttpMetric { x := &HttpMetric{ Requests: metrics.NewCounter(), Rate: metrics.NewMeter(), Responses2xx: metrics.NewCounter(), Responses3xx: metrics.NewCounter(), Responses4xx: metrics.NewCounter(), Responses5xx: metrics.NewCounter(), ResponsesXxx: metrics.NewCounter(), Latency: metrics.NewHistogram(metrics.NewExpDecaySample(1028, 0.015)), } return x }
func (m *StreamingMetrics) Register(op string) { m.OpGroups[op] = MetricsGroup{metrics.NewCounter(), metrics.NewCounter(), metrics.NewGauge()} }
func NewDefaultDestinationFilter() *DefaultDestinationFilter { total := metrics.NewCounter() metrics.Register("DefaultDestinationFilter-Total", total) return &DefaultDestinationFilter{total} }