Skip to content

Add some helpful logging attribute methods #7089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@

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.stringArrayKey;
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;
import io.opentelemetry.context.Context;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -101,6 +108,36 @@ default LogRecordBuilder setAllAttributes(Attributes attributes) {
/** Sets an attribute. */
<T> LogRecordBuilder setAttribute(AttributeKey<T> key, T value);

/** Sets a string attribute. */
default LogRecordBuilder setAttribute(String key, String value) {
return setAttribute(stringKey(key), value);
}

/** Sets a Long attribute. */
default LogRecordBuilder setAttribute(String key, Long value) {
return setAttribute(longKey(key), value);
}

/** Sets a Double attribute. */
default LogRecordBuilder setAttribute(String key, Double value) {
return setAttribute(doubleKey(key), value);
}

/** Sets a Boolean attribute. */
default LogRecordBuilder setAttribute(String key, Boolean value) {
return setAttribute(booleanKey(key), value);
}

/** Sets an Integer attribute. */
default LogRecordBuilder setAttribute(String key, Integer value) {
return setAttribute(key, value.longValue());
}

/** Sets a string array attribute. */
default LogRecordBuilder setAttribute(String key, List<String> value) {
return setAttribute(stringArrayKey(key), value);
}

/** Emit the log record. */
void emit();
}
11 changes: 9 additions & 2 deletions docs/apidiffs/current_vs_latest/opentelemetry-api.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
Comparing source compatibility of opentelemetry-api-1.48.0-SNAPSHOT.jar against opentelemetry-api-1.47.0.jar
No changes.
Comparing source compatibility of opentelemetry-api-1.48.0-SNAPSHOT.jar against opentelemetry-api-1.46.0.jar
*** 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, java.lang.Long)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, java.lang.Double)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, java.lang.Boolean)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, java.lang.Integer)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, java.util.List<java.lang.String>)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@

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.stringArrayKey;
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;
Expand All @@ -21,6 +27,8 @@
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.resources.Resource;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -77,7 +85,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()));
Expand Down Expand Up @@ -116,4 +124,25 @@ void emit_NoFields() {
.hasSpanContext(SpanContext.getInvalid())
.hasSeverity(Severity.UNDEFINED_SEVERITY_NUMBER);
}

@Test
void testConvenienceAttributeMethods() {
List<String> valueList = Arrays.asList("foo", "bar", "baz");
builder
.setAttribute("foo", "bar")
.setAttribute("lk", 12L)
.setAttribute("dk", 12.123)
.setAttribute("bk", true)
.setAttribute("ik", 13)
.setAttribute("list", valueList)
.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),
equalTo(stringArrayKey("list"), valueList));
}
}
Loading