Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
b0fdaf4f
Commit
b0fdaf4f
authored
Dec 05, 2018
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refine code
parent
c8f2d324
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
48 deletions
+48
-48
prometheus_metrics_service.cpp
src/brpc/builtin/prometheus_metrics_service.cpp
+48
-48
No files found.
src/brpc/builtin/prometheus_metrics_service.cpp
View file @
b0fdaf4f
...
...
@@ -52,22 +52,22 @@ public:
private
:
DISALLOW_COPY_AND_ASSIGN
(
PrometheusMetricsDumper
);
bool
ProcessLatencyRecorderSuffix
(
const
butil
::
StringPiece
&
name
,
const
butil
::
StringPiece
&
desc
);
bool
ProcessPercentileSuffix
(
const
butil
::
StringPiece
&
name
,
const
butil
::
StringPiece
&
desc
,
butil
::
StringPiece
*
metric_name_out
);
bool
DumpLatencyRecorderSuffix
(
const
butil
::
StringPiece
&
name
,
const
butil
::
StringPiece
&
desc
);
// 6 is the number of bvars in LatencyRecorder that indicating percentiles
static
const
int
NPERCENTILES
=
6
;
private
:
struct
SummaryItems
{
std
::
string
latency_percentiles
[
NPERCENTILES
];
std
::
string
latency_avg
;
std
::
string
count
;
std
::
string
metric_name
;
};
SummaryItems
*
ProcessLatencyRecorderSuffix
(
const
butil
::
StringPiece
&
name
,
const
butil
::
StringPiece
&
desc
);
private
:
butil
::
IOBufBuilder
*
_os
;
const
std
::
string
_server_prefix
;
std
::
map
<
std
::
string
,
SummaryItems
>
_m
;
...
...
@@ -79,9 +79,9 @@ bool PrometheusMetricsDumper::dump(const std::string& name,
// there is no necessary to monitor string in prometheus
return
true
;
}
if
(
Process
LatencyRecorderSuffix
(
name
,
desc
))
{
if
(
Dump
LatencyRecorderSuffix
(
name
,
desc
))
{
// Has encountered name with suffix exposed by LatencyRecorder,
// Leave it to
Process
LatencyRecorderSuffix to output Summary.
// Leave it to
Dump
LatencyRecorderSuffix to output Summary.
return
true
;
}
*
_os
<<
"# HELP "
<<
name
<<
'\n'
...
...
@@ -90,72 +90,72 @@ bool PrometheusMetricsDumper::dump(const std::string& name,
return
true
;
}
bool
PrometheusMetricsDumper
::
ProcessPercentileSuffix
(
const
butil
::
StringPiece
&
name
,
const
butil
::
StringPiece
&
desc
,
butil
::
StringPiece
*
metric_name_out
)
{
PrometheusMetricsDumper
::
SummaryItems
*
PrometheusMetricsDumper
::
ProcessLatencyRecorderSuffix
(
const
butil
::
StringPiece
&
name
,
const
butil
::
StringPiece
&
desc
)
{
static
std
::
string
latency_names
[]
=
{
butil
::
string_printf
(
"_latency_%d"
,
(
int
)
bvar
::
FLAGS_bvar_latency_p1
),
butil
::
string_printf
(
"_latency_%d"
,
(
int
)
bvar
::
FLAGS_bvar_latency_p2
),
butil
::
string_printf
(
"_latency_%d"
,
(
int
)
bvar
::
FLAGS_bvar_latency_p3
),
"_latency_999"
,
"_latency_9999"
,
"_max_latency"
};
CHECK
(
NPERCENTILES
==
sizeof
(
latency_names
)
/
sizeof
(
std
::
string
));
CHECK
(
NPERCENTILES
==
arraysize
(
latency_names
));
butil
::
StringPiece
metric_name
(
name
);
for
(
int
i
=
0
;
i
<
NPERCENTILES
;
++
i
)
{
if
(
!
metric_name
.
ends_with
(
latency_names
[
i
]))
{
continue
;
}
metric_name
.
remove_suffix
(
latency_names
[
i
].
size
());
_m
[
metric_name
.
as_string
()].
latency_percentiles
[
i
]
=
desc
.
as_string
();
SummaryItems
*
si
=
&
_m
[
metric_name
.
as_string
()];
si
->
latency_percentiles
[
i
]
=
desc
.
as_string
();
if
(
i
==
NPERCENTILES
-
1
)
{
// 'max_latency' is the last suffix name that appear in the sorted bvar
// list, which means all related percentiles have been gathered and we are
// ready to output a Summary.
*
metric_name_out
=
metric_name
;
return
true
;
si
->
metric_name
=
metric_name
.
as_string
()
;
return
si
;
}
break
;
return
NULL
;
}
return
false
;
// Get the average of latency in recent window size
if
(
metric_name
.
ends_with
(
"_latency"
))
{
metric_name
.
remove_suffix
(
8
);
_m
[
metric_name
.
as_string
()].
latency_avg
=
desc
.
as_string
();
return
NULL
;
}
if
(
metric_name
.
ends_with
(
"_count"
))
{
metric_name
.
remove_suffix
(
6
);
_m
[
metric_name
.
as_string
()].
count
=
desc
.
as_string
();
return
NULL
;
}
return
NULL
;
}
bool
PrometheusMetricsDumper
::
Process
LatencyRecorderSuffix
(
const
butil
::
StringPiece
&
name
,
const
butil
::
StringPiece
&
desc
)
{
bool
PrometheusMetricsDumper
::
Dump
LatencyRecorderSuffix
(
const
butil
::
StringPiece
&
name
,
const
butil
::
StringPiece
&
desc
)
{
if
(
!
name
.
starts_with
(
_server_prefix
))
{
return
false
;
}
butil
::
StringPiece
metric_name
(
name
);
if
(
ProcessPercentileSuffix
(
name
,
desc
,
&
metric_name
))
{
const
SummaryItems
&
items
=
_m
[
metric_name
.
as_string
()];
*
_os
<<
"# HELP "
<<
metric_name
<<
'\n'
<<
"# TYPE "
<<
metric_name
<<
" summary
\n
"
<<
metric_name
<<
"{quantile=
\"
"
<<
std
::
setprecision
(
2
)
SummaryItems
*
si
=
NULL
;
if
((
si
=
ProcessLatencyRecorderSuffix
(
name
,
desc
)))
{
*
_os
<<
"# HELP "
<<
si
->
metric_name
<<
'\n'
<<
"# TYPE "
<<
si
->
metric_name
<<
" summary
\n
"
<<
si
->
metric_name
<<
"{quantile=
\"
"
<<
std
::
setprecision
(
2
)
<<
(
double
)(
bvar
::
FLAGS_bvar_latency_p1
)
/
100
<<
"
\"
} "
<<
items
.
latency_percentiles
[
0
]
<<
'\n'
<<
metric_name
<<
"{quantile=
\"
"
<<
std
::
setprecision
(
2
)
<<
si
->
latency_percentiles
[
0
]
<<
'\n'
<<
si
->
metric_name
<<
"{quantile=
\"
"
<<
std
::
setprecision
(
2
)
<<
(
double
)(
bvar
::
FLAGS_bvar_latency_p2
)
/
100
<<
"
\"
} "
<<
items
.
latency_percentiles
[
1
]
<<
'\n'
<<
metric_name
<<
"{quantile=
\"
"
<<
std
::
setprecision
(
2
)
<<
si
->
latency_percentiles
[
1
]
<<
'\n'
<<
si
->
metric_name
<<
"{quantile=
\"
"
<<
std
::
setprecision
(
2
)
<<
(
double
)(
bvar
::
FLAGS_bvar_latency_p3
)
/
100
<<
"
\"
} "
<<
items
.
latency_percentiles
[
2
]
<<
'\n'
<<
metric_name
<<
"{quantile=
\"
0.999
\"
} "
<<
items
.
latency_percentiles
[
3
]
<<
'\n'
<<
metric_name
<<
"{quantile=
\"
0.9999
\"
} "
<<
items
.
latency_percentiles
[
4
]
<<
'\n'
<<
metric_name
<<
"{quantile=
\"
1
\"
} "
<<
items
.
latency_percentiles
[
5
]
<<
'\n'
<<
metric_name
<<
"_sum "
<<
si
->
latency_percentiles
[
2
]
<<
'\n'
<<
si
->
metric_name
<<
"{quantile=
\"
0.999
\"
} "
<<
si
->
latency_percentiles
[
3
]
<<
'\n'
<<
si
->
metric_name
<<
"{quantile=
\"
0.9999
\"
} "
<<
si
->
latency_percentiles
[
4
]
<<
'\n'
<<
si
->
metric_name
<<
"{quantile=
\"
1
\"
} "
<<
si
->
latency_percentiles
[
5
]
<<
'\n'
<<
si
->
metric_name
<<
"_sum "
// There is no sum of latency in bvar output, just use average * count as approximation
<<
strtoll
(
items
.
latency_avg
.
data
(),
NULL
,
10
)
*
strtoll
(
items
.
count
.
data
(),
NULL
,
10
)
<<
'\n'
<<
metric_name
<<
"_count "
<<
items
.
count
<<
'\n'
;
return
true
;
}
// Get the average of latency in recent window size
if
(
metric_name
.
ends_with
(
"_latency"
))
{
metric_name
.
remove_suffix
(
8
);
_m
[
metric_name
.
as_string
()].
latency_avg
=
desc
.
as_string
();
return
true
;
}
if
(
metric_name
.
ends_with
(
"_count"
))
{
metric_name
.
remove_suffix
(
6
);
_m
[
metric_name
.
as_string
()].
count
=
desc
.
as_string
();
<<
strtoll
(
si
->
latency_avg
.
data
(),
NULL
,
10
)
*
strtoll
(
si
->
count
.
data
(),
NULL
,
10
)
<<
'\n'
<<
si
->
metric_name
<<
"_count "
<<
si
->
count
<<
'\n'
;
return
true
;
}
return
false
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment