Skip to content

Commit 8da03f0

Browse files
committed
Fix log total attribute count conversion
1 parent bd07563 commit 8da03f0

3 files changed

Lines changed: 27 additions & 33 deletions

File tree

instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/testing/AbstractJdbcInstrumentationTest.java

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@
1616
import static io.opentelemetry.instrumentation.testing.util.TestLatestDeps.testLatestDeps;
1717
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
1818
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
19-
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
2019
import static io.opentelemetry.semconv.DbAttributes.DB_NAMESPACE;
2120
import static io.opentelemetry.semconv.DbAttributes.DB_OPERATION_BATCH_SIZE;
2221
import static io.opentelemetry.semconv.DbAttributes.DB_QUERY_SUMMARY;
2322
import static io.opentelemetry.semconv.DbAttributes.DB_STORED_PROCEDURE_NAME;
2423
import static io.opentelemetry.semconv.DbAttributes.DB_SYSTEM_NAME;
25-
import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_MESSAGE;
26-
import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_STACKTRACE;
27-
import static io.opentelemetry.semconv.ExceptionAttributes.EXCEPTION_TYPE;
2824
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
2925
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_CONNECTION_STRING;
3026
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_NAME;
@@ -36,7 +32,6 @@
3632
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DbSystemNameIncubatingValues.HSQLDB;
3733
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DbSystemNameIncubatingValues.OTHER_SQL;
3834
import static java.util.Arrays.asList;
39-
import static java.util.stream.Collectors.toList;
4035
import static org.assertj.core.api.Assertions.assertThat;
4136
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4237
import static org.assertj.core.api.Assertions.catchThrowable;
@@ -52,7 +47,6 @@
5247
import io.opentelemetry.instrumentation.jdbc.TestDriver;
5348
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
5449
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
55-
import io.opentelemetry.sdk.logs.data.LogRecordData;
5650
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
5751
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
5852
import io.opentelemetry.sdk.testing.assertj.TraceAssert;
@@ -78,7 +72,6 @@
7872
import org.apache.derby.jdbc.EmbeddedDataSource;
7973
import org.apache.derby.jdbc.EmbeddedDriver;
8074
import org.assertj.core.api.ThrowingConsumer;
81-
import org.awaitility.Awaitility;
8275
import org.h2.jdbcx.JdbcDataSource;
8376
import org.hsqldb.jdbc.JDBCDriver;
8477
import org.junit.jupiter.api.BeforeAll;
@@ -459,30 +452,23 @@ void testFailedStatement() throws SQLException {
459452
.hasException(emitExceptionAsSpanEvents() ? error : null)));
460453

461454
if (emitExceptionAsLogs()) {
462-
assertExceptionLog();
455+
testing()
456+
.waitAndAssertLogRecords(
457+
logRecord ->
458+
logRecord
459+
.hasSeverity(Severity.WARN)
460+
.hasEventName("db.client.operation.exception")
461+
.hasException(error)
462+
.hasTotalAttributeCount(3),
463+
logRecord ->
464+
logRecord
465+
.hasSeverity(Severity.WARN)
466+
.hasEventName("exception")
467+
.hasException(error)
468+
.hasTotalAttributeCount(3));
463469
}
464470
}
465471

466-
private void assertExceptionLog() {
467-
Awaitility.await()
468-
.untilAsserted(
469-
() -> {
470-
List<LogRecordData> logs =
471-
testing().logRecords().stream()
472-
.filter(log -> "db.client.operation.exception".equals(log.getEventName()))
473-
.collect(toList());
474-
475-
assertThat(logs).hasSize(1);
476-
assertThat(logs.get(0))
477-
.hasSeverity(Severity.WARN)
478-
.hasEventName("db.client.operation.exception")
479-
.hasAttributesSatisfyingExactly(
480-
satisfies(EXCEPTION_TYPE, val -> val.isNotNull()),
481-
satisfies(EXCEPTION_MESSAGE, val -> val.isNotNull()),
482-
satisfies(EXCEPTION_STACKTRACE, val -> val.isNotNull()));
483-
});
484-
}
485-
486472
static Stream<Arguments> preparedStatementStream() throws SQLException {
487473
return Stream.of(
488474
Arguments.of(

testing-common/src/main/java/io/opentelemetry/instrumentation/testing/internal/TelemetryConverter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,9 @@ private static LogRecordData createLogData(
313313
TraceState.getDefault())) // logs proto doesn't have trace state
314314
.setSeverity(fromProto(logRecord.getSeverityNumber()))
315315
.setSeverityText(logRecord.getSeverityText())
316-
.setAttributes(fromProto(logRecord.getAttributesList()));
316+
.setAttributes(fromProto(logRecord.getAttributesList()))
317+
.setTotalAttributeCount(
318+
logRecord.getAttributesCount() + logRecord.getDroppedAttributesCount());
317319
if (canUseValue) {
318320
builder.setBodyValue(getBodyValue(logRecord.getBody()));
319321
} else {
@@ -340,7 +342,9 @@ private static LogRecordData createExtendedLogData(
340342
.setSeverity(fromProto(logRecord.getSeverityNumber()))
341343
.setSeverityText(logRecord.getSeverityText())
342344
.setEventName(logRecord.getEventName())
343-
.setBodyValue(getBodyValue(logRecord.getBody()));
345+
.setBodyValue(getBodyValue(logRecord.getBody()))
346+
.setTotalAttributeCount(
347+
logRecord.getAttributesCount() + logRecord.getDroppedAttributesCount());
344348
if (hasExtendedAttributes) {
345349
builder.setExtendedAttributes(fromProtoExtended(logRecord.getAttributesList()));
346350
} else {

testing/agent-for-testing/src/test/java/io/opentelemetry/javaagent/testing/AgentForTestingTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.javaagent.testing;
77

8+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
89
import static org.assertj.core.api.Assertions.assertThat;
910

1011
import io.opentelemetry.api.GlobalOpenTelemetry;
@@ -51,10 +52,13 @@ void exportAndRetrieveMetrics() {
5152
@Test
5253
void exportAndRetrieveLogRecords() {
5354
Logger logger = GlobalOpenTelemetry.get().getLogsBridge().loggerBuilder("test").build();
54-
logger.logRecordBuilder().setBody("testBody").emit();
55+
logger.logRecordBuilder().setBody("testBody").setAttribute("testKey", "testValue").emit();
5556

5657
List<LogRecordData> logRecords = AgentTestingExporterAccess.getExportedLogRecords();
57-
assertThat(logRecords.size()).isEqualTo(1);
58-
assertThat(logRecords.get(0).getBodyValue().getValue()).isEqualTo("testBody");
58+
assertThat(logRecords).hasSize(1);
59+
LogRecordData logRecord = logRecords.get(0);
60+
assertThat(logRecord.getBodyValue().getValue()).isEqualTo("testBody");
61+
assertThat(logRecord.getAttributes().get(stringKey("testKey"))).isEqualTo("testValue");
62+
assertThat(logRecord.getTotalAttributeCount()).isEqualTo(1);
5963
}
6064
}

0 commit comments

Comments
 (0)