Skip to content

Commit c073669

Browse files
authored
Merge pull request #11 from stackify/RT-471
RT-471 - adding in a skipJson toggle to add #SKIPJSON tag to any mess…
2 parents e2d7644 + d904818 commit c073669

8 files changed

Lines changed: 133 additions & 172 deletions

File tree

src/main/java/com/stackify/api/common/ApiConfiguration.java

Lines changed: 13 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
package com.stackify.api.common;
1717

1818
import com.stackify.api.EnvironmentDetail;
19+
import lombok.Builder;
20+
import lombok.Getter;
21+
import lombok.ToString;
1922

2023
/**
2124
* ApiConfiguration
2225
* @author Eric Martin
2326
*/
27+
@ToString
28+
@Getter
29+
@Builder(builderClassName = "Builder", toBuilder = true, builderMethodName = "newBuilder")
2430
public class ApiConfiguration {
2531

2632
/**
@@ -54,166 +60,15 @@ public class ApiConfiguration {
5460
private final EnvironmentDetail envDetail;
5561

5662
/**
57-
* @return the apiUrl
58-
*/
59-
public String getApiUrl() {
60-
return apiUrl != null ? apiUrl : DEFAULT_API_URL;
61-
}
62-
63-
/**
64-
* @return the apiKey
65-
*/
66-
public String getApiKey() {
67-
return apiKey;
68-
}
69-
70-
/**
71-
* @return the application
72-
*/
73-
public String getApplication() {
74-
return application;
75-
}
76-
77-
/**
78-
* @return the environment
63+
* Add #SKIPJSON tag to messages containing Json
7964
*/
80-
public String getEnvironment() {
81-
return environment;
82-
}
83-
84-
/**
85-
* @return the envDetail
86-
*/
87-
public EnvironmentDetail getEnvDetail() {
88-
return envDetail;
89-
}
65+
private final Boolean skipJson;
9066

9167
/**
92-
* @param builder The Builder object that contains all of the values for initialization
93-
*/
94-
private ApiConfiguration(final Builder builder) {
95-
this.apiUrl = builder.apiUrl;
96-
this.apiKey = builder.apiKey;
97-
this.application = builder.application;
98-
this.environment = builder.environment;
99-
this.envDetail = builder.envDetail;
100-
}
101-
102-
/**
103-
* @return A new instance of the Builder
104-
*/
105-
public static Builder newBuilder() {
106-
return new Builder();
107-
}
108-
109-
110-
/**
111-
* @return a Builder object based on current instance
112-
*/
113-
public Builder toBuilder() {
114-
return newBuilder()
115-
.apiUrl(apiUrl)
116-
.apiKey(apiKey)
117-
.application(application)
118-
.environment(environment)
119-
.envDetail(envDetail);
120-
}
121-
122-
/**
123-
* ApiConfiguration.Builder separates the construction of a ApiConfiguration from its representation
124-
*/
125-
public static class Builder {
126-
127-
/**
128-
* The builder's apiUrl
129-
*/
130-
private String apiUrl;
131-
132-
/**
133-
* The builder's apiKey
134-
*/
135-
private String apiKey;
136-
137-
/**
138-
* The builder's application
139-
*/
140-
private String application;
141-
142-
/**
143-
* The builder's environment
144-
*/
145-
private String environment;
146-
147-
/**
148-
* The builder's envDetail
149-
*/
150-
private EnvironmentDetail envDetail;
151-
152-
/**
153-
* Sets the builder's apiUrl
154-
* @param apiUrl The apiUrl to be set
155-
* @return Reference to the current object
156-
*/
157-
public Builder apiUrl(final String apiUrl) {
158-
this.apiUrl = apiUrl;
159-
return this;
160-
}
161-
162-
/**
163-
* Sets the builder's apiKey
164-
* @param apiKey The apiKey to be set
165-
* @return Reference to the current object
166-
*/
167-
public Builder apiKey(final String apiKey) {
168-
this.apiKey = apiKey;
169-
return this;
170-
}
171-
172-
/**
173-
* Sets the builder's application
174-
* @param application The application to be set
175-
* @return Reference to the current object
176-
*/
177-
public Builder application(final String application) {
178-
this.application = application;
179-
return this;
180-
}
181-
182-
/**
183-
* Sets the builder's environment
184-
* @param environment The environment to be set
185-
* @return Reference to the current object
186-
*/
187-
public Builder environment(final String environment) {
188-
this.environment = environment;
189-
return this;
190-
}
191-
192-
/**
193-
* Sets the builder's envDetail
194-
* @param envDetail The envDetail to be set
195-
* @return Reference to the current object
196-
*/
197-
public Builder envDetail(final EnvironmentDetail envDetail) {
198-
this.envDetail = envDetail;
199-
return this;
200-
}
201-
202-
/**
203-
* @return A new object constructed from this builder
204-
*/
205-
public ApiConfiguration build() {
206-
return new ApiConfiguration(this);
207-
}
208-
}
209-
210-
/**
211-
* @see java.lang.Object#toString()
68+
* @return the apiUrl
21269
*/
213-
@Override
214-
public String toString() {
215-
return "ApiConfiguration [apiUrl=" + apiUrl + ", apiKey=" + apiKey
216-
+ ", application=" + application + ", environment="
217-
+ environment + ", envDetail=" + envDetail + "]";
70+
public String getApiUrl() {
71+
return apiUrl != null ? apiUrl : DEFAULT_API_URL;
21872
}
219-
}
73+
74+
}

