Skip to content

Commit

Permalink
added collection of principal data
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilpipre committed Dec 9, 2024
1 parent c1a518a commit 721d872
Show file tree
Hide file tree
Showing 14 changed files with 912 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class AdapterLoggingConfig {

private String contextadapterLog = null;
private String supplementaadapterLog = null;
private String principaladapterLog = null;

public AdapterLoggingConfig() {
File newRelicDir = ConfigFileHelper.getNewRelicDirectory();
Expand All @@ -32,14 +33,23 @@ public AdapterLoggingConfig() {

File logFile2 = new File(logDirectory,EJBAdapterLogger.DATA_LOGFILENAME);
supplementaadapterLog = logFile2.getAbsolutePath();

File logFile3 = new File(logDirectory, EJBAdapterLogger.DATA_PRINCIPAL_LOGFILENAME);
principaladapterLog = logFile3.getAbsolutePath();
} else {
File logFile = new File(newRelicDir, EJBAdapterLogger.CONTEXT_LOGFILENAME);
contextadapterLog = logFile.getAbsolutePath();
File logFile2 = new File(newRelicDir,EJBAdapterLogger.DATA_LOGFILENAME);
supplementaadapterLog = logFile2.getAbsolutePath();
File logFile3 = new File(newRelicDir, EJBAdapterLogger.DATA_PRINCIPAL_LOGFILENAME);
principaladapterLog = logFile3.getAbsolutePath();
}
}

public String getPrincipaladapterLog() {
return principaladapterLog;
}

public String getContextLog() {
return contextadapterLog;
}
Expand All @@ -48,3 +58,4 @@ public String getSupplementalLog() {
return supplementaadapterLog;
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,70 +1,136 @@
package com.newrelic.instrumentation.labs.sap.adapters.ejb;

import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.newrelic.api.agent.NewRelic;

public class EJBAdapterLogger {

public static boolean initialized = false;
private static Logger LOGGER;
private static Logger LOGGER2;
private static PrintWriter LOGGER;
private static PrintWriter LOGGER2;
private static PrintWriter LOGGER3;
private static AdapterLoggingConfig config;
private static NRLabsHandler ctx_handler;
private static NRLabsHandler supp_handler;
private static final Set<String> principalAttributesList;
private static final Set<String> loggedAttributes = new HashSet<String>();
private static final Set<String> loggedSupplemental = new HashSet<String>();
private static final Set<String> loggedPrincipal = new HashSet<String>();

protected static final String CONTEXT_LOGFILENAME = "context-data-attributes-ejb.log";
protected static final String DATA_LOGFILENAME = "moduledata-supplemental-attributes-ejb.log";
protected static final String DATA_PRINCIPAL_LOGFILENAME = "moduledata-principal-attributes-ejb.log";

static {
principalAttributesList = new HashSet<String>();
principalAttributesList.add("Message-Action");
principalAttributesList.add("Message-CorrelationId");
principalAttributesList.add("Message-FromParty");
principalAttributesList.add("Message-FromService");
principalAttributesList.add("Message-Id");
principalAttributesList.add("Message-Protocol");
principalAttributesList.add("Message-SequenceId");
principalAttributesList.add("Message-ToParty");
principalAttributesList.add("Message-ToService");
principalAttributesList.add("Message-AttachmentCount");
principalAttributesList.add("Message-AttachmentNames");
principalAttributesList.add("Message-ErrorInfo");
principalAttributesList.add("Message-Description");
principalAttributesList.add("Message-RefToMessageId");
principalAttributesList.add("Message-TimeReceived");
principalAttributesList.add("Message-TimeSent");

public static void initialize() {
config = new AdapterLoggingConfig();
String logFileName = config.getContextLog();

try {
ctx_handler = new NRLabsHandler(logFileName);
} catch (IOException e) {
NewRelic.getAgent().getLogger().log(Level.FINE, e, "Failed to create Attribute log file at {0}", logFileName);
}

if(LOGGER == null) {
LOGGER = Logger.getLogger("AdaptersEJBLog");
}

if(LOGGER != null && ctx_handler != null) {
LOGGER.addHandler(ctx_handler);
}

logFileName = config.getSupplementalLog();
principalAttributesList.add("TransportableMessage-MessagePriority");
principalAttributesList.add("TransportableMessage-Retries");
principalAttributesList.add("TransportableMessage-SequenceNumber");
principalAttributesList.add("TransportableMessage-PersistUntil");
principalAttributesList.add("TransportableMessage-ValidUntil");
principalAttributesList.add("TransportableMessage-VersionNumber");
principalAttributesList.add("TransportableMessage-ParentId");

principalAttributesList.add("MessageKey-Direction");
principalAttributesList.add("MessageKey-ID");

try {
supp_handler = new NRLabsHandler(logFileName);
} catch (IOException e) {
NewRelic.getAgent().getLogger().log(Level.FINE, e, "Failed to create supplemental attribute log file at {0}", logFileName);
}
principalAttributesList.add("Endpoint-Address");
principalAttributesList.add("Endpoint-Transport");

principalAttributesList.add("Payload-Name");
principalAttributesList.add("Payload-Description");
principalAttributesList.add("Payload-ContentType");
principalAttributesList.add("Payload-AttributeNames");

}

public static void initialize() {

if(LOGGER2 == null) {
LOGGER2 = Logger.getLogger("AdaptersSupplementalLog");
if(initialized) return;
config = new AdapterLoggingConfig();
String logFileName = config.getContextLog();
try {
FileOutputStream fos = new FileOutputStream(logFileName, false);
LOGGER = new PrintWriter(fos);
} catch (FileNotFoundException e) {
NewRelic.getAgent().getLogger().log(Level.FINE, e, "Failed to find file {0} for logging", logFileName);
}

logFileName = config.getSupplementalLog();
try {
FileOutputStream fos2 = new FileOutputStream(logFileName, false);
LOGGER2 = new PrintWriter(fos2);
} catch (FileNotFoundException e) {
NewRelic.getAgent().getLogger().log(Level.FINE, e, "Failed to find file {0} for logging", logFileName);
}

logFileName = config.getPrincipaladapterLog();
try {
FileOutputStream fos3 = new FileOutputStream(logFileName, false);
LOGGER3 = new PrintWriter(fos3);
} catch (FileNotFoundException e) {
NewRelic.getAgent().getLogger().log(Level.FINE, e, "Failed to find file {0} for logging", logFileName);
}
initialized = true;
}

public static void logNewAttribute(String attribute) {
if(!initialized) {
initialize();
}

if(LOGGER2 != null && supp_handler != null) {
LOGGER2.addHandler(supp_handler);
if(!loggedAttributes.contains(attribute)) {
loggedAttributes.add(attribute);
NewRelic.getAgent().getLogger().log(Level.FINE, "Adding attribute {0} to loggedAttributes, size is now {1}", attribute, loggedAttributes.size());
LOGGER.println(attribute);
LOGGER.flush();
}
initialized = true;
}

public static void logNewAttribute(String attribute) {
public static void logNewSupplementalAttribute(String attribute) {
if(!initialized) {
initialize();
}
LOGGER.info(attribute);
if(!loggedSupplemental.contains(attribute)) {
loggedSupplemental.add(attribute);

NewRelic.getAgent().getLogger().log(Level.FINE, "Adding attribute {0} to loggedSupplemental, size is now {1}", attribute, loggedSupplemental.size());
LOGGER2.println(attribute);
LOGGER2.flush();
}
}

public static void logNewSupplementalAttribute(String attribute) {
public static void logNewPrincipalAttribute(String type, String attribute) {
if(!initialized) {
initialize();
}
LOGGER2.info(attribute);
String attrName = type+": " +attribute;

if(!loggedPrincipal.contains(attrName)) {
loggedPrincipal.add(attrName);
NewRelic.getAgent().getLogger().log(Level.FINE, "Adding attrName {0} to loggedPrincipal, size is now {1}", attrName, loggedPrincipal.size());
LOGGER3.println(type+": " +attribute);
LOGGER3.flush();
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package com.newrelic.instrumentation.labs.sap.adapters.ejb.attrributeservice;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;

import com.newrelic.api.agent.NewRelic;
import com.sap.engine.interfaces.messaging.api.MessagePropertyKey;

public class AttributeConfiguration {

Expand All @@ -11,21 +20,59 @@ public class AttributeConfiguration {
private static Set<String> supplementalAttributes = new HashSet<String>();
private static boolean principalEnabled = false;
private static Set<String> principalAttributes = new HashSet<String>();
private static boolean principalDefaultsEnabled = true;
private static Map<String, List<String>> payloadAttributesToCapture = new HashMap<String, List<String>>();
private static Set<MessagePropertyKey> messagePropsToCapture = new HashSet<MessagePropertyKey>();


public static boolean isPrincipalDefaultsEnabled() {
return principalDefaultsEnabled;
}

public static void setPrincipalDefaultsEnabled(boolean b) {
principalDefaultsEnabled = b;
}

public static boolean isCollectContextChannelId() {
return collectContextChannelId;
}


/*
* called when reseting the attributes to collect
*/
public static void clearAttributes() {
contextAttributes.clear();
supplementalAttributes.clear();
principalAttributes.clear();
messagePropsToCapture.clear();
payloadAttributesToCapture.clear();
}

public static void addMessagePropertyKey(String ns, String name) {
MessagePropertyKey key = new MessagePropertyKey(name, ns);
NewRelic.getAgent().getLogger().log(Level.FINE, "Will capture MessageKey {0}", key);

messagePropsToCapture.add(key);
}

public static Set<MessagePropertyKey> getMessagePropertiesToCapture() {
return messagePropsToCapture;
}

public static void addAttributeKey(String name, String key) {
List<String> current = payloadAttributesToCapture.get(name);
if(current == null) {
current = new ArrayList<String>();
}
NewRelic.getAgent().getLogger().log(Level.FINE, "Current list of attributes for payload {0} is {1}", name, current);
current.add(key);
payloadAttributesToCapture.put(name, current);
NewRelic.getAgent().getLogger().log(Level.FINE, "Will capture attribute {0} from payloads with the name {1}", key,name);
}

public static void addContextAttribute(String attribute) {

contextAttributes.add(attribute);
}

Expand All @@ -51,6 +98,9 @@ public static void setPrincipalEnabled(boolean principalEnabled) {
AttributeConfiguration.principalEnabled = principalEnabled;
}

public static List<String> getAttachmentAttributes(String name) {
return payloadAttributesToCapture.get(name);
}

public static Set<String> getContextAttributes() {
return contextAttributes;
Expand All @@ -67,7 +117,5 @@ public static boolean isPrincipalEnabled() {
public static Set<String> getPrincipalAttributes() {
return principalAttributes;
}



}

}
Loading

0 comments on commit 721d872

Please sign in to comment.