Skip to content

Commit

Permalink
updates to how gateway messages were logged
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilpipre committed Jun 11, 2024
1 parent 0d19455 commit eb62779
Show file tree
Hide file tree
Showing 20 changed files with 883 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.newrelic.instrumentation.labs.sap.mpl.processing;

import java.util.Date;
import java.util.Map;

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

public class GatewayUtils {

Expand All @@ -13,10 +15,37 @@ public static void addAttribute(Map<String, Object> attributes, String key, Obje
}

public static void reportMPL(MessageProcessingLogPart logPart) {
String logString = logPart.toLogString();
if(logString != null && !logString.isEmpty()) {
GatewayLogger.log(logString);
Date startDate = logPart.get(TypedMessageProcessingLogKeys.TK_START);
Date endDate = logPart.get(TypedMessageProcessingLogKeys.TK_STOP);
long duration = -1;
if(endDate != null && startDate != null) {
duration = endDate.getTime() - startDate.getTime();
}
StringBuffer sb = new StringBuffer();
addKeyValuePair(sb, "Start", startDate);
addKeyValuePair(sb, "End", endDate);
addKeyValuePair(sb, "Sender", logPart.get(TypedMessageProcessingLogKeys.TK_SENDER_ID));
addKeyValuePair(sb, "IntegrationFlow", logPart.get(TypedMessageProcessingLogKeys.TK_CONTEXT_NAME));
addKeyValuePair(sb, "Status", logPart.get(TypedMessageProcessingLogKeys.TK_OVERALL_STATUS));
if(duration > 0) {
addKeyValuePair(sb, "Duration", duration);
}
addKeyValuePair(sb, "MessageGuid", logPart.get(TypedMessageProcessingLogKeys.TK_MESSAGE_GUID));
addKeyValuePair(sb, "CorrelationId", logPart.get(TypedMessageProcessingLogKeys.TK_CORRELATION_ID));
addKeyValuePair(sb, "TransactionId", logPart.get(TypedMessageProcessingLogKeys.TK_TRANSACTION_ID));

String result = sb.toString();
if(!result.isEmpty()) {
GatewayLogger.log(result);
}
}

private static void addKeyValuePair(StringBuffer sb, String key, Object value) {
if(key != null && !key.isEmpty() && value != null) {
sb.append(key);
sb.append(": ");
sb.append(value);
sb.append(", ");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
import com.newrelic.instrumentation.labs.sap.mpl.MessageProcessingUtils;
import com.newrelic.instrumentation.labs.sap.mpl.processing.GatewayUtils;
import com.sap.it.op.mpl.MessageProcessingLogPart;

@Weave(type = MatchType.BaseClass)
public abstract class AbstractMessageProcessingLogSink {

protected void putMpl(MessageProcessingLogPart logPart) {
MessageProcessingUtils.reportMPL(logPart);
GatewayUtils.reportMPL(logPart);
Weaver.callOriginal();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ public class TraceMessageLogger implements HarvestListener {
protected static final String TRACEMESSAGELOGGINGMAXFILES = "SAP.gatewaylog.log_file_count";
protected static final String TRACEMESSAGELOGENABLED = "SAP.tracemessagelog.enabled";
private static final String LOGEVENTS = "SAP/TracelogLogger/Events";

private static TraceLogConfig currentTracelogConfig = null;

private static TraceMessageLogger INSTANCE = null;

private static NRLabsHandler handler;

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

private TraceMessageLogger() {

}

protected static void checkConfig() {

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

TraceLogConfig newConfig = getConfig(agentConfig);
if(currentTracelogConfig == null || !newConfig.equals(currentTracelogConfig)) {
NewRelic.getAgent().getLogger().log(Level.FINE, "In TracelogLogger.checkConfig, config changed, reinitializing");
Expand All @@ -58,20 +58,20 @@ protected static void checkConfig() {
init();
}
}

protected TraceLogConfig getConfig() {
if(currentTracelogConfig != null) return currentTracelogConfig;
Config agentConfig = NewRelic.getAgent().getConfig();
return getConfig(agentConfig);
}

public static TraceLogConfig getConfig(Config agentConfig) {
TraceLogConfig tracemessageConfig = new TraceLogConfig();
Integer rolloverMinutes = agentConfig.getValue(TRACEMESSAGELOGGINGROLLOVERINTERVAL);
if(rolloverMinutes != null) {
tracemessageConfig.setRolloverMinutes(rolloverMinutes);
}

Integer maxFile = agentConfig.getValue(TRACEMESSAGELOGGINGMAXFILES);
if(maxFile != null) {
tracemessageConfig.setMaxLogFiles(maxFile);
Expand All @@ -95,16 +95,16 @@ public static TraceLogConfig getConfig(Config agentConfig) {

boolean enabled = agentConfig.getValue(TRACEMESSAGELOGENABLED, Boolean.TRUE);
tracemessageConfig.setTracelog_enabled(enabled);


return tracemessageConfig;
}

@SuppressWarnings("unchecked")
public static void log(TraceMessage traceMsg, String modelStepId) {
if(traceMsg != null) {
JSONObject json = new JSONObject();

String encoding = traceMsg.getEncoding();
Map<String, String> exchangeProps = traceMsg.getExchangeProperties();
Map<String,String> headers = traceMsg.getHeaders();
Expand All @@ -124,30 +124,30 @@ public static void log(TraceMessage traceMsg, String modelStepId) {
if(traceMsgType != null) {
addStringPair(json, "TraceMessageType" , traceMsgType.name());
}

JSONObject headerJson = addMapValues(headers);
if(headerJson != null) {
json.put("Headers", headerJson);
}

JSONObject exchangeProperties = addMapValues(exchangeProps);
if(exchangeProperties != null) {
json.put("Exchange-Properties", exchangeProperties);
}
log(json.toJSONString());
}

}

protected static void log(JSONObject json) {
log(json.toJSONString());
}

@SuppressWarnings("unchecked")
public static void log(TraceMessage traceMsg) {
if(traceMsg != null) {
JSONObject json = new JSONObject();

String encoding = traceMsg.getEncoding();
Map<String, String> exchangeProps = traceMsg.getExchangeProperties();
Map<String,String> headers = traceMsg.getHeaders();
Expand All @@ -162,27 +162,27 @@ public static void log(TraceMessage traceMsg) {
if(traceMsgType != null) {
addStringPair(json, "TraceMessageType" , traceMsgType.name());
}

JSONObject headerJson = addMapValues(headers);
if(headerJson != null) {
json.put("Headers", headerJson);
}

JSONObject exchangeProperties = addMapValues(exchangeProps);
if(exchangeProperties != null) {
json.put("Exchange-Properties", exchangeProperties);
}
log(json.toJSONString());
}
}

@SuppressWarnings("unchecked")
protected static void addStringPair(JSONObject json, String key, String value) {
if(json != null && key != null && !key.isEmpty() && value != null) {
json.put(key, value);
}
}

@SuppressWarnings("unchecked")
protected static JSONObject addMapValues(Map<String, String> attributes) {
if(attributes != null) {
Expand All @@ -192,62 +192,54 @@ protected static JSONObject addMapValues(Map<String, String> attributes) {
}
return null;
}

public static void log(String message) {
NewRelic.getAgent().getLogger().log(Level.FINE, "Call to TraceMessageLogger.log({0})", message);
if(!enabled) return;

if(!initialized) {
init();
}
Level level = TRACEMESSAGE_LOGGER.getLevel();
if(level == null || !level.equals(Level.INFO)) {
TRACEMESSAGE_LOGGER.setLevel(Level.INFO);
}

NewRelic.getAgent().getLogger().log(Level.FINE, "Sending message to trace log: {0}", message);
NewRelic.getAgent().getLogger().log(Level.FINE,"TRACEMESSAGE_LOGGER logging level is {0}", TRACEMESSAGE_LOGGER.getLevel());

Handler[] handlers = TRACEMESSAGE_LOGGER.getHandlers();
NewRelic.getAgent().getLogger().log(Level.FINE,"TRACEMESSAGE_LOGGER has {0} handlers", handlers != null ? handlers.length : "null");
if(handlers.length == 0) {
if(handler != null) {
TRACEMESSAGE_LOGGER.addHandler(handler);
handlers = TRACEMESSAGE_LOGGER.getHandlers();
} else {
NewRelic.getAgent().getLogger().log(Level.FINE, "Couldn't add NRHandler for TraceMessageLogger because handler is not initialized");
}
}
for(Handler h : handlers) {
NewRelic.getAgent().getLogger().log(Level.FINE, "TraceMessageLogger has handler: {0}",h);

Handler[] handlers = TRACEMESSAGE_LOGGER.getHandlers();
if(handlers.length == 0) {
if(handler != null) {
TRACEMESSAGE_LOGGER.addHandler(handler);
handlers = TRACEMESSAGE_LOGGER.getHandlers();
} else {
NewRelic.getAgent().getLogger().log(Level.FINE, "Couldn't add NRHandler for TraceMessageLogger because handler is not initialized");
}
}
TRACEMESSAGE_LOGGER.log(Level.INFO, message);
LoggingCount++;
}

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

enabled = currentTracelogConfig.isTracelog_enabled();

if(INSTANCE == null) {
INSTANCE = new TraceMessageLogger();
}

if(timerTask == null) {
timerTask = new NRLabsTimerTask();
Timer timer = new Timer("TracelogConfig");
timer.scheduleAtFixedRate(timerTask, 2L * 60000L, 2L * 60000);
}

int rolloverMinutes = currentTracelogConfig.getRolloverMinutes();
if(rolloverMinutes < 1) {
rolloverMinutes = 60;
}

String rolloverSize = currentTracelogConfig.getRolloverSize();
StringBuffer sb = new StringBuffer();
int length = rolloverSize.length();
Expand All @@ -257,7 +249,7 @@ public static void init() {
sb.append(c);
}
}

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

// disallow less than 10K
if(size < 10 * 1024L) {
size = 10 * 1024L;
Expand All @@ -280,15 +272,15 @@ public static void init() {
String tracemessageFileName = currentTracelogConfig.getTraceMessageLog();
File file = new File(tracemessageFileName);
File parent = file.getParentFile();

if(!parent.exists()) {
boolean result = parent.mkdirs();
if(result) {
NewRelic.getAgent().getLogger().log(Level.FINE, "Created directories needed for tracemessage log files: {0})", tracemessageFileName);
} else {
NewRelic.getAgent().getLogger().log(Level.FINE, "Failed to created directories needed for tracemessage log files: {0})", tracemessageFileName); }
}

int maxFiles = currentTracelogConfig.getMaxLogFiles();
if(handler != null) {
handler.flush();
Expand All @@ -298,31 +290,31 @@ public static void init() {
}
handler = null;
}

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

if(TRACEMESSAGE_LOGGER == null) {
TRACEMESSAGE_LOGGER = Logger.getLogger("TraceMessageLog");
}
TRACEMESSAGE_LOGGER.setLevel(Level.INFO);
TRACEMESSAGE_LOGGER.setUseParentHandlers(false);

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

if(TRACEMESSAGE_LOGGER != null) {
Handler[] handlers = TRACEMESSAGE_LOGGER.getHandlers();
for(Handler h : handlers) {
NewRelic.getAgent().getLogger().log(Level.FINE, "TraceMessageLogger has handler: {0}",h);
}
}

Map<String, Object> event = new HashMap<>();
event.put("Initialized", new Date());
event.put("GateLogFile",tracemessageFileName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.newrelic.instrumentation.labs.sap.tracemessage.processing;

public enum TraceMessageSource {

ArchiveMessageWriteService,TraceWriteService
}
Loading

0 comments on commit eb62779

Please sign in to comment.