github.com/theQRL/go-zond@v0.1.1/metrics/opentsdb_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func ExampleOpenTSDB() {
    13  	addr, _ := net.ResolveTCPAddr("net", ":2003")
    14  	go OpenTSDB(DefaultRegistry, 1*time.Second, "some.prefix", addr)
    15  }
    16  
    17  func ExampleOpenTSDBWithConfig() {
    18  	addr, _ := net.ResolveTCPAddr("net", ":2003")
    19  	go OpenTSDBWithConfig(OpenTSDBConfig{
    20  		Addr:          addr,
    21  		Registry:      DefaultRegistry,
    22  		FlushInterval: 1 * time.Second,
    23  		DurationUnit:  time.Millisecond,
    24  	})
    25  }
    26  
    27  func TestExampleOpenTSB(t *testing.T) {
    28  	r := NewOrderedRegistry()
    29  	NewRegisteredGaugeInfo("foo", r).Update(GaugeInfoValue{"chain_id": "5"})
    30  	NewRegisteredGaugeFloat64("pi", r).Update(3.14)
    31  	NewRegisteredCounter("months", r).Inc(12)
    32  	NewRegisteredCounterFloat64("tau", r).Inc(1.57)
    33  	NewRegisteredMeter("elite", r).Mark(1337)
    34  	NewRegisteredTimer("second", r).Update(time.Second)
    35  	NewRegisteredCounterFloat64("tau", r).Inc(1.57)
    36  	NewRegisteredCounterFloat64("tau", r).Inc(1.57)
    37  
    38  	w := new(strings.Builder)
    39  	(&OpenTSDBConfig{
    40  		Registry:     r,
    41  		DurationUnit: time.Millisecond,
    42  		Prefix:       "pre",
    43  	}).writeRegistry(w, 978307200, "hal9000")
    44  
    45  	wantB, err := os.ReadFile("./testdata/opentsb.want")
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	if have, want := w.String(), string(wantB); have != want {
    50  		t.Errorf("\nhave:\n%v\nwant:\n%v\n", have, want)
    51  		t.Logf("have vs want:\n%v", findFirstDiffPos(have, want))
    52  	}
    53  }
    54  
    55  func findFirstDiffPos(a, b string) string {
    56  	yy := strings.Split(b, "\n")
    57  	for i, x := range strings.Split(a, "\n") {
    58  		if i >= len(yy) {
    59  			return fmt.Sprintf("have:%d: %s\nwant:%d: <EOF>", i, x, i)
    60  		}
    61  		if y := yy[i]; x != y {
    62  			return fmt.Sprintf("have:%d: %s\nwant:%d: %s", i, x, i, y)
    63  		}
    64  	}
    65  	return ""
    66  }