Skip to content

Commit

Permalink
Add option to export variables in tracing data
Browse files Browse the repository at this point in the history
  • Loading branch information
SanojPunchihewa committed Dec 19, 2024
1 parent 44331a7 commit 524c404
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Integer reportEntryEvent(MessageContext messageContext, String com
}

boolean isCollectingTracing = false;
if (isCollectingProperties() || isCollectingPayloads()) {
if (isCollectingProperties() || isCollectingPayloads() || isCollectingVariables()) {
isCollectingTracing = (aspectConfiguration != null && aspectConfiguration.isTracingEnabled());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public abstract class RuntimeStatisticCollector {
*/
private static boolean isCollectingProperties;

/**
* Is message context variable collection enabled in synapse.properties file.
*/
private static boolean isCollectingVariables;

/**
* Is collecting statistics of all artifacts
*/
Expand Down Expand Up @@ -102,6 +107,13 @@ public static void init() {
log.debug("Property collecting is not enabled in \'synapse.properties\' file.");
}

isCollectingVariables =
SynapsePropertiesLoader.getBooleanProperty(StatisticsConstants.COLLECT_MESSAGE_VARIABLES, false);

if (!isCollectingVariables && log.isDebugEnabled()) {
log.debug("Variable collecting is not enabled in \'synapse.properties\' file.");
}

isCollectingAllStatistics =
SynapsePropertiesLoader.getBooleanProperty(StatisticsConstants.COLLECT_ALL_STATISTICS, false);

Expand All @@ -116,7 +128,7 @@ public static void init() {
log.debug("OpenTelemetry based tracing is enabled");
}
OpenTelemetryManagerHolder.loadTracerConfigurations();
OpenTelemetryManagerHolder.setCollectingFlags(isCollectingPayloads, isCollectingProperties);
OpenTelemetryManagerHolder.setCollectingFlags(isCollectingPayloads, isCollectingProperties, isCollectingVariables);
}
} else {
if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -202,6 +214,16 @@ public static boolean isCollectingProperties() {
return isStatisticsEnabled && isCollectingProperties;
}

/**
* Return whether collecting message variables is enabled.
*
* @return true if need to collect message-variables.
*/
public static boolean isCollectingVariables() {

return isStatisticsEnabled && isCollectingVariables;
}

/**
* Return whether collecting statistics for all artifacts is enabled (this also needs isStatisticsEnabled)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public class StatisticDataUnit extends BasicStatisticDataUnit {
*/
private Map<String, Object> contextPropertyMap;

/**
* Message context variable map.
*/
private Map<String, Object> contextVariableMap;

/**
* Transport property map.
*/
Expand Down Expand Up @@ -191,6 +196,14 @@ public void setContextPropertyMap(Map<String, Object> contextPropertyMap) {
this.contextPropertyMap = contextPropertyMap;
}

public void setContextVariableMap(Map<String, Object> contextVariableMap) {
this.contextVariableMap = contextVariableMap;
}

public Map<String, Object> getContextVariableMap() {
return contextVariableMap;
}

public Map<String, Object> getTransportPropertyMap() {
return transportPropertyMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public class StatisticsLog {
*/
private Map<String, Object> contextPropertyMap;

/**
* Synapse Message context variables for the component.
*/
private Map<String, Object> contextVariableMap;

/**
* Transport properties for the component.
*/
Expand Down Expand Up @@ -181,6 +186,7 @@ public StatisticsLog(StatisticDataUnit statisticDataUnit) {
this.messageFlowId = statisticDataUnit.getStatisticId();
this.beforePayload = statisticDataUnit.getPayload();
this.contextPropertyMap = statisticDataUnit.getContextPropertyMap();
this.contextVariableMap = statisticDataUnit.getContextVariableMap();
this.transportPropertyMap = statisticDataUnit.getTransportPropertyMap();
this.componentType = statisticDataUnit.getComponentType();
this.hashCode = statisticDataUnit.getHashCode();
Expand Down Expand Up @@ -279,6 +285,10 @@ public Map<String, Object> getContextPropertyMap() {
return contextPropertyMap;
}

public Map<String, Object> getContextVariableMap() {
return contextVariableMap;
}

public void setAfterPayload(String afterPayload) {
this.afterPayload = afterPayload;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class PublishingEvent {

private Map contextPropertyMap;
private Map transportPropertyMap;
private Map contextVariableMap;

private Integer[] children;

Expand All @@ -72,6 +73,7 @@ public PublishingEvent(String flowId, int componentIndex, StatisticsLog statisti

this.contextPropertyMap = extractProperties(statisticsLog.getContextPropertyMap());
this.transportPropertyMap = extractProperties(statisticsLog.getTransportPropertyMap());
this.contextVariableMap = extractProperties(statisticsLog.getContextVariableMap());

if (statisticsLog.getChildren().size() > 0) {
this.children = new Integer[statisticsLog.getChildren().size()];
Expand Down Expand Up @@ -290,6 +292,12 @@ public ArrayList<Object> getObjectAsList() {
objectList.add(this.transportPropertyMap.toString());
}

if (this.contextVariableMap == null) {
objectList.add(null);
} else {
objectList.add(this.contextVariableMap.toString());
}

objectList.add(Arrays.toString(this.children));

objectList.add(this.entryPoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class OpenTelemetryManagerHolder {
private static Log logger = LogFactory.getLog(OpenTelemetryManagerHolder.class);
private static boolean isCollectingPayloads;
private static boolean isCollectingProperties;
private static boolean isCollectingVariables;
private static OpenTelemetryManager openTelemetryManager;

/**
Expand Down Expand Up @@ -60,10 +61,12 @@ public static void loadTracerConfigurations() {
*
* @param collectPayloads Whether to collect payloads
* @param collectProperties Whether to collect properties
* @param collectVariables Whether to collect variables
*/
public static void setCollectingFlags(boolean collectPayloads, boolean collectProperties) {
public static void setCollectingFlags(boolean collectPayloads, boolean collectProperties, boolean collectVariables) {
isCollectingPayloads = collectPayloads;
isCollectingProperties = collectProperties;
isCollectingVariables = collectVariables;
}

public static boolean isCollectingPayloads() {
Expand All @@ -74,6 +77,10 @@ public static boolean isCollectingProperties() {
return isCollectingProperties;
}

public static boolean isCollectingVariables() {
return isCollectingVariables;
}

public static OpenTelemetryManager getOpenTelemetryManager() {
return openTelemetryManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class TelemetryConstants {
public static final String AFTER_PAYLOAD_ATTRIBUTE_KEY = "afterPayload";
public static final String BEFORE_CONTEXT_PROPERTY_MAP_ATTRIBUTE_KEY = "beforeContextPropertyMap";
public static final String AFTER_CONTEXT_PROPERTY_MAP_ATTRIBUTE_KEY = "afterContextPropertyMap";
public static final String BEFORE_CONTEXT_VARIABLE_MAP_ATTRIBUTE_KEY = "beforeContextVariableMap";
public static final String AFTER_CONTEXT_VARIABLE_MAP_ATTRIBUTE_KEY = "afterContextVariableMap";
public static final String PROPERTY_MEDIATOR_VALUE_ATTRIBUTE_KEY = "propertyMediatorValue";
public static final String COMPONENT_NAME_ATTRIBUTE_KEY = "componentName";
public static final String COMPONENT_TYPE_ATTRIBUTE_KEY = "componentType";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ private SpanTagger() {}
public static void setSpanTags(SpanWrapper spanWrapper, MessageContext synCtx) {
StatisticsLog openStatisticsLog = new StatisticsLog(spanWrapper.getStatisticDataUnit());
Span span = spanWrapper.getSpan();
if (OpenTelemetryManagerHolder.isCollectingPayloads() || OpenTelemetryManagerHolder.isCollectingProperties()) {
if (OpenTelemetryManagerHolder.isCollectingPayloads() || OpenTelemetryManagerHolder.isCollectingProperties()
|| OpenTelemetryManagerHolder.isCollectingVariables()) {
if (OpenTelemetryManagerHolder.isCollectingPayloads()) {
if(openStatisticsLog.getBeforePayload() != null) {
span.setAttribute(TelemetryConstants.BEFORE_PAYLOAD_ATTRIBUTE_KEY,
Expand Down Expand Up @@ -85,6 +86,22 @@ public static void setSpanTags(SpanWrapper spanWrapper, MessageContext synCtx) {
spanWrapper.getCloseEventStatisticDataUnit().getPropertyValue());
}
}

if (OpenTelemetryManagerHolder.isCollectingVariables()) {
if (spanWrapper.getStatisticDataUnit().getContextVariableMap() != null) {
span.setAttribute(TelemetryConstants.BEFORE_CONTEXT_VARIABLE_MAP_ATTRIBUTE_KEY,
spanWrapper.getStatisticDataUnit().getContextVariableMap().toString());
}
if (spanWrapper.getCloseEventStatisticDataUnit() != null) {
if (spanWrapper.getCloseEventStatisticDataUnit().getContextVariableMap() != null) {
span.setAttribute(TelemetryConstants.AFTER_CONTEXT_VARIABLE_MAP_ATTRIBUTE_KEY,
spanWrapper.getCloseEventStatisticDataUnit().getContextVariableMap().toString());
}
} else if (openStatisticsLog.getContextVariableMap() != null) {
span.setAttribute(TelemetryConstants.AFTER_CONTEXT_VARIABLE_MAP_ATTRIBUTE_KEY,
openStatisticsLog.getContextVariableMap().toString());
}
}
}
if (openStatisticsLog.getComponentName() != null) {
span.setAttribute(TelemetryConstants.COMPONENT_NAME_ATTRIBUTE_KEY, openStatisticsLog.getComponentName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public static void collectData(MessageContext messageContext, boolean isContentA
statisticDataUnit.setTransportPropertyMap(
TracingDataCollectionHelper.extractTransportProperties(messageContext));
}
if (RuntimeStatisticCollector.isCollectingVariables()) {
statisticDataUnit
.setContextVariableMap(TracingDataCollectionHelper.extractContextVariables(messageContext));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public static String getComponentTypeToString(ComponentType componentType) {
*/
public final static String COLLECT_MESSAGE_PROPERTIES = "mediation.flow.statistics.tracer.collect.properties";

/**
* Enable collecting message context variables.
*/
public final static String COLLECT_MESSAGE_VARIABLES = "mediation.flow.statistics.tracer.collect.variables";

/**
* Enable statistics collecting for all artifacts
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ public static Map<String, Object> extractContextProperties(MessageContext synCtx
return propertyMap;
}

/**
* Extract context variables from the synapse message context.
*
* @param synCtx synapse message context
* @return context variable map
*/
public static Map<String, Object> extractContextVariables(MessageContext synCtx) {

Set<String> variableSet = synCtx.getVariableKeySet();
Map<String, Object> variableMap = new TreeMap<>();

for (String variable : variableSet) {
Object variableValue = synCtx.getVariable(variable);
variableMap.put(variable, variableValue);
}
return variableMap;
}

/**
* Extract transport headers from the synapse message context.
*
Expand Down

0 comments on commit 524c404

Please sign in to comment.