Skip to content
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

Prototype: User facing logs API (including emit event) #6761

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
14 changes: 14 additions & 0 deletions api/all/src/main/java/io/opentelemetry/api/OpenTelemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.api;

import io.opentelemetry.api.logs.Logger;
import io.opentelemetry.api.logs.LoggerBuilder;
import io.opentelemetry.api.logs.LoggerProvider;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.MeterBuilder;
Expand Down Expand Up @@ -114,6 +116,18 @@ default MeterBuilder meterBuilder(String instrumentationScopeName) {
return getMeterProvider().meterBuilder(instrumentationScopeName);
}

default LoggerProvider getLoggerProvider() {
return LoggerProvider.noop();
}

default Logger getLogger(String instrumentationScopeName) {
return getLoggerProvider().get(instrumentationScopeName);
}

default LoggerBuilder getLoggerBuilder(String instrumentationScopeName) {
return getLoggerProvider().loggerBuilder(instrumentationScopeName);
}

/**
* Returns the {@link LoggerProvider} for bridging logs into OpenTelemetry.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ static Value<List<KeyValue>> of(Map<String, Value<?>> value) {
return KeyValueList.createFromMap(value);
}

static ValueMapBuilder mapBuilder() {
return new ValueMapBuilderImpl();
}

static ValueListBuilder listBuilder() {
return new ValueListBuilderImpl();
}

/** Returns the type of this {@link Value}. Useful for building switch statements. */
ValueType getType();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.common;

