Skip to content

Commit

Permalink
initial commit of channel monitor logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilpipre committed Sep 19, 2024
1 parent b765ddf commit feb3bdd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,7 @@ public HashMap<String, Object> getCurrentSettings() {
return attributes;
}

public boolean isIntervalChangeOnly(ChannelMonitoringConfig newConfig ) {
return newConfig.channelLog.equals(channelLog) && newConfig.maxLogFiles == maxLogFiles && newConfig.rolloverMinutes == rolloverMinutes && newConfig.rolloverSize.equals(rolloverSize) && newConfig.enabled == enabled && newConfig.collection_period != collection_period;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public class ChannelMonitoringLogger implements AgentConfigListener {
public static final String log_file_name = "channels.log";
public static final String state_log_name = "channel-state.log";
private static ChannelMonitoringConfig currentChannelConfig = null;

private static ChannelMonitoringLogger INSTANCE = null;
private static NRLabsHandler channelsHandler;
private static NRLabsHandler statesHandler;

private ChannelMonitoringLogger() {

}

protected static ChannelMonitoringConfig getConfig() {
Expand All @@ -48,7 +48,7 @@ protected static void logToChannelMonitor(String message) {
}
CHANNELSLOGGER.log(Level.INFO, message);
}

protected static void logToStateLog(String message) {
if(!initialized) {
init();
Expand All @@ -57,9 +57,11 @@ protected static void logToStateLog(String message) {
}

public static void init() {
if(INSTANCE == null) {
INSTANCE = new ChannelMonitoringLogger();
ServiceFactory.getConfigService().addIAgentConfigListener(INSTANCE);
synchronized(INSTANCE) {
if(INSTANCE == null) {
INSTANCE = new ChannelMonitoringLogger();
ServiceFactory.getConfigService().addIAgentConfigListener(INSTANCE);
}
}
if(currentChannelConfig == null) {
Config agentConfig = NewRelic.getAgent().getConfig();
Expand All @@ -81,7 +83,7 @@ public static void init() {
sb.append(c);
}
}

long size = Long.parseLong(sb.toString());
char end = rolloverSize.charAt(length-1);
switch (end) {
Expand All @@ -95,7 +97,7 @@ public static void init() {
size *= 1024L*1024L*1024L;
break;
}

// disallow less than 10K
if(size < 10 * 1024L) {
size = 10 * 1024L;
Expand Down Expand Up @@ -124,7 +126,7 @@ public static void init() {
}

int maxFiles = currentChannelConfig.getMaxLogFiles();

// If we have already initialized the handler then perform cleanup
if(channelsHandler != null) {
channelsHandler.flush();
Expand Down Expand Up @@ -157,7 +159,7 @@ public static void init() {
if(CHANNELSLOGGER != null && channelsHandler != null) {
CHANNELSLOGGER.addHandler(channelsHandler);
}

if(statesHandler != null) {
statesHandler.flush();
statesHandler.close();
Expand All @@ -166,7 +168,7 @@ public static void init() {
}
statesHandler = null;
}

try {
statesHandler = new NRLabsHandler(state_log_name, rolloverMinutes, size, maxFiles);
statesHandler.setFormatter(new NRLabsFormatter());
Expand All @@ -175,7 +177,7 @@ public static void init() {
} catch (IOException e) {
NewRelic.getAgent().getLogger().log(Level.FINE, e, "Failed to create channel state log file at {0}", state_log_name);
}

if(STATELOGGER == null) {
try {
NewRelic.getAgent().getLogger().log(Level.FINE, "Building Log File, name: {0}, size: {1}, maxfiles: {2}", stateLogFileName, size, maxFiles);
Expand All @@ -184,11 +186,11 @@ public static void init() {
NewRelic.getAgent().getLogger().log(Level.FINE, e, "Failed to create channel state log file at {0}", channelMonitorFileName);
}
}

if(STATELOGGER != null && statesHandler != null) {
STATELOGGER.addHandler(statesHandler);
}

initialized = true;
}

Expand All @@ -212,7 +214,7 @@ public static void checkConfig() {
}
}
}

public static ChannelMonitoringConfig getConfig(Config agentConfig) {
ChannelMonitoringConfig channelConifg = new ChannelMonitoringConfig();
Integer rolloverMinutes = agentConfig.getValue(CHANNELLOGROLLOVERINTERVAL);
Expand Down Expand Up @@ -244,7 +246,7 @@ public static ChannelMonitoringConfig getConfig(Config agentConfig) {

boolean enabled = agentConfig.getValue(CHANNELSLOGENABLED, Boolean.TRUE);
channelConifg.setEnabled(enabled);

Integer collection = agentConfig.getValue(CHANNELSLOGCOLLECTIONPERIOD);
if(collection != null) {
channelConifg.setCollection_period(collection);
Expand All @@ -266,9 +268,14 @@ public void configChanged(String appName, AgentConfig agentConfig) {
} else {
if(!channelConfig.equals(currentChannelConfig)) {
currentChannelConfig = channelConfig;

NewRelic.getAgent().getInsights().recordCustomEvent("ChannelMonitoringConfig", currentChannelConfig.getCurrentSettings());
initialized = false;
init();
if(currentChannelConfig.isIntervalChangeOnly(channelConfig)) {
ChannelMonitor.reinitialScheduled(channelConfig.getCollection_period());
} else {
initialized = false;
init();
}
}
}
}
Expand Down

0 comments on commit feb3bdd

Please sign in to comment.