Skip to content

Commit

Permalink
embed jmx exporter in agent
Browse files Browse the repository at this point in the history
  • Loading branch information
jmazzitelli authored and jshaughn committed Dec 18, 2017
1 parent 1ece284 commit c4c94f6
Show file tree
Hide file tree
Showing 20 changed files with 276 additions and 1,399 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,12 @@
public class AgentCoreEngineConfiguration {
private static final MsgLogger log = AgentLoggers.getLogger(AgentCoreEngineConfiguration.class);

public enum StorageReportTo {
HAWKULAR, // stores metrics to a Hawkular system
METRICS // stores metrics to just a Hawkular-Metrics standalone system
}

/**
* If feed ID is expicitly set to this value, it means the feed ID should be autogenerated at runtime.
*/
private static final String FEED_ID_AUTOGENERATE = "autogenerate";

public static class StorageAdapterConfiguration {
private final StorageReportTo type;
private final String username;
private final String password;
private final String feedId;
Expand All @@ -66,7 +60,6 @@ public static class StorageAdapterConfiguration {
private final int readTimeoutSeconds;

public StorageAdapterConfiguration(
StorageReportTo type,
String username,
String password,
String feedId,
Expand All @@ -80,8 +73,6 @@ public StorageAdapterConfiguration(
String securityRealm,
int connectTimeoutSeconds,
int readTimeoutSeconds) {
super();
this.type = type;
this.username = username;
this.password = password;
this.feedId = (FEED_ID_AUTOGENERATE.equalsIgnoreCase(feedId)) ? null : feedId;
Expand All @@ -97,10 +88,6 @@ public StorageAdapterConfiguration(
this.readTimeoutSeconds = readTimeoutSeconds;
}

public StorageReportTo getType() {
return type;
}

public String getUsername() {
return username;
}
Expand Down Expand Up @@ -232,6 +219,41 @@ public int getNumDmrSchedulerThreads() {
}
}

public static class MetricsExporterConfiguration {

private final boolean enabled;
private final String host;
private final int port;
private final String configFile;

public MetricsExporterConfiguration(
boolean enabled,
String host,
int port,
String configFile) {
this.enabled = enabled;
this.host = host;
this.port = port;
this.configFile = configFile;
}

public boolean isEnabled() {
return enabled;
}

public String getHost() {
return host;
}

public int getPort() {
return port;
}

public String getConfigFile() {
return configFile;
}
}

public static class ProtocolConfiguration<L> {

public static <L> Builder<L> builder() {
Expand Down Expand Up @@ -398,20 +420,24 @@ public Collection<Name> getResourceTypeSets() {
}

private final GlobalConfiguration globalConfiguration;
private final MetricsExporterConfiguration metricsExporterConfiguration;
private final DiagnosticsConfiguration diagnostics;
private final StorageAdapterConfiguration storageAdapter;
private final ProtocolConfiguration<DMRNodeLocation> dmrConfiguration;
private final ProtocolConfiguration<JMXNodeLocation> jmxConfiguration;
private final ProtocolConfiguration<PlatformNodeLocation> platformConfiguration;

public AgentCoreEngineConfiguration(GlobalConfiguration globalConfiguration,
public AgentCoreEngineConfiguration(
GlobalConfiguration globalConfiguration,
MetricsExporterConfiguration metricsExporterConfiguration,
DiagnosticsConfiguration diagnostics,
StorageAdapterConfiguration storageAdapter,
ProtocolConfiguration<DMRNodeLocation> dmrConfiguration,
ProtocolConfiguration<JMXNodeLocation> jmxConfiguration,
ProtocolConfiguration<PlatformNodeLocation> platformConfiguration) {
super();
this.globalConfiguration = globalConfiguration;
this.metricsExporterConfiguration = metricsExporterConfiguration;
this.diagnostics = diagnostics;
this.storageAdapter = storageAdapter;
this.dmrConfiguration = dmrConfiguration;
Expand All @@ -423,6 +449,10 @@ public GlobalConfiguration getGlobalConfiguration() {
return globalConfiguration;
}

public MetricsExporterConfiguration getMetricsExporterConfiguration() {
return metricsExporterConfiguration;
}

public StorageAdapterConfiguration getStorageAdapter() {
return storageAdapter;
}
Expand All @@ -432,9 +462,14 @@ public DiagnosticsConfiguration getDiagnostics() {
}

public AgentCoreEngineConfiguration cloneWith(StorageAdapterConfiguration newStorageAdapter) {
return new AgentCoreEngineConfiguration(globalConfiguration,
diagnostics, newStorageAdapter, dmrConfiguration,
jmxConfiguration, platformConfiguration);
return new AgentCoreEngineConfiguration(
globalConfiguration,
metricsExporterConfiguration,
diagnostics,
newStorageAdapter,
dmrConfiguration,
jmxConfiguration,
platformConfiguration);
}

public ProtocolConfiguration<DMRNodeLocation> getDmrConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,10 @@ public interface MsgLogger extends BasicLogger {
@Message(id = 10002, value = "Hawkular Agent is disabled - it will not be started")
void infoAgentDisabled();

@LogMessage(level = Level.ERROR)
@Message(id = 10008, value = "A metric collection failed")
void errorMetricCollectionFailed(@Cause Throwable t);

@LogMessage(level = Level.ERROR)
@Message(id = 10009, value = "An availability check failed")
void errorAvailCheckFailed(@Cause Throwable t);

@LogMessage(level = Level.ERROR)
@Message(id = 10010, value = "Failed to store metric data: %s")
void errorFailedToStoreMetricData(@Cause Throwable t, String data);

@LogMessage(level = Level.ERROR)
@Message(id = 10011, value = "Failed to store avail data: %s")
void errorFailedToStoreAvailData(@Cause Throwable t, String data);

@LogMessage(level = Level.INFO)
@Message(id = 10012, value = "Starting scheduler")
void infoStartingScheduler();

@LogMessage(level = Level.INFO)
@Message(id = 10013, value = "Stopping scheduler")
void infoStoppingScheduler();

@LogMessage(level = Level.WARN)
@Message(id = 10016, value = "The resource type [%s] wants to use an unknown metric set [%s]")
void warnMetricSetDoesNotExist(String resourceTypeName, String metricSetName);

@LogMessage(level = Level.WARN)
@Message(id = 10017, value = "The resource type [%s] wants to use an unknown avail set [%s]")
void warnAvailSetDoesNotExist(String resourceTypeName, String availSetName);

@LogMessage(level = Level.INFO)
@Message(id = 10019, value = "Managed server [%s] is disabled. It will not be monitored.")
void infoManagedServerDisabled(String name);
Expand All @@ -101,10 +73,6 @@ public interface MsgLogger extends BasicLogger {
@Message(id = 10028, value = "Cannot start storage adapter; aborting startup")
void errorCannotStartStorageAdapter(@Cause Throwable t);

@LogMessage(level = Level.ERROR)
@Message(id = 10029, value = "Scheduler failed to initialize; aborting startup")
void errorCannotInitializeScheduler(@Cause Throwable t);

@LogMessage(level = Level.INFO)
@Message(id = 10030, value = "Using keystore at [%s]")
void infoUseKeystore(String keystorePath);
Expand Down Expand Up @@ -165,14 +133,6 @@ public interface MsgLogger extends BasicLogger {
@Message(id = 10049, value = "Could not access resources of endpoint [%s]")
void errorCouldNotAccess(EndpointService<?, ?> endpoint, @Cause Throwable e);

@LogMessage(level = Level.ERROR)
@Message(id = 10052, value = "Could not store metrics for monitored endpoint [%s]")
void errorFailedToStoreMetrics(String endpoint, @Cause Throwable t);

@LogMessage(level = Level.ERROR)
@Message(id = 10053, value = "Could not store availability data for monitored endpoint [%s]")
void errorFailedToStoreAvails(String endpoint, @Cause Throwable t);

@LogMessage(level = Level.ERROR)
@Message(id = 10054, value = "Agent encountered errors during start up and will be stopped.")
void errorFailedToStartAgent(@Cause Throwable t);
Expand Down Expand Up @@ -226,10 +186,6 @@ public interface MsgLogger extends BasicLogger {
+ "Make sure at least one of these resource types are defined and enabled: %s")
void errorInvalidRootResourceType(String idString, Collection<Name> parents);

@LogMessage(level = Level.ERROR)
@Message(id = 10077, value = "Failed to store metric tags: %s")
void errorFailedToStoreMetricTags(@Cause Throwable t, String data);

@LogMessage(level = Level.WARN)
@Message(id = 10078, value = "Tried %d times to reach the server %s endpoint at %s. Is it up?")
void warnConnectionDelayed(int count, String what, String url);
Expand Down Expand Up @@ -257,4 +213,12 @@ public interface MsgLogger extends BasicLogger {
@LogMessage(level = Level.ERROR)
@Message(id = 10085, value = "Failed to create notification: %s")
void errorFailedToCreateNotification(@Cause Throwable t, String notificationName);

@LogMessage(level = Level.INFO)
@Message(id = 10086, value = "Metrics exporter starting - binding to [%s] using configuration [%s]")
void infoStartMetricsExporter(String hostPort, String configFile);

@LogMessage(level = Level.INFO)
@Message(id = 10087, value = "Metrics exporter is disabled; this agent will not expose any metrics")
void infoMetricsExporterDisabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.hawkular.agent.monitor.cmd.FeedCommProcessor;
import org.hawkular.agent.monitor.cmd.WebSocketClientBuilder;
import org.hawkular.agent.monitor.config.AgentCoreEngineConfiguration;
import org.hawkular.agent.monitor.config.AgentCoreEngineConfiguration.StorageReportTo;
import org.hawkular.agent.monitor.diagnostics.Diagnostics;
import org.hawkular.agent.monitor.diagnostics.DiagnosticsImpl;
import org.hawkular.agent.monitor.diagnostics.JBossLoggingReporter;
Expand All @@ -52,6 +51,7 @@
import com.codahale.metrics.ScheduledReporter;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.prometheus.jmx.WebServer;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
Expand Down Expand Up @@ -259,34 +259,21 @@ public void startHawkularAgent(AgentCoreEngineConfiguration newConfiguration) {
// Before we go on, we must make sure the Hawkular Server is up and ready
waitForHawkularServer();

// perform some things that are dependent upon what mode the agent is in
switch (this.configuration.getStorageAdapter().getType()) {
case HAWKULAR:
// if we are participating in a full Hawkular environment, we need to do some additional things:
// try to connect to the server via command-gateway channel; keep going on error
try {
this.webSocketClientBuilder = new WebSocketClientBuilder(
this.configuration.getStorageAdapter(), ssl, x509TrustManager);
this.feedComm = new FeedCommProcessor(
this.webSocketClientBuilder,
buildAdditionalCommands(),
this.feedId,
this);
this.feedComm.connect();
} catch (Exception e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
log.errorCannotEstablishFeedComm(e);
}
break;

case METRICS:
// nothing special needs to be done
break;
default:
throw new IllegalStateException(
"Unknown storage adapter type: " + this.configuration.getStorageAdapter().getType());
// try to connect to the server via command-gateway channel; keep going on error
try {
this.webSocketClientBuilder = new WebSocketClientBuilder(
this.configuration.getStorageAdapter(), ssl, x509TrustManager);
this.feedComm = new FeedCommProcessor(
this.webSocketClientBuilder,
buildAdditionalCommands(),
this.feedId,
this);
this.feedComm.connect();
} catch (Exception e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
log.errorCannotEstablishFeedComm(e);
}

// start the storage adapter
Expand All @@ -297,12 +284,8 @@ public void startHawkularAgent(AgentCoreEngineConfiguration newConfiguration) {
throw new Exception("Agent cannot start storage adapter");
}

// now that we started the storage adapter, we can create our dispatcher but only if in hawkular mode
if (this.configuration.getStorageAdapter().getType() == StorageReportTo.HAWKULAR) {
this.notificationDispatcher = new NotificationDispatcher(this.storageAdapter, this.feedId);
} else {
this.notificationDispatcher = null;
}
// now that we started the storage adapter, we can create our dispatcher
this.notificationDispatcher = new NotificationDispatcher(this.storageAdapter, this.feedId);

// build the protocol services
ProtocolServices ps = createProtocolServicesBuilder()
Expand All @@ -321,6 +304,23 @@ public void startHawkularAgent(AgentCoreEngineConfiguration newConfiguration) {
// start all protocol services - this should perform the initial discovery scans
protocolServices.start();

// start the metrics exporter if enabled
if (configuration.getMetricsExporterConfiguration().isEnabled()) {
String host = configuration.getMetricsExporterConfiguration().getHost();
int port = configuration.getMetricsExporterConfiguration().getPort();
String configFile = configuration.getMetricsExporterConfiguration().getConfigFile();
String hostPort;
if (host != null) {
hostPort = String.format("%s:%d", host, port);
} else {
hostPort = String.format("%d", port);
}
log.infoStartMetricsExporter(hostPort, configFile);
new WebServer().main(new String[] { hostPort, configFile });
} else {
log.infoMetricsExporterDisabled();
}

setStatus(ServiceStatus.RUNNING);

} catch (Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.hawkular.agent.monitor.api.InventoryEvent;
import org.hawkular.agent.monitor.api.NotificationPayloadBuilder;
import org.hawkular.agent.monitor.config.AgentCoreEngineConfiguration;
import org.hawkular.agent.monitor.config.AgentCoreEngineConfiguration.StorageReportTo;
import org.hawkular.agent.monitor.diagnostics.Diagnostics;
import org.hawkular.agent.monitor.log.AgentLoggers;
import org.hawkular.agent.monitor.log.MsgLogger;
Expand Down Expand Up @@ -54,25 +53,7 @@ public void initialize(
this.config = config;
this.diagnostics = diag;
this.httpClientBuilder = httpClientBuilder;

switch (config.getType()) {
case HAWKULAR:
// We are in a full hawkular environment - so we will integrate with inventory.
this.inventoryStorage = new AsyncInventoryStorage(
feedId,
config,
httpClientBuilder,
diagnostics);
break;

case METRICS:
// We are only integrating with standalone Hawkular Metrics which does not support inventory.
this.inventoryStorage = null;
break;

default:
throw new IllegalArgumentException("Invalid type. Please report this bug: " + config.getType());
}
this.inventoryStorage = new AsyncInventoryStorage(feedId, config, httpClientBuilder, diagnostics);
}

@Override
Expand Down Expand Up @@ -101,11 +82,6 @@ public NotificationPayloadBuilder createNotificationPayloadBuilder() {

@Override
public void store(NotificationPayloadBuilder payloadBuilder, long waitMillis) {
// if we are not in full hawkular mode, there is nothing for us to do
if (this.config.getType() != StorageReportTo.HAWKULAR) {
return;
}

try {
// get the payload
String payload = Util.toJson(payloadBuilder.toPayload());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ diagnostics:
time-units: minutes

storage-adapter:
type: HAWKULAR
feed-id: itest-rest-feed
url: http://127.0.0.1:8080
username: itest-rest
Expand Down
Loading

0 comments on commit c4c94f6

Please sign in to comment.