Skip to content

Commit

Permalink
Merge 298d4ea into bdf5b70
Browse files Browse the repository at this point in the history
  • Loading branch information
FoghostCn committed Aug 30, 2023
2 parents bdf5b70 + 298d4ea commit 8b03a45
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 52 deletions.
24 changes: 13 additions & 11 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,9 +404,19 @@ const (

// metrics key
const (
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
56 changes: 56 additions & 0 deletions common/constant/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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"
)

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"
)
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
33 changes: 22 additions & 11 deletions metrics/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,48 @@

package metrics

import (
"sync"
)

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

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, ""))
// defalut 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
}

// AddCollector add more indicators, like metadata、sla、configcenter etc
func AddCollector(name string, fun func(MetricRegistry, *ReporterConfig)) {
func AddCollector(name string, fun func(MetricRegistry, *common.URL)) {
collectors = append(collectors, fun)
}

Expand All @@ -74,7 +85,7 @@ type RtOpts struct {
// rs []MetricRegistry
// }

// Type metric type, save with micrometer
// Type metric type, same with micrometer
type Type uint8

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
6 changes: 3 additions & 3 deletions metrics/prometheus/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (

const (
reporterName = "prometheus"
applicationNameKey = constant.ApplicationNameKey
applicationNameKey = constant.TagApplicationName
groupKey = constant.GroupKey
hostnameKey = constant.HostnameKey
hostnameKey = constant.TagHostname
interfaceKey = constant.InterfaceKey
ipKey = constant.IpKey
ipKey = constant.TagIp
methodKey = constant.MethodKey
versionKey = constant.VersionKey
)
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
Loading

0 comments on commit 8b03a45

Please sign in to comment.