dubbo.apache.org/dubbo-go/v3@v3.1.1/metrics/rpc/util.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 rpc
    19  
    20  import (
    21  	"strconv"
    22  	"strings"
    23  )
    24  
    25  import (
    26  	"github.com/dubbogo/gost/log/logger"
    27  )
    28  
    29  import (
    30  	"dubbo.apache.org/dubbo-go/v3/common"
    31  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    32  	"dubbo.apache.org/dubbo-go/v3/protocol"
    33  )
    34  
    35  // buildLabels will build the labels for the rpc metrics
    36  func buildLabels(url *common.URL, invocation protocol.Invocation) map[string]string {
    37  	return map[string]string{
    38  		constant.TagApplicationName:    url.GetParam(constant.ApplicationKey, ""),
    39  		constant.TagApplicationVersion: url.GetParam(constant.AppVersionKey, ""),
    40  		constant.TagHostname:           common.GetLocalHostName(),
    41  		constant.TagIp:                 common.GetLocalIp(),
    42  		constant.TagInterface:          url.Service(),
    43  		constant.TagMethod:             invocation.MethodName(),
    44  		constant.TagGroup:              url.Group(),
    45  		constant.TagVersion:            url.GetParam(constant.VersionKey, ""),
    46  	}
    47  }
    48  
    49  // getRole will get the application role from the url
    50  func getRole(url *common.URL) (role string) {
    51  	if isProvider(url) {
    52  		role = constant.SideProvider
    53  	} else if isConsumer(url) {
    54  		role = constant.SideConsumer
    55  	} else {
    56  		logger.Warnf("The url belongs neither the consumer nor the provider, "+
    57  			"so the invocation will be ignored. url: %s", url.String())
    58  	}
    59  	return
    60  }
    61  
    62  // isProvider shows whether this url represents the application received the request as server
    63  func isProvider(url *common.URL) bool {
    64  	role := url.GetParam(constant.RegistryRoleKey, "")
    65  	return strings.EqualFold(role, strconv.Itoa(common.PROVIDER))
    66  }
    67  
    68  // isConsumer shows whether this url represents the application sent then request as client
    69  func isConsumer(url *common.URL) bool {
    70  	role := url.GetParam(constant.RegistryRoleKey, "")
    71  	return strings.EqualFold(role, strconv.Itoa(common.CONSUMER))
    72  }