Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
22a7f0099
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Summer2022
22a7f0099
Commits
2d665520
Commit
2d665520
authored
5 years ago
by
flycash
Browse files
Options
Downloads
Patches
Plain Diff
Support histogram
parent
10d0ac89
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
config/metric_config.go
+15
-1
15 additions, 1 deletion
config/metric_config.go
metrics/prometheus/reporter.go
+53
-14
53 additions, 14 deletions
metrics/prometheus/reporter.go
metrics/prometheus/reporter_test.go
+20
-0
20 additions, 0 deletions
metrics/prometheus/reporter_test.go
with
88 additions
and
15 deletions
config/metric_config.go
+
15
−
1
View file @
2d665520
...
...
@@ -17,9 +17,14 @@
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"`
Reporters
[]
string
`yaml:"reporters" json:"reporters,omitempty"`
HistogramBucket
[]
float64
`yaml:"histogram_bucket" json:"histogram_bucket,omitempty"`
}
// find the MetricConfig
...
...
@@ -30,3 +35,12 @@ func GetMetricConfig() *MetricConfig {
}
return
metricConfig
}
// 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
}
This diff is collapsed.
Click to expand it.
metrics/prometheus/reporter.go
+
53
−
14
View file @
2d665520
...
...
@@ -32,6 +32,7 @@ import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/logger"
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/metrics"
"github.com/apache/dubbo-go/protocol"
)
...
...
@@ -48,15 +49,28 @@ const (
providerKey
=
"provider"
consumerKey
=
"consumer"
// to identify the metric's type
histogramSuffix
=
"_histogram"
// to identify the metric's type
summarySuffix
=
"_summary"
)
var
(
labelNames
=
[]
string
{
serviceKey
,
groupKey
,
versionKey
,
methodKey
,
timeoutKey
,
remoteKey
,
localKey
}
)
// should initialize after loading configuration
func
init
()
{
rpt
:=
&
PrometheusReporter
{
consumerVec
:
newSummaryVec
(
consumerKey
),
providerVec
:
newSummaryVec
(
providerKey
),
consumerSummaryVec
:
newSummaryVec
(
consumerKey
),
providerSummaryVec
:
newSummaryVec
(
providerKey
),
consumerHistogramVec
:
newHistogramVec
(
consumerKey
),
providerHistogramVec
:
newHistogramVec
(
providerKey
),
}
prometheus
.
MustRegister
(
rpt
.
consumerVec
,
rpt
.
providerVec
)
prometheus
.
MustRegister
(
rpt
.
consumerSummaryVec
,
rpt
.
providerSummaryVec
,
rpt
.
consumerHistogramVec
,
rpt
.
providerHistogramVec
)
extension
.
SetMetricReporter
(
reporterName
,
rpt
)
}
...
...
@@ -65,27 +79,35 @@ func init() {
// https://prometheus.io/docs/guides/go-application/
type
PrometheusReporter
struct
{
// report the consumer-side's data
consumerVec
*
prometheus
.
SummaryVec
// report the provider-side's data
providerVec
*
prometheus
.
SummaryVec
// report the consumer-side's summary data
consumerSummaryVec
*
prometheus
.
SummaryVec
// report the provider-side's summary data
providerSummaryVec
*
prometheus
.
SummaryVec
// report the provider-side's histogram data
providerHistogramVec
*
prometheus
.
HistogramVec
// report the consumer-side's histogram data
consumerHistogramVec
*
prometheus
.
HistogramVec
}
// report the duration to Prometheus
func
(
reporter
*
PrometheusReporter
)
Report
(
ctx
context
.
Context
,
invoker
protocol
.
Invoker
,
invocation
protocol
.
Invocation
,
cost
time
.
Duration
,
res
protocol
.
Result
)
{
url
:=
invoker
.
GetUrl
()
var
sumVec
*
prometheus
.
SummaryVec
var
hisVec
*
prometheus
.
HistogramVec
if
isProvider
(
url
)
{
sumVec
=
reporter
.
providerVec
sumVec
=
reporter
.
providerSummaryVec
hisVec
=
reporter
.
providerHistogramVec
}
else
if
isConsumer
(
url
)
{
sumVec
=
reporter
.
consumerVec
sumVec
=
reporter
.
consumerSummaryVec
hisVec
=
reporter
.
consumerHistogramVec
}
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
{
labels
:=
prometheus
.
Labels
{
serviceKey
:
url
.
Service
(),
groupKey
:
url
.
GetParam
(
groupKey
,
""
),
versionKey
:
url
.
GetParam
(
versionKey
,
""
),
...
...
@@ -93,7 +115,24 @@ func (reporter *PrometheusReporter) Report(ctx context.Context, invoker protocol
timeoutKey
:
url
.
GetParam
(
timeoutKey
,
""
),
remoteKey
:
invocation
.
AttachmentsByKey
(
constant
.
REMOTE_ADDR
,
""
),
localKey
:
invocation
.
AttachmentsByKey
(
constant
.
REMOTE_ADDR
,
""
),
})
.
Observe
(
float64
(
cost
.
Nanoseconds
()
/
constant
.
MsToNanoRate
))
}
costMs
:=
float64
(
cost
.
Nanoseconds
()
/
constant
.
MsToNanoRate
)
sumVec
.
With
(
labels
)
.
Observe
(
costMs
)
hisVec
.
With
(
labels
)
.
Observe
(
costMs
)
}
func
newHistogramVec
(
side
string
)
*
prometheus
.
HistogramVec
{
mc
:=
config
.
GetMetricConfig
()
return
prometheus
.
NewHistogramVec
(
prometheus
.
HistogramOpts
{
Namespace
:
metrics
.
NameSpace
,
Subsystem
:
side
,
Name
:
serviceKey
+
histogramSuffix
,
Help
:
"This is the dubbo's histogram metrics"
,
Buckets
:
mc
.
GetHistogramBucket
(),
},
labelNames
)
}
// whether this url represents the application received the request as server
...
...
@@ -115,9 +154,9 @@ func newSummaryVec(side string) *prometheus.SummaryVec {
return
prometheus
.
NewSummaryVec
(
prometheus
.
SummaryOpts
{
Namespace
:
metrics
.
NameSpace
,
Help
:
"
t
his is the dubbo's metrics"
,
Help
:
"
T
his is the dubbo's
summary
metrics"
,
Subsystem
:
side
,
Name
:
serviceKey
,
Name
:
serviceKey
+
summarySuffix
,
Objectives
:
map
[
float64
]
float64
{
0.5
:
0.01
,
0.75
:
0.01
,
...
...
@@ -127,6 +166,6 @@ func newSummaryVec(side string) *prometheus.SummaryVec {
0.999
:
0.0001
,
},
},
[]
string
{
serviceKey
,
groupKey
,
versionKey
,
methodKey
,
timeoutKey
,
remoteKey
,
localKey
}
,
labelNames
,
)
}
This diff is collapsed.
Click to expand it.
metrics/prometheus/reporter_test.go
+
20
−
0
View file @
2d665520
...
...
@@ -49,4 +49,24 @@ func TestPrometheusReporter_Report(t *testing.T) {
assert
.
False
(
t
,
isConsumer
(
url
))
ctx
:=
context
.
Background
()
reporter
.
Report
(
ctx
,
invoker
,
inv
,
100
*
time
.
Millisecond
,
nil
)
// consumer side
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®istry.role=0&retries=&"
+
"service.filter=echo%2Ctoken%2Caccesslog×tamp=1569153406&token=934804bf-b007-4174-94eb-96e3e1d60cc7&version=&warmup=100"
)
invoker
=
protocol
.
NewBaseInvoker
(
url
)
reporter
.
Report
(
ctx
,
invoker
,
inv
,
100
*
time
.
Millisecond
,
nil
)
// invalid role
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®istry.role=9&retries=&"
+
"service.filter=echo%2Ctoken%2Caccesslog×tamp=1569153406&token=934804bf-b007-4174-94eb-96e3e1d60cc7&version=&warmup=100"
)
invoker
=
protocol
.
NewBaseInvoker
(
url
)
reporter
.
Report
(
ctx
,
invoker
,
inv
,
100
*
time
.
Millisecond
,
nil
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment