Skip to content

Commit 63e9f9c

Browse files
authored
Merge pull request #1617 from malakaganga/jaeger_trace_prop_value
Add property values to the Jaeger tracing
2 parents 5e58533 + f895419 commit 63e9f9c

File tree

10 files changed

+135
-46
lines changed

10 files changed

+135
-46
lines changed

modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/collectors/CloseEventCollector.java

+25-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.apache.synapse.aspects.flow.statistics.log.templates.EndFlowEvent;
2828
import org.apache.synapse.aspects.flow.statistics.log.templates.StatisticsCloseEvent;
2929
import org.apache.synapse.aspects.flow.statistics.opentracing.OpenTracingManagerHolder;
30-
import org.apache.synapse.aspects.flow.statistics.opentracing.management.helpers.TracingUtils;
3130
import org.apache.synapse.aspects.flow.statistics.util.StatisticDataCollectionHelper;
3231
import org.apache.synapse.aspects.flow.statistics.util.StatisticsConstants;
3332

@@ -50,11 +49,32 @@ public class CloseEventCollector extends RuntimeStatisticCollector {
5049
* @param isContentAltering true if content is altered
5150
*/
5251
public static void closeEntryEvent(MessageContext messageContext, String componentName, ComponentType componentType,
53-
Integer currentIndex, boolean isContentAltering) {
52+
Integer currentIndex, boolean isContentAltering) {
53+
closeEntryEvent(messageContext, componentName, componentType, currentIndex, isContentAltering, null);
54+
}
55+
56+
/**
57+
* Enqueue statistics event to the event queue. This method receives statistics events from synapse mediation
58+
* engine for all the component types.
59+
*
60+
* @param messageContext synapse message context.
61+
* @param componentName name of the component reporting statistics.
62+
* @param componentType component type of the reporting component.
63+
* @param currentIndex component's level in this message flow.
64+
* @param isContentAltering true if content is altered
65+
* @param propertyValue value of the property
66+
*/
67+
public static void closeEntryEvent(MessageContext messageContext, String componentName,
68+
ComponentType componentType,
69+
Integer currentIndex, boolean isContentAltering, String propertyValue) {
70+
5471
if (shouldReportStatistic(messageContext)) {
5572
Boolean isCollectingTracing =
5673
(Boolean) messageContext.getProperty(StatisticsConstants.FLOW_TRACE_IS_COLLECTED);
5774
StatisticDataUnit statisticDataUnit = new StatisticDataUnit();
75+
if (propertyValue != null) {
76+
statisticDataUnit.setPropertyValue(propertyValue);
77+
}
5878
statisticDataUnit.setComponentName(componentName);
5979
statisticDataUnit.setComponentType(componentType);
6080
if (currentIndex == null) {
@@ -69,15 +89,15 @@ public static void closeEntryEvent(MessageContext messageContext, String compone
6989
.collectData(messageContext, isContentAltering, isCollectingTracing, statisticDataUnit);
7090

7191
StatisticsCloseEvent closeEvent = new StatisticsCloseEvent(statisticDataUnit);
72-
if(currentIndex == null){
92+
if (currentIndex == null) {
7393
addEvent(messageContext, closeEvent);
74-
}else {
94+
} else {
7595
addEventAndDecrementCount(messageContext, closeEvent);
7696
}
7797

7898
if (isOpenTracingEnabled()) {
7999
OpenTracingManagerHolder.getOpenTracingManager().getHandler().
80-
handleCloseEntryEvent(statisticDataUnit, messageContext);
100+
handleCloseEntryEvent(statisticDataUnit, messageContext);
81101
}
82102

83103
}

modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/data/raw/StatisticDataUnit.java

+10
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public class StatisticDataUnit extends BasicStatisticDataUnit {
8383
*/
8484
private String componentId;
8585

86+
private String propertyValue;
87+
8688
/**
8789
* HashCode of the reporting component.
8890
*/
@@ -222,4 +224,12 @@ public boolean isContinuationCall() {
222224
public void setContinuationCall(boolean continuationCall) {
223225
this.continuationCall = continuationCall;
224226
}
227+
228+
public String getPropertyValue() {
229+
return propertyValue;
230+
}
231+
232+
public void setPropertyValue(String propertyValue) {
233+
this.propertyValue = propertyValue;
234+
}
225235
}

modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/data/raw/StatisticsLog.java

+14
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ public class StatisticsLog {
102102
*/
103103
private String componentId;
104104

105+
/**
106+
* Value of the property which is in trace scope..
107+
*/
108+
private String propertyValue;
109+
105110
/**
106111
* HashCode of the reporting component.
107112
*/
@@ -151,6 +156,7 @@ public StatisticsLog(StatisticDataUnit statisticDataUnit) {
151156
this.transportPropertyMap = statisticDataUnit.getTransportPropertyMap();
152157
this.componentType = statisticDataUnit.getComponentType();
153158
this.hashCode = statisticDataUnit.getHashCode();
159+
this.propertyValue = statisticDataUnit.getPropertyValue();
154160
if (statisticDataUnit.getComponentId() == null) {
155161
this.componentId = StatisticsConstants.HASH_CODE_NULL_COMPONENT;
156162
} else {
@@ -349,4 +355,12 @@ public void setStartTime(long startTime) {
349355
public void setComponentType(ComponentType componentType) {
350356
this.componentType = componentType;
351357
}
358+
359+
public String getPropertyValue() {
360+
return propertyValue;
361+
}
362+
363+
public void setPropertyValue(String propertyValue) {
364+
this.propertyValue = propertyValue;
365+
}
352366
}

modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/opentracing/management/handling/span/JaegerSpanHandler.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.apache.synapse.aspects.ComponentType;
3131
import org.apache.synapse.aspects.flow.statistics.data.raw.BasicStatisticDataUnit;
3232
import org.apache.synapse.aspects.flow.statistics.data.raw.StatisticDataUnit;
33-
import org.apache.synapse.aspects.flow.statistics.opentracing.management.helpers.SpanTagger;
3433
import org.apache.synapse.aspects.flow.statistics.opentracing.management.helpers.TracingUtils;
3534
import org.apache.synapse.aspects.flow.statistics.opentracing.management.parentresolving.ParentResolver;
3635
import org.apache.synapse.aspects.flow.statistics.opentracing.management.scoping.TracingScope;
@@ -155,8 +154,9 @@ private void startSpan(StatisticDataUnit statisticDataUnit, MessageContext synCt
155154
span = tracer.buildSpan(statisticDataUnit.getComponentName()).asChildOf(parentSpan).start();
156155
spanContext = span.context();
157156
}
158-
// Set tracing headers
157+
//Fix null pointer issue occurs when spanContext become null
159158
if (spanContext != null) {
159+
// Set tracing headers
160160
tracer.inject(spanContext, Format.Builtin.HTTP_HEADERS, new TextMapInjectAdapter(tracerSpecificCarrier));
161161
}
162162
// Set text map key value pairs as HTTP headers
@@ -259,7 +259,10 @@ private void finishSpan(BasicStatisticDataUnit basicStatisticDataUnit,
259259
TracingScope tracingScope) {
260260
String spanWrapperId = TracingUtils.extractId(basicStatisticDataUnit);
261261
SpanWrapper spanWrapper = spanStore.getSpanWrapper(spanWrapperId);
262-
262+
//Set the statistic data unit of the close event into the span wrapper
263+
if (spanWrapper != null && (basicStatisticDataUnit instanceof StatisticDataUnit)) {
264+
spanWrapper.setCloseEventStatisticDataUnit((StatisticDataUnit) basicStatisticDataUnit);
265+
}
263266
if (!Objects.equals(spanWrapper, spanStore.getOuterLevelSpanWrapper())) {
264267
// A non-outer level span
265268
spanStore.finishSpan(spanWrapper);

modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/opentracing/management/helpers/SpanTagger.java

+32-27
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
package org.apache.synapse.aspects.flow.statistics.opentracing.management.helpers;
2020

2121
import io.opentracing.Span;
22-
import org.apache.synapse.MessageContext;
23-
import org.apache.synapse.aspects.flow.statistics.data.raw.BasicStatisticDataUnit;
24-
import org.apache.synapse.aspects.flow.statistics.data.raw.StatisticDataUnit;
2522
import org.apache.synapse.aspects.flow.statistics.data.raw.StatisticsLog;
2623
import org.apache.synapse.aspects.flow.statistics.opentracing.OpenTracingManagerHolder;
2724
import org.apache.synapse.aspects.flow.statistics.opentracing.models.SpanWrapper;
@@ -40,37 +37,45 @@ private SpanTagger() {}
4037
* Sets tags to the span which is contained in the provided span wrapper, from information acquired from the
4138
* given basic statistic data unit.
4239
* @param spanWrapper Span wrapper that contains the target span.
43-
* @param basicStatisticDataUnit Basic statistic data unit from which, tag data will be acquired.
4440
*/
45-
public static void setSpanTags(SpanWrapper spanWrapper, BasicStatisticDataUnit basicStatisticDataUnit) {
46-
StatisticsLog statisticsLog = new StatisticsLog(spanWrapper.getStatisticDataUnit());
41+
public static void setSpanTags(SpanWrapper spanWrapper) {
42+
StatisticsLog openStatisticsLog = new StatisticsLog(spanWrapper.getStatisticDataUnit());
4743
Span span = spanWrapper.getSpan();
48-
if (basicStatisticDataUnit instanceof StatisticDataUnit) {
49-
if (OpenTracingManagerHolder.isCollectingPayloads() || OpenTracingManagerHolder.isCollectingProperties()) {
50-
StatisticDataUnit endEventDataUnit = (StatisticDataUnit) basicStatisticDataUnit;
51-
52-
if (OpenTracingManagerHolder.isCollectingPayloads()) {
53-
statisticsLog.setAfterPayload(endEventDataUnit.getPayload());
54-
span.setTag("beforePayload", statisticsLog.getBeforePayload());
55-
span.setTag("afterPayload", statisticsLog.getAfterPayload());
44+
if (OpenTracingManagerHolder.isCollectingPayloads() || OpenTracingManagerHolder.isCollectingProperties()) {
45+
if (OpenTracingManagerHolder.isCollectingPayloads()) {
46+
span.setTag("beforePayload", openStatisticsLog.getBeforePayload());
47+
if (spanWrapper.getCloseEventStatisticDataUnit() != null) {
48+
span.setTag("afterPayload", spanWrapper.getCloseEventStatisticDataUnit().getPayload());
49+
} else {
50+
//This means a close event hasn't been triggered so payload is equal to before payload
51+
span.setTag("afterPayload", openStatisticsLog.getBeforePayload());
5652
}
53+
}
5754

58-
if (OpenTracingManagerHolder.isCollectingProperties()) {
59-
if (spanWrapper.getStatisticDataUnit().getContextPropertyMap() != null) {
60-
span.setTag("beforeContextPropertyMap",
61-
spanWrapper.getStatisticDataUnit().getContextPropertyMap().toString());
62-
}
63-
if (statisticsLog.getContextPropertyMap() != null) {
64-
span.setTag("afterContextPropertyMap", statisticsLog.getContextPropertyMap().toString());
55+
if (OpenTracingManagerHolder.isCollectingProperties()) {
56+
if (spanWrapper.getStatisticDataUnit().getContextPropertyMap() != null) {
57+
span.setTag("beforeContextPropertyMap",
58+
spanWrapper.getStatisticDataUnit().getContextPropertyMap().toString());
59+
}
60+
if (spanWrapper.getCloseEventStatisticDataUnit() != null) {
61+
if (spanWrapper.getCloseEventStatisticDataUnit().getContextPropertyMap() != null) {
62+
span.setTag("afterContextPropertyMap",
63+
spanWrapper.getCloseEventStatisticDataUnit().getContextPropertyMap().toString());
6564
}
65+
} else if (openStatisticsLog.getContextPropertyMap() != null) {
66+
span.setTag("afterContextPropertyMap", openStatisticsLog.getContextPropertyMap().toString());
67+
}
68+
if (spanWrapper.getCloseEventStatisticDataUnit() != null &&
69+
spanWrapper.getCloseEventStatisticDataUnit().getPropertyValue() != null) {
70+
span.setTag("propertyMediatorValue",
71+
spanWrapper.getCloseEventStatisticDataUnit().getPropertyValue());
6672
}
6773
}
68-
69-
span.setTag("componentName", statisticsLog.getComponentName());
70-
span.setTag("componentType", statisticsLog.getComponentTypeToString());
71-
span.setTag("threadId", Thread.currentThread().getId());
72-
span.setTag("componentId", statisticsLog.getComponentId());
73-
span.setTag("hashcode", statisticsLog.getHashCode());
7474
}
75+
span.setTag("componentName", openStatisticsLog.getComponentName());
76+
span.setTag("componentType", openStatisticsLog.getComponentTypeToString());
77+
span.setTag("threadId", Thread.currentThread().getId());
78+
span.setTag("componentId", openStatisticsLog.getComponentId());
79+
span.setTag("hashcode", openStatisticsLog.getHashCode());
7580
}
7681
}

modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/opentracing/models/SpanWrapper.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ public class SpanWrapper {
4141
/**
4242
* Statistic data unit that has been collected during an open event, which carries data related to the span.
4343
*/
44-
private StatisticDataUnit statisticDataUnit;
44+
private StatisticDataUnit openEventStatisticDataUnit;
45+
46+
/**
47+
* Statistic data unit that has been collected during an closing event, which carries data related to the span.
48+
*/
49+
private StatisticDataUnit closeEventStatisticDataUnit;
4550

4651
/**
4752
* Parent span wrapper for this span wrapper.
@@ -82,10 +87,10 @@ public class SpanWrapper {
8287
*/
8388
private Set<String> childStructuredElementIds;
8489

85-
public SpanWrapper(String id, Span span, StatisticDataUnit statisticDataUnit, SpanWrapper parentSpanWrapper) {
90+
public SpanWrapper(String id, Span span, StatisticDataUnit openEventStatisticDataUnit, SpanWrapper parentSpanWrapper) {
8691
this.id = id;
8792
this.span = span;
88-
this.statisticDataUnit = statisticDataUnit;
93+
this.openEventStatisticDataUnit = openEventStatisticDataUnit;
8994
this.anonymousSequences = new LinkedHashMap<>();
9095
this.parentSpanWrapper = parentSpanWrapper;
9196
this.childStructuredElementIds = new HashSet<>();
@@ -97,11 +102,19 @@ public Span getSpan() {
97102
}
98103

99104
public StatisticDataUnit getStatisticDataUnit() {
100-
return statisticDataUnit;
105+
return openEventStatisticDataUnit;
101106
}
102107

103108
public void setStatisticDataUnit(StatisticDataUnit statisticDataUnit) {
104-
this.statisticDataUnit = statisticDataUnit;
109+
this.openEventStatisticDataUnit = statisticDataUnit;
110+
}
111+
112+
public StatisticDataUnit getCloseEventStatisticDataUnit() {
113+
return closeEventStatisticDataUnit;
114+
}
115+
116+
public void setCloseEventStatisticDataUnit(StatisticDataUnit closeEventStatisticDataUnit) {
117+
this.closeEventStatisticDataUnit = closeEventStatisticDataUnit;
105118
}
106119

107120
public void addAnonymousSequence(String id, SpanWrapper anonymousSequenceSpanWrapper) {

modules/core/src/main/java/org/apache/synapse/aspects/flow/statistics/opentracing/stores/SpanStore.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public SpanWrapper addSpanWrapper(String spanId,
120120
public void finishSpan(SpanWrapper spanWrapper) {
121121
if (spanWrapper != null && spanWrapper.getSpan() != null) {
122122
if (spanWrapper.getStatisticDataUnit() != null) {
123-
SpanTagger.setSpanTags(spanWrapper, spanWrapper.getStatisticDataUnit());
123+
SpanTagger.setSpanTags(spanWrapper);
124124
}
125125
spanWrapper.getSpan().finish();
126126
activeSpanWrappers.remove(spanWrapper);

modules/core/src/main/java/org/apache/synapse/config/xml/PropertyMediatorFactory.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,20 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) {
148148
if (scope != null) {
149149
String valueStr = scope.getAttributeValue();
150150
if (!XMLConfigConstants.SCOPE_AXIS2.equals(valueStr) &&
151-
!XMLConfigConstants.SCOPE_TRANSPORT.equals(valueStr) &&
152-
!XMLConfigConstants.SCOPE_OPERATION.equals(valueStr) &&
153-
!XMLConfigConstants.SCOPE_DEFAULT.equals(valueStr) &&
154-
!XMLConfigConstants.SCOPE_CLIENT.equals(valueStr) &&
155-
!XMLConfigConstants.SCOPE_REGISTRY.equals(valueStr)) {
151+
!XMLConfigConstants.SCOPE_TRANSPORT.equals(valueStr) &&
152+
!XMLConfigConstants.SCOPE_OPERATION.equals(valueStr) &&
153+
!XMLConfigConstants.SCOPE_DEFAULT.equals(valueStr) &&
154+
!XMLConfigConstants.SCOPE_CLIENT.equals(valueStr) &&
155+
!XMLConfigConstants.SCOPE_REGISTRY.equals(valueStr) &&
156+
!XMLConfigConstants.SCOPE_TRACE.equals(valueStr)) {
156157

157158
String msg = "Only '" + XMLConfigConstants.SCOPE_AXIS2 +
158159
"' or '" + XMLConfigConstants.SCOPE_TRANSPORT +
159160
"' or '" + XMLConfigConstants.SCOPE_CLIENT +
160161
"' or '" + XMLConfigConstants.SCOPE_DEFAULT +
161162
"' or '" + XMLConfigConstants.SCOPE_OPERATION +
162163
"' or '" + XMLConfigConstants.SCOPE_REGISTRY +
164+
"' or '" + XMLConfigConstants.SCOPE_TRACE +
163165
"' values are allowed for attribute scope for a property mediator" +
164166
", Unsupported scope " + valueStr;
165167
log.error(msg);

modules/core/src/main/java/org/apache/synapse/config/xml/XMLConfigConstants.java

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class XMLConfigConstants {
5353
public static final String SCOPE_SYSTEM = "system";
5454
/** The scope name for environment variables */
5555
public static final String SCOPE_ENVIRONMENT = "env";
56+
/** The scope name for properties used for tracing */
57+
public static final String SCOPE_TRACE = "trace";
5658
public static final String KEY = "key";
5759
public static final String NAME = "name";
5860
public static final String LOCATION = "location";

modules/core/src/main/java/org/apache/synapse/mediators/builtin/PropertyMediator.java

+20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.apache.synapse.MessageContext;
2525
import org.apache.synapse.SynapseLog;
2626
import org.apache.synapse.SynapseException;
27+
import org.apache.synapse.aspects.ComponentType;
28+
import org.apache.synapse.aspects.flow.statistics.collectors.CloseEventCollector;
2729
import org.apache.synapse.config.xml.SynapsePath;
2830
import org.apache.synapse.config.xml.XMLConfigConstants;
2931
import org.apache.synapse.config.SynapseConfigUtils;
@@ -86,6 +88,9 @@ public class PropertyMediator extends AbstractMediator {
8688

8789
private static final String EMPTY_CONTENT = "";
8890

91+
/** To keep the Property Value for tracing **/
92+
private String propertyValue = null;
93+
8994
/**
9095
* Sets a property into the current (local) Synapse Context or into the Axis Message Context
9196
* or into Transports Header and removes above properties from the corresponding locations.
@@ -145,6 +150,15 @@ public boolean mediate(MessageContext synCtx) {
145150

146151
synCtx.setProperty(name, resultValue);
147152

153+
} else if (XMLConfigConstants.SCOPE_TRACE.equals(scope)) {
154+
//Setting property value into the propertyValue variable for tracing purposes
155+
if (resultValue != null ) {
156+
if (resultValue instanceof OMElement) {
157+
((OMElement) resultValue).build();
158+
}
159+
//Converted to string since only used to display as a span tag
160+
propertyValue = resultValue.toString();
161+
}
148162
} else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope)
149163
&& synCtx instanceof Axis2MessageContext) {
150164
//Setting property into the Axis2 Message Context
@@ -336,6 +350,12 @@ public void setExpression(SynapsePath expression) {
336350
setExpression(expression, null);
337351
}
338352

353+
public void reportCloseStatistics(MessageContext messageContext, Integer currentIndex) {
354+
CloseEventCollector
355+
.closeEntryEvent(messageContext, getMediatorName(), ComponentType.MEDIATOR, currentIndex,
356+
isContentAltering(), propertyValue);
357+
}
358+
339359
public void setExpression(SynapsePath expression, String type) {
340360
this.expression = expression;
341361
// Save the type information for now

0 commit comments

Comments
 (0)