Skip to content

Commit

Permalink
Add previous input to generated error events; add more logging to aid…
Browse files Browse the repository at this point in the history
… troubleshooting.
  • Loading branch information
msqr committed Jul 11, 2024
1 parent 2a43844 commit ee402fc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
* DAO implementation of {@link DatumInputEndpointBiz}.
*
* @author matt
* @version 1.4
* @version 1.5
*/
public class DaoDatumInputEndpointBiz implements DatumInputEndpointBiz, CentralDinUserEvents {

Expand Down Expand Up @@ -123,7 +123,7 @@ public DaoDatumInputEndpointBiz(SolarNodeOwnershipDao nodeOwnershipDao,
private static final MimeType TEXT_TYPE = MimeType.valueOf("text/*");

private static LogEventInfo importErrorEvent(String msg, EndpointConfiguration endpoint,
TransformConfiguration xform, MimeType contentType, byte[] content,
TransformConfiguration xform, MimeType contentType, byte[] content, byte[] previousContent,
Map<String, String> parameters) {
var eventData = new LinkedHashMap<>(8);
eventData.put(ENDPOINT_ID_DATA_KEY, endpoint.getEndpointId());
Expand All @@ -132,22 +132,32 @@ private static LogEventInfo importErrorEvent(String msg, EndpointConfiguration e
eventData.put(CONTENT_TYPE_DATA_KEY, contentType.toString());

if ( content != null && content.length > 0 ) {
String value = null;
if ( JSON_TYPE.isCompatibleWith(contentType) || TEXT_TYPE.isCompatibleWith(contentType) ) {
value = new String(content, contentType.getCharset() != null ? contentType.getCharset()
: StandardCharsets.UTF_8);
} else {
value = Base64.getEncoder().encodeToString(content);
}
String value = eventContentValue(contentType, content);
eventData.put(CONTENT_DATA_KEY, value);
}

if ( previousContent != null && previousContent.length > 0 ) {
String value = eventContentValue(contentType, previousContent);
eventData.put(PREVIOUS_CONTENT_DATA_KEY, value);
}

if ( parameters != null ) {
eventData.put(PARAMETERS_DATA_KEY, parameters);
}
return event(DATUM_TAGS, msg, getJSONString(eventData, null), ERROR_TAG);
}

private static String eventContentValue(MimeType contentType, byte[] content) {
String value = null;
if ( JSON_TYPE.isCompatibleWith(contentType) || TEXT_TYPE.isCompatibleWith(contentType) ) {
value = new String(content, contentType.getCharset() != null ? contentType.getCharset()
: StandardCharsets.UTF_8);
} else {
value = Base64.getEncoder().encodeToString(content);
}
return value;
}

@Override
public Collection<DatumId> importDatum(Long userId, UUID endpointId, MimeType contentType,
InputStream in, Map<String, String> parameters) throws IOException {
Expand Down Expand Up @@ -181,7 +191,7 @@ public Collection<DatumId> importDatum(Long userId, UUID endpointId, MimeType co
String msg = "Transform service %s does not support input type %s with %s."
.formatted(xformServiceId, contentType, in.getClass().getSimpleName());
// @formatter:off
addEvent(userEventAppenderBiz, userId, importErrorEvent(msg, endpoint, xform, contentType, inputData,parameters));
addEvent(userEventAppenderBiz, userId, importErrorEvent(msg, endpoint, xform, contentType, inputData,null,parameters));
// @formatter:on
throw new IllegalArgumentException(msg);
}
Expand All @@ -195,6 +205,7 @@ public Collection<DatumId> importDatum(Long userId, UUID endpointId, MimeType co
params.put(TransformService.PARAM_TRANSFORM_ID, endpoint.getTransformId());
params.put(TransformService.PARAM_CONFIGURATION_CACHE_KEY, xform.ident());

byte[] prevInputData = null;
Iterable<Datum> datum;
try {
// check for previous input support
Expand All @@ -212,24 +223,44 @@ public Collection<DatumId> importDatum(Long userId, UUID endpointId, MimeType co
if ( nodeId != null && sourceId != null ) {
UserLongStringCompositePK key = new UserLongStringCompositePK(userId, nodeId,
sourceId);
byte[] previousInput = previousInputDao.getAndPut(key, inputData);
if ( previousInput == null ) {
prevInputData = previousInputDao.getAndPut(key, inputData);
if ( prevInputData == null ) {
return Collections.emptyList();
}
params.put(TransformService.PARAM_PREVIOUS_INPUT,
new ByteArrayInputStream(previousInput));
new ByteArrayInputStream(prevInputData));
}
} catch ( IllegalArgumentException e ) {
// ignore and continue
}
}

datum = xformService.transform(in, contentType, xform, params);

if ( log.isTraceEnabled() ) {
log.trace(
"Transformed user {} endpoint {} to datum with service {}; parameters {}; previous content [{}]; content [{}]; datum: {}",
userId, endpointId, xformServiceId, params,
eventContentValue(contentType, prevInputData),
eventContentValue(contentType, inputData), datum);
}
} catch ( Exception e ) {
String msg = "Error executing transform: " + e.getMessage();
// @formatter:off
addEvent(userEventAppenderBiz, userId, importErrorEvent(msg, endpoint, xform, contentType, inputData,parameters));
// @formatter:on
Throwable root = e;
while ( root.getCause() != null ) {
root = root.getCause();
}
String msg = "Error executing transform: " + root.getMessage();

if ( log.isWarnEnabled() ) {
log.warn(
"Error transforming user {} endpoint {} to datum with service {}; parameters {}; previous content [{}]; content: [{}]",
userId, endpointId, xformServiceId, params,
eventContentValue(contentType, prevInputData),
eventContentValue(contentType, inputData), e);
}

addEvent(userEventAppenderBiz, userId, importErrorEvent(msg, endpoint, xform, contentType,
inputData, prevInputData, parameters));
if ( e instanceof IOException ioe ) {
throw ioe;
} else if ( e instanceof RuntimeException re ) {
Expand Down Expand Up @@ -267,7 +298,7 @@ public Collection<DatumId> importDatum(Long userId, UUID endpointId, MimeType co
if ( !userId.equals(owner.getUserId()) ) {
var ex = new AuthorizationException(Reason.ACCESS_DENIED, nodeId);
addEvent(userEventAppenderBiz, userId, importErrorEvent(ex.getMessage(), endpoint,
xform, contentType, inputData, parameters));
xform, contentType, inputData, prevInputData, parameters));
throw ex;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* Constants for central datum input (DIN) user events.
*
* @author matt
* @version 1.2
* @version 1.3
*/
public interface CentralDinUserEvents {

Expand Down Expand Up @@ -74,4 +74,16 @@ public interface CentralDinUserEvents {
*/
String CONTENT_DATA_KEY = "content";

/**
* User event data key for the tracked previous request content value.
*
* <p>
* If the content is textual, the content will be included as-is. Otherwise
* it will be Base64 encoded.
* </p>
*
* @since 1.3
*/
String PREVIOUS_CONTENT_DATA_KEY = "previousContent";

}

0 comments on commit ee402fc

Please sign in to comment.