dubbo.apache.org/dubbo-go/v3@v3.1.1/config/metric_config.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 config 19 20 import ( 21 "strconv" 22 ) 23 24 import ( 25 "github.com/creasty/defaults" 26 27 "github.com/pkg/errors" 28 ) 29 30 import ( 31 "dubbo.apache.org/dubbo-go/v3/common" 32 "dubbo.apache.org/dubbo-go/v3/common/constant" 33 "dubbo.apache.org/dubbo-go/v3/metrics" 34 ) 35 36 // MetricConfig This is the config struct for all metrics implementation 37 type MetricConfig struct { 38 Enable *bool `default:"false" yaml:"enable" json:"enable,omitempty" property:"enable"` 39 Port string `default:"9090" yaml:"port" json:"port,omitempty" property:"port"` 40 Path string `default:"/metrics" yaml:"path" json:"path,omitempty" property:"path"` 41 Protocol string `default:"prometheus" yaml:"protocol" json:"protocol,omitempty" property:"protocol"` 42 EnableMetadata *bool `default:"false" yaml:"enable-metadata" json:"enable-metadata,omitempty" property:"enable-metadata"` 43 EnableRegistry *bool `default:"false" yaml:"enable-registry" json:"enable-registry,omitempty" property:"enable-registry"` 44 EnableConfigCenter *bool `default:"false" yaml:"enable-config-center" json:"enable-config-center,omitempty" property:"enable-config-center"` 45 Prometheus *PrometheusConfig `yaml:"prometheus" json:"prometheus" property:"prometheus"` 46 Aggregation *AggregateConfig `yaml:"aggregation" json:"aggregation" property:"aggregation"` 47 rootConfig *RootConfig 48 } 49 50 type AggregateConfig struct { 51 Enabled *bool `default:"false" yaml:"enabled" json:"enabled,omitempty" property:"enabled"` 52 BucketNum int `default:"10" yaml:"bucket-num" json:"bucket-num,omitempty" property:"bucket-num"` 53 TimeWindowSeconds int `default:"120" yaml:"time-window-seconds" json:"time-window-seconds,omitempty" property:"time-window-seconds"` 54 } 55 56 type PrometheusConfig struct { 57 Exporter *Exporter `yaml:"exporter" json:"exporter,omitempty" property:"exporter"` 58 Pushgateway *PushgatewayConfig `yaml:"pushgateway" json:"pushgateway,omitempty" property:"pushgateway"` 59 } 60 61 type Exporter struct { 62 Enabled *bool `default:"true" yaml:"enabled" json:"enabled,omitempty" property:"enabled"` 63 } 64 65 type PushgatewayConfig struct { 66 Enabled *bool `default:"false" yaml:"enabled" json:"enabled,omitempty" property:"enabled"` 67 BaseUrl string `default:"" yaml:"base-url" json:"base-url,omitempty" property:"base-url"` 68 Job string `default:"default_dubbo_job" yaml:"job" json:"job,omitempty" property:"job"` 69 Username string `default:"" yaml:"username" json:"username,omitempty" property:"username"` 70 Password string `default:"" yaml:"password" json:"password,omitempty" property:"password"` 71 PushInterval int `default:"30" yaml:"push-interval" json:"push-interval,omitempty" property:"push-interval"` 72 } 73 74 func (mc *MetricConfig) ToReporterConfig() *metrics.ReporterConfig { 75 defaultMetricsReportConfig := metrics.NewReporterConfig() 76 77 defaultMetricsReportConfig.Enable = *mc.Enable 78 defaultMetricsReportConfig.Port = mc.Port 79 defaultMetricsReportConfig.Path = mc.Path 80 defaultMetricsReportConfig.Protocol = mc.Protocol 81 return defaultMetricsReportConfig 82 } 83 84 func (mc *MetricConfig) Init(rc *RootConfig) error { 85 if mc == nil { 86 return errors.New("metrics config is null") 87 } 88 if err := defaults.Set(mc); err != nil { 89 return err 90 } 91 if err := verify(mc); err != nil { 92 return err 93 } 94 mc.rootConfig = rc 95 if *mc.Enable { 96 metrics.Init(mc.toURL()) 97 } 98 return nil 99 } 100 101 type MetricConfigBuilder struct { 102 metricConfig *MetricConfig 103 } 104 105 func NewMetricConfigBuilder() *MetricConfigBuilder { 106 return &MetricConfigBuilder{metricConfig: &MetricConfig{}} 107 } 108 109 func (mcb *MetricConfigBuilder) SetMetadataEnabled(enabled bool) *MetricConfigBuilder { 110 mcb.metricConfig.EnableMetadata = &enabled 111 return mcb 112 } 113 114 func (mcb *MetricConfigBuilder) SetRegistryEnabled(enabled bool) *MetricConfigBuilder { 115 mcb.metricConfig.EnableRegistry = &enabled 116 return mcb 117 } 118 119 func (mcb *MetricConfigBuilder) SetConfigCenterEnabled(enabled bool) *MetricConfigBuilder { 120 mcb.metricConfig.EnableConfigCenter = &enabled 121 return mcb 122 } 123 124 func (mcb *MetricConfigBuilder) Build() *MetricConfig { 125 return mcb.metricConfig 126 } 127 128 // DynamicUpdateProperties dynamically update properties. 129 func (mc *MetricConfig) DynamicUpdateProperties(newMetricConfig *MetricConfig) { 130 // TODO update 131 } 132 133 // prometheus://localhost:9090?&histogram.enabled=false&prometheus.exporter.enabled=false 134 func (mc *MetricConfig) toURL() *common.URL { 135 url, _ := common.NewURL("localhost", common.WithProtocol(mc.Protocol)) 136 url.SetParam(constant.PrometheusExporterMetricsPortKey, mc.Port) 137 url.SetParam(constant.PrometheusExporterMetricsPathKey, mc.Path) 138 url.SetParam(constant.ApplicationKey, mc.rootConfig.Application.Name) 139 url.SetParam(constant.AppVersionKey, mc.rootConfig.Application.Version) 140 url.SetParam(constant.RpcEnabledKey, strconv.FormatBool(*mc.Enable)) 141 url.SetParam(constant.MetadataEnabledKey, strconv.FormatBool(*mc.EnableMetadata)) 142 url.SetParam(constant.RegistryEnabledKey, strconv.FormatBool(*mc.EnableRegistry)) 143 url.SetParam(constant.ConfigCenterEnabledKey, strconv.FormatBool(*mc.EnableConfigCenter)) 144 if mc.Aggregation != nil { 145 url.SetParam(constant.AggregationEnabledKey, strconv.FormatBool(*mc.Aggregation.Enabled)) 146 url.SetParam(constant.AggregationBucketNumKey, strconv.Itoa(mc.Aggregation.BucketNum)) 147 url.SetParam(constant.AggregationTimeWindowSecondsKey, strconv.Itoa(mc.Aggregation.TimeWindowSeconds)) 148 } 149 if mc.Prometheus != nil { 150 if mc.Prometheus.Exporter != nil { 151 exporter := mc.Prometheus.Exporter 152 url.SetParam(constant.PrometheusExporterEnabledKey, strconv.FormatBool(*exporter.Enabled)) 153 } 154 if mc.Prometheus.Pushgateway != nil { 155 pushGateWay := mc.Prometheus.Pushgateway 156 url.SetParam(constant.PrometheusPushgatewayEnabledKey, strconv.FormatBool(*pushGateWay.Enabled)) 157 url.SetParam(constant.PrometheusPushgatewayBaseUrlKey, pushGateWay.BaseUrl) 158 url.SetParam(constant.PrometheusPushgatewayUsernameKey, pushGateWay.Username) 159 url.SetParam(constant.PrometheusPushgatewayPasswordKey, pushGateWay.Password) 160 url.SetParam(constant.PrometheusPushgatewayPushIntervalKey, strconv.Itoa(pushGateWay.PushInterval)) 161 url.SetParam(constant.PrometheusPushgatewayJobKey, pushGateWay.Job) 162 } 163 } 164 return url 165 }