Skip to content

Commit

Permalink
Merge pull request #15 from newrelic-experimental/gateway-monitoring
Browse files Browse the repository at this point in the history
Gateway monitoring
  • Loading branch information
dhilpipre authored May 30, 2024
2 parents b76417a + 08335ad commit ca83307
Show file tree
Hide file tree
Showing 29 changed files with 1,269 additions and 98 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ bin
.project
*/lib/*.jar
.DS_Store
libs
.gradle
3 changes: 0 additions & 3 deletions sap-aii-af/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ dependencies {
// Example:
// implementation 'javax.servlet:servlet-api:2.5'

// New Relic Labs Java Agent dependencies
implementation 'com.newrelic.agent.java:newrelic-agent:6.4.0'
implementation 'com.newrelic.agent.java:newrelic-api:6.4.0'
implementation fileTree(include: ['*.jar'], dir: '../libs')
implementation fileTree(include: ['*.jar'], dir: 'lib')
implementation fileTree(include: ['*.jar'], dir: '../test-lib')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static void premain(String s, Instrumentation inst) {
public static void init() {
if(!isSAP()) {
NewRelic.getAgent().getLogger().log(Level.FINE, "Skipping Communication Channel logging, application does not appear to be SAP");
return;
}
NewRelic.getAgent().getLogger().log(Level.FINE, "Call to initialize Communcation Channel Logging");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.newrelic.instrumentation.labs.sap.mpl;
package com.newrelic.instrumentation.labs.sap.mpl.processing;

import java.io.File;
import java.util.HashMap;
Expand Down Expand Up @@ -51,18 +51,13 @@ public void setRolloverMinutes(int rolloverMinutes) {
this.rolloverMinutes = rolloverMinutes;
}

public boolean isDetailedEnabled() {
return gateway_enabled;
}

@Override
public boolean equals(Object obj) {
if(obj == null) return false;

if(!(obj instanceof GatewayConfig)) return false;

GatewayConfig newConfig = (GatewayConfig)obj;

return newConfig.gatewayLog.equals(gatewayLog) && newConfig.maxLogFiles == maxLogFiles && newConfig.rolloverMinutes == rolloverMinutes && newConfig.rolloverSize.equals(rolloverSize) && newConfig.gateway_enabled == gateway_enabled;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
package com.newrelic.instrumentation.labs.sap.mpl;
package com.newrelic.instrumentation.labs.sap.mpl.processing;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.newrelic.agent.HarvestListener;
import com.newrelic.agent.stats.StatsEngine;
import com.newrelic.api.agent.Config;
import com.newrelic.api.agent.Insights;
import com.newrelic.api.agent.NewRelic;

public class GatewayLogger implements HarvestListener {

protected static final String MPL_LOG_NAME = "gateway.log";
public static boolean initialized = false;
private static Logger GATEWAYLOGGER;
private static Logger GATEWAY_LOGGER;
protected static final String GATEWAYLOGFILENAME = "SAP.gatewaylog.log_file_name";
protected static final String GATEWAYLOGGINGROLLOVERINTERVAL = "SAP.gatewaylog.log_file_interval";
protected static final String GATEWAYLOGGINGROLLOVERSIZE = "SAP.gatewaylog.log_size_limit";
protected static final String GATEWAYLOGGINGROLLOVERSIZE2 = "SAP.gatewaylog.log_file_size";
protected static final String GATEWAYLOGGINGMAXFILES = "SAP.gatewaylog.log_file_count";
protected static final String CHANNELSLOGENABLED = "SAP.gatewaylog.enabled";
protected static final String GATEWAYLOGENABLED = "SAP.gatewaylog.enabled";
private static final String LOGEVENTS = "SAP/GatewayLogger/Events";

private static GatewayConfig currentGatewayConfig = null;
Expand All @@ -32,16 +36,19 @@ public class GatewayLogger implements HarvestListener {

private static NRLabsTimerTask timerTask = null;
private static Integer LoggingCount = 0;
private static boolean enabled = true;

private GatewayLogger() {

}

protected static void checkConfig() {

Config agentConfig = NewRelic.getAgent().getConfig();

GatewayConfig newConfig = getConfig(agentConfig);
if(newConfig != currentGatewayConfig) {
if(currentGatewayConfig == null || !newConfig.equals(currentGatewayConfig)) {
NewRelic.getAgent().getLogger().log(Level.FINE, "In GatewayLogger.checkConfig, config changed, reinitializing");
// reinitialize log
currentGatewayConfig = newConfig;
init();
Expand Down Expand Up @@ -82,22 +89,32 @@ public static GatewayConfig getConfig(Config agentConfig) {
gatewayConfig.setGatewayLog(filename);
}

boolean enabled = agentConfig.getValue(CHANNELSLOGENABLED, Boolean.TRUE);
boolean enabled = agentConfig.getValue(GATEWAYLOGENABLED, Boolean.TRUE);
gatewayConfig.setGateway_enabled(enabled);


return gatewayConfig;
}

public static void log(String message) {
if(!enabled) return;

if(!initialized) {
init();
}
GATEWAYLOGGER.log(Level.INFO, message);

GATEWAY_LOGGER.log(Level.INFO, message);
LoggingCount++;
}

public static void init() {
if(currentGatewayConfig == null) {
Config agentConfig = NewRelic.getAgent().getConfig();
currentGatewayConfig = getConfig(agentConfig);
}

enabled = currentGatewayConfig.isGateway_enabled();

if(INSTANCE == null) {
INSTANCE = new GatewayLogger();
}
Expand All @@ -108,11 +125,6 @@ public static void init() {
timer.scheduleAtFixedRate(timerTask, 2L * 60000L, 2L * 60000);
}

if(currentGatewayConfig == null) {
Config agentConfig = NewRelic.getAgent().getConfig();
currentGatewayConfig = getConfig(agentConfig);
}

int rolloverMinutes = currentGatewayConfig.getRolloverMinutes();
if(rolloverMinutes < 1) {
rolloverMinutes = 60;
Expand Down Expand Up @@ -147,7 +159,7 @@ public static void init() {
size = 10 * 1024L;
}

String gatewayFileName = currentGatewayConfig.getGatewayLog();;
String gatewayFileName = currentGatewayConfig.getGatewayLog();
File file = new File(gatewayFileName);
File parent = file.getParentFile();

Expand All @@ -163,26 +175,41 @@ public static void init() {
if(handler != null) {
handler.flush();
handler.close();
if(GATEWAYLOGGER != null) {
GATEWAYLOGGER.removeHandler(handler);
if(GATEWAY_LOGGER != null) {
GATEWAY_LOGGER.removeHandler(handler);
}
handler = null;
}

try {
handler = new NRLabsHandler(gatewayFileName, rolloverMinutes, size, maxFiles);
handler.setFormatter(new NRLabsFormatter());
} catch (IOException e) {
NewRelic.getAgent().getLogger().log(Level.FINE, e, "Failed to create NRHandler for gateway log");
}

if(GATEWAYLOGGER == null) {
GATEWAYLOGGER = Logger.getLogger("GatewayLog");
if(GATEWAY_LOGGER == null) {
GATEWAY_LOGGER = Logger.getLogger("GatewayLog");
}
GATEWAY_LOGGER.setLevel(Level.INFO);
GATEWAY_LOGGER.setUseParentHandlers(false);

if(GATEWAYLOGGER != null && handler != null) {
GATEWAYLOGGER.addHandler(handler);
if(GATEWAY_LOGGER != null && handler != null) {
GATEWAY_LOGGER.addHandler(handler);
}

Map<String, Object> event = new HashMap<>();
event.put("Initialized", new Date());
event.put("GateLogFile",gatewayFileName);
event.put("RolloverSize",rolloverSize);
event.put("RolloverMinutes",rolloverMinutes);
event.put("Enabled", enabled);
Insights insights = NewRelic.getAgent().getInsights();
if(insights != null) {
insights.recordCustomEvent("GatewayLogInit", event);
} else {
NewRelic.getAgent().getLogger().log(Level.FINE, "Constructed GATEWAY_LOGGER using : {0}", event);
}
initialized = true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.newrelic.instrumentation.labs.sap.mpl.processing;

import java.util.Map;

import com.sap.it.op.mpl.MessageProcessingLogPart;

public class GatewayUtils {

public static void addAttribute(Map<String, Object> attributes, String key, Object value) {
if(value != null && attributes != null && key != null && !key.isEmpty()) {
attributes.put(key, value);
}
}

public static void reportMPL(MessageProcessingLogPart logPart) {
String logString = logPart.toLogString();
if(logString != null && !logString.isEmpty()) {
GatewayLogger.log(logString);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.newrelic.instrumentation.labs.sap.mpl.processing;

import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public class NRLabsFormatter extends Formatter {

@Override
public String format(LogRecord record) {
return record.getMessage() + "\n";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.newrelic.instrumentation.labs.sap.mpl.processing;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import com.newrelic.api.agent.NewRelic;

public class NRLabsHandler extends Handler {

private int maxFiles;
private long maxSize;
private long rolloverTime;
private int index = 0;
private File currentLog;
private PrintWriter logWriter;
private String logFileName;
private long logCreated;

public NRLabsHandler(String filename, int rolltime, long size, int max) throws IOException {
logFileName = filename;
if(max < 1) max = 1;
maxFiles = max;
if(size <= 0) {
maxSize = 1024L*1024L;
} else {
maxSize = size;
}
if(rolltime <= 0) {
rolloverTime = 0;
} else {
rolloverTime = rolltime * 60000L;
}
currentLog = new File(logFileName);
FileWriter fWriter = new FileWriter(currentLog, true);
logWriter = new PrintWriter(fWriter, true);
logCreated = System.currentTimeMillis();
NewRelic.getAgent().getLogger().log(Level.FINE, "Constructed NRLabsHandler: {0}", toString());
}

@Override
public void publish(LogRecord record) {
if(!isLoggable(record)) {
NewRelic.getAgent().getLogger().log(Level.FINE, "The LogRecord {0} is not loggable", record);
return;
}

if(logWriter == null) {
NewRelic.getAgent().getLogger().log(Level.FINE, "Cannot write record because logWriter is null: {0}", record);
return;
}

String msg = record.getMessage();
logWriter.println(msg);
logWriter.flush();
}

@Override
public void flush() {
logWriter.flush();
}

@Override
public void close() throws SecurityException {
logWriter.close();
}

private void rollLog() {
logWriter.close();

int fileEnding = index + 1;
if(fileEnding >= maxFiles) {
fileEnding = 1;
}
if (maxFiles > 1) {
File oldLog = new File(logFileName + "." + fileEnding);
if (oldLog.exists()) {
oldLog.delete();
}
currentLog.renameTo(oldLog);
currentLog = new File(logFileName);
try {
logWriter = new PrintWriter(currentLog);
} catch (FileNotFoundException e) {
NewRelic.getAgent().getLogger().log(Level.FINE, e, "Failed to open log file");
}
index++;
if(index == maxFiles) index = 0;
} else {
currentLog.delete();
currentLog = new File(logFileName);
try {
logWriter = new PrintWriter(currentLog);
} catch (FileNotFoundException e) {
NewRelic.getAgent().getLogger().log(Level.FINE, e, "Failed to open log file");
}
}
logCreated = System.currentTimeMillis();
}

@Override
public String toString() {
return "NRLabsHandler [maxFiles=" + maxFiles + ", maxSize=" + maxSize + ", rolloverTime=" + rolloverTime
+ ", index=" + index + ", currentLog=" + currentLog + ", logWriter=" + logWriter + ", logFileName="
+ logFileName + ", logCreated=" + logCreated + "]";
}

public void checkForRoll() {
boolean rolled = false;
long logSize = currentLog.length();
if(logSize > maxSize) {
NewRelic.getAgent().getLogger().log(Level.FINE, "Rolling log {0} because current size {1} exceeds max file {2}", logFileName, logSize, maxSize);
rollLog();
rolled = true;
}

long timeExisted = System.currentTimeMillis() - logCreated;

if(!rolled && rolloverTime != 0 && (timeExisted > rolloverTime)) {
NewRelic.getAgent().getLogger().log(Level.FINE, "Rolling log {0} because file existence {1} exceeds max time {2}", logFileName, timeExisted, rolloverTime);
rollLog();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.newrelic.instrumentation.labs.sap.mpl.processing;

import java.util.TimerTask;

public class NRLabsTimerTask extends TimerTask {

@Override
public void run() {
GatewayLogger.checkConfig();
}

}
Loading

0 comments on commit ca83307

Please sign in to comment.