Skip to content

Commit a4088b5

Browse files
authored
Revert "added regex support and improved retrying (#192)" (#194)
This reverts commit 1318917.
1 parent 1318917 commit a4088b5

File tree

7 files changed

+67
-57
lines changed

7 files changed

+67
-57
lines changed

validator/src/main/java/com/amazon/aoc/exception/ExceptionCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public enum ExceptionCode {
2525

2626
// validating errors
2727
TRACE_ID_NOT_MATCHED(50001, "trace id not matched"),
28-
DATA_MODEL_NOT_MATCHED(50006, "data model not matched"),
28+
DATA_MODEL_NOT_MATCHED(50006, "trace id not matched"),
2929
TRACE_SPAN_LIST_NOT_MATCHED(50002, "trace span list has different length"),
3030
TRACE_SPAN_NOT_MATCHED(50003, "trace span not matched"),
3131
TRACE_LIST_NOT_MATCHED(50004, "trace list has different length"),

validator/src/main/java/com/amazon/aoc/helpers/RetryHelper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public static boolean retry(
3737
int retryCount, int sleepInMilliSeconds, boolean throwExceptionInTheEnd, Retryable retryable)
3838
throws Exception {
3939
Exception exceptionInTheEnd = null;
40-
int initialCount = retryCount;
4140
while (retryCount-- > 0) {
4241
try {
4342
log.info("retry attempt left : {} ", retryCount);
@@ -50,8 +49,8 @@ public static boolean retry(
5049
}
5150
}
5251

53-
log.error("All {} retries exhausted", initialCount);
5452
if (throwExceptionInTheEnd) {
53+
log.error("retries exhausted, possible");
5554
throw exceptionInTheEnd;
5655
}
5756
return false;

validator/src/main/java/com/amazon/aoc/models/xray/Entity.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
@Getter
1717
@Setter
18-
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
1918
public class Entity {
2019
private String name;
2120
private String id;
@@ -26,13 +25,21 @@ public class Entity {
2625
private String origin;
2726
private String traceId;
2827

28+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
2929
private double endTime;
30+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
3031
private boolean fault;
32+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
3133
private boolean error;
34+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
3235
private boolean throttle;
36+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
3337
private boolean inProgress;
38+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
3439
private boolean inferred;
40+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
3541
private boolean stubbed;
42+
3643
private String namespace;
3744

3845
private List<Entity> subsegments;
@@ -41,7 +48,6 @@ public class Entity {
4148
private Map<String, Object> http;
4249
private Map<String, Object> aws;
4350
private Map<String, Object> sql;
44-
private Map<String, Object> service;
4551

4652
private Map<String, Map<String, Object>> metadata;
4753
private Map<String, Object> annotations;

validator/src/main/java/com/amazon/aoc/validators/TraceValidator.java

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import java.util.List;
4242
import java.util.Map;
4343
import java.util.concurrent.atomic.AtomicReference;
44-
import java.util.regex.Pattern;
4544

4645
@Log4j2
4746
public class TraceValidator implements IValidator {
@@ -66,47 +65,39 @@ public void init(
6665

6766
@Override
6867
public void validate() throws Exception {
69-
// 2 retries for calling the sample app to handle the Lambda case,
70-
// where first request might be a cold start and have an additional unexpected subsegment
71-
boolean isMatched = RetryHelper.retry(2,
68+
// get stored trace
69+
Map<String, Object> storedTrace = this.getStoredTrace();
70+
log.info("value of stored trace map: {}", storedTrace);
71+
// create trace id list to retrieve trace from x-ray service
72+
String traceId = (String) storedTrace.get("[0].trace_id");
73+
List<String> traceIdList = Collections.singletonList(traceId);
74+
75+
// get retrieved trace from x-ray service
76+
boolean isMatched = RetryHelper.retry(10,
7277
Integer.parseInt(GenericConstants.SLEEP_IN_MILLISECONDS.getVal()),
7378
false,
7479
() -> {
75-
// Call sample app and get locally stored trace
76-
Map<String, Object> storedTrace = this.getStoredTrace();
77-
log.info("value of stored trace map: {}", storedTrace);
78-
79-
// prepare list of trace IDs to retrieve from X-Ray service
80-
String traceId = (String) storedTrace.get("[0].trace_id");
81-
List<String> traceIdList = Collections.singletonList(traceId);
82-
83-
// Retry 5 times to since segments might not be immediately available in X-Ray service
84-
RetryHelper.retry(
85-
5,
86-
() -> {
87-
// get retrieved trace from x-ray service
88-
Map<String, Object> retrievedTrace = this.getRetrievedTrace(traceIdList);
89-
log.info("value of retrieved trace map: {}", retrievedTrace);
90-
91-
// data model validation of other fields of segment document
92-
for (Map.Entry<String, Object> entry : storedTrace.entrySet()) {
93-
String targetKey = entry.getKey();
94-
if (retrievedTrace.get(targetKey) == null) {
95-
log.error("mis target data: {}", targetKey);
96-
throw new BaseException(ExceptionCode.DATA_MODEL_NOT_MATCHED);
97-
}
98-
99-
if (!Pattern.matches(entry.getValue().toString(),
100-
retrievedTrace.get(targetKey).toString())) {
101-
log.error("data model validation failed");
102-
log.info("mis matched data model field list");
103-
log.info("value of stored trace map: {}", entry.getValue());
104-
log.info("value of retrieved map: {}", retrievedTrace.get(entry.getKey()));
105-
log.info("==========================================");
106-
throw new BaseException(ExceptionCode.DATA_MODEL_NOT_MATCHED);
107-
}
108-
}
109-
});
80+
Map<String, Object> retrievedTrace = this.getRetrievedTrace(traceIdList);
81+
log.info("value of retrieved trace map: {}", retrievedTrace);
82+
// data model validation of other fields of segment document
83+
for (Map.Entry<String, Object> entry : storedTrace.entrySet()) {
84+
String targetKey = entry.getKey();
85+
if (retrievedTrace.get(targetKey) == null) {
86+
log.error("mis target data: {}", targetKey);
87+
throw new BaseException(ExceptionCode.DATA_MODEL_NOT_MATCHED);
88+
}
89+
if (!entry
90+
.getValue()
91+
.toString()
92+
.equalsIgnoreCase(retrievedTrace.get(targetKey).toString())) {
93+
log.error("data model validation failed");
94+
log.info("mis matched data model field list");
95+
log.info("value of stored trace map: {}", entry.getValue());
96+
log.info("value of retrieved map: {}", retrievedTrace.get(entry.getKey()));
97+
log.info("==========================================");
98+
throw new BaseException(ExceptionCode.DATA_MODEL_NOT_MATCHED);
99+
}
100+
}
110101
});
111102

112103
if (!isMatched) {
@@ -118,12 +109,28 @@ public void validate() throws Exception {
118109

119110
// this method will hit get trace from x-ray service and get retrieved trace
120111
private Map<String, Object> getRetrievedTrace(List<String> traceIdList) throws Exception {
121-
List<Trace> retrieveTraceList = xrayService.listTraceByIds(traceIdList);
122-
if (retrieveTraceList == null || retrieveTraceList.isEmpty()) {
123-
throw new BaseException(ExceptionCode.EMPTY_LIST);
124-
}
125-
126-
return this.flattenDocument(retrieveTraceList.get(0).getSegments());
112+
AtomicReference<Map<String, Object>> flattenedJsonMapForRetrievedTrace =
113+
new AtomicReference<>();
114+
RetryHelper.retry(
115+
30,
116+
() -> {
117+
List<Trace> retrieveTraceList = null;
118+
retrieveTraceList = xrayService.listTraceByIds(traceIdList);
119+
if (retrieveTraceList == null || retrieveTraceList.isEmpty()) {
120+
throw new BaseException(ExceptionCode.EMPTY_LIST);
121+
}
122+
123+
// in case the json format is wrong, retry it.
124+
if (!retrieveTraceList.isEmpty()) {
125+
flattenedJsonMapForRetrievedTrace.set(
126+
this.flattenDocument(retrieveTraceList.get(0).getSegments()));
127+
} else {
128+
log.error("retrieved trace list is empty or null");
129+
throw new BaseException(ExceptionCode.EMPTY_LIST);
130+
}
131+
});
132+
133+
return flattenedJsonMapForRetrievedTrace.get();
127134
}
128135

129136
private Map<String, Object> flattenDocument(List<Segment> segmentList) {

validator/src/main/resources/expected-data-template/lambdaExpectedTrace.mustache

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
]
99
},
1010
{
11-
"name":"serverlessrepo-aot-py38-sar-function-.*",
1211
"http":{
1312
"response":{
1413
"status":200
@@ -17,18 +16,17 @@
1716
"origin":"AWS::Lambda"
1817
},
1918
{
20-
"name":"serverlessrepo-aot-py38-sar-function-.*",
2119
"origin":"AWS::Lambda::Function",
2220
"subsegments":[
2321
{
2422
"name":"Invocation",
2523
"subsegments":[
2624
{
27-
"name":"lambda_function\\.lambda_handler",
25+
"name":"lambda_function.lambda_handler",
2826
"aws":{
2927
"xray":{
3028
"auto_instrumentation":false,
31-
"sdk_version":"0\\.16b1",
29+
"sdk_version":"0.16b1",
3230
"sdk":"opentelemetry for python"
3331
}
3432
}
@@ -46,7 +44,7 @@
4644
"inferred":true,
4745
"http":{
4846
"request":{
49-
"url":"http://httpbin\\.org/",
47+
"url":"http://httpbin.org/",
5048
"method":"GET"
5149
}
5250
}

validator/src/main/resources/expected-data-template/otelSDKexpectedHTTPTrace.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"name": "HTTP GET",
1919
"http": {
2020
"request": {
21-
"url": "https://aws\\.amazon\\.com/",
21+
"url": "https://aws.amazon.com/",
2222
"method": "GET"
2323
},
2424
"response": {

validator/src/main/resources/expected-data-template/xraySDKexpectedHTTPTrace.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"name": "aws.amazon.com",
1414
"http": {
1515
"request": {
16-
"url": "https://aws\\.amazon\\.com",
16+
"url": "https://aws.amazon.com",
1717
"method": "get"
1818
}
1919
},

0 commit comments

Comments
 (0)