github.com/braveheart12/just@v0.8.7/metrics/metrics_test.go (about)

     1  /*
     2   *    Copyright 2019 Insolar Technologies
     3   *
     4   *    Licensed under the Apache License, Version 2.0 (the "License");
     5   *    you may not use this file except in compliance with the License.
     6   *    You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *    Unless required by applicable law or agreed to in writing, software
    11   *    distributed under the License is distributed on an "AS IS" BASIS,
    12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *    See the License for the specific language governing permissions and
    14   *    limitations under the License.
    15   */
    16  
    17  package metrics_test
    18  
    19  import (
    20  	"math/rand"
    21  	"net/http"
    22  	"regexp"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  	"go.opencensus.io/stats"
    28  	"go.opencensus.io/stats/view"
    29  	"go.opencensus.io/tag"
    30  
    31  	"github.com/insolar/insolar/instrumentation/inslogger"
    32  	"github.com/insolar/insolar/instrumentation/insmetrics"
    33  	"github.com/insolar/insolar/ledger/storage/storagetest"
    34  	"github.com/insolar/insolar/testutils/testmetrics"
    35  )
    36  
    37  func TestMetrics_NewMetrics(t *testing.T) {
    38  	t.Parallel()
    39  	ctx := inslogger.TestContext(t)
    40  	testm := testmetrics.Start(ctx, t)
    41  
    42  	var (
    43  		// https://godoc.org/go.opencensus.io/stats
    44  		videoCount = stats.Int64("example.com/measures/video_count", "number of processed videos", stats.UnitDimensionless)
    45  		videoSize  = stats.Int64("video_size", "size of processed video", stats.UnitBytes)
    46  	)
    47  	osxtag := insmetrics.MustTagKey("osx")
    48  
    49  	err := view.Register(
    50  		&view.View{
    51  			Measure:     videoCount,
    52  			Aggregation: view.Count(),
    53  			TagKeys:     []tag.Key{osxtag},
    54  		},
    55  		&view.View{
    56  			Name:        "video_size",
    57  			Measure:     videoSize,
    58  			Aggregation: view.Distribution(0, 1<<16, 1<<32),
    59  			TagKeys:     []tag.Key{osxtag},
    60  		},
    61  	)
    62  	require.NoError(t, err)
    63  
    64  	newctx := insmetrics.ChangeTags(ctx, tag.Insert(osxtag, "11.12.13"))
    65  	stats.Record(newctx, videoCount.M(1), videoSize.M(rand.Int63()))
    66  
    67  	content, err := testm.FetchContent()
    68  	require.NoError(t, err)
    69  	// fmt.Println("/metrics => ", content)
    70  
    71  	assert.Regexp(t, regexp.MustCompile(`insolar_video_size_count{[^}]*osx="11\.12\.13"[^}]*} 1`), content )
    72  	assert.Regexp(t, regexp.MustCompile(`insolar_example_com_measures_video_count{[^}]*osx="11\.12\.13"[^}]*} 1`), content)
    73  
    74  	assert.NoError(t, testm.Stop())
    75  }
    76  
    77  func TestMetrics_ZPages(t *testing.T) {
    78  	t.Parallel()
    79  	ctx := inslogger.TestContext(t)
    80  	testm := testmetrics.Start(ctx, t)
    81  
    82  	// One more thing... from https://github.com/rakyll/opencensus-grpc-demo
    83  	// also check /debug/rpcz
    84  	code, content, err := testm.FetchURL("/debug/tracez")
    85  	_ = content
    86  	require.NoError(t, err)
    87  	require.Equal(t, http.StatusOK, code)
    88  	// fmt.Println("/debug/tracez => ", content)
    89  
    90  	assert.NoError(t, testm.Stop())
    91  }
    92  
    93  func TestMetrics_Badger(t *testing.T) {
    94  	t.Parallel()
    95  	ctx := inslogger.TestContext(t)
    96  
    97  	_, cleaner := storagetest.TmpDB(ctx, t)
    98  	defer cleaner()
    99  
   100  	testm := testmetrics.Start(ctx, t)
   101  
   102  	code, content, err := testm.FetchURL("/metrics")
   103  	_ = content
   104  	require.NoError(t, err)
   105  	require.Equal(t, http.StatusOK, code)
   106  	// fmt.Println("/metrics => ", content)
   107  	assert.Contains(t, content, "insolar_badger_blocked_puts_total")
   108  
   109  	assert.NoError(t, testm.Stop())
   110  }
   111  
   112  func TestMetrics_Status(t *testing.T) {
   113  	t.Parallel()
   114  	ctx := inslogger.TestContext(t)
   115  	testm := testmetrics.Start(ctx, t)
   116  
   117  	code, _, err := testm.FetchURL("/_status")
   118  	require.NoError(t, err)
   119  	require.Equal(t, http.StatusOK, code)
   120  
   121  	assert.NoError(t, testm.Stop())
   122  }