Skip to content

Commit

Permalink
try to turn on all WildFly statistics if monitoring a WildFly server (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jmazzitelli authored and jshaughn committed Dec 18, 2017
1 parent 225a290 commit 2865e18
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,20 @@ public final void start() {
} while (!ready);
}

postStart();

status = ServiceStatus.RUNNING;

LOG.debugf("Started [%s]", toString());
}

/**
* Let subclasses perform some stuff after the service is ready and about to start.
*/
protected void postStart() {
// no-op
}

public void stop() {
status.assertRunning(getClass(), "stop()");
status = ServiceStatus.STOPPING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.hawkular.agent.monitor.protocol.dmr;

import java.io.IOException;
import java.util.Map;

import org.hawkular.agent.monitor.config.AgentCoreEngineConfiguration.EndpointConfiguration;
import org.hawkular.agent.monitor.diagnostics.ProtocolDiagnostics;
Expand All @@ -35,6 +36,8 @@
public class DMREndpointService
extends EndpointService<DMRNodeLocation, DMRSession> {

public static final String ENABLE_STATISTICS_KEY = "enable-statistics";

public static String lookupServerIdentifier(ModelControllerClient client) throws IOException {
ModelNode rootNode = OperationBuilder.readResource().includeRuntime().execute(client).assertSuccess()
.getResultNode();
Expand Down Expand Up @@ -105,4 +108,21 @@ public DMRSession openSession() {
getLocationResolver(), client);
}

@Override
protected void postStart() {
// see if we are to enable statistics - default is 'yes'
Boolean enable = Boolean.TRUE;
Map<String, ? extends Object> customData = getMonitoredEndpoint().getEndpointConfiguration().getCustomData();
if (customData != null && customData.containsKey(ENABLE_STATISTICS_KEY)) {
enable = (Boolean) customData.get(ENABLE_STATISTICS_KEY);
}

if (enable) {
try (ModelControllerClient mcc = this.modelControllerClientFactory.createClient()) {
new StatisticsControl().enableStatistics(mcc);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* Copyright 2015-2017 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed 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.hawkular.agent.monitor.protocol.dmr;

import java.util.Collections;
import java.util.List;

import org.hawkular.agent.monitor.log.AgentLoggers;
import org.hawkular.agent.monitor.log.MsgLogger;
import org.hawkular.agent.monitor.util.WildflyCompatibilityUtils;
import org.hawkular.dmr.api.OperationBuilder;
import org.hawkular.dmr.api.OperationBuilder.CompositeOperationBuilder;
import org.hawkular.dmr.api.OperationBuilder.OperationResult;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.client.ModelControllerClient;

/**
* Turns on or off statistics for several WildFly subsystems.
*/
public class StatisticsControl {
private static final MsgLogger log = AgentLoggers.getLogger(StatisticsControl.class);

public StatisticsControl() {
}

public void enableStatistics(ModelControllerClient mcc) {
execute(mcc, true);
}

public void disableStatistics(ModelControllerClient mcc) {
execute(mcc, false);
}

private void execute(ModelControllerClient mcc, boolean enable) {

try {
List<String> hosts = getDomainHosts(mcc);
if (hosts.isEmpty()) {
executeForServer(mcc, enable, "");
} else {
for (String host : hosts) {
List<String> servers = getDomainHostServers(mcc, host);
for (String server : servers) {
// I was trying to be too clever. This would work awesomely if WildFly would just allow it.
// However, this error occurs when you try to set attributes on slave servers:
// "User operations are not permitted to directly update the persistent configuration
// of a server in a managed domain."
// So, for now, comment this out until perhaps in the future WildFly/EAP will allow this.
// This means when in domain mode, the user must manually turn on statistics themselves.
//executeForServer(mcc, enable, String.format("/host=%s/server=%s", host, server));
log.debugf("Statistics for server [/host=%s/server=%s] must be enabled manually", host,
server);
}
}
}
} catch (Exception e) {
log.errorf(e, "Aborting statistics enablement");
}
}

private void executeForServer(ModelControllerClient mcc, boolean enable, String serverPrefix) {

try {
String enableStr = String.valueOf(enable);
CompositeOperationBuilder<?> batch;

// datasources
batch = OperationBuilder.composite();
List<String> dsList = getChildrenNames(
WildflyCompatibilityUtils.parseCLIStyleAddress(serverPrefix + "/subsystem=datasources"),
"data-source", mcc);
List<String> xaList = getChildrenNames(
WildflyCompatibilityUtils.parseCLIStyleAddress(serverPrefix + "/subsystem=datasources"),
"xa-data-source", mcc);
for (String ds : dsList) {
String dsAddr = String.format(serverPrefix + "/subsystem=datasources/data-source=%s", ds);
batch.writeAttribute()
.address(WildflyCompatibilityUtils.parseCLIStyleAddress(dsAddr))
.attribute("statistics-enabled", enableStr)
.parentBuilder();
}
for (String xa : xaList) {
String xaAddr = String.format(serverPrefix + "/subsystem=datasources/xa-data-source=%s", xa);
batch.writeAttribute()
.address(WildflyCompatibilityUtils.parseCLIStyleAddress(xaAddr))
.attribute("statistics-enabled", enableStr)
.parentBuilder();
}
execute(mcc, batch, "datasources", enable);

// ejb3
batch = OperationBuilder.composite();
batch.writeAttribute()
.address(WildflyCompatibilityUtils.parseCLIStyleAddress(serverPrefix + "/subsystem=ejb3"))
.attribute("enable-statistics", enableStr)
.parentBuilder();
execute(mcc, batch, "ejb3", enable);

// infinispan
batch = OperationBuilder.composite();
List<String> infinispanList = getChildrenNames(
WildflyCompatibilityUtils.parseCLIStyleAddress(serverPrefix + "/subsystem=infinispan"),
"cache-container", mcc);
for (String name : infinispanList) {
String addr = String.format(serverPrefix + "/subsystem=infinispan/cache-container=%s", name);
batch.writeAttribute()
.address(WildflyCompatibilityUtils.parseCLIStyleAddress(addr))
.attribute("statistics-enabled", enableStr)
.parentBuilder();
}
execute(mcc, batch, "infinispan", enable);

// activemq
batch = OperationBuilder.composite();
List<String> activeMqList = getChildrenNames(
WildflyCompatibilityUtils.parseCLIStyleAddress(serverPrefix + "/subsystem=messaging-activemq"),
"server", mcc);
for (String name : activeMqList) {
String addr = String.format(serverPrefix + "/subsystem=messaging-activemq/server=%s", name);
batch.writeAttribute()
.address(WildflyCompatibilityUtils.parseCLIStyleAddress(addr))
.attribute("statistics-enabled", enableStr)
.parentBuilder();
}
execute(mcc, batch, "activemq", enable);

// transactions
batch = OperationBuilder.composite();
batch.writeAttribute()
.address(WildflyCompatibilityUtils.parseCLIStyleAddress(serverPrefix + "/subsystem=transactions"))
.attribute("enable-statistics", enableStr)
.parentBuilder();
execute(mcc, batch, "transactions", enable);

// undertow
batch = OperationBuilder.composite();
batch.writeAttribute()
.address(WildflyCompatibilityUtils.parseCLIStyleAddress(serverPrefix + "/subsystem=undertow"))
.attribute("statistics-enabled", enableStr)
.parentBuilder();
execute(mcc, batch, "undertow", enable);

} catch (Exception e) {
log.errorf(e, "Aborting statistics enablement");
}
}

private void execute(ModelControllerClient controllerClient, CompositeOperationBuilder<?> batch, String name,
boolean enable) {
OperationResult<?> opResult = null;
try {
opResult = batch.execute(controllerClient).assertSuccess();
log.debugf("%s statistics for [%s]", enable ? "Enabled" : "Disabled", name);
} catch (Exception e) {
String errorStr = (opResult != null) ? opResult.getResultNode().toJSONString(true) : "unknown result";
log.debugf(e, "Cannot set statistics for [%s]: %s", name, errorStr);
}
}

private List<String> getChildrenNames(PathAddress parentPath, String childType, ModelControllerClient mcc) {
try {
return OperationBuilder.readChildrenNames()
.address(parentPath)
.childType(childType)
.execute(mcc)
.assertSuccess()
.getList();
} catch (Exception e) {
log.debugf("Cannot get children of [" + parentPath + "] of type [" + childType + "]");
return Collections.emptyList();
}
}

// returns empty list if not in domain mode
private List<String> getDomainHosts(ModelControllerClient mcc) {
return getChildrenNames(PathAddress.EMPTY_ADDRESS, "host", mcc);
}

// returns empty list if not in domain mode
private List<String> getDomainHostServers(ModelControllerClient mcc, String hostName) {
return getChildrenNames(PathAddress.EMPTY_ADDRESS.append("host", hostName), "server", mcc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ managed-servers:
- name: /
metric-labels:
feed_id: "%FeedId"
enable-statistics: false

local-jmx:
name: Local JMX
Expand All @@ -179,6 +180,7 @@ managed-servers:
- name: /
metric-labels:
feed_id: "%FeedId"
enable-statistics: false

remote-jmx:
- name: Remote JMX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ managed-servers:
- name: /
metric-labels:
feed_id: "%FeedId"
enable-statistics: false

local-jmx:
name: Local JMX
Expand All @@ -179,6 +180,7 @@ managed-servers:
- name: /
metric-labels:
feed_id: "%FeedId"
enable-statistics: false

platform:
enabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.hawkular.agent.monitor.inventory.TypeSet;
import org.hawkular.agent.monitor.inventory.TypeSet.TypeSetBuilder;
import org.hawkular.agent.monitor.inventory.TypeSets;
import org.hawkular.agent.monitor.protocol.dmr.DMREndpointService;
import org.hawkular.agent.monitor.protocol.dmr.DMRNodeLocation;
import org.hawkular.agent.monitor.protocol.jmx.JMXEndpointService;
import org.hawkular.agent.monitor.protocol.jmx.JMXNodeLocation;
Expand Down Expand Up @@ -316,7 +317,8 @@ private ProtocolConfiguration<DMRNodeLocation> buildDmrConfiguration(Configurati
connectionData,
null,
config.getManagedServers().getLocalDmr().getMetricLabels(),
null,
Collections.singletonMap(DMREndpointService.ENABLE_STATISTICS_KEY,
config.getManagedServers().getLocalDmr().getEnableStatistics()),
asWaitForList(config.getManagedServers().getLocalDmr().getWaitFor()));
managedServers.put(config.getManagedServers().getLocalDmr().getName(), localDmrEndpointConfig);
}
Expand Down Expand Up @@ -346,7 +348,8 @@ private ProtocolConfiguration<DMRNodeLocation> buildDmrConfiguration(Configurati
connectionData,
remoteDmr.getSecurityRealmName(),
remoteDmr.getMetricLabels(),
null,
Collections.singletonMap(DMREndpointService.ENABLE_STATISTICS_KEY,
remoteDmr.getEnableStatistics()),
asWaitForList(remoteDmr.getWaitFor()));

managedServers.put(remoteDmr.getName(), remoteDmrEndpointConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class LocalDMR implements Validatable {
@JsonProperty("wait-for")
private WaitFor[] waitFor;

@JsonProperty("enable-statistics")
private BooleanExpression enableStatistics = new BooleanExpression(Boolean.TRUE);

public LocalDMR() {
}

Expand All @@ -58,6 +61,8 @@ public LocalDMR(LocalDMR original) {
: Arrays.copyOf(original.resourceTypeSets, original.resourceTypeSets.length);
this.metricLabels = original.metricLabels == null ? null : new HashMap<>(original.metricLabels);
this.waitFor = original.waitFor == null ? null : Arrays.copyOf(original.waitFor, original.waitFor.length);
this.enableStatistics = original.enableStatistics == null ? null
: new BooleanExpression(original.enableStatistics);
}

@Override
Expand Down Expand Up @@ -126,4 +131,16 @@ public WaitFor[] getWaitFor() {
public void setWaitFor(WaitFor[] waitFor) {
this.waitFor = waitFor;
}

public Boolean getEnableStatistics() {
return enableStatistics == null ? null : enableStatistics.get();
}

public void setEnableStatistics(Boolean enableStatistics) {
if (this.enableStatistics != null) {
this.enableStatistics.set(enableStatistics);
} else {
this.enableStatistics = new BooleanExpression(enableStatistics);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public class RemoteDMR implements Validatable {
@JsonProperty("wait-for")
private WaitFor[] waitFor;

@JsonProperty("enable-statistics")
private BooleanExpression enableStatistics = new BooleanExpression(Boolean.TRUE);

public RemoteDMR() {
}

Expand All @@ -87,6 +90,8 @@ public RemoteDMR(RemoteDMR original) {
: Arrays.copyOf(original.resourceTypeSets, original.resourceTypeSets.length);
this.metricLabels = original.metricLabels == null ? null : new HashMap<>(original.metricLabels);
this.waitFor = original.waitFor == null ? null : Arrays.copyOf(original.waitFor, original.waitFor.length);
this.enableStatistics = original.enableStatistics == null ? null
: new BooleanExpression(original.enableStatistics);
}

@Override
Expand Down Expand Up @@ -238,4 +243,16 @@ public WaitFor[] getWaitFor() {
public void setWaitFor(WaitFor[] waitFor) {
this.waitFor = waitFor;
}

public Boolean getEnableStatistics() {
return enableStatistics == null ? null : enableStatistics.get();
}

public void setEnableStatistics(Boolean enableStatistics) {
if (this.enableStatistics != null) {
this.enableStatistics.set(enableStatistics);
} else {
this.enableStatistics = new BooleanExpression(enableStatistics);
}
}
}
Loading

0 comments on commit 2865e18

Please sign in to comment.