github.com/daefrom/go-dae@v1.0.1/metrics/prometheus/collector_test.go (about)

     1  package prometheus
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/daefrom/go-dae/metrics"
     9  )
    10  
    11  func TestMain(m *testing.M) {
    12  	metrics.Enabled = true
    13  	os.Exit(m.Run())
    14  }
    15  
    16  func TestCollector(t *testing.T) {
    17  	c := newCollector()
    18  
    19  	counter := metrics.NewCounter()
    20  	counter.Inc(12345)
    21  	c.addCounter("test/counter", counter)
    22  
    23  	gauge := metrics.NewGauge()
    24  	gauge.Update(23456)
    25  	c.addGauge("test/gauge", gauge)
    26  
    27  	gaugeFloat64 := metrics.NewGaugeFloat64()
    28  	gaugeFloat64.Update(34567.89)
    29  	c.addGaugeFloat64("test/gauge_float64", gaugeFloat64)
    30  
    31  	histogram := metrics.NewHistogram(&metrics.NilSample{})
    32  	c.addHistogram("test/histogram", histogram)
    33  
    34  	meter := metrics.NewMeter()
    35  	defer meter.Stop()
    36  	meter.Mark(9999999)
    37  	c.addMeter("test/meter", meter)
    38  
    39  	timer := metrics.NewTimer()
    40  	defer timer.Stop()
    41  	timer.Update(20 * time.Millisecond)
    42  	timer.Update(21 * time.Millisecond)
    43  	timer.Update(22 * time.Millisecond)
    44  	timer.Update(120 * time.Millisecond)
    45  	timer.Update(23 * time.Millisecond)
    46  	timer.Update(24 * time.Millisecond)
    47  	c.addTimer("test/timer", timer)
    48  
    49  	resettingTimer := metrics.NewResettingTimer()
    50  	resettingTimer.Update(10 * time.Millisecond)
    51  	resettingTimer.Update(11 * time.Millisecond)
    52  	resettingTimer.Update(12 * time.Millisecond)
    53  	resettingTimer.Update(120 * time.Millisecond)
    54  	resettingTimer.Update(13 * time.Millisecond)
    55  	resettingTimer.Update(14 * time.Millisecond)
    56  	c.addResettingTimer("test/resetting_timer", resettingTimer.Snapshot())
    57  
    58  	emptyResettingTimer := metrics.NewResettingTimer().Snapshot()
    59  	c.addResettingTimer("test/empty_resetting_timer", emptyResettingTimer)
    60  
    61  	const expectedOutput = `# TYPE test_counter gauge
    62  test_counter 12345
    63  
    64  # TYPE test_gauge gauge
    65  test_gauge 23456
    66  
    67  # TYPE test_gauge_float64 gauge
    68  test_gauge_float64 34567.89
    69  
    70  # TYPE test_histogram_count counter
    71  test_histogram_count 0
    72  
    73  # TYPE test_histogram summary
    74  test_histogram {quantile="0.5"} 0
    75  test_histogram {quantile="0.75"} 0
    76  test_histogram {quantile="0.95"} 0
    77  test_histogram {quantile="0.99"} 0
    78  test_histogram {quantile="0.999"} 0
    79  test_histogram {quantile="0.9999"} 0
    80  
    81  # TYPE test_meter gauge
    82  test_meter 9999999
    83  
    84  # TYPE test_timer_count counter
    85  test_timer_count 6
    86  
    87  # TYPE test_timer summary
    88  test_timer {quantile="0.5"} 2.25e+07
    89  test_timer {quantile="0.75"} 4.8e+07
    90  test_timer {quantile="0.95"} 1.2e+08
    91  test_timer {quantile="0.99"} 1.2e+08
    92  test_timer {quantile="0.999"} 1.2e+08
    93  test_timer {quantile="0.9999"} 1.2e+08
    94  
    95  # TYPE test_resetting_timer_count counter
    96  test_resetting_timer_count 6
    97  
    98  # TYPE test_resetting_timer summary
    99  test_resetting_timer {quantile="0.50"} 12000000
   100  test_resetting_timer {quantile="0.95"} 120000000
   101  test_resetting_timer {quantile="0.99"} 120000000
   102  
   103  `
   104  	exp := c.buff.String()
   105  	if exp != expectedOutput {
   106  		t.Log("Expected Output:\n", expectedOutput)
   107  		t.Log("Actual Output:\n", exp)
   108  		t.Fatal("unexpected collector output")
   109  	}
   110  }