public interface ValueListBuilder {

ValueListBuilder add(Value<?> value);

Value<?> build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.common;

import java.util.ArrayList;
import java.util.List;

class ValueListBuilderImpl implements ValueListBuilder {

private final List<Value<?>> values = new ArrayList<>();

@Override
public ValueListBuilder add(Value<?> value) {
values.add(value);
return this;
}

@Override
public Value<?> build() {
return Value.of(values.toArray(new Value[0]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.common;

public interface ValueMapBuilder {

default ValueMapBuilder put(String key, String value) {
put(key, Value.of(value));
return this;
}

default ValueMapBuilder put(String key, long value) {
put(key, Value.of(value));
return this;
}

default ValueMapBuilder put(String key, double value) {
put(key, Value.of(value));
return this;
}

default ValueMapBuilder put(String key, boolean value) {
put(key, Value.of(value));
return this;
}

ValueMapBuilder put(String key, Value<?> value);

Value<?> build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.common;

import java.util.ArrayList;
import java.util.List;

class ValueMapBuilderImpl implements ValueMapBuilder {

private final List<KeyValue> keyValues = new ArrayList<>();

@Override
public ValueMapBuilder put(String key, Value<?> value) {
keyValues.add(KeyValue.of(key, value));
return this;
}

@Override
public Value<?> build() {
return Value.of(keyValues.toArray(new KeyValue[0]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.api.logs;

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;
Expand All @@ -15,6 +16,7 @@ class DefaultLogger implements Logger {

private static final Logger INSTANCE = new DefaultLogger();
private static final LogRecordBuilder NOOP_LOG_RECORD_BUILDER = new NoopLogRecordBuilder();
private static final EventBuilder NOOP_EVENT_BUILDER = new NoopEventBuilder();

private DefaultLogger() {}

Expand All @@ -27,6 +29,11 @@ public LogRecordBuilder logRecordBuilder() {
return NOOP_LOG_RECORD_BUILDER;
}

@Override
public EventBuilder eventBuilder(String eventName) {
return NOOP_EVENT_BUILDER;
}

private static final class NoopLogRecordBuilder implements LogRecordBuilder {

private NoopLogRecordBuilder() {}
Expand Down Expand Up @@ -84,4 +91,40 @@ public <T> LogRecordBuilder setAttribute(AttributeKey<T> key, T value) {
@Override
public void emit() {}
}

private static class NoopEventBuilder implements EventBuilder {

@Override
public EventBuilder setBody(Value<?> body) {
return this;
}

@Override
public EventBuilder setTimestamp(long timestamp, TimeUnit unit) {
return this;
}

@Override
public EventBuilder setTimestamp(Instant instant) {
return this;
}

@Override
public EventBuilder setContext(Context context) {
return this;
}

@Override
public EventBuilder setSeverity(Severity severity) {
return this;
}

@Override
public EventBuilder setAttributes(Attributes attributes) {
return this;
}

@Override
public void emit() {}
}
}
53 changes: 53 additions & 0 deletions api/all/src/main/java/io/opentelemetry/api/logs/EventBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.logs;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.Value;
import io.opentelemetry.context.Context;
import java.time.Instant;
import java.util.concurrent.TimeUnit;

/** The EventBuilder is used to {@link #emit()} events. */
public interface EventBuilder {
trask marked this conversation as resolved.
Show resolved Hide resolved

EventBuilder setBody(Value<?> body);

/**
* Set the epoch {@code timestamp}, using the timestamp and unit.
*
* <p>The {@code timestamp} is the time at which the event occurred. If unset, it will be set to
* the current time when {@link #emit()} is called.
*/
EventBuilder setTimestamp(long timestamp, TimeUnit unit);

/**
* Set the epoch {@code timestamp}, using the instant.
*
* <p>The {@code timestamp} is the time at which the event occurred. If unset, it will be set to
* the current time when {@link #emit()} is called.
*/
EventBuilder setTimestamp(Instant instant);

/** Set the context. */
EventBuilder setContext(Context context);

/** Set the severity. */
EventBuilder setSeverity(Severity severity);

/**
* Set the attributes.
*
* <p>Event {@link Attributes} provide additional details about the Event which are not part of
* the well-defined {@link Value} payload. Setting event attributes is less common than adding
* entries to the event payload. Most users will want to call one of the {@code #put(String, ?)}
* methods instead.
*/
EventBuilder setAttributes(Attributes attributes);

/** Emit an event. */
void emit();
}
13 changes: 13 additions & 0 deletions api/all/src/main/java/io/opentelemetry/api/logs/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ public interface Logger {
* LogRecordBuilder#emit()}.
*/
LogRecordBuilder logRecordBuilder();

/**
* Return a {@link LogRecordBuilder} to emit an event.
*
* @param eventName the event name, which identifies the class or type of event. Event with the
* same name are structurally similar to one another. Event names are subject to the same
* naming rules as attribute names. Notably, they are namespaced to avoid collisions. See <a
* href="https://opentelemetry.io/docs/specs/semconv/general/events/">event.name semantic
* conventions</a> for more details.
*/
default EventBuilder eventBuilder(String eventName) {
return DefaultLogger.getInstance().eventBuilder(eventName);
trask marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
package io.opentelemetry.api.incubator.logs;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.Value;
import io.opentelemetry.api.logs.EventBuilder;
import io.opentelemetry.api.logs.LogRecordBuilder;
import io.opentelemetry.api.logs.Logger;
import io.opentelemetry.api.logs.Severity;
Expand All @@ -18,6 +20,7 @@ class ExtendedDefaultLogger implements ExtendedLogger {

private static final Logger INSTANCE = new ExtendedDefaultLogger();
private static final LogRecordBuilder NOOP_LOG_RECORD_BUILDER = new NoopLogRecordBuilder();
private static final EventBuilder NOOP_EVENT_BUILDER = new NoopEventBuilder();

private ExtendedDefaultLogger() {}

Expand All @@ -30,6 +33,11 @@ public LogRecordBuilder logRecordBuilder() {
return NOOP_LOG_RECORD_BUILDER;
}

@Override
public EventBuilder eventBuilder(String eventName) {
return NOOP_EVENT_BUILDER;
}

private static final class NoopLogRecordBuilder implements ExtendedLogRecordBuilder {

private NoopLogRecordBuilder() {}
Expand Down Expand Up @@ -87,4 +95,40 @@ public <T> LogRecordBuilder setAttribute(AttributeKey<T> key, T value) {
@Override
public void emit() {}
}

private static class NoopEventBuilder implements EventBuilder {

@Override
public EventBuilder setBody(Value<?> body) {
return this;
}

@Override
public EventBuilder setTimestamp(long timestamp, TimeUnit unit) {
return this;
}

@Override
public EventBuilder setTimestamp(Instant instant) {
return this;
}

@Override
public EventBuilder setContext(Context context) {
return this;
}

@Override
public EventBuilder setSeverity(Severity severity) {
return this;
}

@Override
public EventBuilder setAttributes(Attributes attributes) {
return this;
}

@Override
public void emit() {}
}
}
Loading
Loading