github.com/uber-go/tally/v4@v4.1.17/prometheus/example/prometheus_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  	"math/rand"
    26  	"net/http"
    27  	"time"
    28  
    29  	tally "github.com/uber-go/tally/v4"
    30  	promreporter "github.com/uber-go/tally/v4/prometheus"
    31  )
    32  
    33  func main() {
    34  	r := promreporter.NewReporter(promreporter.Options{})
    35  
    36  	// Note: `promreporter.DefaultSeparator` is "_".
    37  	// Prometheus doesnt like metrics with "." or "-" in them.
    38  	scope, closer := tally.NewRootScope(tally.ScopeOptions{
    39  		Prefix:         "my_service",
    40  		Tags:           map[string]string{},
    41  		CachedReporter: r,
    42  		Separator:      promreporter.DefaultSeparator,
    43  	}, 1*time.Second)
    44  	defer closer.Close()
    45  
    46  	counter := scope.Tagged(map[string]string{
    47  		"foo": "bar",
    48  	}).Counter("test_counter")
    49  
    50  	gauge := scope.Tagged(map[string]string{
    51  		"foo": "baz",
    52  	}).Gauge("test_gauge")
    53  
    54  	timer := scope.Tagged(map[string]string{
    55  		"foo": "qux",
    56  	}).Timer("test_timer_summary")
    57  
    58  	histogram := scope.Tagged(map[string]string{
    59  		"foo": "quk",
    60  	}).Histogram("test_histogram", tally.DefaultBuckets)
    61  
    62  	go func() {
    63  		for {
    64  			counter.Inc(1)
    65  			time.Sleep(time.Second)
    66  		}
    67  	}()
    68  
    69  	go func() {
    70  		for {
    71  			gauge.Update(rand.Float64() * 1000)
    72  			time.Sleep(time.Second)
    73  		}
    74  	}()
    75  
    76  	go func() {
    77  		for {
    78  			tsw := timer.Start()
    79  			hsw := histogram.Start()
    80  			time.Sleep(time.Duration(rand.Float64() * float64(time.Second)))
    81  			tsw.Stop()
    82  			hsw.Stop()
    83  		}
    84  	}()
    85  
    86  	http.Handle("/metrics", r.HTTPHandler())
    87  	fmt.Printf("Serving :8080/metrics\n")
    88  	fmt.Printf("%v\n", http.ListenAndServe(":8080", nil))
    89  	select {}
    90  }