src/main/java/com/stackify/api/common/ApiConfigurations.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static ApiConfiguration fromPropertiesWithOverrides(final String apiUrl,
4949
String mergedApiKey = ((apiKey != null) && (0 < apiKey.length())) ? apiKey : props.getApiKey();
5050
String mergedApplication = ((application != null) && (0 < application.length())) ? application : props.getApplication();
5151
String mergedEnvironment = ((environment != null) && (0 < environment.length())) ? environment : props.getEnvironment();
52-
52+
5353
ApiConfiguration.Builder builder = ApiConfiguration.newBuilder();
5454
builder.apiUrl(mergedApiUrl);
5555
builder.apiKey(mergedApiKey);
@@ -91,12 +91,14 @@ public static ApiConfiguration fromProperties() {
9191
String apiKey = confProps.getProperty("stackify.apiKey");
9292
String application = confProps.getProperty("stackify.application");
9393
String environment = confProps.getProperty("stackify.environment");
94-
94+
Boolean skipJson = Boolean.parseBoolean(confProps.getProperty("stackify.skipJson", "false"));
95+
9596
builder.apiUrl(apiUrl);
9697
builder.apiKey(apiKey);
9798
builder.application(application);
9899
builder.environment(environment);
99100
builder.envDetail(EnvironmentDetails.getEnvironmentDetail(application, environment));
101+
builder.skipJson(skipJson);
100102
}
101103
}
102104
} catch (Throwable t) {

src/main/java/com/stackify/api/common/log/LogAppender.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,30 @@ public class LogAppender<T> implements Closeable {
6666

6767
private final Masker masker;
6868

69+
private final boolean skipJson;
70+
6971
/**
7072
* Constructor
7173
* @param logger Logger project name
7274
*/
7375
public LogAppender(@NonNull final String logger,
7476
@NonNull final EventAdapter<T> eventAdapter,
75-
final Masker masker) {
77+
final Masker masker,
78+
final boolean skipJson) {
7679
this.logger = logger;
7780
this.eventAdapter = eventAdapter;
7881
this.masker = masker;
82+
this.skipJson = skipJson;
83+
}
84+
85+
/**
86+
* Constructor
87+
* @param logger Logger project name
88+
*/
89+
public LogAppender(@NonNull final String logger,
90+
@NonNull final EventAdapter<T> eventAdapter,
91+
final Masker masker) {
92+
this(logger, eventAdapter, masker, false);
7993
}
8094

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

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

104-
LogSender sender = new LogSender(apiConfig, objectMapper, this.masker);
118+
LogSender sender = new LogSender(apiConfig, objectMapper, this.masker, this.skipJson);
105119

106120
// build the background service to asynchronously post errors to Stackify
107121
// startup the background service

src/main/java/com/stackify/api/common/log/LogSender.java

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.stackify.api.common.http.HttpResendQueue;
2626
import com.stackify.api.common.mask.Masker;
2727
import com.stackify.api.common.util.Preconditions;
28+
import com.stackify.api.common.util.SkipJsonUtil;
2829
import lombok.NonNull;
2930
import org.slf4j.Logger;
3031
import org.slf4j.LoggerFactory;
@@ -67,29 +68,75 @@ public class LogSender {
6768

6869
private final Masker masker;
6970

71+
private final boolean skipJson;
72+
7073
/**
7174
* Default constructor
7275
* @param apiConfig API configuration
7376
* @param objectMapper JSON object mapper
7477
* @param masker Message Masker
78+
* @param skipJson Messages detected w/ json will have the #SKIPJSON tag added
7579
*/
7680
public LogSender(@NonNull final ApiConfiguration apiConfig,
7781
@NonNull final ObjectMapper objectMapper,
78-
final Masker masker) {
82+
final Masker masker,
83+
final boolean skipJson) {
7984
this.apiConfig = apiConfig;
8085
this.objectMapper = objectMapper;
8186
this.masker = masker;
87+
this.skipJson = skipJson;
88+
}
89+
90+
/**
91+
* Default constructor
92+
* @param apiConfig API configuration
93+
* @param objectMapper JSON object mapper
94+
* @param masker Message Masker
95+
*/
96+
public LogSender(@NonNull final ApiConfiguration apiConfig,
97+
@NonNull final ObjectMapper objectMapper,
98+
final Masker masker) {
99+
this(apiConfig, objectMapper, masker, false);
100+
}
101+
102+
private void executeSkipJsonTag(final LogMsgGroup group) {
103+
if (skipJson) {
104+
if (group.getMsgs().size() > 0) {
105+
for (LogMsg logMsg : group.getMsgs()) {
106+
if (logMsg.getEx() != null) {
107+
executeSkipJsonTag(logMsg.getEx().getError());
108+
}
109+
logMsg.setData(SkipJsonUtil.execute(logMsg.getData()));
110+
logMsg.setMsg(SkipJsonUtil.execute(logMsg.getMsg()));
111+
}
112+
}
113+
114+
}
115+
}
116+
117+
private void executeSkipJsonTag(final ErrorItem errorItem) {
118+
if (skipJson) {
119+
if (errorItem != null) {
120+
errorItem.setMessage(SkipJsonUtil.execute(errorItem.getMessage()));
121+
if (errorItem.getData() != null) {
122+
for (Map.Entry<String, String> entry : errorItem.getData().entrySet()) {
123+
entry.setValue(SkipJsonUtil.execute(entry.getValue()));
124+
}
125+
}
126+
executeSkipJsonTag(errorItem.getInnerError());
127+
}
128+
}
82129
}
83130

84131
/**
85132
* Applies masking to passed in LogMsgGroup.
86133
*/
87-
private void mask(final LogMsgGroup group) {
134+
private void executeMask(final LogMsgGroup group) {
88135
if (masker != null) {
89136
if (group.getMsgs().size() > 0) {
90137
for (LogMsg logMsg : group.getMsgs()) {
91138
if (logMsg.getEx() != null) {
92-
mask(logMsg.getEx().getError());
139+
executeMask(logMsg.getEx().getError());
93140
}
94141
logMsg.setData(masker.mask(logMsg.getData()));
95142
logMsg.setMsg(masker.mask(logMsg.getMsg()));
@@ -98,15 +145,15 @@ private void mask(final LogMsgGroup group) {
98145
}
99146
}
100147

101-
private void mask(final ErrorItem errorItem) {
148+
private void executeMask(final ErrorItem errorItem) {
102149
if (errorItem != null) {
103150
errorItem.setMessage(masker.mask(errorItem.getMessage()));
104151
if (errorItem.getData() != null) {
105152
for (Map.Entry<String, String> entry : errorItem.getData().entrySet()) {
106153
entry.setValue(masker.mask(entry.getValue()));
107154
}
108155
}
109-
mask(errorItem.getInnerError());
156+
executeMask(errorItem.getInnerError());
110157
}
111158
}
112159

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

122-
mask(group);
169+
executeMask(group);
170+
executeSkipJsonTag(group);
123171

124172
HttpClient httpClient = new HttpClient(apiConfig);
125173

src/main/java/com/stackify/api/common/log/direct/LogManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private static synchronized void startup() {
7171

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

74-
LOG_APPENDER = new LogAppender<LogEvent>(clientName, new LogEventAdapter(CONFIG.getEnvDetail()), MaskerConfiguration.fromProperties());
74+
LOG_APPENDER = new LogAppender<LogEvent>(clientName, new LogEventAdapter(CONFIG.getEnvDetail()), MaskerConfiguration.fromProperties(), CONFIG.getSkipJson());
7575
LOG_APPENDER.activate(CONFIG);
7676
} catch (Throwable t) {
7777
LOGGER.error("Exception starting Stackify Log API service", t);

0 commit comments

Comments
 (0)