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

Ftr: Prometheus Support —— Summary & Histogram #342

Merged
merged 16 commits into from
Feb 5, 2020
26 changes: 26 additions & 0 deletions common/constant/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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

import (
"time"
)

var (
MsToNanoRate = int64(time.Millisecond / time.Nanosecond)
)
44 changes: 44 additions & 0 deletions common/extension/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 extension

import (
"github.com/apache/dubbo-go/metrics"
)

var (
// we couldn't store the instance because the some instance may initialize before loading configuration
// so lazy initialization will be better.
metricReporterMap = make(map[string]func() metrics.Reporter, 4)
)

// SetMetricReporter set a reporter with the name
func SetMetricReporter(name string, reporterFunc func() metrics.Reporter) {
flycash marked this conversation as resolved.
Show resolved Hide resolved
metricReporterMap[name] = reporterFunc
}

// GetMetricReporter find the reporter with name.
// if not found, it will panic.
// we should know that this method usually is called when system starts, so we should panic
func GetMetricReporter(name string) metrics.Reporter {
reporterFunc, found := metricReporterMap[name]
if !found {
panic("Cannot find the reporter with name: " + name)
}
return reporterFunc()
}
49 changes: 49 additions & 0 deletions common/extension/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 extension

import (
"context"
"testing"
"time"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/apache/dubbo-go/metrics"
"github.com/apache/dubbo-go/protocol"
)

func TestGetMetricReporter(t *testing.T) {
reporter := &mockReporter{}
name := "mock"
SetMetricReporter(name, func() metrics.Reporter {
return reporter
})
res := GetMetricReporter(name)
assert.Equal(t, reporter, res)
}

type mockReporter struct {
}

func (m mockReporter) Report(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation, cost time.Duration, res protocol.Result) {
}
4 changes: 3 additions & 1 deletion config/base_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ type multiConfiger interface {
Prefix() string
}

// BaseConfig ...
// BaseConfig is the common configuration for provider and consumer
type BaseConfig struct {
ConfigCenterConfig *ConfigCenterConfig `yaml:"config_center" json:"config_center,omitempty"`
configCenterUrl *common.URL
prefix string
fatherConfig interface{}

MetricConfig *MetricConfig `yaml:"metrics" json:"metrics,omitempty"`
}

func (c *BaseConfig) startConfigCenter(ctx context.Context) error {
Expand Down
56 changes: 53 additions & 3 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ import (
)

var (
consumerConfig *ConsumerConfig
providerConfig *ProviderConfig
maxWait = 3
consumerConfig *ConsumerConfig
providerConfig *ProviderConfig
metricConfig *MetricConfig
applicationConfig *ApplicationConfig
flycash marked this conversation as resolved.
Show resolved Hide resolved
maxWait = 3
)

// loaded consumer & provider config from xxx.yml, and log config from xxx.xml
Expand Down Expand Up @@ -75,6 +77,10 @@ func Load() {
if consumerConfig == nil {
logger.Warnf("consumerConfig is nil!")
} else {

metricConfig = consumerConfig.MetricConfig
applicationConfig = consumerConfig.ApplicationConfig

checkApplicationName(consumerConfig.ApplicationConfig)
if err := configCenterRefreshConsumer(); err != nil {
logger.Errorf("[consumer config center refresh] %#v", err)
Expand Down Expand Up @@ -131,6 +137,11 @@ func Load() {
if providerConfig == nil {
logger.Warnf("providerConfig is nil!")
} else {

// so, you should know that the consumer's config will be override
metricConfig = providerConfig.MetricConfig
applicationConfig = providerConfig.ApplicationConfig

checkApplicationName(providerConfig.ApplicationConfig)
if err := configCenterRefreshProvider(); err != nil {
logger.Errorf("[provider config center refresh] %#v", err)
Expand Down Expand Up @@ -162,3 +173,42 @@ func GetRPCService(name string) common.RPCService {
func RPCService(service common.RPCService) {
consumerConfig.References[service.Reference()].Implement(service)
}

// GetMetricConfig find the MetricConfig
// if it is nil, create a new one
func GetMetricConfig() *MetricConfig {
if metricConfig == nil {
metricConfig = &MetricConfig{}
}
return metricConfig
}

// GetApplicationConfig find the application config
// if not, we will create one
// Usually applicationConfig will be initialized when system start
func GetApplicationConfig() *ApplicationConfig {
if applicationConfig == nil {
applicationConfig = &ApplicationConfig{}
}
return applicationConfig
}

// GetProviderConfig find the provider config
// if not found, create new one
func GetProviderConfig() ProviderConfig {
if providerConfig == nil {
logger.Warnf("providerConfig is nil!")
return ProviderConfig{}
}
return *providerConfig
}

// GetConsumerConfig find the consumer config
// if not found, create new one
func GetConsumerConfig() ConsumerConfig {
if consumerConfig == nil {
logger.Warnf("consumerConfig is nil!")
return ConsumerConfig{}
}
return *consumerConfig
}
9 changes: 0 additions & 9 deletions config/consumer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,6 @@ func SetConsumerConfig(c ConsumerConfig) {
consumerConfig = &c
}

// GetConsumerConfig ...
func GetConsumerConfig() ConsumerConfig {
if consumerConfig == nil {
logger.Warnf("consumerConfig is nil!")
return ConsumerConfig{}
}
return *consumerConfig
}

// ConsumerInit ...
func ConsumerInit(confConFile string) error {
if confConFile == "" {
Expand Down
37 changes: 37 additions & 0 deletions config/metric_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 config

var (
defaultHistogramBucket = []float64{10, 50, 100, 200, 500, 1000, 10000}
)

// This is the config struct for all metrics implementation
type MetricConfig struct {
Reporters []string `yaml:"reporters" json:"reporters,omitempty"`
HistogramBucket []float64 `yaml:"histogram_bucket" json:"histogram_bucket,omitempty"`
}

// find the histogram bucket
// if it's empty, the default value will be return
func (mc *MetricConfig) GetHistogramBucket() []float64 {
if len(mc.HistogramBucket) == 0 {
mc.HistogramBucket = defaultHistogramBucket
}
return mc.HistogramBucket
}
31 changes: 31 additions & 0 deletions config/metric_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 config

import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

func TestGetMetricConfig(t *testing.T) {
empty := GetMetricConfig()
assert.NotNil(t, empty)
}
9 changes: 0 additions & 9 deletions config/provider_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,6 @@ func SetProviderConfig(p ProviderConfig) {
providerConfig = &p
}

// GetProviderConfig ...
func GetProviderConfig() ProviderConfig {
if providerConfig == nil {
logger.Warnf("providerConfig is nil!")
return ProviderConfig{}
}
return *providerConfig
}

// ProviderInit ...
func ProviderInit(confProFile string) error {
if len(confProFile) == 0 {
Expand Down
Loading