Add request body and some other details in action execution data point (#3096)

This commit is contained in:
Shrikant Sharat Kandula 2021-02-19 14:39:40 +05:30
parent 60a4ddb3c1
commit ef732f1389
2 changed files with 92 additions and 26 deletions

View File

@ -2,12 +2,20 @@ package com.appsmith.server.configurations;
import com.segment.analytics.Analytics;
import com.segment.analytics.Log;
import com.segment.analytics.messages.TrackMessage;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;
@Slf4j
@Configuration
public class SegmentConfig {
@ -21,30 +29,64 @@ public class SegmentConfig {
@Bean
@ConditionalOnExpression(value = "!'${segment.writeKey:}'.isEmpty()")
public Analytics analyticsRunner() {
final Log logProcessor = new Log() {
@Override
public void print(Level level, String format, Object... args) {
print(level, null, format, args);
}
final LogProcessor logProcessor = new LogProcessor();
Analytics analyticsOnAnalytics = Analytics.builder(writeKey).log(logProcessor).build();
@Override
public void print(Level level, Throwable error, String format, Object... args) {
final String message = "SEGMENT: " + format;
if (level == Level.VERBOSE) {
log.trace(message, error, args);
} else if (level == Level.DEBUG) {
log.debug(message, error, args);
} else if (level == Level.ERROR) {
log.error(message, error, args);
}
}
};
// We use a different analytics instance for sending events about the analytics system itself so we don't end up
// in a recursive state.
final LogProcessor logProcessorWithErrorHandler = new LogProcessor();
final Analytics analytics = Analytics.builder(writeKey).log(logProcessorWithErrorHandler).build();
logProcessorWithErrorHandler.onError(logData -> {
analyticsOnAnalytics.enqueue(TrackMessage.builder("segment_error")
.properties(Map.of(
"message", logData.getMessage(),
"error", logData.getError().getMessage(),
"args", ObjectUtils.defaultIfNull(logData.getArgs(), Collections.emptyList())
))
);
});
return Analytics.builder(writeKey).log(logProcessor).build();
return analytics;
}
public String getCeKey() {
return ceKey;
}
private static class LogProcessor implements Log {
private Consumer<LogData> errorHandler = null;
@Override
public void print(Level level, String format, Object... args) {
print(level, null, format, args);
}
@Override
public void print(Level level, Throwable error, String format, Object... args) {
final String message = "SEGMENT: " + format;
if (level == Level.VERBOSE) {
log.trace(message, error, args);
} else if (level == Level.DEBUG) {
log.debug(message, error, args);
} else if (level == Level.ERROR) {
if (errorHandler != null) {
errorHandler.accept(new LogData(error, format, args));
}
log.error(message, error, args);
}
}
public void onError(Consumer<LogData> handler) {
errorHandler = handler;
}
}
@Data
@RequiredArgsConstructor
private static class LogData {
final Throwable error;
final String message;
final Object[] args;
}
}

View File

@ -36,6 +36,8 @@ import com.appsmith.server.exceptions.AppsmithException;
import com.appsmith.server.helpers.MustacheHelper;
import com.appsmith.server.helpers.PluginExecutorHelper;
import com.appsmith.server.repositories.NewActionRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.ObjectUtils;
@ -667,20 +669,42 @@ public class NewActionServiceImpl extends BaseService<NewActionRepository, NewAc
final PluginType pluginType = action.getPluginType();
final ActionConfiguration actionConfiguration = actionDTO.getActionConfiguration();
final ObjectMapper objectMapper = new ObjectMapper();
final Map<String, Object> requestData = new HashMap<>();
if (pluginType == PluginType.API) {
String headersJson;
try {
headersJson = objectMapper.writeValueAsString(actionConfiguration
.getHeaders()
.stream()
.filter(p -> !StringUtils.isEmpty(p.getKey()))
.collect(Collectors.toMap(Property::getKey, Property::getValue, (a, b) -> b)));
} catch (JsonProcessingException e) {
log.warn("Couldn't serialize headers to JSON", e);
headersJson = "";
}
String paramsJson;
try {
paramsJson = objectMapper.writeValueAsString(actionConfiguration
.getQueryParameters()
.stream()
.filter(p -> !StringUtils.isEmpty(p.getKey()))
.collect(Collectors.toMap(Property::getKey, Property::getValue, (a, b) -> b)));
} catch (JsonProcessingException e) {
log.warn("Couldn't serialize params to JSON", e);
paramsJson = "";
}
requestData.putAll(Map.of(
"url", actionDTO.getDatasource().getDatasourceConfiguration().getUrl() + actionConfiguration.getPath(),
"headers", actionConfiguration
.getHeaders()
.stream()
.collect(Collectors.toMap(Property::getKey, Property::getValue)),
"parameters", actionConfiguration
.getQueryParameters()
.stream()
.collect(Collectors.toMap(Property::getKey, Property::getValue))
"headers", headersJson,
"parameters", paramsJson,
"body", ObjectUtils.defaultIfNull(actionConfiguration.getBody(), "")
));
} else if (pluginType == PluginType.DB) {
requestData.putAll(Map.of(
"query", ObjectUtils.defaultIfNull(actionConfiguration.getBody(), ""),