Skip to content

Commit

Permalink
Merge pull request #3142 from jGauravGupta/PAYARA-2975
Browse files Browse the repository at this point in the history
PAYARA-2975 'metrics.xml' parsed and metadata re-registered on every call to '/metrics' endpoint
  • Loading branch information
arjantijms authored Sep 13, 2018
2 parents 8b14073 + c386905 commit ff1a04e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import fish.payara.microprofile.metrics.exception.NoSuchMetricException;
import fish.payara.microprofile.metrics.exception.NoSuchRegistryException;
import fish.payara.microprofile.metrics.impl.MetricRegistryImpl;
import fish.payara.microprofile.metrics.jmx.MBeanMetadata;
import fish.payara.microprofile.metrics.jmx.MBeanMetadataConfig;
import fish.payara.microprofile.metrics.jmx.MBeanMetadataHelper;
import fish.payara.nucleus.executorservice.PayaraExecutorService;
Expand All @@ -54,6 +55,7 @@
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -99,6 +101,10 @@ public class MetricsService implements EventListener {

private Boolean metricsSecure;

private List<MBeanMetadata> unresolvedBaseMetadataList;

private List<MBeanMetadata> unresolvedVendorMetadataList;

private final Map<String, MetricRegistry> REGISTRIES = new ConcurrentHashMap<>();//stores registries of base, vendor, app1, app2, ... app(n) etc

public MetricsService() {
Expand All @@ -112,7 +118,10 @@ public void init() {
// Only start if metrics are enabled
if (isMetricsEnabled()) {
PayaraExecutorService payaraExecutor = serviceLocator.getService(PayaraExecutorService.class, new Annotation[0]);
payaraExecutor.submit(() -> initMetadataConfig(getConfig(), false));
payaraExecutor.submit(() -> {
MBeanMetadataConfig metadataConfig = getConfig();
initMetadataConfig(metadataConfig.getBaseMetadata(), metadataConfig.getVendorMetadata(), false);
});
}
}

Expand All @@ -127,32 +136,36 @@ public void event(Event event) {
}
}

/**
* Initialise metrics from the
* metrics.xml containing the base & vendor metrics metadata.
/**
* Initialize metrics from the metrics.xml containing the base & vendor
* metrics metadata.
*
* @param metadataConfig
*/
private void initMetadataConfig(MBeanMetadataConfig metadataConfig, boolean isRetry) {
private void initMetadataConfig(List<MBeanMetadata> baseMetadataList, List<MBeanMetadata> vendorMetadataList, boolean isRetry) {
Map<String, String> globalTags = MetricsHelper.getGlobalTagsMap();
helper.registerMetadata(
getOrAddRegistry(BASE.getName()),
metadataConfig.getBaseMetadata(),
globalTags, isRetry);
helper.registerMetadata(
getOrAddRegistry(VENDOR.getName()),
metadataConfig.getVendorMetadata(),
globalTags, isRetry);
if (!baseMetadataList.isEmpty()) {
unresolvedBaseMetadataList = helper.registerMetadata(
getOrAddRegistry(BASE.getName()),
baseMetadataList,
globalTags, isRetry);
}
if (!vendorMetadataList.isEmpty()) {
unresolvedVendorMetadataList = helper.registerMetadata(
getOrAddRegistry(VENDOR.getName()),
vendorMetadataList,
globalTags, isRetry);
}
}

/**
* Rereads the domain.xml and registers MBeans if they have been started after
* the metrics service.
* Registers unresolved MBeans if they have been started after the metrics
* service.
*/
public void reregisterMetadataConfig(){
initMetadataConfig(getConfig(), true);
public void reregisterMetadataConfig() {
initMetadataConfig(unresolvedBaseMetadataList, unresolvedVendorMetadataList, true);
}

private MBeanMetadataConfig getConfig() {

InputStream defaultConfig = MetricsHelper.class.getResourceAsStream("/metrics.xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@
@XmlAccessorType(XmlAccessType.FIELD)
public class MBeanMetadata extends Metadata {

@XmlElement(name="mbean")
@XmlElement(name = "mbean")
private String mBean;

@XmlElement
private boolean dynamic = true;

public MBeanMetadata() {
super(null, MetricType.INVALID);
}
Expand All @@ -69,4 +72,13 @@ public void setMBean(String mBean) {
this.mBean = mBean;
}

public boolean isDynamic() {
return dynamic;
}

public void setDynamic(boolean dynamic) {
this.dynamic = dynamic;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -61,57 +61,60 @@ public class MBeanMetadataHelper {
private static final String SPECIFIER = "%s";

private static final Logger LOGGER = Logger.getLogger(MBeanMetadataHelper.class.getName());

private List<MBeanMetadata> unresolvedMetadataList;

/**
* Registers metrics as MBeans
*
* @param metricRegistry Registry to add metrics to
* @param metadataList List of all {@link MBeanMetadata} representing a {@link Metric}
* @param globalTags
* @param isRetry true if this is not initial registration, this is used to register
* lazy-loaded MBeans
* @param metadataList List of all {@link MBeanMetadata} representing a
* {@link Metric}
* @param globalTags
* @param isRetry true if this is not initial registration, this is used to
* register lazy-loaded MBeans
* @return the list of unresolved MBean Metadata
*/
public void registerMetadata(MetricRegistry metricRegistry,
public List<MBeanMetadata> registerMetadata(MetricRegistry metricRegistry,
List<MBeanMetadata> metadataList, Map<String, String> globalTags, boolean isRetry) {

if (!metricRegistry.getMetadata().isEmpty()) {
if (!metricRegistry.getMetadata().isEmpty() && !isRetry) {
metricRegistry.removeMatching(MetricFilter.ALL);
}

resolveDynamicMetadata(metadataList);
for (MBeanMetadata beanMetadata: metadataList){
try {
if (metricRegistry.getNames().contains(beanMetadata.getName()) && isRetry){
//
continue;
}
beanMetadata.getTags().putAll(globalTags);
Metric type;
MBeanExpression mBeanExpression = new MBeanExpression(beanMetadata.getMBean());
switch (beanMetadata.getTypeRaw()) {
case COUNTER:
type = new MBeanCounterImpl(mBeanExpression);
break;
case GAUGE:
type = (Gauge<Number>) mBeanExpression::getNumberValue;
break;
default:
throw new IllegalStateException("Unsupported type : " + beanMetadata);
}
metricRegistry.register(beanMetadata, type);
List<MBeanMetadata> unresolvedMetadataList = resolveDynamicMetadata(metadataList);
for (MBeanMetadata beanMetadata : metadataList) {
try {
if (metricRegistry.getNames().contains(beanMetadata.getName())) {
continue;
}
beanMetadata.getTags().putAll(globalTags);
Metric type;
MBeanExpression mBeanExpression = new MBeanExpression(beanMetadata.getMBean());
switch (beanMetadata.getTypeRaw()) {
case COUNTER:
type = new MBeanCounterImpl(mBeanExpression);
break;
case GAUGE:
type = (Gauge<Number>) mBeanExpression::getNumberValue;
break;
default:
throw new IllegalStateException("Unsupported type : " + beanMetadata);
}
metricRegistry.register(beanMetadata, type);
} catch (IllegalArgumentException e) {
LOGGER.log(Level.WARNING, e.getMessage());
}
}
return unresolvedMetadataList;
}

/**
* Resolve dynamic metadata by replacing specifier <b>%s</b> with the mbean value.
*
* @param metadataList list of MBean Metadata
* @return the list of unresolved MBean Metadata
*/
public void resolveDynamicMetadata(List<MBeanMetadata> metadataList) {
public List<MBeanMetadata> resolveDynamicMetadata(List<MBeanMetadata> metadataList) {
List<MBeanMetadata> unresolvedMetadataList = new ArrayList<>();
List<MBeanMetadata> resolvedMetadataList = new ArrayList<>();
List<Metadata> removedMetadataList = new ArrayList<>(metadataList.size());
for (MBeanMetadata metadata : metadataList) {
Expand All @@ -126,7 +129,10 @@ public void resolveDynamicMetadata(List<MBeanMetadata> metadataList) {
String dynamicKey = mBeanExpression.findDynamicKey();
Set<ObjectName> mBeanObjects = mBeanExpression.queryNames(null);
if (mBeanObjects.isEmpty()){
unresolvedMetadataList.add(metadata);
LOGGER.log(Level.INFO, "{0} does not correspond to any MBeans", metadata.getMBean());
} else if (metadata.isDynamic()) {
unresolvedMetadataList.add(metadata);
}
for (ObjectName objName : mBeanObjects) {
String dynamicValue = objName.getKeyPropertyList().get(dynamicKey);
Expand Down Expand Up @@ -159,9 +165,10 @@ public void resolveDynamicMetadata(List<MBeanMetadata> metadataList) {
}
}
}

metadataList.removeAll(removedMetadataList);
metadataList.addAll(resolvedMetadataList);
return unresolvedMetadataList;
}

private boolean validateMetadata(MBeanMetadata metadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ holder.
<metadata>
<name>gc.%s.count</name>
<mbean>java.lang:type=GarbageCollector,name=%s/CollectionCount</mbean>
<dynamic>false</dynamic>
<type>counter</type>
<unit>none</unit>
<displayName>Garbage Collection Count</displayName>
Expand All @@ -147,6 +148,7 @@ holder.
<metadata>
<name>gc.%s.time</name>
<mbean>java.lang:type=GarbageCollector,name=%s/CollectionTime</mbean>
<dynamic>false</dynamic>
<type>counter</type>
<unit>milliseconds</unit>
<displayName>Garbage Collection Time</displayName>
Expand Down

0 comments on commit ff1a04e

Please sign in to comment.