diff --git a/api/all/src/main/java/io/opentelemetry/api/logs/LogRecordBuilder.java b/api/all/src/main/java/io/opentelemetry/api/logs/LogRecordBuilder.java index 2166ab2b6b8..cc5da82c22e 100644 --- a/api/all/src/main/java/io/opentelemetry/api/logs/LogRecordBuilder.java +++ b/api/all/src/main/java/io/opentelemetry/api/logs/LogRecordBuilder.java @@ -5,6 +5,11 @@ package io.opentelemetry.api.logs; +import static io.opentelemetry.api.common.AttributeKey.booleanKey; +import static io.opentelemetry.api.common.AttributeKey.doubleKey; +import static io.opentelemetry.api.common.AttributeKey.longKey; +import static io.opentelemetry.api.common.AttributeKey.stringKey; + import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.Value; @@ -98,9 +103,91 @@ default LogRecordBuilder setAllAttributes(Attributes attributes) { return this; } - /** Sets an attribute. */ + /** + * Sets an attribute on the {@code LogRecord}. If the {@code LogRecord} previously contained a + * mapping for the key, the old value is replaced by the specified value. + * + * @param key the key for this attribute. + * @param value the value for this attribute. + * @return this. + */ LogRecordBuilder setAttribute(AttributeKey key, T value); + /** + * Sets a String attribute on the {@code LogRecord}. If the {@code LogRecord} previously contained + * a mapping for the key, the old value is replaced by the specified value. + * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * + * @param key the key for this attribute. + * @param value the value for this attribute. + * @return this. + */ + default LogRecordBuilder setAttribute(String key, String value) { + return setAttribute(stringKey(key), value); + } + + /** + * Sets a Long attribute on the {@code LogRecord}. If the {@code LogRecord} previously contained a + * mapping for the key, the old value is replaced by the specified value. + * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * + * @param key the key for this attribute. + * @param value the value for this attribute. + * @return this. + */ + default LogRecordBuilder setAttribute(String key, long value) { + return setAttribute(longKey(key), value); + } + + /** + * Sets a Double attribute on the {@code LogRecord}. If the {@code LogRecord} previously contained + * a mapping for the key, the old value is replaced by the specified value. + * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * + * @param key the key for this attribute. + * @param value the value for this attribute. + * @return this. + */ + default LogRecordBuilder setAttribute(String key, double value) { + return setAttribute(doubleKey(key), value); + } + + /** + * Sets a Boolean attribute on the {@code LogRecord}. If the {@code LogRecord} previously + * contained a mapping for the key, the old value is replaced by the specified value. + * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * + * @param key the key for this attribute. + * @param value the value for this attribute. + * @return this. + */ + default LogRecordBuilder setAttribute(String key, boolean value) { + return setAttribute(booleanKey(key), value); + } + + /** + * Sets an Integer attribute on the {@code LogRecord}. If the {@code LogRecord} previously + * contained a mapping for the key, the old value is replaced by the specified value. + * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * + * @param key the key for this attribute. + * @param value the value for this attribute. + * @return this. + */ + default LogRecordBuilder setAttribute(String key, int value) { + return setAttribute(key, (long) value); + } + /** Emit the log record. */ void emit(); } diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-api.txt b/docs/apidiffs/current_vs_latest/opentelemetry-api.txt index ef68fdedeeb..bd14d189fd7 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-api.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-api.txt @@ -1,2 +1,8 @@ Comparing source compatibility of opentelemetry-api-1.48.0-SNAPSHOT.jar against opentelemetry-api-1.47.0.jar -No changes. \ No newline at end of file +*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.logs.LogRecordBuilder (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, java.lang.String) + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, long) + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, double) + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, boolean) + +++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, int) diff --git a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilderTest.java b/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilderTest.java index 0be11e67b31..202f73917f1 100644 --- a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilderTest.java +++ b/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilderTest.java @@ -5,7 +5,12 @@ package io.opentelemetry.sdk.logs; +import static io.opentelemetry.api.common.AttributeKey.booleanKey; +import static io.opentelemetry.api.common.AttributeKey.doubleKey; +import static io.opentelemetry.api.common.AttributeKey.longKey; +import static io.opentelemetry.api.common.AttributeKey.stringKey; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; import static org.mockito.Mockito.when; import io.opentelemetry.api.common.AttributeKey; @@ -77,7 +82,7 @@ void emit_AllFields() { builder.setTimestamp(timestamp); builder.setObservedTimestamp(456, TimeUnit.SECONDS); builder.setObservedTimestamp(observedTimestamp); - builder.setAttribute(null, null); + builder.setAttribute((String) null, (String) null); builder.setAttribute(AttributeKey.stringKey("k1"), "v1"); builder.setAllAttributes(Attributes.builder().put("k2", "v2").put("k3", "v3").build()); builder.setContext(Span.wrap(spanContext).storeInContext(Context.root())); @@ -116,4 +121,22 @@ void emit_NoFields() { .hasSpanContext(SpanContext.getInvalid()) .hasSeverity(Severity.UNDEFINED_SEVERITY_NUMBER); } + + @Test + void testConvenienceAttributeMethods() { + builder + .setAttribute("foo", "bar") + .setAttribute("lk", 12L) + .setAttribute("dk", 12.123) + .setAttribute("bk", true) + .setAttribute("ik", 13) + .emit(); + assertThat(emittedLog.get().toLogRecordData()) + .hasAttributesSatisfyingExactly( + equalTo(stringKey("foo"), "bar"), + equalTo(longKey("lk"), 12L), + equalTo(doubleKey("dk"), 12.123), + equalTo(booleanKey("bk"), true), + equalTo(longKey("ik"), 13L)); + } }