Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 13 additions & 158 deletions src/main/java/com/stackify/api/common/ApiConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
package com.stackify.api.common;

import com.stackify.api.EnvironmentDetail;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

/**
* ApiConfiguration
* @author Eric Martin
*/
@ToString
@Getter
@Builder(builderClassName = "Builder", toBuilder = true, builderMethodName = "newBuilder")
public class ApiConfiguration {

/**
Expand Down Expand Up @@ -54,166 +60,15 @@ public class ApiConfiguration {
private final EnvironmentDetail envDetail;

/**
* @return the apiUrl
*/
public String getApiUrl() {
return apiUrl != null ? apiUrl : DEFAULT_API_URL;
}

/**
* @return the apiKey
*/
public String getApiKey() {
return apiKey;
}

/**
* @return the application
*/
public String getApplication() {
return application;
}

/**
* @return the environment
* Add #SKIPJSON tag to messages containing Json
*/
public String getEnvironment() {
return environment;
}

/**
* @return the envDetail
*/
public EnvironmentDetail getEnvDetail() {
return envDetail;
}
private final Boolean skipJson;

/**
* @param builder The Builder object that contains all of the values for initialization
*/
private ApiConfiguration(final Builder builder) {
this.apiUrl = builder.apiUrl;
this.apiKey = builder.apiKey;
this.application = builder.application;
this.environment = builder.environment;
this.envDetail = builder.envDetail;
}

/**
* @return A new instance of the Builder
*/
public static Builder newBuilder() {
return new Builder();
}


/**
* @return a Builder object based on current instance
*/
public Builder toBuilder() {
return newBuilder()
.apiUrl(apiUrl)
.apiKey(apiKey)
.application(application)
.environment(environment)
.envDetail(envDetail);
}

/**
* ApiConfiguration.Builder separates the construction of a ApiConfiguration from its representation
*/
public static class Builder {

/**
* The builder's apiUrl
*/
private String apiUrl;

/**
* The builder's apiKey
*/
private String apiKey;

/**
* The builder's application
*/
private String application;

/**
* The builder's environment
*/
private String environment;

/**
* The builder's envDetail
*/
private EnvironmentDetail envDetail;

/**
* Sets the builder's apiUrl
* @param apiUrl The apiUrl to be set
* @return Reference to the current object
*/
public Builder apiUrl(final String apiUrl) {
this.apiUrl = apiUrl;
return this;
}

/**
* Sets the builder's apiKey
* @param apiKey The apiKey to be set
* @return Reference to the current object
*/
public Builder apiKey(final String apiKey) {
this.apiKey = apiKey;
return this;
}

/**
* Sets the builder's application
* @param application The application to be set
* @return Reference to the current object
*/
public Builder application(final String application) {
this.application = application;
return this;
}

/**
* Sets the builder's environment
* @param environment The environment to be set
* @return Reference to the current object
*/
public Builder environment(final String environment) {
this.environment = environment;
return this;
}

/**
* Sets the builder's envDetail
* @param envDetail The envDetail to be set
* @return Reference to the current object
*/
public Builder envDetail(final EnvironmentDetail envDetail) {
this.envDetail = envDetail;
return this;
}

/**
* @return A new object constructed from this builder
*/
public ApiConfiguration build() {
return new ApiConfiguration(this);
}
}

/**
* @see java.lang.Object#toString()
* @return the apiUrl
*/
@Override
public String toString() {
return "ApiConfiguration [apiUrl=" + apiUrl + ", apiKey=" + apiKey
+ ", application=" + application + ", environment="
+ environment + ", envDetail=" + envDetail + "]";
public String getApiUrl() {
return apiUrl != null ? apiUrl : DEFAULT_API_URL;
}
}

}
6 changes: 4 additions & 2 deletions src/main/java/com/stackify/api/common/ApiConfigurations.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static ApiConfiguration fromPropertiesWithOverrides(final String apiUrl,
String mergedApiKey = ((apiKey != null) && (0 < apiKey.length())) ? apiKey : props.getApiKey();
String mergedApplication = ((application != null) && (0 < application.length())) ? application : props.getApplication();
String mergedEnvironment = ((environment != null) && (0 < environment.length())) ? environment : props.getEnvironment();

ApiConfiguration.Builder builder = ApiConfiguration.newBuilder();
builder.apiUrl(mergedApiUrl);
builder.apiKey(mergedApiKey);
Expand Down Expand Up @@ -91,12 +91,14 @@ public static ApiConfiguration fromProperties() {
String apiKey = confProps.getProperty("stackify.apiKey");
String application = confProps.getProperty("stackify.application");
String environment = confProps.getProperty("stackify.environment");

Boolean skipJson = Boolean.parseBoolean(confProps.getProperty("stackify.skipJson", "false"));

builder.apiUrl(apiUrl);
builder.apiKey(apiKey);
builder.application(application);
builder.environment(environment);
builder.envDetail(EnvironmentDetails.getEnvironmentDetail(application, environment));
builder.skipJson(skipJson);
}
}
} catch (Throwable t) {
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/stackify/api/common/log/LogAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,30 @@ public class LogAppender<T> implements Closeable {

private final Masker masker;

private final boolean skipJson;

/**
* Constructor
* @param logger Logger project name
*/
public LogAppender(@NonNull final String logger,
@NonNull final EventAdapter<T> eventAdapter,
final Masker masker) {
final Masker masker,
final boolean skipJson) {
this.logger = logger;
this.eventAdapter = eventAdapter;
this.masker = masker;
this.skipJson = skipJson;
}

/**
* Constructor
* @param logger Logger project name
*/
public LogAppender(@NonNull final String logger,
@NonNull final EventAdapter<T> eventAdapter,
final Masker masker) {
this(logger, eventAdapter, masker, false);
}

/**
Expand All @@ -101,7 +115,7 @@ public void activate(final ApiConfiguration apiConfig) {

this.collector = new LogCollector(logger, apiConfig.getEnvDetail(), appIdentityService);

LogSender sender = new LogSender(apiConfig, objectMapper, this.masker);
LogSender sender = new LogSender(apiConfig, objectMapper, this.masker, this.skipJson);

// build the background service to asynchronously post errors to Stackify
// startup the background service
Expand Down
60 changes: 54 additions & 6 deletions src/main/java/com/stackify/api/common/log/LogSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.stackify.api.common.http.HttpResendQueue;
import com.stackify.api.common.mask.Masker;
import com.stackify.api.common.util.Preconditions;
import com.stackify.api.common.util.SkipJsonUtil;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -67,29 +68,75 @@ public class LogSender {

private final Masker masker;

private final boolean skipJson;

/**
* Default constructor
* @param apiConfig API configuration
* @param objectMapper JSON object mapper
* @param masker Message Masker
* @param skipJson Messages detected w/ json will have the #SKIPJSON tag added
*/
public LogSender(@NonNull final ApiConfiguration apiConfig,
@NonNull final ObjectMapper objectMapper,
final Masker masker) {
final Masker masker,
final boolean skipJson) {
this.apiConfig = apiConfig;
this.objectMapper = objectMapper;
this.masker = masker;
this.skipJson = skipJson;
}

/**
* Default constructor
* @param apiConfig API configuration
* @param objectMapper JSON object mapper
* @param masker Message Masker
*/
public LogSender(@NonNull final ApiConfiguration apiConfig,
@NonNull final ObjectMapper objectMapper,
final Masker masker) {
this(apiConfig, objectMapper, masker, false);
}

private void executeSkipJsonTag(final LogMsgGroup group) {
if (skipJson) {
if (group.getMsgs().size() > 0) {
for (LogMsg logMsg : group.getMsgs()) {
if (logMsg.getEx() != null) {
executeSkipJsonTag(logMsg.getEx().getError());
}
logMsg.setData(SkipJsonUtil.execute(logMsg.getData()));
logMsg.setMsg(SkipJsonUtil.execute(logMsg.getMsg()));
}
}

}
}

private void executeSkipJsonTag(final ErrorItem errorItem) {
if (skipJson) {
if (errorItem != null) {
errorItem.setMessage(SkipJsonUtil.execute(errorItem.getMessage()));
if (errorItem.getData() != null) {
for (Map.Entry<String, String> entry : errorItem.getData().entrySet()) {
entry.setValue(SkipJsonUtil.execute(entry.getValue()));
}
}
executeSkipJsonTag(errorItem.getInnerError());
}
}
}

/**
* Applies masking to passed in LogMsgGroup.
*/
private void mask(final LogMsgGroup group) {
private void executeMask(final LogMsgGroup group) {
if (masker != null) {
if (group.getMsgs().size() > 0) {
for (LogMsg logMsg : group.getMsgs()) {
if (logMsg.getEx() != null) {
mask(logMsg.getEx().getError());
executeMask(logMsg.getEx().getError());
}
logMsg.setData(masker.mask(logMsg.getData()));
logMsg.setMsg(masker.mask(logMsg.getMsg()));
Expand All @@ -98,15 +145,15 @@ private void mask(final LogMsgGroup group) {
}
}

private void mask(final ErrorItem errorItem) {
private void executeMask(final ErrorItem errorItem) {
if (errorItem != null) {
errorItem.setMessage(masker.mask(errorItem.getMessage()));
if (errorItem.getData() != null) {
for (Map.Entry<String, String> entry : errorItem.getData().entrySet()) {
entry.setValue(masker.mask(entry.getValue()));
}
}
mask(errorItem.getInnerError());
executeMask(errorItem.getInnerError());
}
}

Expand All @@ -119,7 +166,8 @@ private void mask(final ErrorItem errorItem) {
public int send(final LogMsgGroup group) throws IOException {
Preconditions.checkNotNull(group);

mask(group);
executeMask(group);
executeSkipJsonTag(group);

HttpClient httpClient = new HttpClient(apiConfig);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static synchronized void startup() {

String clientName = ApiClients.getApiClient(LogManager.class, "/stackify-api-common.properties", "stackify-api-common");

LOG_APPENDER = new LogAppender<LogEvent>(clientName, new LogEventAdapter(CONFIG.getEnvDetail()), MaskerConfiguration.fromProperties());
LOG_APPENDER = new LogAppender<LogEvent>(clientName, new LogEventAdapter(CONFIG.getEnvDetail()), MaskerConfiguration.fromProperties(), CONFIG.getSkipJson());
LOG_APPENDER.activate(CONFIG);
} catch (Throwable t) {
LOGGER.error("Exception starting Stackify Log API service", t);
Expand Down
Loading