Browse Source
Reviewers: Colin P. McCabe <cmccabe@apache.org>, Ismael Juma <ismael@juma.me.uk>pull/6660/head
Stanislav Kozlovski
6 years ago
committed by
Colin Patrick McCabe
9 changed files with 117 additions and 69 deletions
@ -0,0 +1,88 @@
@@ -0,0 +1,88 @@
|
||||
/* |
||||
* 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 org.apache.kafka.common.utils; |
||||
|
||||
import org.apache.kafka.common.metrics.Metrics; |
||||
import org.junit.After; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
|
||||
import javax.management.JMException; |
||||
import javax.management.MBeanServer; |
||||
import javax.management.MalformedObjectNameException; |
||||
import javax.management.ObjectName; |
||||
|
||||
import java.lang.management.ManagementFactory; |
||||
|
||||
import static org.junit.Assert.assertNull; |
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertFalse; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
public class AppInfoParserTest { |
||||
private static final String EXPECTED_COMMIT_VERSION = AppInfoParser.DEFAULT_VALUE; |
||||
private static final String EXPECTED_VERSION = AppInfoParser.DEFAULT_VALUE; |
||||
private static final Long EXPECTED_START_MS = 1552313875722L; |
||||
private static final String METRICS_PREFIX = "app-info-test"; |
||||
private static final String METRICS_ID = "test"; |
||||
|
||||
private Metrics metrics; |
||||
private MBeanServer mBeanServer; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
metrics = new Metrics(new MockTime(1)); |
||||
mBeanServer = ManagementFactory.getPlatformMBeanServer(); |
||||
} |
||||
|
||||
@After |
||||
public void tearDown() { |
||||
metrics.close(); |
||||
} |
||||
|
||||
@Test |
||||
public void testRegisterAppInfoRegistersMetrics() throws JMException { |
||||
registerAppInfo(); |
||||
} |
||||
|
||||
@Test |
||||
public void testUnregisterAppInfoUnregistersMetrics() throws JMException { |
||||
registerAppInfo(); |
||||
AppInfoParser.unregisterAppInfo(METRICS_PREFIX, METRICS_ID, metrics); |
||||
|
||||
assertFalse(mBeanServer.isRegistered(expectedAppObjectName())); |
||||
assertNull(metrics.metric(metrics.metricName("commit-id", "app-info"))); |
||||
assertNull(metrics.metric(metrics.metricName("version", "app-info"))); |
||||
assertNull(metrics.metric(metrics.metricName("start-time-ms", "app-info"))); |
||||
} |
||||
|
||||
private void registerAppInfo() throws JMException { |
||||
assertEquals(EXPECTED_COMMIT_VERSION, AppInfoParser.getCommitId()); |
||||
assertEquals(EXPECTED_VERSION, AppInfoParser.getVersion()); |
||||
|
||||
AppInfoParser.registerAppInfo(METRICS_PREFIX, METRICS_ID, metrics, EXPECTED_START_MS); |
||||
|
||||
assertTrue(mBeanServer.isRegistered(expectedAppObjectName())); |
||||
assertEquals(EXPECTED_COMMIT_VERSION, metrics.metric(metrics.metricName("commit-id", "app-info")).metricValue()); |
||||
assertEquals(EXPECTED_VERSION, metrics.metric(metrics.metricName("version", "app-info")).metricValue()); |
||||
assertEquals(EXPECTED_START_MS, metrics.metric(metrics.metricName("start-time-ms", "app-info")).metricValue()); |
||||
} |
||||
|
||||
private ObjectName expectedAppObjectName() throws MalformedObjectNameException { |
||||
return new ObjectName(METRICS_PREFIX + ":type=app-info,id=" + METRICS_ID); |
||||
} |
||||
} |
@ -1,54 +0,0 @@
@@ -1,54 +0,0 @@
|
||||
/** |
||||
* 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 kafka.common |
||||
|
||||
import com.yammer.metrics.core.Gauge |
||||
import kafka.metrics.KafkaMetricsGroup |
||||
import org.apache.kafka.common.utils.AppInfoParser |
||||
|
||||
object AppInfo extends KafkaMetricsGroup { |
||||
private var isRegistered = false |
||||
private val lock = new Object() |
||||
|
||||
def registerInfo(): Unit = { |
||||
lock.synchronized { |
||||
if (isRegistered) { |
||||
return |
||||
} |
||||
} |
||||
|
||||
newGauge("Version", |
||||
new Gauge[String] { |
||||
def value = { |
||||
AppInfoParser.getVersion() |
||||
} |
||||
}) |
||||
|
||||
newGauge("CommitID", |
||||
new Gauge[String] { |
||||
def value = { |
||||
AppInfoParser.getCommitId() |
||||
} |
||||
}) |
||||
|
||||
lock.synchronized { |
||||
isRegistered = true |
||||
} |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue