@@ -23,7 +23,7 @@ import scala.util.matching.Regex
23
23
import scala .util .{Failure , Try }
24
24
25
25
object KafkaJMX extends Logging {
26
-
26
+
27
27
private [this ] val defaultJmxConnectorProperties = Map [String , Any ] (
28
28
" jmx.remote.x.request.waiting.timeout" -> " 3000" ,
29
29
" jmx.remote.x.notification.fetch.timeout" -> " 3000" ,
@@ -99,7 +99,7 @@ object KafkaMetrics {
99
99
private def getBrokerTopicMeterMetrics (kafkaVersion : KafkaVersion , mbsc : MBeanServerConnection , metricName : String , topicOption : Option [String ]) = {
100
100
getMeterMetric(mbsc, getObjectName(kafkaVersion, metricName, topicOption))
101
101
}
102
-
102
+
103
103
private def getSep (kafkaVersion : KafkaVersion ) : String = {
104
104
kafkaVersion match {
105
105
case Kafka_0_8_1_1 => " \" "
@@ -110,7 +110,7 @@ object KafkaMetrics {
110
110
def getObjectName (kafkaVersion : KafkaVersion , name : String , topicOption : Option [String ] = None ) = {
111
111
val sep = getSep(kafkaVersion)
112
112
val topicAndName = kafkaVersion match {
113
- case Kafka_0_8_1_1 =>
113
+ case Kafka_0_8_1_1 =>
114
114
topicOption.map( topic => s " ${sep}$topic- $name${sep}" ).getOrElse(s " ${sep}AllTopics $name${sep}" )
115
115
case _ =>
116
116
val topicProp = topicOption.map(topic => s " ,topic= $topic" ).getOrElse(" " )
@@ -127,11 +127,11 @@ object KafkaMetrics {
127
127
/* Gauge, Value : 0 */
128
128
private val replicaFetcherManagerMaxLag = new ObjectName (
129
129
" kafka.server:type=ReplicaFetcherManager,name=MaxLag,clientId=Replica" )
130
-
130
+
131
131
/* Gauge, Value : 0 */
132
132
private val kafkaControllerActiveControllerCount = new ObjectName (
133
133
" kafka.controller:type=KafkaController,name=ActiveControllerCount" )
134
-
134
+
135
135
/* Gauge, Value : 0 */
136
136
private val kafkaControllerOfflinePartitionsCount = new ObjectName (
137
137
" kafka.controller:type=KafkaController,name=OfflinePartitionsCount" )
@@ -144,16 +144,18 @@ object KafkaMetrics {
144
144
private val operatingSystemObjectName = new ObjectName (" java.lang:type=OperatingSystem" )
145
145
146
146
/* Log Segments */
147
- private val logSegmentObjectName = new ObjectName (" kafka.log:type=Log,name=*- LogSegments" )
147
+ private val logSegmentObjectName = new ObjectName (" kafka.log:type=Log,name=LogSegments,topic=*,partition=* " )
148
148
149
- private val directoryObjectName = new ObjectName (" kafka.log:type=Log,name=*- Directory" )
149
+ private val directoryObjectName = new ObjectName (" kafka.log:type=Log,name=Directory,topic=*,partition=* " )
150
150
151
- private val LogSegmentsNameRegex = new Regex (" %s-LogSegments" .format(""" (.*)-(\d*)""" ), " topic" , " partition" )
151
+ // exp: kafka.log:type=Log,name=LogSegments,topic=WEB_post,partition=19/Value (ArrayList) = [baseOffset=0, created=1527666489299, logSize=55232565, indexSize=252680]
152
+ // private val LogSegmentsNameRegex = new Regex("%s-LogSegments".format("""(.*)-(\d*)"""), "topic", "partition")
152
153
153
- private val DirectoryNameRegex = new Regex (" %s-Directory" .format(""" (.*)-(\d*)""" ), " topic" , " partition" )
154
+ // exp: kafka.log:type=Log,name=Directory,topic=WEB_post,partition=16/Value (String) = /log/kafka/WEB_post-16
155
+ // private val DirectoryNameRegex = new Regex("%s-Directory".format("""(.*)-(\d*)"""), "topic", "partition")
154
156
155
157
val LogSegmentRegex = new Regex (
156
- " baseOffset=(.*), created=(.*), logSize=(.*), indexSize=(.*)" ,
158
+ " baseOffset=(.*); created=(.*); logSize=(.*); indexSize=(.*)" ,
157
159
" baseOffset" , " created" , " logSize" , " indexSize"
158
160
)
159
161
@@ -172,7 +174,7 @@ object KafkaMetrics {
172
174
case _ : InstanceNotFoundException => OSMetric (0D , 0D )
173
175
}
174
176
}
175
-
177
+
176
178
private def getMeterMetric (mbsc : MBeanServerConnection , name : ObjectName ) = {
177
179
import scala .collection .JavaConverters ._
178
180
try {
@@ -187,7 +189,7 @@ object KafkaMetrics {
187
189
case _ : InstanceNotFoundException => MeterMetric (0 ,0 ,0 ,0 ,0 )
188
190
}
189
191
}
190
-
192
+
191
193
private def getLongValue (attributes : Seq [Attribute ], name : String ) = {
192
194
attributes.find(_.getName == name).map(_.getValue.asInstanceOf [Long ]).getOrElse(0L )
193
195
}
@@ -196,27 +198,22 @@ object KafkaMetrics {
196
198
attributes.find(_.getName == name).map(_.getValue.asInstanceOf [Double ]).getOrElse(0D )
197
199
}
198
200
199
- private def topicAndPartition (name : String , regex : Regex ) = {
201
+ private def topicAndPartition (objectName : ObjectName ) = {
200
202
try {
201
- val matches = regex.findAllIn(name).matchData.toSeq
202
- require(matches.size == 1 )
203
- val m = matches.head
204
-
205
- val topic = m.group(" topic" )
206
- val partition = m.group(" partition" ).toInt
207
-
203
+ val topic = objectName.getKeyProperty(" topic" )
204
+ val partition = objectName.getKeyProperty(" partition" ).toInt
208
205
(topic, partition)
209
206
}
210
207
catch {
211
208
case e : Exception =>
212
- throw new IllegalStateException (" Can't parse topic and partition from: <%s>" .format(name ), e)
209
+ throw new IllegalStateException (" Can't parse topic and partition from: <%s>" .format(objectName ), e)
213
210
}
214
211
}
215
212
216
213
private def queryValues [K , V ](
217
214
mbsc : MBeanServerConnection ,
218
215
objectName : ObjectName ,
219
- keyConverter : String => K ,
216
+ keyConverter : ObjectName => K ,
220
217
valueConverter : Object => V
221
218
) = {
222
219
val logsSizeObjectNames = mbsc.queryNames(objectName, null ).asScala.toSeq
@@ -228,12 +225,12 @@ object KafkaMetrics {
228
225
private def queryValue [K , V ](
229
226
mbsc : MBeanServerConnection ,
230
227
objectName : ObjectName ,
231
- keyConverter : String => K ,
228
+ keyConverter : ObjectName => K ,
232
229
valueConverter : Object => V
233
230
) = {
234
- val name = objectName.getKeyProperty(" name" )
231
+ // val name = objectName.getKeyProperty("name")
235
232
val mbean = MBeanServerInvocationHandler .newProxyInstance(mbsc, objectName, classOf [GaugeMBean ], true )
236
- (keyConverter(name ), valueConverter(mbean.getValue))
233
+ (keyConverter(objectName ), valueConverter(mbean.getValue))
237
234
}
238
235
239
236
private def parseLogSegment (str : String ): LogSegment = {
@@ -259,7 +256,7 @@ object KafkaMetrics {
259
256
queryValues(
260
257
mbsc,
261
258
logSegmentObjectName,
262
- key => topicAndPartition(key, LogSegmentsNameRegex ),
259
+ key => topicAndPartition(key),
263
260
value => {
264
261
val lst = value.asInstanceOf [ju.List [String ]]
265
262
lst.asScala.map(parseLogSegment).toSeq
@@ -271,7 +268,7 @@ object KafkaMetrics {
271
268
queryValues(
272
269
mbsc,
273
270
directoryObjectName,
274
- key => topicAndPartition(key, DirectoryNameRegex ),
271
+ key => topicAndPartition(key),
275
272
value => value.asInstanceOf [String ]
276
273
)
277
274
}.toMap
@@ -355,10 +352,10 @@ case class MeterMetric(count: Long,
355
352
356
353
def + (o : MeterMetric ) : MeterMetric = {
357
354
MeterMetric (
358
- o.count + count,
359
- o.fifteenMinuteRate + fifteenMinuteRate,
360
- o.fiveMinuteRate + fiveMinuteRate,
361
- o.oneMinuteRate + oneMinuteRate,
355
+ o.count + count,
356
+ o.fifteenMinuteRate + fifteenMinuteRate,
357
+ o.fiveMinuteRate + fiveMinuteRate,
358
+ o.oneMinuteRate + oneMinuteRate,
362
359
o.meanRate + meanRate)
363
360
}
364
361
}
0 commit comments