trpc.group/trpc-go/trpc-go@v1.0.3/metrics/sink_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package metrics_test
    15  
    16  import (
    17  	"testing"
    18  	"time"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"trpc.group/trpc-go/trpc-go/metrics"
    22  )
    23  
    24  func TestConsoleSink(t *testing.T) {
    25  	sink := metrics.NewConsoleSink()
    26  
    27  	// 以 cpu 负载上报为例
    28  	rec := metrics.NewSingleDimensionMetrics("cpu.load.top", 70.0, metrics.PolicyMAX)
    29  	err := sink.Report(rec)
    30  	assert.Nil(t, err, "report cpu.load.top")
    31  
    32  	// method 1
    33  	rec = metrics.NewSingleDimensionMetrics("cpu.load.avg", 70.0, metrics.PolicyAVG)
    34  	err = sink.Report(rec)
    35  	assert.Nil(t, err, "report cpu.load.avg")
    36  
    37  	// method 2
    38  	metrics.ReportSingleDimensionMetrics("cpu.load.top", 70.0, metrics.PolicyMAX)
    39  	metrics.ReportSingleDimensionMetrics("cpu.load.avg", 70.0, metrics.PolicyAVG)
    40  
    41  	// 以模调等多维上报为例
    42  	dims := []*metrics.Dimension{
    43  		{
    44  			Name:  "srcService",
    45  			Value: "HelloASvr",
    46  		}, {
    47  			Name:  "dstService",
    48  			Value: "HelloBSvr",
    49  		}, {
    50  			Name:  "interface",
    51  			Value: "HelloAction",
    52  		},
    53  	}
    54  
    55  	indices := []*metrics.Metrics{
    56  		metrics.NewMetrics("req.timecost", float64(time.Second), metrics.PolicyAVG),
    57  		metrics.NewMetrics("req.count", float64(1), metrics.PolicySUM),
    58  	}
    59  
    60  	// method 1
    61  	rec = metrics.NewMultiDimensionMetricsX("a", dims, indices)
    62  	metrics.Report(rec)
    63  
    64  	// method 2
    65  	metrics.ReportMultiDimensionMetricsX("b", dims, indices)
    66  }
    67  
    68  func TestMetrics(t *testing.T) {
    69  	m := metrics.NewMetrics("req.count", float64(1), metrics.PolicySUM)
    70  	assert.Equal(t, m.Name(), "req.count")
    71  	assert.Equal(t, m.Value(), float64(1))
    72  	assert.Equal(t, m.Policy(), metrics.Policy(metrics.PolicySUM))
    73  
    74  	var n *metrics.Metrics
    75  	assert.Zero(t, n.Name(), "")
    76  	assert.Zero(t, n.Value())
    77  	assert.Zero(t, n.Policy())
    78  }
    79  
    80  func TestRecord(t *testing.T) {
    81  	rec := metrics.NewSingleDimensionMetrics("cpu.load.avg", 70.0, metrics.PolicyAVG)
    82  	d := rec.GetDimensions()
    83  	assert.Equal(t, len(d), 0)
    84  
    85  	m := rec.GetMetrics()
    86  	assert.Equal(t, m[0].Name(), "cpu.load.avg")
    87  	assert.Equal(t, m[0].Value(), 70.0)
    88  	assert.Equal(t, m[0].Policy(), metrics.Policy(metrics.PolicyAVG))
    89  }
    90  
    91  func TestPolicy(t *testing.T) {
    92  	r := metrics.NewSingleDimensionMetrics("cpu.load.top", 70.0, metrics.PolicyMIN)
    93  	assert.Equal(t, r.GetMetrics()[0].Policy(), metrics.Policy(metrics.PolicyMIN))
    94  
    95  	r = metrics.NewSingleDimensionMetrics("cpu.load.top", 70.0, metrics.PolicyMAX)
    96  	assert.Equal(t, r.GetMetrics()[0].Policy(), metrics.Policy(metrics.PolicyMAX))
    97  
    98  	r = metrics.NewSingleDimensionMetrics("cpu.load.top", 70.0, metrics.PolicyMID)
    99  	assert.Equal(t, r.GetMetrics()[0].Policy(), metrics.Policy(metrics.PolicyMID))
   100  }