-
Notifications
You must be signed in to change notification settings - Fork 921
Sketch out ergonomic log event API #7907
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
Draft
jack-berg
wants to merge
1
commit into
open-telemetry:main
Choose a base branch
from
jack-berg:ergonomic-log-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+596
−4
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/LogEventBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,142 @@ | ||||||
| /* | ||||||
| * Copyright The OpenTelemetry Authors | ||||||
| * SPDX-License-Identifier: Apache-2.0 | ||||||
| */ | ||||||
|
|
||||||
| package io.opentelemetry.api.incubator.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; | ||||||
| import io.opentelemetry.api.logs.LogRecordBuilder; | ||||||
| import io.opentelemetry.context.Context; | ||||||
| import javax.annotation.Nullable; | ||||||
|
|
||||||
| /** | ||||||
| * Experimental fluent builder for <a | ||||||
| * href="https://opentelemetry.io/docs/specs/semconv/general/events/">log events</a>. | ||||||
| */ | ||||||
| public interface LogEventBuilder { | ||||||
|
|
||||||
| /** Set the context. */ | ||||||
| LogEventBuilder setContext(Context context); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| /** Set the body {@link Value}. */ | ||||||
| LogEventBuilder setBody(Value<?> body); | ||||||
|
|
||||||
| /** | ||||||
| * Set the body string. | ||||||
| * | ||||||
| * <p>Shorthand for calling {@link #setBody(Value)} with {@link Value#of(String)}. | ||||||
| */ | ||||||
| LogEventBuilder setBody(String body); | ||||||
|
|
||||||
| /** | ||||||
| * Sets attributes. If the {@link LogRecordBuilder} previously contained a mapping for any of the | ||||||
| * keys, the old values are replaced by the specified values. | ||||||
| */ | ||||||
| @SuppressWarnings("unchecked") | ||||||
| default LogEventBuilder setAllAttributes(Attributes attributes) { | ||||||
| if (attributes == null || attributes.isEmpty()) { | ||||||
| return this; | ||||||
| } | ||||||
| attributes.forEach( | ||||||
| (attributeKey, value) -> setAttribute((AttributeKey<Object>) attributeKey, value)); | ||||||
| return this; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * 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. | ||||||
| * | ||||||
| * <p>Note: Providing a null value is a no-op and will not remove previously set values. | ||||||
| * | ||||||
| * @param key the key for this attribute. | ||||||
| * @param value the value for this attribute. | ||||||
| * @return this. | ||||||
| */ | ||||||
| <T> LogEventBuilder setAttribute(AttributeKey<T> key, @Nullable 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. | ||||||
| * | ||||||
| * <p>Note: Providing a null value is a no-op and will not remove previously set values. | ||||||
| * | ||||||
| * <p>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. | ||||||
| */ | ||||||
| default LogEventBuilder setAttribute(String key, @Nullable 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. | ||||||
| * | ||||||
| * <p>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. | ||||||
| */ | ||||||
| default LogEventBuilder 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. | ||||||
| * | ||||||
| * <p>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. | ||||||
| */ | ||||||
| default LogEventBuilder 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. | ||||||
| * | ||||||
| * <p>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. | ||||||
| */ | ||||||
| default LogEventBuilder 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. | ||||||
| * | ||||||
| * <p>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. | ||||||
| */ | ||||||
| default LogEventBuilder setAttribute(String key, int value) { | ||||||
| return setAttribute(key, (long) value); | ||||||
| } | ||||||
|
|
||||||
| /** Set standard {@code exception.*} attributes based on the {@code throwable}. */ | ||||||
| LogEventBuilder setException(Throwable throwable); | ||||||
|
|
||||||
| /** Emit the log event. */ | ||||||
| void emit(); | ||||||
| } | ||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
callback methods are popular to short circuit if the severity is not reached, e.g. https://logging.apache.org/log4j/2.x/javadoc/log4j-api/org/apache/logging/log4j/Logger.html#info(java.lang.String,org.apache.logging.log4j.util.Supplier...)
we could add