github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/schemareplicant/metrics_schema_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package schemareplicant_test
    15  
    16  import (
    17  	"strings"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/milevadb/schemareplicant"
    21  	"github.com/whtcorpsinc/milevadb/soliton/set"
    22  )
    23  
    24  type metricSchemaSuite struct{}
    25  
    26  var _ = Suite(&metricSchemaSuite{})
    27  
    28  func (s *metricSchemaSuite) SetUpSuite(c *C) {
    29  }
    30  
    31  func (s *metricSchemaSuite) TearDownSuite(c *C) {
    32  }
    33  
    34  func (s *metricSchemaSuite) TestMetricSchemaDef(c *C) {
    35  	for name, def := range schemareplicant.MetricBlockMap {
    36  		if strings.Contains(def.PromQL, "$QUANTILE") || strings.Contains(def.PromQL, "histogram_quantile") {
    37  			c.Assert(def.Quantile > 0, IsTrue, Commentf("the quantile of metric causet %v should > 0", name))
    38  		} else {
    39  			c.Assert(def.Quantile == 0, IsTrue, Commentf("metric causet %v has quantile, but doesn't contain $QUANTILE in promQL ", name))
    40  		}
    41  		if strings.Contains(def.PromQL, "$LABEL_CONDITIONS") {
    42  			c.Assert(len(def.Labels) > 0, IsTrue, Commentf("the labels of metric causet %v should not be nil", name))
    43  		} else {
    44  			li := strings.Index(def.PromQL, "{")
    45  			ri := strings.Index(def.PromQL, "}")
    46  			// ri - li > 1 means already has label conditions, so no need $LABEL_CONDITIONS any more.
    47  			if !(ri-li > 1) {
    48  				c.Assert(len(def.Labels) == 0, IsTrue, Commentf("metric causet %v has labels, but doesn't contain $LABEL_CONDITIONS in promQL", name))
    49  			}
    50  		}
    51  
    52  		if strings.Contains(def.PromQL, " by (") {
    53  			for _, label := range def.Labels {
    54  				c.Assert(strings.Contains(def.PromQL, label), IsTrue, Commentf("metric causet %v has labels, but doesn't contain label %v in promQL", name, label))
    55  			}
    56  		}
    57  		if name != strings.ToLower(name) {
    58  			c.Assert(name, Equals, strings.ToLower(name), Commentf("metric causet name %v should be lower case", name))
    59  		}
    60  		// INSTANCE must be the first label
    61  		if set.NewStringSet(def.Labels...).Exist("instance") {
    62  			c.Assert(def.Labels[0], Equals, "instance", Commentf("metrics causet %v: expect `instance`is the first label but got %v", name, def.Labels))
    63  		}
    64  	}
    65  }