Skip to content

Commit b2f04ab

Browse files
authored
Deserialization validation (#1571)
1 parent 1fa0760 commit b2f04ab

File tree

13 files changed

+123
-51
lines changed

13 files changed

+123
-51
lines changed

disk-buffering/src/main/java/io/opentelemetry/contrib/disk/buffering/internal/exporter/FromDiskExporterImpl.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
package io.opentelemetry.contrib.disk.buffering.internal.exporter;
77

8+
import io.opentelemetry.contrib.disk.buffering.internal.serialization.deserializers.DeserializationException;
89
import io.opentelemetry.contrib.disk.buffering.internal.serialization.deserializers.SignalDeserializer;
910
import io.opentelemetry.contrib.disk.buffering.internal.storage.Storage;
11+
import io.opentelemetry.contrib.disk.buffering.internal.storage.files.reader.ProcessResult;
1012
import io.opentelemetry.contrib.disk.buffering.internal.storage.responses.ReadableResult;
1113
import io.opentelemetry.contrib.disk.buffering.internal.utils.DebugLogger;
1214
import io.opentelemetry.sdk.common.CompletableResultCode;
@@ -64,11 +66,15 @@ public boolean exportStoredBatch(long timeout, TimeUnit unit) throws IOException
6466
+ " "
6567
+ deserializer.signalType()
6668
+ " bytes from storage.");
67-
List<EXPORT_DATA> telemetry = deserializer.deserialize(bytes);
68-
logger.log(
69-
"Now exporting batch of " + telemetry.size() + " " + deserializer.signalType());
70-
CompletableResultCode join = exportFunction.apply(telemetry).join(timeout, unit);
71-
return join.isSuccess();
69+
try {
70+
List<EXPORT_DATA> telemetry = deserializer.deserialize(bytes);
71+
logger.log(
72+
"Now exporting batch of " + telemetry.size() + " " + deserializer.signalType());
73+
CompletableResultCode join = exportFunction.apply(telemetry).join(timeout, unit);
74+
return join.isSuccess() ? ProcessResult.SUCCEEDED : ProcessResult.TRY_LATER;
75+
} catch (DeserializationException e) {
76+
return ProcessResult.CONTENT_INVALID;
77+
}
7278
});
7379
return result == ReadableResult.SUCCEEDED;
7480
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.disk.buffering.internal.serialization.deserializers;
7+
8+
import java.io.IOException;
9+
10+
@SuppressWarnings("serial")
11+
public class DeserializationException extends IOException {
12+
public DeserializationException(Throwable cause) {
13+
super(cause);
14+
}
15+
}

disk-buffering/src/main/java/io/opentelemetry/contrib/disk/buffering/internal/serialization/deserializers/LogRecordDataDeserializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ static LogRecordDataDeserializer getInstance() {
2222
}
2323

