Skip to content

Commit ee731aa

Browse files
Add some helpful logging attribute methods (#7089)
Co-authored-by: jack-berg <[email protected]>
1 parent 56941a5 commit ee731aa

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

api/all/src/main/java/io/opentelemetry/api/logs/LogRecordBuilder.java

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
package io.opentelemetry.api.logs;
77

8+
import static io.opentelemetry.api.common.AttributeKey.booleanKey;
9+
import static io.opentelemetry.api.common.AttributeKey.doubleKey;
10+
import static io.opentelemetry.api.common.AttributeKey.longKey;
11+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
12+
813
import io.opentelemetry.api.common.AttributeKey;
914
import io.opentelemetry.api.common.Attributes;
1015
import io.opentelemetry.api.common.Value;
@@ -98,9 +103,91 @@ default LogRecordBuilder setAllAttributes(Attributes attributes) {
98103
return this;
99104
}
100105

101-
/** Sets an attribute. */
106+
/**
107+
* Sets an attribute on the {@code LogRecord}. If the {@code LogRecord} previously contained a
108+
* mapping for the key, the old value is replaced by the specified value.
109+
*
110+
* @param key the key for this attribute.
111+
* @param value the value for this attribute.
112+
* @return this.
113+
*/
102114
<T> LogRecordBuilder setAttribute(AttributeKey<T> key, T value);
103115

116+
/**
117+
* Sets a String attribute on the {@code LogRecord}. If the {@code LogRecord} previously contained
118+
* a mapping for the key, the old value is replaced by the specified value.
119+
*
120+
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
121+
* pre-allocate your keys, if possible.
122+
*
123+
* @param key the key for this attribute.
124+
* @param value the value for this attribute.
125+
* @return this.
126+
*/
127+
default LogRecordBuilder setAttribute(String key, String value) {
128+
return setAttribute(stringKey(key), value);
129+
}
130+
131+
/**
132+
* Sets a Long attribute on the {@code LogRecord}. If the {@code LogRecord} previously contained a
133+
* mapping for the key, the old value is replaced by the specified value.
134+
*
135+
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
136+
* pre-allocate your keys, if possible.
137+
*
138+
* @param key the key for this attribute.
139+
* @param value the value for this attribute.
140+
* @return this.
141+
*/
142+
default LogRecordBuilder setAttribute(String key, long value) {
143+
return setAttribute(longKey(key), value);
144+
}
145+
146+
/**
147+
* Sets a Double attribute on the {@code LogRecord}. If the {@code LogRecord} previously contained
148+
* a mapping for the key, the old value is replaced by the specified value.
149+
*
150+
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
151+
* pre-allocate your keys, if possible.
152+
*
153+
* @param key the key for this attribute.
154+
* @param value the value for this attribute.
155+
* @return this.
156+
*/
157+
default LogRecordBuilder setAttribute(String key, double value) {
158+
return setAttribute(doubleKey(key), value);
159+
}
160+
161+
/**
162+
* Sets a Boolean attribute on the {@code LogRecord}. If the {@code LogRecord} previously
163+
* contained a mapping for the key, the old value is replaced by the specified value.
164+
*
165+
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
166+
* pre-allocate your keys, if possible.
167+
*
168+
* @param key the key for this attribute.
169+
* @param value the value for this attribute.
170+
* @return this.
171+
*/
172+
default LogRecordBuilder setAttribute(String key, boolean value) {
173+
return setAttribute(booleanKey(key), value);
174+
}
175+
176+
/**
177+
* Sets an Integer attribute on the {@code LogRecord}. If the {@code LogRecord} previously
178+
* contained a mapping for the key, the old value is replaced by the specified value.
179+
*
180+
* <p>Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and
181+
* pre-allocate your keys, if possible.
182+
*
183+
* @param key the key for this attribute.
184+
* @param value the value for this attribute.
185+
* @return this.
186+
*/
187+
default LogRecordBuilder setAttribute(String key, int value) {
188+
return setAttribute(key, (long) value);
189+
}
190+
104191
/** Emit the log record. */
105192
void emit();
106193
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
Comparing source compatibility of opentelemetry-api-1.48.0-SNAPSHOT.jar against opentelemetry-api-1.47.0.jar
2-
No changes.
2+
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.api.logs.LogRecordBuilder (not serializable)
3+
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
4+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, java.lang.String)
5+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, long)
6+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, double)
7+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, boolean)
8+
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.api.logs.LogRecordBuilder setAttribute(java.lang.String, int)

sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilderTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55

66
package io.opentelemetry.sdk.logs;
77

8+
import static io.opentelemetry.api.common.AttributeKey.booleanKey;
9+
import static io.opentelemetry.api.common.AttributeKey.doubleKey;
10+
import static io.opentelemetry.api.common.AttributeKey.longKey;
11+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
812
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
13+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
914
import static org.mockito.Mockito.when;
1015

1116
import io.opentelemetry.api.common.AttributeKey;
@@ -77,7 +82,7 @@ void emit_AllFields() {
7782
builder.setTimestamp(timestamp);
7883
builder.setObservedTimestamp(456, TimeUnit.SECONDS);
7984
builder.setObservedTimestamp(observedTimestamp);
80-
builder.setAttribute(null, null);
85+
builder.setAttribute((String) null, (String) null);
8186
builder.setAttribute(AttributeKey.stringKey("k1"), "v1");
8287
builder.setAllAttributes(Attributes.builder().put("k2", "v2").put("k3", "v3").build());
8388
builder.setContext(Span.wrap(spanContext).storeInContext(Context.root()));
@@ -116,4 +121,22 @@ void emit_NoFields() {
116121
.hasSpanContext(SpanContext.getInvalid())
117122
.hasSeverity(Severity.UNDEFINED_SEVERITY_NUMBER);
118123
}
124+
125+
@Test
126+
void testConvenienceAttributeMethods() {
127+
builder
128+
.setAttribute("foo", "bar")
129+
.setAttribute("lk", 12L)
130+
.setAttribute("dk", 12.123)
131+
.setAttribute("bk", true)
132+
.setAttribute("ik", 13)
133+
.emit();
134+
assertThat(emittedLog.get().toLogRecordData())
135+
.hasAttributesSatisfyingExactly(
136+
equalTo(stringKey("foo"), "bar"),
137+
equalTo(longKey("lk"), 12L),
138+
equalTo(doubleKey("dk"), 12.123),
139+
equalTo(booleanKey("bk"), true),
140+
equalTo(longKey("ik"), 13L));
141+
}
119142
}

0 commit comments

Comments
 (0)