Skip to content

Commit 26785ca

Browse files
committed
Prototype: User facing logs API (including emit event)
1 parent 361f035 commit 26785ca

File tree

7 files changed

+366
-0
lines changed

7 files changed

+366
-0
lines changed

api/all/src/main/java/io/opentelemetry/api/OpenTelemetry.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.opentelemetry.api;
77

8+
import io.opentelemetry.api.logs.Logger;
9+
import io.opentelemetry.api.logs.LoggerBuilder;
810
import io.opentelemetry.api.logs.LoggerProvider;
911
import io.opentelemetry.api.metrics.Meter;
1012
import io.opentelemetry.api.metrics.MeterBuilder;
@@ -114,6 +116,18 @@ default MeterBuilder meterBuilder(String instrumentationScopeName) {
114116
return getMeterProvider().meterBuilder(instrumentationScopeName);
115117
}
116118

119+
default LoggerProvider getLoggerProvider() {
120+
return LoggerProvider.noop();
121+
}
122+
123+
default Logger getLogger(String instrumentationScopeName) {
124+
return getLoggerProvider().get(instrumentationScopeName);
125+
}
126+
127+
default LoggerBuilder getLoggerBuilder(String instrumentationScopeName) {
128+
return getLoggerProvider().loggerBuilder(instrumentationScopeName);
129+
}
130+
117131
/**
118132
* Returns the {@link LoggerProvider} for bridging logs into OpenTelemetry.
119133
*

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.api.logs;
77

88
import io.opentelemetry.api.common.AttributeKey;
9+
import io.opentelemetry.api.common.Attributes;
910
import io.opentelemetry.api.common.Value;
1011
import io.opentelemetry.context.Context;
1112
import java.time.Instant;
@@ -15,6 +16,7 @@ class DefaultLogger implements Logger {
1516

1617
private static final Logger INSTANCE = new DefaultLogger();
1718
private static final LogRecordBuilder NOOP_LOG_RECORD_BUILDER = new NoopLogRecordBuilder();
19+
private static final EventBuilder NOOP_EVENT_BUILDER = new NoopEventBuilder();
1820

1921
private DefaultLogger() {}
2022

@@ -27,6 +29,11 @@ public LogRecordBuilder logRecordBuilder() {
2729
return NOOP_LOG_RECORD_BUILDER;
2830
}
2931

32+
@Override
33+
public EventBuilder eventBuilder(String eventName) {
34+
return NOOP_EVENT_BUILDER;
35+
}
36+
3037
private static final class NoopLogRecordBuilder implements LogRecordBuilder {
3138

3239
private NoopLogRecordBuilder() {}
@@ -84,4 +91,40 @@ public <T> LogRecordBuilder setAttribute(AttributeKey<T> key, T value) {
8491
@Override
8592
public void emit() {}
8693
}
94+
95+
private static class NoopEventBuilder implements EventBuilder {
96+
97+
@Override
98+
public EventBuilder put(String key, Value<?> value) {
99+
return this;
100+
}
101+
102+
@Override
103+
public EventBuilder setTimestamp(long timestamp, TimeUnit unit) {
104+
return this;
105+
}
106+
107+
@Override
108+
public EventBuilder setTimestamp(Instant instant) {
109+
return this;
110+
}
111+
112+
@Override
113+
public EventBuilder setContext(Context context) {
114+
return this;
115+
}
116+
117+
@Override
118+
public EventBuilder setSeverity(Severity severity) {
119+
return this;
120+
}
121+
122+
@Override
123+
public EventBuilder setAttributes(Attributes attributes) {
124+
return this;
125+
}
126+
127+
@Override
128+
public void emit() {}
129+
}
87130
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.api.logs;
7+
8+
import static java.util.stream.Collectors.toList;
9+
10+
import io.opentelemetry.api.common.AttributeKey;
11+
import io.opentelemetry.api.common.Attributes;
12+
import io.opentelemetry.api.common.Value;
13+
import io.opentelemetry.context.Context;
14+
import java.time.Instant;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.concurrent.TimeUnit;
18+
19+
/** The EventBuilder is used to {@link #emit()} events. */
20+
public interface EventBuilder {
21+
22+
/** Put the given {@code key} and {@code value} in the payload. */
23+
default EventBuilder put(String key, String value) {
24+
return put(key, Value.of(value));
25+
}
26+
27+
/** Put the given {@code key} and {@code value} in the payload. */
28+
default EventBuilder put(String key, long value) {
29+
return put(key, Value.of(value));
30+
}
31+
32+
/** Put the given {@code key} and {@code value} in the payload. */
33+
default EventBuilder put(String key, double value) {
34+
return put(key, Value.of(value));
35+
}
36+
37+
/** Put the given {@code key} and {@code value} in the payload. */
38+
default EventBuilder put(String key, boolean value) {
39+
return put(key, Value.of(value));
40+
}
41+
42+
/** Put the given {@code key} and {@code value} in the payload. */
43+
default EventBuilder put(String key, String... value) {
44+
List<Value<?>> values = new ArrayList<>(value.length);
45+
for (String val : value) {
46+
values.add(Value.of(val));
47+
}
48+
return put(key, Value.of(values));
49+
}
50+
51+
/** Put the given {@code key} and {@code value} in the payload. */
52+
default EventBuilder put(String key, long... value) {
53+
List<Value<?>> values = new ArrayList<>(value.length);
54+
for (long val : value) {
55+
values.add(Value.of(val));
56+
}
57+
return put(key, Value.of(values));
58+
}
59+
60+
/** Put the given {@code key} and {@code value} in the payload. */
61+
default EventBuilder put(String key, double... value) {
62+
List<Value<?>> values = new ArrayList<>(value.length);
63+
for (double val : value) {
64+
values.add(Value.of(val));
65+
}
66+
return put(key, Value.of(values));
67+
}
68+
69+
/** Put the given {@code key} and {@code value} in the payload. */
70+
default EventBuilder put(String key, boolean... value) {
71+
List<Value<?>> values = new ArrayList<>(value.length);
72+
for (boolean val : value) {
73+
values.add(Value.of(val));
74+
}
75+
return put(key, Value.of(values));
76+
}
77+
78+
/**
79+
* Put the given key and value in the payload.
80+
*
81+
* <p>NOTE: The key value pair is NOT added to the event attributes. Setting event attributes is
82+
* less common than adding entries to the event payload. Use {@link #setAttributes(Attributes)} if
83+
* intending the data to be set in attributes instead of the payload.
84+
*/
85+
@SuppressWarnings("unchecked")
86+
default <T> EventBuilder put(AttributeKey<T> key, T value) {
87+
switch (key.getType()) {
88+
case STRING:
89+
return put(key.getKey(), (String) value);
90+
case BOOLEAN:
91+
return put(key.getKey(), (boolean) value);
92+
case LONG:
93+
return put(key.getKey(), (long) value);
94+
case DOUBLE:
95+
return put(key.getKey(), (double) value);
96+
case STRING_ARRAY:
97+
return put(
98+
key.getKey(),
99+
Value.of(((List<String>) value).stream().map(Value::of).collect(toList())));
100+
case BOOLEAN_ARRAY:
101+
return put(
102+
key.getKey(),
103+
Value.of(((List<Boolean>) value).stream().map(Value::of).collect(toList())));
104+
case LONG_ARRAY:
105+
return put(
106+
key.getKey(), Value.of(((List<Long>) value).stream().map(Value::of).collect(toList())));
107+
case DOUBLE_ARRAY:
108+
return put(
109+
key.getKey(),
110+
Value.of(((List<Double>) value).stream().map(Value::of).collect(toList())));
111+
}
112+
return this;
113+
}
114+
115+
/** Put the given {@code key} and {@code value} in the payload. */
116+
EventBuilder put(String key, Value<?> value);
117+
118+
/**
119+
* Set the epoch {@code timestamp}, using the timestamp and unit.
120+
*
121+
* <p>The {@code timestamp} is the time at which the event occurred. If unset, it will be set to
122+
* the current time when {@link #emit()} is called.
123+
*/
124+
EventBuilder setTimestamp(long timestamp, TimeUnit unit);
125+
126+
/**
127+
* Set the epoch {@code timestamp}, using the instant.
128+
*
129+
* <p>The {@code timestamp} is the time at which the event occurred. If unset, it will be set to
130+
* the current time when {@link #emit()} is called.
131+
*/
132+
EventBuilder setTimestamp(Instant instant);
133+
134+
/** Set the context. */
135+
EventBuilder setContext(Context context);
136+
137+
/** Set the severity. */
138+
EventBuilder setSeverity(Severity severity);
139+
140+
/**
141+
* Set the attributes.
142+
*
143+
* <p>Event {@link Attributes} provide additional details about the Event which are not part of
144+
* the well-defined {@link Value} payload. Setting event attributes is less common than adding
145+
* entries to the event payload. Most users will want to call one of the {@code #put(String, ?)}
146+
* methods instead.
147+
*/
148+
EventBuilder setAttributes(Attributes attributes);
149+
150+
/** Emit an event. */
151+
void emit();
152+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,15 @@ public interface Logger {
3333
* LogRecordBuilder#emit()}.
3434
*/
3535
LogRecordBuilder logRecordBuilder();
36+
37+
/**
38+
* Return a {@link LogRecordBuilder} to emit an event.
39+
*
40+
* @param eventName the event name, which identifies the class or type of event. Event with the
41+
* same name are structurally similar to one another. Event names are subject to the same
42+
* naming rules as attribute names. Notably, they are namespaced to avoid collisions. See <a
43+
* href="https://opentelemetry.io/docs/specs/semconv/general/events/">event.name semantic
44+
* conventions</a> for more details.
45+
*/
46+
EventBuilder eventBuilder(String eventName);
3647
}

api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
package io.opentelemetry.api.incubator.logs;
77

88
import io.opentelemetry.api.common.AttributeKey;
9+
import io.opentelemetry.api.common.Attributes;
910
import io.opentelemetry.api.common.Value;
11+
import io.opentelemetry.api.logs.EventBuilder;
1012
import io.opentelemetry.api.logs.LogRecordBuilder;
1113
import io.opentelemetry.api.logs.Logger;
1214
import io.opentelemetry.api.logs.Severity;
@@ -18,6 +20,7 @@ class ExtendedDefaultLogger implements ExtendedLogger {
1820

1921
private static final Logger INSTANCE = new ExtendedDefaultLogger();
2022
private static final LogRecordBuilder NOOP_LOG_RECORD_BUILDER = new NoopLogRecordBuilder();
23+
private static final EventBuilder NOOP_EVENT_BUILDER = new NoopEventBuilder();
2124

2225
private ExtendedDefaultLogger() {}
2326

@@ -30,6 +33,11 @@ public LogRecordBuilder logRecordBuilder() {
3033
return NOOP_LOG_RECORD_BUILDER;
3134
}
3235

36+
@Override
37+
public EventBuilder eventBuilder(String eventName) {
38+
return NOOP_EVENT_BUILDER;
39+
}
40+
3341
private static final class NoopLogRecordBuilder implements ExtendedLogRecordBuilder {
3442

3543
private NoopLogRecordBuilder() {}
@@ -87,4 +95,40 @@ public <T> LogRecordBuilder setAttribute(AttributeKey<T> key, T value) {
8795
@Override
8896
public void emit() {}
8997
}
98+
99+
private static class NoopEventBuilder implements EventBuilder {
100+
101+
@Override
102+
public EventBuilder put(String key, Value<?> value) {
103+
return this;
104+
}
105+
106+
@Override
107+
public EventBuilder setTimestamp(long timestamp, TimeUnit unit) {
108+
return this;
109+
}
110+
111+
@Override
112+
public EventBuilder setTimestamp(Instant instant) {
113+
return this;
114+
}
115+
116+
@Override
117+
public EventBuilder setContext(Context context) {
118+
return this;
119+
}
120+
121+
@Override
122+
public EventBuilder setSeverity(Severity severity) {
123+
return this;
124+
}
125+
126+
@Override
127+
public EventBuilder setAttributes(Attributes attributes) {
128+
return this;
129+
}
130+
131+
@Override
132+
public void emit() {}
133+
}
90134
}

0 commit comments

Comments
 (0)