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)
)
42 changes: 42 additions & 0 deletions common/extension/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 (
metricReporterMap = make(map[string]metrics.Reporter, 4)
)

// set a reporter with the name
func SetMetricReporter(name string, reporter metrics.Reporter) {
metricReporterMap[name] = reporter
}

// 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 {
reporter, found := metricReporterMap[name]
if !found {
panic("Cannot find the reporter with name: " + name)
}
return reporter
}
46 changes: 46 additions & 0 deletions common/extension/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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/protocol"
)

func TestGetMetricReporter(t *testing.T) {
reporter := &mockReporter{}
name := "mock"
SetMetricReporter(name, 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
8 changes: 8 additions & 0 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
var (
consumerConfig *ConsumerConfig
providerConfig *ProviderConfig
metricConfig *MetricConfig
maxWait = 3
)

Expand Down Expand Up @@ -75,6 +76,9 @@ func Load() {
if consumerConfig == nil {
logger.Warnf("consumerConfig is nil!")
} else {

metricConfig = consumerConfig.MetricConfig

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

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

checkApplicationName(providerConfig.ApplicationConfig)
if err := configCenterRefreshProvider(); err != nil {
logger.Errorf("[provider config center refresh] %#v", err)
Expand Down
48 changes: 48 additions & 0 deletions config/metric_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 (
"github.com/creasty/defaults"
)

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

// parse the config from yml
func (c *MetricConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := defaults.Set(c); err != nil {
return err
}
type plain MetricConfig
if err := unmarshal((*plain)(c)); err != nil {
return err
}
flycash marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

// find the MetricConfig
// if it is nil, create a new one
func GetMetricConfig() *MetricConfig {
flycash marked this conversation as resolved.
Show resolved Hide resolved
if metricConfig == nil {
flycash marked this conversation as resolved.
Show resolved Hide resolved
metricConfig = &MetricConfig{}
}
return metricConfig
}
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)
}
93 changes: 93 additions & 0 deletions filter/filter_impl/metrics_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 filter_impl

import (
"context"
"time"
)

import (
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/filter"
"github.com/apache/dubbo-go/metrics"
"github.com/apache/dubbo-go/protocol"
)

const (
metricFilterName = "metrics"
)

var (
metricFilterInstance filter.Filter
)

// must initialized before using the filter and after loading configuration
func init() {
extension.SetFilter(metricFilterName, newMetricsFilter)
}

// metricFilter will calculate the invocation's duration and the report to the reporters
// If you want to use this filter to collect the metrics,
// Adding this into your configuration file, like:
// filter: "metrics"
// metrics:
// reporter:
// - "your reporter" # here you should specify the reporter, for example 'prometheus'
// more info please take a look at dubbo-samples projects
type metricsFilter struct {
reporters []metrics.Reporter
}

// using goroutine to report the duration.
flycash marked this conversation as resolved.
Show resolved Hide resolved
func (p *metricsFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
start := time.Now()
res := invoker.Invoke(ctx, invocation)
end := time.Now()
duration := end.Sub(start)
go func() {
flycash marked this conversation as resolved.
Show resolved Hide resolved
for _, reporter := range p.reporters {
reporter.Report(ctx, invoker, invocation, duration, nil)
}
}()
return res
}

// do nothing and return the result
func (p *metricsFilter) OnResponse(ctx context.Context, res protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
return res
}

// the metricsFilter is singleton.
// it's lazy initialization
// make sure that the configuration had been loaded before invoking this method.
func newMetricsFilter() filter.Filter {
if metricFilterInstance == nil {
reporterNames := config.GetMetricConfig().Reporters
reporters := make([]metrics.Reporter, 0, len(reporterNames))
for _, name := range reporterNames {
reporters = append(reporters, extension.GetMetricReporter(name))
}
metricFilterInstance = &metricsFilter{
reporters: reporters,
}
}

return metricFilterInstance
}
Loading