2424
@Override
25-
public List<LogRecordData> deserialize(byte[] source) {
25+
public List<LogRecordData> deserialize(byte[] source) throws DeserializationException {
2626
try {
2727
return ProtoLogsDataMapper.getInstance().fromProto(LogsData.ADAPTER.decode(source));
2828
} catch (IOException e) {
29-
throw new IllegalArgumentException(e);
29+
throw new DeserializationException(e);
3030
}
3131
}
3232

disk-buffering/src/main/java/io/opentelemetry/contrib/disk/buffering/internal/serialization/deserializers/MetricDataDeserializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ static MetricDataDeserializer getInstance() {
2222
}
2323

2424
@Override
25-
public List<MetricData> deserialize(byte[] source) {
25+
public List<MetricData> deserialize(byte[] source) throws DeserializationException {
2626
try {
2727
return ProtoMetricsDataMapper.getInstance().fromProto(MetricsData.ADAPTER.decode(source));
2828
} catch (IOException e) {
29-
throw new IllegalArgumentException(e);
29+
throw new DeserializationException(e);
3030
}
3131
}
3232

disk-buffering/src/main/java/io/opentelemetry/contrib/disk/buffering/internal/serialization/deserializers/SignalDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static SignalDeserializer<LogRecordData> ofLogs() {
2525
}
2626

2727
/** Deserializes the given byte array into a list of telemetry items. */
28-
List<SDK_ITEM> deserialize(byte[] source);
28+
List<SDK_ITEM> deserialize(byte[] source) throws DeserializationException;
2929

3030
/** Returns the name of the stored type of signal -- one of "metrics", "spans", or "logs". */
3131
default String signalType() {

disk-buffering/src/main/java/io/opentelemetry/contrib/disk/buffering/internal/serialization/deserializers/SpanDataDeserializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ static SpanDataDeserializer getInstance() {
2222
}
2323

2424
@Override
25-
public List<SpanData> deserialize(byte[] source) {
25+
public List<SpanData> deserialize(byte[] source) throws DeserializationException {
2626
try {
2727
return ProtoSpansDataMapper.getInstance().fromProto(TracesData.ADAPTER.decode(source));
2828
} catch (IOException e) {
29-
throw new IllegalArgumentException(e);
29+
throw new DeserializationException(e);
3030
}
3131
}
3232

disk-buffering/src/main/java/io/opentelemetry/contrib/disk/buffering/internal/storage/Storage.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.opentelemetry.contrib.disk.buffering.internal.exporter.FromDiskExporterImpl;
1111
import io.opentelemetry.contrib.disk.buffering.internal.storage.files.ReadableFile;
1212
import io.opentelemetry.contrib.disk.buffering.internal.storage.files.WritableFile;
13+
import io.opentelemetry.contrib.disk.buffering.internal.storage.files.reader.ProcessResult;
1314
import io.opentelemetry.contrib.disk.buffering.internal.storage.responses.ReadableResult;
1415
import io.opentelemetry.contrib.disk.buffering.internal.storage.responses.WritableResult;
1516
import io.opentelemetry.contrib.disk.buffering.internal.utils.DebugLogger;
@@ -77,12 +78,13 @@ private boolean write(byte[] item, int attemptNumber) throws IOException {
7778
* @param processing Is passed over to {@link ReadableFile#readAndProcess(Function)}.
7879
* @throws IOException If an unexpected error happens.
7980
*/
80-
public ReadableResult readAndProcess(Function<byte[], Boolean> processing) throws IOException {
81+
public ReadableResult readAndProcess(Function<byte[], ProcessResult> processing)
82+
throws IOException {
8183
return readAndProcess(processing, 1);
8284
}
8385

84-
private ReadableResult readAndProcess(Function<byte[], Boolean> processing, int attemptNumber)
85-
throws IOException {
86+
private ReadableResult readAndProcess(
87+
Function<byte[], ProcessResult> processing, int attemptNumber) throws IOException {
8688
if (isClosed.get()) {
8789
logger.log("Refusing to read from storage after being closed.");
8890
return ReadableResult.FAILED;
@@ -103,7 +105,7 @@ private ReadableResult readAndProcess(Function<byte[], Boolean> processing, int
103105
ReadableResult result = readableFile.readAndProcess(processing);
104106
switch (result) {
105107
case SUCCEEDED:
106-
case PROCESSING_FAILED:
108+
case TRY_LATER:
107109
return result;
108110
default:
109111
// Retry with new file

disk-buffering/src/main/java/io/opentelemetry/contrib/disk/buffering/internal/storage/files/ReadableFile.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import io.opentelemetry.contrib.disk.buffering.StorageConfiguration;
1111
import io.opentelemetry.contrib.disk.buffering.internal.storage.files.reader.DelimitedProtoStreamReader;
12+
import io.opentelemetry.contrib.disk.buffering.internal.storage.files.reader.ProcessResult;
1213
import io.opentelemetry.contrib.disk.buffering.internal.storage.files.reader.ReadResult;
1314
import io.opentelemetry.contrib.disk.buffering.internal.storage.files.reader.StreamReader;
1415
import io.opentelemetry.contrib.disk.buffering.internal.storage.files.utils.FileTransferUtil;
@@ -83,7 +84,7 @@ public ReadableFile(
8384
* If the processing function returns TRUE, then the provided line will be deleted from the
8485
* source file. If the function returns FALSE, no changes will be applied to the source file.
8586
*/
86-
public synchronized ReadableResult readAndProcess(Function<byte[], Boolean> processing)
87+
public synchronized ReadableResult readAndProcess(Function<byte[], ProcessResult> processing)
8788
throws IOException {
8889
if (isClosed.get()) {
8990
return ReadableResult.FAILED;
@@ -97,20 +98,25 @@ public synchronized ReadableResult readAndProcess(Function<byte[], Boolean> proc
9798
cleanUp();
9899
return ReadableResult.FAILED;
99100
}
100-
if (processing.apply(read.content)) {
101-
unconsumedResult = null;
102-
readBytes += read.totalReadLength;
103-
int amountOfBytesToTransfer = originalFileSize - readBytes;
104-
if (amountOfBytesToTransfer > 0) {
105-
fileTransferUtil.transferBytes(readBytes, amountOfBytesToTransfer);
106-
} else {
101+
switch (processing.apply(read.content)) {
102+
case SUCCEEDED:
103+
unconsumedResult = null;
104+
readBytes += read.totalReadLength;
105+
int amountOfBytesToTransfer = originalFileSize - readBytes;
106+
if (amountOfBytesToTransfer > 0) {
107+
fileTransferUtil.transferBytes(readBytes, amountOfBytesToTransfer);
108+
} else {
109+
cleanUp();
110+
}
111+
return ReadableResult.SUCCEEDED;
112+
case TRY_LATER:
113+
unconsumedResult = read;
114+
return ReadableResult.TRY_LATER;
115+
case CONTENT_INVALID:
107116
cleanUp();
108-
}
109-
return ReadableResult.SUCCEEDED;
110-
} else {
111-
unconsumedResult = read;
112-
return ReadableResult.PROCESSING_FAILED;
117+
return ReadableResult.FAILED;
113118
}
119+
return ReadableResult.FAILED;
114120
}
115121

116122
@Nullable
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.disk.buffering.internal.storage.files.reader;
7+
8+
/** Result of processing the contents of a file. */
9+
public enum ProcessResult {
10+
SUCCEEDED,
11+
TRY_LATER,
12+
CONTENT_INVALID
13+
}

disk-buffering/src/main/java/io/opentelemetry/contrib/disk/buffering/internal/storage/responses/ReadableResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
public enum ReadableResult {
99
SUCCEEDED,
1010
FAILED,
11-
PROCESSING_FAILED
11+
TRY_LATER
1212
}

0 commit comments

Comments
 (0)