github.com/uber-go/tally/v4@v4.1.17/example/main.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package main
    22  
    23  import (
    24  	"fmt"
    25  	"time"
    26  
    27  	tally "github.com/uber-go/tally/v4"
    28  )
    29  
    30  type printStatsReporter struct{}
    31  
    32  func newPrintStatsReporter() tally.StatsReporter {
    33  	return &printStatsReporter{}
    34  }
    35  
    36  func (r *printStatsReporter) ReportCounter(name string, _ map[string]string, value int64) {
    37  	fmt.Printf("count %s %d\n", name, value)
    38  }
    39  
    40  func (r *printStatsReporter) ReportGauge(name string, _ map[string]string, value float64) {
    41  	fmt.Printf("gauge %s %f\n", name, value)
    42  }
    43  
    44  func (r *printStatsReporter) ReportTimer(name string, _ map[string]string, interval time.Duration) {
    45  	fmt.Printf("timer %s %s\n", name, interval.String())
    46  }
    47  
    48  func (r *printStatsReporter) ReportHistogramValueSamples(
    49  	name string,
    50  	_ map[string]string,
    51  	_ tally.Buckets,
    52  	bucketLowerBound,
    53  	bucketUpperBound float64,
    54  	samples int64,
    55  ) {
    56  	fmt.Printf("histogram %s bucket lower %f upper %f samples %d\n",
    57  		name, bucketLowerBound, bucketUpperBound, samples)
    58  }
    59  
    60  func (r *printStatsReporter) ReportHistogramDurationSamples(
    61  	name string,
    62  	_ map[string]string,
    63  	_ tally.Buckets,
    64  	bucketLowerBound,
    65  	bucketUpperBound time.Duration,
    66  	samples int64,
    67  ) {
    68  	fmt.Printf("histogram %s bucket lower %v upper %v samples %d\n",
    69  		name, bucketLowerBound, bucketUpperBound, samples)
    70  }
    71  
    72  func (r *printStatsReporter) Capabilities() tally.Capabilities {
    73  	return r
    74  }
    75  
    76  func (r *printStatsReporter) Reporting() bool {
    77  	return true
    78  }
    79  
    80  func (r *printStatsReporter) Tagging() bool {
    81  	return false
    82  }
    83  
    84  func (r *printStatsReporter) Flush() {
    85  	fmt.Printf("flush\n")
    86  }
    87  
    88  func main() {
    89  	reporter := newPrintStatsReporter()
    90  	rootScope, closer := tally.NewRootScope(tally.ScopeOptions{
    91  		Reporter: reporter,
    92  	}, time.Second)
    93  	defer closer.Close()
    94  	subScope := rootScope.SubScope("requests")
    95  
    96  	bighand := time.NewTicker(time.Millisecond * 2300)
    97  	littlehand := time.NewTicker(time.Millisecond * 10)
    98  	hugehand := time.NewTicker(time.Millisecond * 5100)
    99  
   100  	measureThing := rootScope.Gauge("thing")
   101  	timings := rootScope.Timer("timings")
   102  	tickCounter := subScope.Counter("ticks")
   103  
   104  	// Spin forever, watch report get called
   105  	go func() {
   106  		for {
   107  			select {
   108  			case <-bighand.C:
   109  				measureThing.Update(42.1)
   110  			case <-littlehand.C:
   111  				tickCounter.Inc(1)
   112  			case <-hugehand.C:
   113  				timings.Record(3200 * time.Millisecond)
   114  			}
   115  		}
   116  	}()
   117  
   118  	select {}
   119  }