Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
commons
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
3
Merge Requests
3
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DevOps
commons
Commits
e40a446b
Commit
e40a446b
authored
Apr 19, 2022
by
王亮
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix some bug of getting ip .
parent
c25256f1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
43 deletions
+25
-43
KafkaMeterRegistry.java
...up/boot/micrometer/register/kafka/KafkaMeterRegistry.java
+25
-43
No files found.
qg-micrometer-register-kafka-starter/src/main/java/cn/quantgroup/boot/micrometer/register/kafka/KafkaMeterRegistry.java
View file @
e40a446b
...
...
@@ -2,8 +2,6 @@ package cn.quantgroup.boot.micrometer.register.kafka;
import
static
java
.
util
.
stream
.
Collectors
.
joining
;
import
com.alibaba.cloud.nacos.NacosDiscoveryProperties
;
import
com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpoint
;
import
io.micrometer.core.instrument.Clock
;
import
io.micrometer.core.instrument.DistributionSummary
;
import
io.micrometer.core.instrument.FunctionTimer
;
...
...
@@ -38,7 +36,8 @@ public class KafkaMeterRegistry extends StepMeterRegistry {
private
final
String
key
;
private
ApplicationContext
applicationContext
;
public
KafkaMeterRegistry
(
KafkaConfig
config
,
Clock
clock
,
ApplicationContext
applicationContext
)
{
public
KafkaMeterRegistry
(
KafkaConfig
config
,
Clock
clock
,
ApplicationContext
applicationContext
)
{
super
(
config
,
clock
);
this
.
config
=
config
;
if
(
ObjectUtils
.
isEmpty
(
System
.
getProperty
(
"NAMESPACE"
)))
{
...
...
@@ -68,19 +67,14 @@ public class KafkaMeterRegistry extends StepMeterRegistry {
protected
void
publish
()
{
for
(
List
<
Meter
>
batch
:
MeterPartition
.
partition
(
this
,
config
.
batchSize
()))
{
batch
.
stream
()
.
flatMap
(
m
->
m
.
match
(
gauge
->
writeGauge
(
gauge
.
getId
(),
gauge
.
value
()),
counter
->
writeCounter
(
counter
.
getId
(),
counter
.
count
()),
this
::
writeTimer
,
this
::
writeSummary
,
this
::
writeLongTaskTimer
,
gauge
->
writeGauge
(
gauge
.
getId
(),
gauge
.
value
(
getBaseTimeUnit
())),
counter
->
writeCounter
(
counter
.
getId
(),
counter
.
count
()),
this
::
writeFunctionTimer
,
this
::
writeMeter
)).
forEach
(
i
->
{
kafkaProducer
.
send
(
new
ProducerRecord
<>(
config
.
topic
(),
key
,
i
));
});
batch
.
stream
().
flatMap
(
m
->
m
.
match
(
gauge
->
writeGauge
(
gauge
.
getId
(),
gauge
.
value
()),
counter
->
writeCounter
(
counter
.
getId
(),
counter
.
count
()),
this
::
writeTimer
,
this
::
writeSummary
,
this
::
writeLongTaskTimer
,
gauge
->
writeGauge
(
gauge
.
getId
(),
gauge
.
value
(
getBaseTimeUnit
())),
counter
->
writeCounter
(
counter
.
getId
(),
counter
.
count
()),
this
::
writeFunctionTimer
,
this
::
writeMeter
)).
forEach
(
i
->
{
kafkaProducer
.
send
(
new
ProducerRecord
<>(
config
.
topic
(),
key
,
i
));
});
}
}
...
...
@@ -97,19 +91,14 @@ public class KafkaMeterRegistry extends StepMeterRegistry {
}
private
String
influxLineProtocol
(
Meter
.
Id
id
,
String
metricType
,
Stream
<
Field
>
fields
)
{
String
tags
=
getConventionTags
(
id
).
stream
()
.
filter
(
t
->
StringUtils
.
isNotBlank
(
t
.
getValue
()))
String
tags
=
getConventionTags
(
id
).
stream
().
filter
(
t
->
StringUtils
.
isNotBlank
(
t
.
getValue
()))
.
map
(
t
->
","
+
escapeField
(
t
.
getKey
())
+
"="
+
escapeField
(
t
.
getValue
()))
.
collect
(
joining
(
""
));
NacosDiscoveryEndpoint
nacosDiscoveryEndpoint
=
(
NacosDiscoveryEndpoint
)
this
.
applicationContext
.
getBean
(
"nacosDiscoveryEndpoint"
);
String
ip
=
((
NacosDiscoveryProperties
)
nacosDiscoveryEndpoint
.
nacosDiscovery
()
.
get
(
"NacosDiscoveryProperties"
)).
getIp
();
String
ip
=
applicationContext
.
getEnvironment
().
getProperty
(
"spring.cloud.client.ip-address"
);
tags
=
tags
+
(
",ip="
+
ip
);
return
getConventionName
(
id
)
+
tags
+
",metric_type="
+
metricType
+
" "
+
fields
.
map
(
Field:
:
toString
).
collect
(
joining
(
","
))
+
" "
+
clock
.
wallTime
();
return
getConventionName
(
id
)
+
tags
+
",metric_type="
+
metricType
+
" "
+
fields
.
map
(
Field:
:
toString
).
collect
(
joining
(
","
))
+
" "
+
clock
.
wallTime
();
}
static
class
Field
{
...
...
@@ -125,6 +114,7 @@ public class KafkaMeterRegistry extends StepMeterRegistry {
this
.
key
=
key
;
this
.
value
=
value
;
}
@Override
public
String
toString
()
{
return
escapeField
(
key
)
+
"="
+
DoubleFormat
.
decimalOrNan
(
value
);
...
...
@@ -139,32 +129,24 @@ public class KafkaMeterRegistry extends StepMeterRegistry {
}
private
Stream
<
String
>
writeTimer
(
Timer
timer
)
{
final
Stream
<
Field
>
fields
=
Stream
.
of
(
new
Field
(
"sum"
,
timer
.
totalTime
(
getBaseTimeUnit
())),
new
Field
(
"count"
,
timer
.
count
()),
new
Field
(
"mean"
,
timer
.
mean
(
getBaseTimeUnit
())),
new
Field
(
"upper"
,
timer
.
max
(
getBaseTimeUnit
()))
);
final
Stream
<
Field
>
fields
=
Stream
.
of
(
new
Field
(
"sum"
,
timer
.
totalTime
(
getBaseTimeUnit
())),
new
Field
(
"count"
,
timer
.
count
()),
new
Field
(
"mean"
,
timer
.
mean
(
getBaseTimeUnit
())),
new
Field
(
"upper"
,
timer
.
max
(
getBaseTimeUnit
())));
return
Stream
.
of
(
influxLineProtocol
(
timer
.
getId
(),
"histogram"
,
fields
));
}
private
Stream
<
String
>
writeSummary
(
DistributionSummary
summary
)
{
final
Stream
<
Field
>
fields
=
Stream
.
of
(
new
Field
(
"sum"
,
summary
.
totalAmount
()),
new
Field
(
"count"
,
summary
.
count
()),
new
Field
(
"mean"
,
summary
.
mean
()),
new
Field
(
"upper"
,
summary
.
max
())
);
final
Stream
<
Field
>
fields
=
Stream
.
of
(
new
Field
(
"sum"
,
summary
.
totalAmount
()),
new
Field
(
"count"
,
summary
.
count
()),
new
Field
(
"mean"
,
summary
.
mean
()),
new
Field
(
"upper"
,
summary
.
max
()));
return
Stream
.
of
(
influxLineProtocol
(
summary
.
getId
(),
"histogram"
,
fields
));
}
private
Stream
<
String
>
writeLongTaskTimer
(
LongTaskTimer
timer
)
{
Stream
<
Field
>
fields
=
Stream
.
of
(
new
Field
(
"active_tasks"
,
timer
.
activeTasks
()),
new
Field
(
"duration"
,
timer
.
duration
(
getBaseTimeUnit
()))
);
Stream
<
Field
>
fields
=
Stream
.
of
(
new
Field
(
"active_tasks"
,
timer
.
activeTasks
()),
new
Field
(
"duration"
,
timer
.
duration
(
getBaseTimeUnit
())));
return
Stream
.
of
(
influxLineProtocol
(
timer
.
getId
(),
"long_task_timer"
,
fields
));
}
...
...
@@ -202,8 +184,8 @@ public class KafkaMeterRegistry extends StepMeterRegistry {
}
private
static
String
escapeField
(
String
names
)
{
return
names
.
replace
(
" "
,
"\\ "
).
replace
(
","
,
"\\,"
);
private
static
String
escapeField
(
String
names
)
{
return
names
.
replace
(
" "
,
"\\ "
).
replace
(
","
,
"\\,"
);
}
}
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