github.com/jd-ly/tools@v0.5.7/internal/event/export/ocagent/metrics_test.go (about)

     1  package ocagent_test
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/jd-ly/tools/internal/event"
     9  	"github.com/jd-ly/tools/internal/event/keys"
    10  )
    11  
    12  func TestEncodeMetric(t *testing.T) {
    13  	exporter := registerExporter()
    14  	const prefix = testNodeStr + `
    15  	"metrics":[`
    16  	const suffix = `]}`
    17  	tests := []struct {
    18  		name string
    19  		run  func(ctx context.Context)
    20  		want string
    21  	}{
    22  		{
    23  			name: "HistogramFloat64, HistogramInt64",
    24  			run: func(ctx context.Context) {
    25  				ctx = event.Label(ctx, keyMethod.Of("godoc.ServeHTTP"))
    26  				event.Metric(ctx, latencyMs.Of(96.58))
    27  				ctx = event.Label(ctx, keys.Err.Of(errors.New("panic: fatal signal")))
    28  				event.Metric(ctx, bytesIn.Of(97e2))
    29  			},
    30  			want: prefix + `
    31  			{
    32  				"metric_descriptor": {
    33  					"name": "latency_ms",
    34  					"description": "The latency of calls in milliseconds",
    35  					"type": 6,
    36  					"label_keys": [
    37  						{
    38  							"key": "method"
    39  						},
    40  						{
    41  							"key": "route"
    42  						}
    43  					]
    44  				},
    45  				"timeseries": [
    46  					{
    47  						"start_timestamp": "1970-01-01T00:00:00Z",
    48  						"points": [
    49  							{
    50  								"timestamp": "1970-01-01T00:00:40Z",
    51  								"distributionValue": {
    52  									"count": 1,
    53  									"sum": 96.58,
    54  									"bucket_options": {
    55  										"explicit": {
    56  											"bounds": [
    57  												0,
    58  												5,
    59  												10,
    60  												25,
    61  												50
    62  											]
    63  										}
    64  									},
    65  									"buckets": [
    66  										{},
    67  										{},
    68  										{},
    69  										{},
    70  										{}
    71  									]
    72  								}
    73  							}
    74  						]
    75  					}
    76  				]
    77  			},
    78  			{
    79  				"metric_descriptor": {
    80  					"name": "latency_ms",
    81  					"description": "The latency of calls in milliseconds",
    82  					"type": 6,
    83  					"label_keys": [
    84  						{
    85  							"key": "method"
    86  						},
    87  						{
    88  							"key": "route"
    89  						}
    90  					]
    91  				},
    92  				"timeseries": [
    93  					{
    94  						"start_timestamp": "1970-01-01T00:00:00Z",
    95  						"points": [
    96  							{
    97  								"timestamp": "1970-01-01T00:00:40Z",
    98  								"distributionValue": {
    99  									"count": 1,
   100  									"sum": 9700,
   101  									"bucket_options": {
   102  										"explicit": {
   103  											"bounds": [
   104  												0,
   105  												10,
   106  												50,
   107  												100,
   108  												500,
   109  												1000,
   110  												2000
   111  											]
   112  										}
   113  									},
   114  									"buckets": [
   115  										{},
   116  										{},
   117  										{},
   118  										{},
   119  										{},
   120  										{},
   121  										{}
   122  									]
   123  								}
   124  							}
   125  						]
   126  					}
   127  				]
   128  			}` + suffix,
   129  		},
   130  	}
   131  
   132  	ctx := context.TODO()
   133  	for _, tt := range tests {
   134  		t.Run(tt.name, func(t *testing.T) {
   135  			tt.run(ctx)
   136  			got := exporter.Output("/v1/metrics")
   137  			checkJSON(t, got, []byte(tt.want))
   138  		})
   139  	}
   140  }