From 5e75c3914db3b7e25d8d65fbbb994f61c0c12895 Mon Sep 17 00:00:00 2001 From: foghost Date: Thu, 31 Aug 2023 12:17:14 +0800 Subject: [PATCH] adapt rpc module to url config --- common/constant/metric.go | 5 +++++ metrics/rpc/collector.go | 28 ++++++++++++++-------------- metrics/rpc/constant.go | 37 ------------------------------------- metrics/rpc/util.go | 19 ++++++++++--------- 4 files changed, 29 insertions(+), 60 deletions(-) delete mode 100644 metrics/rpc/constant.go diff --git a/common/constant/metric.go b/common/constant/metric.go index d1ac285598..4b35721eba 100644 --- a/common/constant/metric.go +++ b/common/constant/metric.go @@ -55,3 +55,8 @@ const ( PrometheusDefaultJobName = "default_dubbo_job" MetricFilterStartTime = "metric_filter_start_time" ) + +const ( + SideProvider = "provider" + SideConsumer = "consumer" +) diff --git a/metrics/rpc/collector.go b/metrics/rpc/collector.go index f59779f6b9..dc9fb53347 100644 --- a/metrics/rpc/collector.go +++ b/metrics/rpc/collector.go @@ -22,6 +22,7 @@ import ( ) import ( + "dubbo.apache.org/dubbo-go/v3/common" "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/metrics" ) @@ -32,8 +33,7 @@ var ( // init will add the rpc collectorFunc to metrics.collectors slice, and lazy start the rpc collector goroutine func init() { - var collectorFunc metrics.CollectorFunc - collectorFunc = func(registry metrics.MetricRegistry, c *metrics.ReporterConfig) { + collectorFunc := func(registry metrics.MetricRegistry, c *common.URL) { rc := &rpcCollector{ registry: registry, metricSet: buildMetricSet(registry), @@ -100,19 +100,19 @@ func (c *rpcCollector) afterInvokeHandler(event *metricsEvent) { func (c *rpcCollector) recordQps(role string, labels map[string]string) { switch role { - case providerField: + case constant.SideProvider: c.metricSet.provider.qpsTotal.Record(labels) - case consumerField: + case constant.SideConsumer: c.metricSet.consumer.qpsTotal.Record(labels) } } func (c *rpcCollector) incRequestsTotal(role string, labels map[string]string) { switch role { - case providerField: + case constant.SideProvider: c.metricSet.provider.requestsTotal.Inc(labels) c.metricSet.provider.requestsTotalAggregate.Inc(labels) - case consumerField: + case constant.SideConsumer: c.metricSet.consumer.requestsTotal.Inc(labels) c.metricSet.consumer.requestsTotalAggregate.Inc(labels) } @@ -120,28 +120,28 @@ func (c *rpcCollector) incRequestsTotal(role string, labels map[string]string) { func (c *rpcCollector) incRequestsProcessingTotal(role string, labels map[string]string) { switch role { - case providerField: + case constant.SideProvider: c.metricSet.provider.requestsProcessingTotal.Inc(labels) - case consumerField: + case constant.SideConsumer: c.metricSet.consumer.requestsProcessingTotal.Inc(labels) } } func (c *rpcCollector) decRequestsProcessingTotal(role string, labels map[string]string) { switch role { - case providerField: + case constant.SideProvider: c.metricSet.provider.requestsProcessingTotal.Dec(labels) - case consumerField: + case constant.SideConsumer: c.metricSet.consumer.requestsProcessingTotal.Dec(labels) } } func (c *rpcCollector) incRequestsSucceedTotal(role string, labels map[string]string) { switch role { - case providerField: + case constant.SideProvider: c.metricSet.provider.requestsSucceedTotal.Inc(labels) c.metricSet.provider.requestsSucceedTotalAggregate.Inc(labels) - case consumerField: + case constant.SideConsumer: c.metricSet.consumer.requestsSucceedTotal.Inc(labels) c.metricSet.consumer.requestsSucceedTotalAggregate.Inc(labels) } @@ -149,11 +149,11 @@ func (c *rpcCollector) incRequestsSucceedTotal(role string, labels map[string]st func (c *rpcCollector) reportRTMilliseconds(role string, labels map[string]string, cost int64) { switch role { - case providerField: + case constant.SideProvider: c.metricSet.provider.rtMilliseconds.Record(labels, float64(cost)) c.metricSet.provider.rtMillisecondsAggregate.Record(labels, float64(cost)) c.metricSet.provider.rtMillisecondsQuantiles.Record(labels, float64(cost)) - case consumerField: + case constant.SideConsumer: c.metricSet.consumer.rtMilliseconds.Record(labels, float64(cost)) c.metricSet.consumer.rtMillisecondsAggregate.Record(labels, float64(cost)) c.metricSet.consumer.rtMillisecondsQuantiles.Record(labels, float64(cost)) diff --git a/metrics/rpc/constant.go b/metrics/rpc/constant.go deleted file mode 100644 index 450febef1b..0000000000 --- a/metrics/rpc/constant.go +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package rpc - -import ( - "dubbo.apache.org/dubbo-go/v3/common/constant" -) - -const ( - applicationNameKey = constant.ApplicationNameKey - groupKey = constant.GroupKey - hostnameKey = constant.HostnameKey - interfaceKey = constant.InterfaceKey - ipKey = constant.IpKey - methodKey = constant.MethodKey - versionKey = constant.VersionKey -) - -const ( - providerField = "provider" - consumerField = "consumer" -) diff --git a/metrics/rpc/util.go b/metrics/rpc/util.go index 7422b390f9..e8437bf957 100644 --- a/metrics/rpc/util.go +++ b/metrics/rpc/util.go @@ -35,22 +35,23 @@ import ( // buildLabels will build the labels for the rpc metrics func buildLabels(url *common.URL, invocation protocol.Invocation) map[string]string { return map[string]string{ - applicationNameKey: url.GetParam(constant.ApplicationKey, ""), - groupKey: url.Group(), - hostnameKey: common.GetLocalHostName(), - interfaceKey: url.Service(), - ipKey: common.GetLocalIp(), - versionKey: url.GetParam(constant.AppVersionKey, ""), - methodKey: invocation.MethodName(), + constant.TagApplicationName: url.GetParam(constant.ApplicationKey, ""), + constant.TagApplicationVersion: url.GetParam(constant.AppVersionKey, ""), + constant.TagHostname: common.GetLocalHostName(), + constant.TagIp: common.GetLocalIp(), + constant.TagInterface: url.Service(), + constant.TagMethod: invocation.MethodName(), + constant.TagGroup: url.Group(), + constant.TagVersion: url.GetParam(constant.VersionKey, ""), } } // getRole will get the application role from the url func getRole(url *common.URL) (role string) { if isProvider(url) { - role = providerField + role = constant.SideProvider } else if isConsumer(url) { - role = consumerField + role = constant.SideConsumer } else { logger.Warnf("The url belongs neither the consumer nor the provider, "+ "so the invocation will be ignored. url: %s", url.String())