Skip to content
Snippets Groups Projects
Commit f61c32d4 authored by flycash's avatar flycash
Browse files

Tested prometheus

parent fc95b50f
No related branches found
No related tags found
No related merge requests found
......@@ -21,7 +21,7 @@ import (
"github.com/apache/dubbo-go/metrics"
)
var(
var (
metricReporterMap = make(map[string]metrics.Reporter, 4)
)
......
......@@ -33,7 +33,7 @@ import (
var (
consumerConfig *ConsumerConfig
providerConfig *ProviderConfig
metricConfig *MetricConfig
metricConfig *MetricConfig
maxWait = 3
)
......
......@@ -23,7 +23,7 @@ import (
// This is the config struct for all metrics implementation
type MetricConfig struct {
Reporters []string `yaml:"reporters" json:"reporters,omitempty"`
Reporters []string `yaml:"reporters" json:"reporters,omitempty"`
}
// parse the config from yml
......@@ -46,5 +46,3 @@ func GetMetricConfig() *MetricConfig {
}
return metricConfig
}
......@@ -25,7 +25,6 @@ import (
"github.com/stretchr/testify/assert"
)
func TestGetMetricConfig(t *testing.T) {
empty := GetMetricConfig()
assert.NotNil(t, empty)
......
......@@ -72,6 +72,9 @@ func (p *metricsFilter) OnResponse(ctx context.Context, res protocol.Result, inv
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
......
......@@ -37,7 +37,7 @@ require (
github.com/nacos-group/nacos-sdk-go v0.0.0-20190723125407-0242d42e3dbb
github.com/opentracing/opentracing-go v1.1.0
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.1.0 // indirect
github.com/prometheus/client_golang v1.1.0
github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec
github.com/satori/go.uuid v1.2.0
github.com/smartystreets/goconvey v0.0.0-20190710185942-9d28bd7c0945 // indirect
......
......@@ -22,6 +22,8 @@ import (
"strconv"
"strings"
"time"
"github.com/apache/dubbo-go/common/logger"
)
import (
"github.com/prometheus/client_golang/prometheus"
......@@ -49,11 +51,19 @@ const (
consumerKey = "consumer"
)
// should initialize after loading configuration
func init() {
extension.SetMetricReporter(reporterName, newPrometheus())
rpt := &PrometheusReporter{
consumerVec: newSummaryVec(consumerKey),
providerVec: newSummaryVec(providerKey),
}
prometheus.MustRegister(rpt.consumerVec, rpt.providerVec)
extension.SetMetricReporter(reporterName, rpt)
}
// it will collect the data for Prometheus
// if you want to use this, you should initialize your prometheus.
// https://prometheus.io/docs/guides/go-application/
type PrometheusReporter struct {
// report the consumer-side's data
......@@ -68,8 +78,12 @@ func (reporter *PrometheusReporter) Report(ctx context.Context, invoker protocol
var sumVec *prometheus.SummaryVec
if isProvider(url) {
sumVec = reporter.providerVec
} else {
} else if isConsumer(url) {
sumVec = reporter.consumerVec
} else {
logger.Warnf("The url is not the consumer's or provider's, "+
"so the invocation will be ignored. url: %s", url.String())
return
}
sumVec.With(prometheus.Labels{
......@@ -83,21 +97,20 @@ func (reporter *PrometheusReporter) Report(ctx context.Context, invoker protocol
}).Observe(float64(cost.Nanoseconds() / constant.MsToNanoRate))
}
// whether this url represents the application received the request as server
func isProvider(url common.URL) bool {
side := url.GetParam(constant.ROLE_KEY, "")
return strings.EqualFold(side, strconv.Itoa(common.PROVIDER))
role := url.GetParam(constant.ROLE_KEY, "")
return strings.EqualFold(role, strconv.Itoa(common.PROVIDER))
}
func newPrometheus() metrics.Reporter {
// cfg := *config.GetMetricConfig().GetPrometheusConfig()
result := &PrometheusReporter{
consumerVec: newSummaryVec(consumerKey),
providerVec: newSummaryVec(providerKey),
}
prometheus.MustRegister(result.consumerVec, result.providerVec)
return result
// whether this url represents the application sent then request as client
func isConsumer(url common.URL) bool {
role := url.GetParam(constant.ROLE_KEY, "")
return strings.EqualFold(role, strconv.Itoa(common.CONSUMER))
}
// create SummaryVec, the Namespace is dubbo
// the objectives is from my experience.
func newSummaryVec(side string) *prometheus.SummaryVec {
return prometheus.NewSummaryVec(
......
/*
* 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 prometheus
import (
"context"
"testing"
"time"
)
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/protocol"
"github.com/apache/dubbo-go/protocol/invocation"
)
func TestPrometheusReporter_Report(t *testing.T) {
reporter := extension.GetMetricReporter(reporterName)
url, _ := common.NewURL(context.Background(),
"dubbo://:20000/UserProvider?app.version=0.0.1&application=BDTService&bean.name=UserProvider"+
"&cluster=failover&environment=dev&group=&interface=com.ikurento.user.UserProvider&loadbalance=random&methods.GetUser."+
"loadbalance=random&methods.GetUser.retries=1&methods.GetUser.weight=0&module=dubbogo+user-info+server&name="+
"BDTService&organization=ikurento.com&owner=ZX&registry.role=3&retries=&"+
"service.filter=echo%2Ctoken%2Caccesslog&timestamp=1569153406&token=934804bf-b007-4174-94eb-96e3e1d60cc7&version=&warmup=100")
invoker := protocol.NewBaseInvoker(url)
attach := make(map[string]string, 10)
inv := invocation.NewRPCInvocation("MethodName", []interface{}{"OK", "Hello"}, attach)
ctx := context.Background()
reporter.Report(ctx, invoker, inv, 100*time.Millisecond)
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment