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

refactor: change metric config to url like other module #2396

Merged
merged 2 commits into from
Sep 2, 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
25 changes: 13 additions & 12 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,6 @@ const (

const (
ApplicationKey = "application"
ApplicationNameKey = "application_name"
ApplicationVersionKey = "application_version"
HostnameKey = "hostname"
IpKey = "ip"
OrganizationKey = "organization"
NameKey = "name"
ModuleKey = "module"
Expand All @@ -198,10 +194,6 @@ 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 @@ -412,10 +404,19 @@ const (

// metrics key
const (
MetricsRpc = "dubbo.metrics.rpc"
MetricsRegistry = "dubbo.metrics.registry"
MetricsMetadata = "dubbo.metrics.metadata"
MetricApp = "dubbo.metrics.app"
AggregationEnabledKey = "aggregation.enabled"
AggregationBucketNumKey = "aggregation.bucket.num"
AggregationTimeWindowSecondsKey = "aggregation.time.window.seconds"
HistogramEnabledKey = "histogram.enabled"
PrometheusExporterEnabledKey = "prometheus.exporter.enabled"
PrometheusExporterMetricsPortKey = "prometheus.exporter.metrics.port"
PrometheusExporterMetricsPathKey = "prometheus.exporter.metrics.path"
PrometheusPushgatewayEnabledKey = "prometheus.pushgateway.enabled"
PrometheusPushgatewayBaseUrlKey = "prometheus.pushgateway.base.url"
PrometheusPushgatewayUsernameKey = "prometheus.pushgateway.username"
PrometheusPushgatewayPasswordKey = "prometheus.pushgateway.password"
PrometheusPushgatewayPushIntervalKey = "prometheus.pushgateway.push.interval"
PrometheusPushgatewayJobKey = "prometheus.pushgateway.job"
)

// default meta cache config
Expand Down
62 changes: 62 additions & 0 deletions common/constant/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 constant

// metrics type
const (
MetricsRegistry = "dubbo.metrics.registry"
MetricsMetadata = "dubbo.metrics.metadata"
MetricsApp = "dubbo.metrics.app"
MetricsConfigCenter = "dubbo.metrics.configCenter"
MetricsRpc = "dubbo.metrics.rpc"
)

const (
TagApplicationName = "application_name"
TagApplicationVersion = "application_version"
TagHostname = "hostname"
TagIp = "ip"
TagGitCommitId = "git_commit_id"
TagConfigCenter = "config_center"
TagChangeType = "change_type"
TagKey = "key"
TagPid = "pid"
TagInterface = "interface"
TagMethod = "method"
TagGroup = "group"
TagVersion = "version"
TagErrorCode = "error"
)
const (
MetricNamespace = "dubbo"
ProtocolPrometheus = "prometheus"
ProtocolDefault = ProtocolPrometheus
AggregationCollectorKey = "aggregation"
AggregationDefaultBucketNum = 10
AggregationDefaultTimeWindowSeconds = 120
PrometheusDefaultMetricsPath = "/metrics"
PrometheusDefaultMetricsPort = "9090"
PrometheusDefaultPushInterval = 30
PrometheusDefaultJobName = "default_dubbo_job"
MetricFilterStartTime = "metric_filter_start_time"
)

const (
SideProvider = "provider"
SideConsumer = "consumer"
)
24 changes: 21 additions & 3 deletions config/metric_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package config

import (
"strconv"
)

import (
"github.com/creasty/defaults"

Expand All @@ -26,6 +30,8 @@ import (
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/metrics"
)
Expand All @@ -40,6 +46,7 @@ type MetricConfig struct {
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"`
rootConfig *RootConfig
}

func (mc *MetricConfig) ToReporterConfig() *metrics.ReporterConfig {
Expand All @@ -60,7 +67,7 @@ func (mc *MetricConfig) ToReporterConfig() *metrics.ReporterConfig {
return defaultMetricsReportConfig
}

func (mc *MetricConfig) Init() error {
func (mc *MetricConfig) Init(rc *RootConfig) error {
if mc == nil {
return errors.New("metrics config is null")
}
Expand All @@ -70,10 +77,10 @@ func (mc *MetricConfig) Init() error {
if err := verify(mc); err != nil {
return err
}
metrics.InitAppInfo(GetRootConfig().Application.Name, GetRootConfig().Application.Version)
mc.rootConfig = rc
config := mc.ToReporterConfig()
extension.GetMetricReporter(mc.Protocol, config)
metrics.Init(config)
metrics.Init(mc.toURL())
return nil
}

Expand All @@ -100,3 +107,14 @@ func (mc *MetricConfig) DynamicUpdateProperties(newMetricConfig *MetricConfig) {
}
}
}

// prometheus://localhost:9090?&histogram.enabled=false&prometheus.exporter.enabled=false
func (mc *MetricConfig) toURL() *common.URL {
url, _ := common.NewURL("localhost", common.WithProtocol(mc.Protocol))
url.SetParam(constant.PrometheusExporterEnabledKey, strconv.FormatBool(*mc.Enable))
url.SetParam(constant.PrometheusExporterMetricsPortKey, mc.Port)
url.SetParam(constant.PrometheusExporterMetricsPathKey, mc.Path)
url.SetParam(constant.ApplicationKey, mc.rootConfig.Application.Name)
url.SetParam(constant.AppVersionKey, mc.rootConfig.Application.Version)
return url
}
2 changes: 1 addition & 1 deletion config/metric_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

func TestMetricConfigBuilder(t *testing.T) {
config := NewMetricConfigBuilder().Build()
err := config.Init()
err := config.Init(&RootConfig{Application: &ApplicationConfig{Name: "dubbo", Version: "1.0.0"}})
assert.NoError(t, err)
reporterConfig := config.ToReporterConfig()
assert.Equal(t, string(reporterConfig.Mode), "pull")
Expand Down
2 changes: 1 addition & 1 deletion config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (rc *RootConfig) Init() error {
if err := rc.Otel.Init(rc.Application); err != nil {
return err
}
if err := rc.Metric.Init(); err != nil {
if err := rc.Metric.Init(rc); err != nil {
return err
}
for _, t := range rc.Tracing {
Expand Down
24 changes: 14 additions & 10 deletions metrics/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/metrics/util/aggregate"
)

Expand All @@ -37,31 +39,33 @@ const (
)

var (
registries = make(map[string]func(*ReporterConfig) MetricRegistry)
registries = make(map[string]func(*common.URL) MetricRegistry)
collectors = make([]CollectorFunc, 0)
registry MetricRegistry
once sync.Once
)

// CollectorFunc used to extend more indicators
type CollectorFunc func(MetricRegistry, *ReporterConfig)
type CollectorFunc func(MetricRegistry, *common.URL)

// Init Metrics module
func Init(config *ReporterConfig) {
if config.Enable {
func Init(url *common.URL) {
once.Do(func() {
InitAppInfo(url.GetParam(constant.ApplicationKey, ""), url.GetParam(constant.AppVersionKey, ""))
// default protocol is already set in metricConfig
regFunc, ok := registries[config.Protocol]
regFunc, ok := registries[url.Protocol]
if ok {
registry = regFunc(config)
registry = regFunc(url)
for _, co := range collectors {
co(registry, config)
co(registry, url)
}
registry.Export()
}
}
})
}

// SetRegistry extend more MetricRegistry, default PrometheusRegistry
func SetRegistry(name string, v func(*ReporterConfig) MetricRegistry) {
func SetRegistry(name string, v func(*common.URL) MetricRegistry) {
registries[name] = v
}

Expand Down Expand Up @@ -93,7 +97,7 @@ type RtOpts struct {
// rs []MetricRegistry
// }

// Type metric type, save with micrometer
// Type metric type, same with micrometer
type Type uint8 // TODO check if Type is is useful

const (
Expand Down
3 changes: 2 additions & 1 deletion metrics/app_info/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package app_info

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

Expand All @@ -29,7 +30,7 @@ import (
var info = metrics.NewMetricKey("dubbo_application_info_total", "Total Application Info") // Total Application Info include application name、version etc

func init() {
metrics.AddCollector("application_info", func(mr metrics.MetricRegistry, config *metrics.ReporterConfig) {
metrics.AddCollector("application_info", func(mr metrics.MetricRegistry, _ *common.URL) {
mr.Counter(&metrics.MetricId{Name: info.Name, Desc: info.Desc, Tags: metrics.GetApplicationLevel().Tags()}).Inc()
})
}
32 changes: 16 additions & 16 deletions metrics/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func GetApplicationLevel() *ApplicationMetricLevel {

func (m *ApplicationMetricLevel) Tags() map[string]string {
tags := make(map[string]string)
tags[constant.IpKey] = m.Ip
tags[constant.HostnameKey] = m.HostName
tags[constant.ApplicationNameKey] = m.ApplicationName
tags[constant.ApplicationVersionKey] = m.Version
tags[constant.GitCommitIdKey] = m.GitCommitId
tags[constant.TagIp] = m.Ip
tags[constant.TagHostname] = m.HostName
tags[constant.TagApplicationName] = m.ApplicationName
tags[constant.TagApplicationVersion] = m.Version
tags[constant.TagGitCommitId] = m.GitCommitId
return tags
}

Expand All @@ -83,7 +83,7 @@ func NewServiceMetric(interfaceName string) *ServiceMetricLevel {

func (m ServiceMetricLevel) Tags() map[string]string {
tags := m.ApplicationMetricLevel.Tags()
tags[constant.InterfaceKey] = m.Interface
tags[constant.TagInterface] = m.Interface
return tags
}

Expand All @@ -96,9 +96,9 @@ type MethodMetricLevel struct {

func (m MethodMetricLevel) Tags() map[string]string {
tags := m.ServiceMetricLevel.Tags()
tags[constant.MethodKey] = m.Method
tags[constant.GroupKey] = m.Group
tags[constant.VersionKey] = m.Version
tags[constant.TagMethod] = m.Method
tags[constant.TagGroup] = m.Group
tags[constant.TagVersion] = m.Version
return tags
}

Expand Down Expand Up @@ -126,12 +126,12 @@ func NewConfigCenterLevel(key string, group string, configCenter string, changeT

func (l ConfigCenterLevel) Tags() map[string]string {
tags := make(map[string]string)
tags[constant.ApplicationKey] = l.ApplicationName
tags[constant.IpKey] = l.Ip
tags[constant.HostnameKey] = l.HostName
tags[constant.KeyKey] = l.Key
tags[constant.GroupKey] = l.Group
tags[constant.ConfigCenterKey] = l.ConfigCenter
tags[constant.ChangeTypeKey] = l.ChangeType
tags[constant.TagApplicationName] = l.ApplicationName
tags[constant.TagIp] = l.Ip
tags[constant.TagHostname] = l.HostName
tags[constant.TagKey] = l.Key
tags[constant.TagGroup] = l.Group
tags[constant.TagConfigCenter] = l.ConfigCenter
tags[constant.TagChangeType] = l.ChangeType
return tags
}
5 changes: 3 additions & 2 deletions metrics/config_center/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
package metrics

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

const eventType = constant.MetricApp
const eventType = constant.MetricsConfigCenter

var ch = make(chan metrics.MetricsEvent, 10)
var info = metrics.NewMetricKey("dubbo_configcenter_total", "Config Changed Total")

func init() {
metrics.AddCollector("application_info", func(mr metrics.MetricRegistry, config *metrics.ReporterConfig) {
metrics.AddCollector("config_center", func(mr metrics.MetricRegistry, _ *common.URL) {
c := &configCenterCollector{r: mr}
c.start()
})
Expand Down
3 changes: 2 additions & 1 deletion metrics/metadata/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -31,7 +32,7 @@ const eventType = constant.MetricsMetadata
var ch = make(chan metrics.MetricsEvent, 10)

func init() {
metrics.AddCollector("metadata", func(mr metrics.MetricRegistry, rc *metrics.ReporterConfig) {
metrics.AddCollector("metadata", func(mr metrics.MetricRegistry, _ *common.URL) {
l := &MetadataMetricCollector{metrics.BaseCollector{R: mr}}
l.start()
})
Expand Down
4 changes: 3 additions & 1 deletion metrics/prometheus/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ import (
)

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

func init() {
metrics.SetRegistry("prometheus", func(rc *metrics.ReporterConfig) metrics.MetricRegistry {
metrics.SetRegistry(constant.ProtocolPrometheus, func(url *common.URL) metrics.MetricRegistry {
return &promMetricRegistry{r: prom.DefaultRegisterer}
})
}
Expand Down
3 changes: 2 additions & 1 deletion metrics/registry/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package registry

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/metrics"
)
Expand All @@ -27,7 +28,7 @@ var (
)

func init() {
metrics.AddCollector("registry", func(m metrics.MetricRegistry, c *metrics.ReporterConfig) {
metrics.AddCollector("registry", func(m metrics.MetricRegistry, _ *common.URL) {
rc := &registryCollector{metrics.BaseCollector{R: m}}
go rc.start()
})
Expand Down
Loading
Loading