Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge branch feature-metrics #2367

Merged
merged 8 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ const (
const (
ApplicationKey = "application"
ApplicationNameKey = "application_name"
ApplicationVersionKey = "application_version"
HostnameKey = "hostname"
IpKey = "ip"
OrganizationKey = "organization"
Expand All @@ -197,6 +198,10 @@ const (
ProvidersCategory = "providers"
RouterKey = "router"
ExportKey = "export"
GitCommitIdKey = "git_commit_id"
ConfigCenterKey = "config_center"
ChangeTypeKey = "change_type"
KeyKey = "key"
)

// config center keys
Expand Down Expand Up @@ -404,3 +409,10 @@ const (
LoggerFileLocalTimeKey = "logger.file.local-time"
LoggerFileCompressKey = "logger.file.compress"
)

// metrics key
const (
MetricsRegistry = "dubbo.metrics.registry"
MetricsMetadata = "dubbo.metrics.metadata"
MetricApp = "dubbo.metrics.app"
)
14 changes: 14 additions & 0 deletions common/host_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

import (
"github.com/dubbogo/gost/log/logger"
gxnet "github.com/dubbogo/gost/net"
)

Expand All @@ -31,6 +32,7 @@ import (
)

var localIp string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果有多个 var 写在一块,要写成

“ var (
)”

这种形式

var localHostname string

func GetLocalIp() string {
if len(localIp) != 0 {
Expand All @@ -40,6 +42,18 @@ func GetLocalIp() string {
return localIp
}

func GetLocalHostName() string {
if len(localHostname) != 0 {
return localHostname
}
hostname, err := os.Hostname()
if err != nil {
logger.Errorf("can not get local hostname")
}
localHostname = hostname
return localHostname
}

func HandleRegisterIPAndPort(url *URL) {
// if developer define registry port and ip, use it first.
if ipToRegistry := os.Getenv(constant.DubboIpToRegistryKey); len(ipToRegistry) > 0 {
Expand Down
4 changes: 4 additions & 0 deletions config/config_center_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/config_center"
"dubbo.apache.org/dubbo-go/v3/metrics"
metricsConfigCenter "dubbo.apache.org/dubbo-go/v3/metrics/config_center"
"dubbo.apache.org/dubbo-go/v3/remoting"
)

// CenterConfig is configuration for config center
Expand Down Expand Up @@ -146,6 +149,7 @@ func startConfigCenter(rc *RootConfig) error {
logger.Warnf("[Config Center] Dynamic config center has started, but config may not be initialized, because: %s", err)
return nil
}
defer metrics.Publish(metricsConfigCenter.NewIncMetricEvent(cc.DataId, cc.Group, remoting.EventTypeAdd, cc.Protocol))
if len(strConf) == 0 {
logger.Warnf("[Config Center] Dynamic config center has started, but got empty config with config-center configuration %+v\n"+
"Please check if your config-center config is correct.", cc)
Expand Down
9 changes: 4 additions & 5 deletions config/instance/metadata_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ var (
)

func GetMetadataReportInstance() report.MetadataReport {
if instance != nil {
return instance
if instance == nil {
instance = report.NewPubMetricEventReport(GetMetadataReportByRegistryProtocol(""))
}

return GetMetadataReportByRegistryProtocol("")
return instance
}

// SetMetadataReportInstance, init metadat report instance
Expand All @@ -49,7 +48,7 @@ func SetMetadataReportInstance(selectiveUrl ...*common.URL) {
url = selectiveUrl[0]
fac := extension.GetMetadataReportFactory(url.Protocol)
if fac != nil {
instance = fac.CreateMetadataReport(url)
instance = report.NewPubMetricEventReport(fac.CreateMetadataReport(url))
}
reportUrl = url
}
Expand Down
9 changes: 7 additions & 2 deletions config/metric_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type MetricConfig struct {
Path string `default:"/metrics" yaml:"path" json:"path,omitempty" property:"path"`
PushGatewayAddress string `default:"" yaml:"push-gateway-address" json:"push-gateway-address,omitempty" property:"push-gateway-address"`
SummaryMaxAge int64 `default:"600000000000" yaml:"summary-max-age" json:"summary-max-age,omitempty" property:"summary-max-age"`
Protocol string `default:"prometheus" yaml:"protocol" json:"protocol,omitempty" property:"protocol"`
}

func (mc *MetricConfig) ToReporterConfig() *metrics.ReporterConfig {
Expand All @@ -55,6 +56,7 @@ func (mc *MetricConfig) ToReporterConfig() *metrics.ReporterConfig {
defaultMetricsReportConfig.Path = mc.Path
defaultMetricsReportConfig.PushGatewayAddress = mc.PushGatewayAddress
defaultMetricsReportConfig.SummaryMaxAge = mc.SummaryMaxAge
defaultMetricsReportConfig.Protocol = mc.Protocol
return defaultMetricsReportConfig
}

Expand All @@ -68,7 +70,10 @@ func (mc *MetricConfig) Init() error {
if err := verify(mc); err != nil {
return err
}
extension.GetMetricReporter("prometheus", mc.ToReporterConfig())
metrics.InitAppInfo(GetRootConfig().Application.Name, GetRootConfig().Application.Version)
config := mc.ToReporterConfig()
extension.GetMetricReporter(mc.Protocol, config)
metrics.Init(config)
return nil
}

Expand All @@ -91,7 +96,7 @@ func (mc *MetricConfig) DynamicUpdateProperties(newMetricConfig *MetricConfig) {
mc.Enable = newMetricConfig.Enable
logger.Infof("MetricConfig's Enable was dynamically updated, new value:%v", mc.Enable)

extension.GetMetricReporter("prometheus", mc.ToReporterConfig())
extension.GetMetricReporter(mc.Protocol, mc.ToReporterConfig())
}
}
}
5 changes: 4 additions & 1 deletion config_center/nacos/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/config_center"
"dubbo.apache.org/dubbo-go/v3/metrics"
metricsConfigCenter "dubbo.apache.org/dubbo-go/v3/metrics/config_center"
"dubbo.apache.org/dubbo-go/v3/remoting"
)

func callback(listener config_center.ConfigurationListener, _, _, dataId, data string) {
func callback(listener config_center.ConfigurationListener, _, group, dataId, data string) {
listener.Process(&config_center.ConfigChangeEvent{Key: dataId, Value: data, ConfigType: remoting.EventTypeUpdate})
metrics.Publish(metricsConfigCenter.NewIncMetricEvent(dataId, group, remoting.EventTypeUpdate, metricsConfigCenter.Nacos))
}

func (n *nacosDynamicConfiguration) addListener(key string, listener config_center.ConfigurationListener) {
Expand Down
13 changes: 9 additions & 4 deletions config_center/zookeeper/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/config_center"
"dubbo.apache.org/dubbo-go/v3/metrics"
metricsConfigCenter "dubbo.apache.org/dubbo-go/v3/metrics/config_center"
"dubbo.apache.org/dubbo-go/v3/remoting"
"dubbo.apache.org/dubbo-go/v3/remoting/zookeeper"
)
Expand Down Expand Up @@ -73,10 +75,12 @@ func (l *CacheListener) DataChange(event remoting.Event) bool {
changeType = remoting.EventTypeDel
}

key, group := l.pathToKeyGroup(event.Path)
defer metrics.Publish(metricsConfigCenter.NewIncMetricEvent(key, group, changeType, metricsConfigCenter.Zookeeper))
if listeners, ok := l.keyListeners.Load(event.Path); ok {
for listener := range listeners.(map[config_center.ConfigurationListener]struct{}) {
listener.Process(&config_center.ConfigChangeEvent{
Key: l.pathToKey(event.Path),
Key: key,
Value: event.Content,
ConfigType: changeType,
})
Expand All @@ -86,10 +90,11 @@ func (l *CacheListener) DataChange(event remoting.Event) bool {
return false
}

func (l *CacheListener) pathToKey(path string) string {
func (l *CacheListener) pathToKeyGroup(path string) (string, string) {
if len(path) == 0 {
return path
return path, ""
}
groupKey := strings.Replace(strings.Replace(path, l.rootPath+constant.PathSeparator, "", -1), constant.PathSeparator, constant.DotSeparator, -1)
return groupKey[strings.Index(groupKey, constant.DotSeparator)+1:]
index := strings.Index(groupKey, constant.DotSeparator)
return groupKey[index+1:], groupKey[0:index]
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/polarismesh/polaris-go v1.3.0
github.com/prometheus/client_golang v1.13.0
github.com/prometheus/common v0.37.0
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.8.2
Expand Down
1 change: 1 addition & 0 deletions imports/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import (
_ "dubbo.apache.org/dubbo-go/v3/metadata/service/exporter/configurable"
_ "dubbo.apache.org/dubbo-go/v3/metadata/service/local"
_ "dubbo.apache.org/dubbo-go/v3/metadata/service/remote"
_ "dubbo.apache.org/dubbo-go/v3/metrics/app_info"
_ "dubbo.apache.org/dubbo-go/v3/metrics/prometheus"
_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo"
_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo3"
Expand Down
66 changes: 66 additions & 0 deletions metadata/report/reporter_metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 report

import (
"time"
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/metadata/identifier"
"dubbo.apache.org/dubbo-go/v3/metrics"
"dubbo.apache.org/dubbo-go/v3/metrics/metadata"
)

type PubMetricEventReport struct {
MetadataReport
}

func NewPubMetricEventReport(r MetadataReport) MetadataReport {
return &PubMetricEventReport{MetadataReport: r}
}

func (r *PubMetricEventReport) StoreProviderMetadata(i *identifier.MetadataIdentifier, s string) error {
event := metadata.NewMetadataMetricTimeEvent(metadata.StoreProvider)
err := r.MetadataReport.StoreProviderMetadata(i, s)
event.Succ = err == nil
event.End = time.Now()
event.Attachment[constant.InterfaceKey] = i.ServiceInterface
metrics.Publish(event)
return err
}

func (r *PubMetricEventReport) GetAppMetadata(i *identifier.SubscriberMetadataIdentifier) (*common.MetadataInfo, error) {
event := metadata.NewMetadataMetricTimeEvent(metadata.MetadataSub)
info, err := r.MetadataReport.GetAppMetadata(i)
event.Succ = err == nil
event.End = time.Now()
metrics.Publish(event)
return info, err
}

func (r *PubMetricEventReport) PublishAppMetadata(i *identifier.SubscriberMetadataIdentifier, info *common.MetadataInfo) error {
event := metadata.NewMetadataMetricTimeEvent(metadata.MetadataPush)
err := r.MetadataReport.PublishAppMetadata(i, info)
event.Succ = err == nil
event.End = time.Now()
metrics.Publish(event)
return err
}
Loading
Loading