Skip to content

Commit

Permalink
Revert "added regex support and improved retrying (#192)" (#194)
Browse files Browse the repository at this point in the history
This reverts commit 1318917.
  • Loading branch information
wyTrivail authored Dec 10, 2020
1 parent 1318917 commit a4088b5
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public enum ExceptionCode {

// validating errors
TRACE_ID_NOT_MATCHED(50001, "trace id not matched"),
DATA_MODEL_NOT_MATCHED(50006, "data model not matched"),
DATA_MODEL_NOT_MATCHED(50006, "trace id not matched"),
TRACE_SPAN_LIST_NOT_MATCHED(50002, "trace span list has different length"),
TRACE_SPAN_NOT_MATCHED(50003, "trace span not matched"),
TRACE_LIST_NOT_MATCHED(50004, "trace list has different length"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public static boolean retry(
int retryCount, int sleepInMilliSeconds, boolean throwExceptionInTheEnd, Retryable retryable)
throws Exception {
Exception exceptionInTheEnd = null;
int initialCount = retryCount;
while (retryCount-- > 0) {
try {
log.info("retry attempt left : {} ", retryCount);
Expand All @@ -50,8 +49,8 @@ public static boolean retry(
}
}

log.error("All {} retries exhausted", initialCount);
if (throwExceptionInTheEnd) {
log.error("retries exhausted, possible");
throw exceptionInTheEnd;
}
return false;
Expand Down
10 changes: 8 additions & 2 deletions validator/src/main/java/com/amazon/aoc/models/xray/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class Entity {
private String name;
private String id;
Expand All @@ -26,13 +25,21 @@ public class Entity {
private String origin;
private String traceId;

@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private double endTime;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private boolean fault;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private boolean error;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private boolean throttle;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private boolean inProgress;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private boolean inferred;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private boolean stubbed;

private String namespace;

private List<Entity> subsegments;
Expand All @@ -41,7 +48,6 @@ public class Entity {
private Map<String, Object> http;
private Map<String, Object> aws;
private Map<String, Object> sql;
private Map<String, Object> service;

private Map<String, Map<String, Object>> metadata;
private Map<String, Object> annotations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;

@Log4j2
public class TraceValidator implements IValidator {
Expand All @@ -66,47 +65,39 @@ public void init(

@Override
public void validate() throws Exception {
// 2 retries for calling the sample app to handle the Lambda case,
// where first request might be a cold start and have an additional unexpected subsegment
boolean isMatched = RetryHelper.retry(2,
// get stored trace
Map<String, Object> storedTrace = this.getStoredTrace();
log.info("value of stored trace map: {}", storedTrace);
// create trace id list to retrieve trace from x-ray service
String traceId = (String) storedTrace.get("[0].trace_id");
List<String> traceIdList = Collections.singletonList(traceId);

// get retrieved trace from x-ray service
boolean isMatched = RetryHelper.retry(10,
Integer.parseInt(GenericConstants.SLEEP_IN_MILLISECONDS.getVal()),
false,
() -> {
// Call sample app and get locally stored trace
Map<String, Object> storedTrace = this.getStoredTrace();
log.info("value of stored trace map: {}", storedTrace);

// prepare list of trace IDs to retrieve from X-Ray service
String traceId = (String) storedTrace.get("[0].trace_id");
List<String> traceIdList = Collections.singletonList(traceId);

// Retry 5 times to since segments might not be immediately available in X-Ray service
RetryHelper.retry(
5,
() -> {
// get retrieved trace from x-ray service
Map<String, Object> retrievedTrace = this.getRetrievedTrace(traceIdList);
log.info("value of retrieved trace map: {}", retrievedTrace);

// data model validation of other fields of segment document
for (Map.Entry<String, Object> entry : storedTrace.entrySet()) {
String targetKey = entry.getKey();
if (retrievedTrace.get(targetKey) == null) {
log.error("mis target data: {}", targetKey);
throw new BaseException(ExceptionCode.DATA_MODEL_NOT_MATCHED);
}

if (!Pattern.matches(entry.getValue().toString(),
retrievedTrace.get(targetKey).toString())) {
log.error("data model validation failed");
log.info("mis matched data model field list");
log.info("value of stored trace map: {}", entry.getValue());
log.info("value of retrieved map: {}", retrievedTrace.get(entry.getKey()));
log.info("==========================================");
throw new BaseException(ExceptionCode.DATA_MODEL_NOT_MATCHED);
}
}
});
Map<String, Object> retrievedTrace = this.getRetrievedTrace(traceIdList);
log.info("value of retrieved trace map: {}", retrievedTrace);
// data model validation of other fields of segment document
for (Map.Entry<String, Object> entry : storedTrace.entrySet()) {
String targetKey = entry.getKey();
if (retrievedTrace.get(targetKey) == null) {
log.error("mis target data: {}", targetKey);
throw new BaseException(ExceptionCode.DATA_MODEL_NOT_MATCHED);
}
if (!entry
.getValue()
.toString()
.equalsIgnoreCase(retrievedTrace.get(targetKey).toString())) {
log.error("data model validation failed");
log.info("mis matched data model field list");
log.info("value of stored trace map: {}", entry.getValue());
log.info("value of retrieved map: {}", retrievedTrace.get(entry.getKey()));
log.info("==========================================");
throw new BaseException(ExceptionCode.DATA_MODEL_NOT_MATCHED);
}
}
});

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

// this method will hit get trace from x-ray service and get retrieved trace
private Map<String, Object> getRetrievedTrace(List<String> traceIdList) throws Exception {
List<Trace> retrieveTraceList = xrayService.listTraceByIds(traceIdList);
if (retrieveTraceList == null || retrieveTraceList.isEmpty()) {
throw new BaseException(ExceptionCode.EMPTY_LIST);
}

return this.flattenDocument(retrieveTraceList.get(0).getSegments());
AtomicReference<Map<String, Object>> flattenedJsonMapForRetrievedTrace =
new AtomicReference<>();
RetryHelper.retry(
30,
() -> {
List<Trace> retrieveTraceList = null;
retrieveTraceList = xrayService.listTraceByIds(traceIdList);
if (retrieveTraceList == null || retrieveTraceList.isEmpty()) {
throw new BaseException(ExceptionCode.EMPTY_LIST);
}

// in case the json format is wrong, retry it.
if (!retrieveTraceList.isEmpty()) {
flattenedJsonMapForRetrievedTrace.set(
this.flattenDocument(retrieveTraceList.get(0).getSegments()));
} else {
log.error("retrieved trace list is empty or null");
throw new BaseException(ExceptionCode.EMPTY_LIST);
}
});

return flattenedJsonMapForRetrievedTrace.get();
}

private Map<String, Object> flattenDocument(List<Segment> segmentList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
]
},
{
"name":"serverlessrepo-aot-py38-sar-function-.*",
"http":{
"response":{
"status":200
Expand All @@ -17,18 +16,17 @@
"origin":"AWS::Lambda"
},
{
"name":"serverlessrepo-aot-py38-sar-function-.*",
"origin":"AWS::Lambda::Function",
"subsegments":[
{
"name":"Invocation",
"subsegments":[
{
"name":"lambda_function\\.lambda_handler",
"name":"lambda_function.lambda_handler",
"aws":{
"xray":{
"auto_instrumentation":false,
"sdk_version":"0\\.16b1",
"sdk_version":"0.16b1",
"sdk":"opentelemetry for python"
}
}
Expand All @@ -46,7 +44,7 @@
"inferred":true,
"http":{
"request":{
"url":"http://httpbin\\.org/",
"url":"http://httpbin.org/",
"method":"GET"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"name": "HTTP GET",
"http": {
"request": {
"url": "https://aws\\.amazon\\.com/",
"url": "https://aws.amazon.com/",
"method": "GET"
},
"response": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"name": "aws.amazon.com",
"http": {
"request": {
"url": "https://aws\\.amazon\\.com",
"url": "https://aws.amazon.com",
"method": "get"
}
},
Expand Down

0 comments on commit a4088b5

Please sign in to comment.