Skip to content

Commit 8ed10f2

Browse files
authored
OTLP exporter should tolerate instances of LogRecordData when incubator is present (#7393)
1 parent 5e50aa7 commit 8ed10f2

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/IncubatingUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,28 @@
2828
*/
2929
public class IncubatingUtil {
3030

31+
private static final boolean INCUBATOR_AVAILABLE;
32+
33+
static {
34+
boolean incubatorAvailable = false;
35+
try {
36+
Class.forName("io.opentelemetry.api.incubator.common.ExtendedAttributes");
37+
incubatorAvailable = true;
38+
} catch (ClassNotFoundException e) {
39+
// Not available
40+
}
41+
INCUBATOR_AVAILABLE = incubatorAvailable;
42+
}
43+
3144
private static final byte[] EMPTY_BYTES = new byte[0];
3245
private static final KeyValueMarshaler[] EMPTY_REPEATED = new KeyValueMarshaler[0];
3346

3447
private IncubatingUtil() {}
3548

49+
public static boolean isExtendedLogRecordData(LogRecordData logRecordData) {
50+
return INCUBATOR_AVAILABLE && logRecordData instanceof ExtendedLogRecordData;
51+
}
52+
3653
@SuppressWarnings("AvoidObjectArrays")
3754
public static KeyValueMarshaler[] createdExtendedAttributesMarhsalers(
3855
LogRecordData logRecordData) {

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogMarshaler.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,6 @@
2424
import javax.annotation.Nullable;
2525

2626
final class LogMarshaler extends MarshalerWithSize {
27-
private static final boolean INCUBATOR_AVAILABLE;
28-
29-
static {
30-
boolean incubatorAvailable = false;
31-
try {
32-
Class.forName("io.opentelemetry.api.incubator.common.ExtendedAttributes");
33-
incubatorAvailable = true;
34-
} catch (ClassNotFoundException e) {
35-
// Not available
36-
}
37-
INCUBATOR_AVAILABLE = incubatorAvailable;
38-
}
39-
4027
private static final String INVALID_TRACE_ID = TraceId.getInvalid();
4128
private static final String INVALID_SPAN_ID = SpanId.getInvalid();
4229

@@ -54,12 +41,12 @@ final class LogMarshaler extends MarshalerWithSize {
5441

5542
static LogMarshaler create(LogRecordData logRecordData) {
5643
KeyValueMarshaler[] attributeMarshalers =
57-
INCUBATOR_AVAILABLE
44+
IncubatingUtil.isExtendedLogRecordData(logRecordData)
5845
? IncubatingUtil.createdExtendedAttributesMarhsalers(logRecordData)
5946
: KeyValueMarshaler.createForAttributes(logRecordData.getAttributes());
6047

6148
int attributeSize =
62-
INCUBATOR_AVAILABLE
49+
IncubatingUtil.isExtendedLogRecordData(logRecordData)
6350
? IncubatingUtil.extendedAttributesSize(logRecordData)
6451
: logRecordData.getAttributes().size();
6552

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogStatelessMarshaler.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@ final class LogStatelessMarshaler implements StatelessMarshaler<LogRecordData> {
2727

2828
private static final String INVALID_TRACE_ID = TraceId.getInvalid();
2929
private static final String INVALID_SPAN_ID = SpanId.getInvalid();
30-
private static final boolean INCUBATOR_AVAILABLE;
31-
32-
static {
33-
boolean incubatorAvailable = false;
34-
try {
35-
Class.forName("io.opentelemetry.api.incubator.common.ExtendedAttributes");
36-
incubatorAvailable = true;
37-
} catch (ClassNotFoundException e) {
38-
// Not available
39-
}
40-
INCUBATOR_AVAILABLE = incubatorAvailable;
41-
}
4230

4331
static final LogStatelessMarshaler INSTANCE = new LogStatelessMarshaler();
4432

@@ -56,7 +44,7 @@ public void writeTo(Serializer output, LogRecordData log, MarshalerContext conte
5644
}
5745

5846
int droppedAttributesCount;
59-
if (INCUBATOR_AVAILABLE) {
47+
if (IncubatingUtil.isExtendedLogRecordData(log)) {
6048
IncubatingUtil.serializeExtendedAttributes(output, log, context);
6149
droppedAttributesCount =
6250
log.getTotalAttributeCount() - IncubatingUtil.extendedAttributesSize(log);
@@ -99,7 +87,7 @@ public int getBinarySerializedSize(LogRecordData log, MarshalerContext context)
9987
StatelessMarshalerUtil.sizeMessageWithContext(
10088
LogRecord.BODY, log.getBodyValue(), AnyValueStatelessMarshaler.INSTANCE, context);
10189
}
102-
if (INCUBATOR_AVAILABLE) {
90+
if (IncubatingUtil.isExtendedLogRecordData(log)) {
10391
size += IncubatingUtil.sizeExtendedAttributes(log, context);
10492

10593
int droppedAttributesCount =

exporters/otlp/common/src/testIncubating/java/io/opentelemetry/exporter/internal/otlp/logs/LogsRequestMarshalerIncubatingTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.exporter.internal.otlp.logs;
77

88
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.assertj.core.api.Assertions.assertThatCode;
910

1011
import com.google.protobuf.ByteString;
1112
import com.google.protobuf.InvalidProtocolBufferException;
@@ -34,6 +35,7 @@
3435
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
3536
import io.opentelemetry.sdk.logs.data.LogRecordData;
3637
import io.opentelemetry.sdk.resources.Resource;
38+
import io.opentelemetry.sdk.testing.logs.TestLogRecordData;
3739
import io.opentelemetry.sdk.testing.logs.internal.TestExtendedLogRecordData;
3840
import java.io.ByteArrayOutputStream;
3941
import java.io.IOException;
@@ -56,6 +58,20 @@ class LogsRequestMarshalerIncubatingTest {
5658
private static final String EVENT_NAME = "hello";
5759
private static final String BODY = "Hello world from this log...";
5860

61+
// Marshalers should not throw if the incubator is on the class path, but are called with
62+
// LogRecordData instances which are not ExtendedLogRecordData.
63+
// See: https://github.com/open-telemetry/opentelemetry-java/issues/7363
64+
@ParameterizedTest
65+
@EnumSource(MarshalerSource.class)
66+
void toProtoLogRecord_NotExtendedLogRecordData_DoesNotThrow(MarshalerSource marshalerSource) {
67+
assertThatCode(
68+
() ->
69+
parse(
70+
LogRecord.getDefaultInstance(),
71+
marshalerSource.create(TestLogRecordData.builder().build())))
72+
.doesNotThrowAnyException();
73+
}
74+
5975
@ParameterizedTest
6076
@EnumSource(MarshalerSource.class)
6177
void toProtoLogRecord(MarshalerSource marshalerSource) {

0 commit comments

Comments
 (0)