Skip to content

Commit ce54ff1

Browse files
committed
Merge branch 'main' of github.com:splunk/splunk-library-javalogging into ci_updates
� Conflicts: � .gitignore
2 parents 05124f0 + 5ad4fa6 commit ce54ff1

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/main/java/com/splunk/logging/HttpEventCollectorLog4jAppender.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717

1818
import java.io.Serializable;
19-
import java.nio.charset.Charset;
2019
import java.nio.charset.StandardCharsets;
2120
import java.util.HashMap;
2221
import java.util.Map;
@@ -162,6 +161,19 @@ public static HttpEventCollectorLog4jAppender createAppender(
162161
@PluginElement("Filter") final Filter filter
163162
)
164163
{
164+
// The raw endpoint presumes that a single post is a single event.
165+
// The batch size should be 1 if "type" is raw, and we should error if batch
166+
// configuration is specified.
167+
int clampedBatchCountDefault = HttpEventCollectorSender.DefaultBatchCount;
168+
169+
if ("raw".equalsIgnoreCase(type)) {
170+
if (batchSize != null || batchCount != null || batchInterval != null) {
171+
LOGGER.error("batch configuration is not compatible with the raw endpoint");
172+
return null;
173+
}
174+
clampedBatchCountDefault = 1;
175+
}
176+
165177
if (name == null)
166178
{
167179
LOGGER.error("No name provided for HttpEventCollectorLog4jAppender");
@@ -199,7 +211,7 @@ public static HttpEventCollectorLog4jAppender createAppender(
199211
includeLoggerName, includeThreadName, includeMDC, includeException, includeMarker,
200212
ignoreExceptionsBool,
201213
parseInt(batchInterval, HttpEventCollectorSender.DefaultBatchInterval),
202-
parseInt(batchCount, HttpEventCollectorSender.DefaultBatchCount),
214+
parseInt(batchCount, clampedBatchCountDefault),
203215
parseInt(batchSize, HttpEventCollectorSender.DefaultBatchSize),
204216
parseInt(retriesOnError, 0),
205217
sendMode,

src/main/java/com/splunk/logging/HttpEventCollectorLogbackAppender.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class HttpEventCollectorLogbackAppender<E> extends AppenderBase<E> {
5353
private String _sendMode;
5454
private long _retriesOnError = 0;
5555
private Map<String, String> _metadata = new HashMap<>();
56+
private boolean _batchingConfigured = false;
5657

5758

5859
private HttpEventCollectorSender.TimeoutSettings timeoutSettings = new HttpEventCollectorSender.TimeoutSettings();
@@ -79,6 +80,11 @@ public void start() {
7980
if (_messageFormat != null)
8081
metadata.put(MetadataTags.MESSAGEFORMAT, _messageFormat);
8182

83+
// This should have been caught at configuration time, but double-check at start
84+
if ("raw".equalsIgnoreCase(_type) && _batchingConfigured) {
85+
throw new IllegalArgumentException("Batching configuration and sending type of raw are incompatible.");
86+
}
87+
8288
this.sender = new HttpEventCollectorSender(
8389
_url, _token, _channel, _type, _batchInterval, _batchCount, _batchSize, _sendMode, metadata, timeoutSettings);
8490

@@ -188,6 +194,18 @@ public String getChannel() {
188194

189195
public void setType(String type) {
190196
this._type = type;
197+
if ("raw".equalsIgnoreCase(type)) {
198+
validateNotBatchedAndRaw();
199+
this._batchCount = 1; // Enforce sending on every event
200+
}
201+
}
202+
203+
private void validateNotBatchedAndRaw() {
204+
if ("raw".equalsIgnoreCase(_type)) {
205+
if (_batchingConfigured) {
206+
throw new IllegalArgumentException("Batching configuration and sending type of raw are incompatible.");
207+
}
208+
}
191209
}
192210

193211
public String getType() {
@@ -292,14 +310,20 @@ public void setDisableCertificateValidation(String disableCertificateValidation)
292310

293311
public void setbatch_size_count(String value) {
294312
_batchCount = parseLong(value, HttpEventCollectorSender.DefaultBatchCount);
313+
_batchingConfigured = true;
314+
validateNotBatchedAndRaw();
295315
}
296316

297317
public void setbatch_size_bytes(String value) {
298318
_batchSize = parseLong(value, HttpEventCollectorSender.DefaultBatchSize);
319+
_batchingConfigured = true;
320+
validateNotBatchedAndRaw();
299321
}
300322

301323
public void setbatch_interval(String value) {
302324
_batchInterval = parseLong(value, HttpEventCollectorSender.DefaultBatchInterval);
325+
_batchingConfigured = true;
326+
validateNotBatchedAndRaw();
303327
}
304328

305329
public void setretries_on_error(String value) {

src/main/java/com/splunk/logging/HttpEventCollectorLoggingHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ public HttpEventCollectorLoggingHandler() {
168168
getConfigurationNumericProperty(WriteTimeoutConfTag, HttpEventCollectorSender.TimeoutSettings.DEFAULT_WRITE_TIMEOUT)
169169
);
170170

171+
if ("raw".equalsIgnoreCase(type)) {
172+
if (batchCount != HttpEventCollectorSender.DefaultBatchCount
173+
|| batchSize != HttpEventCollectorSender.DefaultBatchSize
174+
|| delay != HttpEventCollectorSender.DefaultBatchInterval) {
175+
throw new IllegalArgumentException("Batching configuration and sending type of raw are incompatible.");
176+
}
177+
batchCount = 1;
178+
}
179+
171180
// delegate all configuration params to event sender
172181
this.sender = new HttpEventCollectorSender(
173182
url, token, channel, type, delay, batchCount, batchSize, sendMode, metadata, timeoutSettings);

src/main/java/com/splunk/logging/HttpEventCollectorSender.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.io.Serializable;
3030
import java.security.cert.CertificateException;
3131
import java.util.*;
32-
import java.util.concurrent.TimeUnit;
3332
import java.util.stream.Collectors;
3433

3534

@@ -112,9 +111,13 @@ public HttpEventCollectorSender(
112111

113112
if ("Raw".equalsIgnoreCase(type)) {
114113
if (channel == null || channel.trim().equals("")) {
115-
throw new IllegalArgumentException("Channel cannot be null or empty");
114+
this.channel = UUID.randomUUID().toString();
116115
}
117-
HttpUrl.Builder urlBuilder = HttpUrl.parse(Url + HttpRawCollectorUriPath)
116+
HttpUrl fullUrl = HttpUrl.parse(Url + HttpRawCollectorUriPath);
117+
if (fullUrl == null) {
118+
throw new IllegalArgumentException(String.format("Unparseable URL argument: %s", Url + HttpEventCollectorUriPath));
119+
}
120+
HttpUrl.Builder urlBuilder = fullUrl
118121
.newBuilder()
119122
.addQueryParameter(ChannelQueryParam, channel);
120123
metadata.forEach(urlBuilder::addQueryParameter);

0 commit comments

Comments
 (0)