Skip to content

Commit

Permalink
Fix for errors when some req/res are missing
Browse files Browse the repository at this point in the history
Signed-off-by: Mikayla Thompson <thomika@amazon.com>
  • Loading branch information
mikaylathompson committed Nov 15, 2023
1 parent d152448 commit d5c57d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ processors:
action: insert
- key: log4j.context_data.channel_id
action: delete
# The following actions convert various should-be-int strings to ints
- key: log4j.context_data.source_http_status
action: convert
converted_type: int
- key: log4j.context_data.target_http_status
action: convert
converted_type: int
- key: log4j.context_data.http_status_match
action: convert
converted_type: int

extensions:
basicauth/client:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.json.JSONObject;
import org.opensearch.migrations.coreutils.MetricsAttributeKey;
import org.opensearch.migrations.coreutils.MetricsEvent;
import org.opensearch.migrations.coreutils.MetricsLogBuilder;
import org.opensearch.migrations.coreutils.MetricsLogger;
import org.opensearch.migrations.replay.datatypes.HttpRequestTransformationStatus;
import org.opensearch.migrations.replay.datatypes.TransformedPackets;
Expand Down Expand Up @@ -125,20 +126,24 @@ private JSONObject toJSONObject(SourceTargetCaptureTuple tuple) {
meta.put("connectionId", tuple.uniqueRequestKey);
Optional.ofNullable(tuple.errorCause).ifPresent(e->meta.put("error", e));

if (tuple.targetRequestData != null && tuple.sourcePair.responseData != null) {
if (meta.has("sourceResponse") && meta.has("targetResponse")) {
String sourceResponseStatus = (String) meta.getJSONObject("sourceResponse").get("Status-Code");
String targetResponseStatus = (String) meta.getJSONObject("targetResponse").get("Status-Code");
String httpMethod = (String) meta.getJSONObject("sourceRequest").get("Method");
String endpoint = (String) meta.getJSONObject("sourceRequest").get("Request-URI");

metricsLogger.atSuccess(MetricsEvent.STATUS_CODE_COMPARISON)
.setAttribute(MetricsAttributeKey.REQUEST_ID, tuple.uniqueRequestKey.toString())
.setAttribute(MetricsAttributeKey.HTTP_METHOD, httpMethod)
.setAttribute(MetricsAttributeKey.HTTP_ENDPOINT, endpoint)
MetricsLogBuilder metric = metricsLogger.atSuccess(MetricsEvent.STATUS_CODE_COMPARISON)
.setAttribute(MetricsAttributeKey.REQUEST_ID,
tuple.uniqueRequestKey.getTrafficStreamKey().getConnectionId() + "." + tuple.uniqueRequestKey.getSourceRequestIndex())
.setAttribute(MetricsAttributeKey.SOURCE_HTTP_STATUS, sourceResponseStatus)
.setAttribute(MetricsAttributeKey.TARGET_HTTP_STATUS, targetResponseStatus)
.setAttribute(MetricsAttributeKey.HTTP_STATUS_MATCH, sourceResponseStatus.equals(targetResponseStatus))
.emit();
.setAttribute(MetricsAttributeKey.HTTP_STATUS_MATCH,
sourceResponseStatus.equals(targetResponseStatus) ? 1 : 0);

if (meta.has("sourceRequest")) {
String httpMethod = (String) meta.getJSONObject("sourceRequest").get("Method");
String endpoint = (String) meta.getJSONObject("sourceRequest").get("Request-URI");
metric = metric.setAttribute(MetricsAttributeKey.HTTP_METHOD, httpMethod)
.setAttribute(MetricsAttributeKey.HTTP_ENDPOINT, endpoint);
}
metric.emit();
}
return meta;
}
Expand Down

0 comments on commit d5c57d5

Please sign in to comment.