dubbo.apache.org/dubbo-go/v3@v3.1.1/metrics/common.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package metrics
    19  
    20  import (
    21  	"dubbo.apache.org/dubbo-go/v3/common"
    22  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    23  )
    24  
    25  type MetricKey struct {
    26  	Name string
    27  	Desc string
    28  }
    29  
    30  func NewMetricKey(name string, desc string) *MetricKey {
    31  	return &MetricKey{Name: name, Desc: desc}
    32  }
    33  
    34  type MetricLevel interface {
    35  	Tags() map[string]string
    36  }
    37  
    38  type ApplicationMetricLevel struct {
    39  	ApplicationName string
    40  	Version         string
    41  	GitCommitId     string
    42  	Ip              string
    43  	HostName        string
    44  }
    45  
    46  var applicationName string
    47  var applicationVersion string
    48  
    49  // cannot import rootConfig,may cause cycle import,so be it
    50  func InitAppInfo(appName string, appVersion string) {
    51  	applicationName = appName
    52  	applicationVersion = appVersion
    53  }
    54  
    55  func GetApplicationLevel() *ApplicationMetricLevel {
    56  	return &ApplicationMetricLevel{
    57  		ApplicationName: applicationName,
    58  		Version:         applicationVersion,
    59  		Ip:              common.GetLocalIp(),
    60  		HostName:        common.GetLocalHostName(),
    61  		GitCommitId:     "",
    62  	}
    63  }
    64  
    65  func (m *ApplicationMetricLevel) Tags() map[string]string {
    66  	tags := make(map[string]string)
    67  	tags[constant.TagIp] = m.Ip
    68  	tags[constant.TagHostname] = m.HostName
    69  	tags[constant.TagApplicationName] = m.ApplicationName
    70  	tags[constant.TagApplicationVersion] = m.Version
    71  	tags[constant.TagGitCommitId] = m.GitCommitId
    72  	return tags
    73  }
    74  
    75  type ServiceMetricLevel struct {
    76  	*ApplicationMetricLevel
    77  	Interface string
    78  }
    79  
    80  func NewServiceMetric(interfaceName string) *ServiceMetricLevel {
    81  	return &ServiceMetricLevel{ApplicationMetricLevel: GetApplicationLevel(), Interface: interfaceName}
    82  }
    83  
    84  func (m ServiceMetricLevel) Tags() map[string]string {
    85  	tags := m.ApplicationMetricLevel.Tags()
    86  	tags[constant.TagInterface] = m.Interface
    87  	return tags
    88  }
    89  
    90  type MethodMetricLevel struct {
    91  	*ServiceMetricLevel
    92  	Method  string
    93  	Group   string
    94  	Version string
    95  }
    96  
    97  func (m MethodMetricLevel) Tags() map[string]string {
    98  	tags := m.ServiceMetricLevel.Tags()
    99  	tags[constant.TagMethod] = m.Method
   100  	tags[constant.TagGroup] = m.Group
   101  	tags[constant.TagVersion] = m.Version
   102  	return tags
   103  }
   104  
   105  type ConfigCenterLevel struct {
   106  	ApplicationName string
   107  	Ip              string
   108  	HostName        string
   109  	Key             string
   110  	Group           string
   111  	ConfigCenter    string
   112  	ChangeType      string
   113  }
   114  
   115  func NewConfigCenterLevel(key string, group string, configCenter string, changeType string) *ConfigCenterLevel {
   116  	return &ConfigCenterLevel{
   117  		ApplicationName: applicationName,
   118  		Ip:              common.GetLocalIp(),
   119  		HostName:        common.GetLocalHostName(),
   120  		Key:             key,
   121  		Group:           group,
   122  		ConfigCenter:    configCenter,
   123  		ChangeType:      changeType,
   124  	}
   125  }
   126  
   127  func (l ConfigCenterLevel) Tags() map[string]string {
   128  	tags := make(map[string]string)
   129  	tags[constant.TagApplicationName] = l.ApplicationName
   130  	tags[constant.TagIp] = l.Ip
   131  	tags[constant.TagHostname] = l.HostName
   132  	tags[constant.TagKey] = l.Key
   133  	tags[constant.TagGroup] = l.Group
   134  	tags[constant.TagConfigCenter] = l.ConfigCenter
   135  	tags[constant.TagChangeType] = l.ChangeType
   136  	return tags
   137  }