diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/collectors/OpenEventCollector.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/collectors/OpenEventCollector.java index ccc228e628..b49386fc4d 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/collectors/OpenEventCollector.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/collectors/OpenEventCollector.java @@ -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()); } diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/collectors/RuntimeStatisticCollector.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/collectors/RuntimeStatisticCollector.java index 5347eb355e..d59a5c80d9 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/collectors/RuntimeStatisticCollector.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/collectors/RuntimeStatisticCollector.java @@ -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 */ @@ -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); @@ -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()) { @@ -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) * diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/data/raw/StatisticDataUnit.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/data/raw/StatisticDataUnit.java index 5577ba6f73..1c2163d70e 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/data/raw/StatisticDataUnit.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/data/raw/StatisticDataUnit.java @@ -101,6 +101,11 @@ public class StatisticDataUnit extends BasicStatisticDataUnit { */ private Map contextPropertyMap; + /** + * Message context variable map. + */ + private Map contextVariableMap; + /** * Transport property map. */ @@ -191,6 +196,14 @@ public void setContextPropertyMap(Map contextPropertyMap) { this.contextPropertyMap = contextPropertyMap; } + public void setContextVariableMap(Map contextVariableMap) { + this.contextVariableMap = contextVariableMap; + } + + public Map getContextVariableMap() { + return contextVariableMap; + } + public Map getTransportPropertyMap() { return transportPropertyMap; } diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/data/raw/StatisticsLog.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/data/raw/StatisticsLog.java index a87506fee1..ca45844b52 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/data/raw/StatisticsLog.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/data/raw/StatisticsLog.java @@ -125,6 +125,11 @@ public class StatisticsLog { */ private Map contextPropertyMap; + /** + * Synapse Message context variables for the component. + */ + private Map contextVariableMap; + /** * Transport properties for the component. */ @@ -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(); @@ -279,6 +285,10 @@ public Map getContextPropertyMap() { return contextPropertyMap; } + public Map getContextVariableMap() { + return contextVariableMap; + } + public void setAfterPayload(String afterPayload) { this.afterPayload = afterPayload; } diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/publishing/PublishingEvent.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/publishing/PublishingEvent.java index 5a0d6f05e1..ab679464c5 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/publishing/PublishingEvent.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/publishing/PublishingEvent.java @@ -46,6 +46,7 @@ public class PublishingEvent { private Map contextPropertyMap; private Map transportPropertyMap; + private Map contextVariableMap; private Integer[] children; @@ -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()]; @@ -290,6 +292,12 @@ public ArrayList 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); diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/OpenTelemetryManagerHolder.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/OpenTelemetryManagerHolder.java index 9713874986..ad3db747aa 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/OpenTelemetryManagerHolder.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/OpenTelemetryManagerHolder.java @@ -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; /** @@ -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() { @@ -74,6 +77,10 @@ public static boolean isCollectingProperties() { return isCollectingProperties; } + public static boolean isCollectingVariables() { + return isCollectingVariables; + } + public static OpenTelemetryManager getOpenTelemetryManager() { return openTelemetryManager; } diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/TelemetryConstants.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/TelemetryConstants.java index 93eea14cdf..99b5d46469 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/TelemetryConstants.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/TelemetryConstants.java @@ -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"; diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/helpers/SpanTagger.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/helpers/SpanTagger.java index 1e1fe707fa..64f60de801 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/helpers/SpanTagger.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/tracing/opentelemetry/management/helpers/SpanTagger.java @@ -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, @@ -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()); diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/util/StatisticDataCollectionHelper.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/util/StatisticDataCollectionHelper.java index b73b66d3a1..cd3597c8a1 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/util/StatisticDataCollectionHelper.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/util/StatisticDataCollectionHelper.java @@ -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)); + } } } diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/util/StatisticsConstants.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/util/StatisticsConstants.java index 8ec8adab35..c32f0fca45 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/util/StatisticsConstants.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/util/StatisticsConstants.java @@ -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 */ diff --git a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/util/TracingDataCollectionHelper.java b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/util/TracingDataCollectionHelper.java index b16896c618..fec9b1d08b 100644 --- a/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/util/TracingDataCollectionHelper.java +++ b/modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/util/TracingDataCollectionHelper.java @@ -89,6 +89,24 @@ public static Map 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 extractContextVariables(MessageContext synCtx) { + + Set variableSet = synCtx.getVariableKeySet(); + Map 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. *