github.com/polarismesh/polaris@v1.17.8/plugin/statis/base/common.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package base
    19  
    20  import (
    21  	"fmt"
    22  	"time"
    23  
    24  	"github.com/polarismesh/polaris/common/metrics"
    25  )
    26  
    27  const (
    28  	MaxLogWaitDuration = 800 * time.Millisecond
    29  	MaxZeroDuration    = 3
    30  	MetricsNumber      = 5
    31  	MaxAddDuration     = 800 * time.Millisecond
    32  )
    33  
    34  type MetricsHandler func(mt metrics.CallMetricType, start time.Time, staticsSlice []*APICallStatisItem)
    35  
    36  // MetricData metric 结构体
    37  type MetricData struct {
    38  	Name       string
    39  	Data       float64
    40  	Labels     map[string]string
    41  	DeleteFlag bool
    42  }
    43  
    44  type metricDesc struct {
    45  	Name       string
    46  	Help       string
    47  	MetricType string
    48  	LabelNames []string
    49  }
    50  
    51  const (
    52  	// metric name
    53  	// MetricForClientRqTimeout time consumed per interface call
    54  	MetricForClientRqTimeout string = "client_rq_timeout"
    55  	// MetricForClientRqIntervalCount total number of client request in current interval
    56  	MetricForClientRqIntervalCount string = "client_rq_interval_count"
    57  	// MetricForClientRqTimeoutMin max latency of client requests
    58  	MetricForClientRqTimeoutMin string = "client_rq_timeout_min"
    59  	// MetricForClientRqTimeoutAvg min latency of client requests
    60  	MetricForClientRqTimeoutAvg string = "client_rq_timeout_avg"
    61  	// MetricForClientRqTimeoutMax average latency of client requests
    62  	MetricForClientRqTimeoutMax string = "client_rq_timeout_max"
    63  
    64  	// metric type
    65  	TypeForGaugeVec string = "gauge_vec"
    66  )
    67  
    68  var (
    69  	// metricDescList Metrics Description Defines the list
    70  	MetricDescList = []metricDesc{
    71  		{
    72  			Name:       MetricForClientRqTimeout,
    73  			Help:       "time consumed per interface call",
    74  			MetricType: TypeForGaugeVec,
    75  			LabelNames: []string{
    76  				metrics.LabelApi,
    77  				metrics.LabelProtocol,
    78  				metrics.LabelErrCode,
    79  			},
    80  		},
    81  		{
    82  			Name:       MetricForClientRqIntervalCount,
    83  			Help:       "total number of client request in current interval",
    84  			MetricType: TypeForGaugeVec,
    85  			LabelNames: []string{
    86  				metrics.LabelApi,
    87  				metrics.LabelProtocol,
    88  				metrics.LabelErrCode,
    89  			},
    90  		},
    91  		{
    92  			Name:       MetricForClientRqTimeoutMax,
    93  			Help:       "max latency of client requests",
    94  			MetricType: TypeForGaugeVec,
    95  			LabelNames: []string{
    96  				metrics.LabelApi,
    97  				metrics.LabelProtocol,
    98  				metrics.LabelErrCode,
    99  			},
   100  		},
   101  		{
   102  			Name:       MetricForClientRqTimeoutMin,
   103  			Help:       "min latency of client requests",
   104  			MetricType: TypeForGaugeVec,
   105  			LabelNames: []string{
   106  				metrics.LabelApi,
   107  				metrics.LabelProtocol,
   108  				metrics.LabelErrCode,
   109  			},
   110  		},
   111  		{
   112  			Name:       MetricForClientRqTimeoutAvg,
   113  			Help:       "average latency of client requests",
   114  			MetricType: TypeForGaugeVec,
   115  			LabelNames: []string{
   116  				metrics.LabelApi,
   117  				metrics.LabelProtocol,
   118  				metrics.LabelErrCode,
   119  			},
   120  		},
   121  	}
   122  )
   123  
   124  // BuildMetricLabels build metric label from APICall or APICallStatisItem
   125  func BuildMetricLabels(item *APICallStatisItem) map[string]string {
   126  	return map[string]string{
   127  		metrics.LabelErrCode:  fmt.Sprintf("%d", item.Code),
   128  		metrics.LabelApi:      item.API,
   129  		metrics.LabelProtocol: item.Protocol,
   130  	}
   131  }