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

Logging sink api #1421

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
40 changes: 40 additions & 0 deletions api/src/main/java/io/opentelemetry/OpenTelemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import io.opentelemetry.correlationcontext.spi.CorrelationContextManagerFactory;
import io.opentelemetry.internal.Obfuscated;
import io.opentelemetry.internal.Utils;
import io.opentelemetry.logs.DefaultLogSinkProvider;
import io.opentelemetry.logs.LogSink;
import io.opentelemetry.logs.spi.LogSinkProvider;
import io.opentelemetry.logs.spi.LogSinkProviderFactory;
import io.opentelemetry.metrics.DefaultMeterProvider;
import io.opentelemetry.metrics.Meter;
import io.opentelemetry.metrics.MeterProvider;
Expand Down Expand Up @@ -55,6 +59,7 @@ public final class OpenTelemetry {
private final TracerProvider tracerProvider;
private final MeterProvider meterProvider;
private final CorrelationContextManager contextManager;
private final LogSinkProvider logSinkProvider;

private volatile ContextPropagators propagators =
DefaultContextPropagators.builder().addHttpTextFormat(new HttpTraceContext()).build();
Expand Down Expand Up @@ -144,6 +149,34 @@ public static Meter getMeter(String instrumentationName, String instrumentationV
return getMeterProvider().get(instrumentationName, instrumentationVersion);
}

/**
* Returns a singleton {@link LogSinkProvider}.
*
* @return registered LogSinkProvider or default via {@link DefaultLogSinkProvider#getInstance()}.
* @throws IllegalStateException if a specified MeterProvider (via system properties) could not be
* found.
* @since 0.1.0
*/
private static LogSinkProvider getLogSinkProvider() {
return getInstance().logSinkProvider;
}

/**
* Gets or creates a named and versioned log sink instance.
*
* <p>This is a shortcut method for <code>
* getLogSinkProvider().get(instrumentationName, instrumentationVersion)</code>.
*
* @param instrumentationName The name of the instrumentation library, not the name of the
* instrument*ed* library.
* @param instrumentationVersion The version of the instrumentation library.
* @return a tracer instance.
* @since 0.7.0
*/
public static LogSink getLogSink(String instrumentationName, String instrumentationVersion) {
return getLogSinkProvider().get(instrumentationName, instrumentationVersion);
}

/**
* Returns a singleton {@link CorrelationContextManager}.
*
Expand Down Expand Up @@ -210,6 +243,13 @@ private OpenTelemetry() {
meterProviderFactory != null
? meterProviderFactory.create()
: DefaultMeterProvider.getInstance();

LogSinkProviderFactory logProviderFactory = loadSpi(LogSinkProviderFactory.class);
this.logSinkProvider =
logProviderFactory != null
? logProviderFactory.create()
: DefaultLogSinkProvider.getInstance();

CorrelationContextManagerFactory contextManagerProvider =
loadSpi(CorrelationContextManagerFactory.class);
contextManager =
Expand Down
291 changes: 291 additions & 0 deletions api/src/main/java/io/opentelemetry/common/AnyValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
/*
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.opentelemetry.common;

import com.google.auto.value.AutoValue;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
* A class that represents all the possible values for a data body. An {@code AnyValue} can have 6
* types of values: {@code String}, {@code boolean}, {@code int}, {@code double}, {@code array}, or
* {@code kvlist}. represented through {@code AnyValue.Type}. A {@code array} or a {@code kvlist}
* can in turn hold other {@code AnyValue} instances, allowing for mapping to JSON-like structures.
*
* @since 0.7.0
*/
@Immutable
public abstract class AnyValue {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have a proto version of this class already. is there a reason we can't use it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely have it in opentelemetry-proto, but I believe that api does not have a dependency on proto. I could be wrong- I'm trying to mirror how we are doing it with AttributeValue.


/**
* An enum that represents all the possible value types for an {@code AnyValue}.
*
* @since 0.7.0
*/
public enum Type {
STRING,
BOOL,
INT,
DOUBLE,
ARRAY,
KVLIST
}

/**
* Returns an {@code AnyValue} with a string value.
*
* @param stringValue The new value.
* @return an {@code AnyValue} with a string value.
* @since 0.7.0
*/
public static AnyValue stringAnyValue(String stringValue) {
return AnyValueString.create(stringValue);
}

/**
* Returns the string value of this {@code AnyValue}. An UnsupportedOperationException will be
* thrown if getType() is not {@link AnyValue.Type#STRING}.
*
* @return the string value of this {@code AttributeValue}.
* @since 0.7.0
*/
public String getStringValue() {
throw new UnsupportedOperationException(
String.format("This type can only return %s data", getType().name()));
}

/**
* Returns an {@code AnyValue} with an int value.
*
* @param intValue The new value.
* @return an {@code AnyValue} with a int value.
* @since 0.7.0
*/
public static AnyValue intAnyValue(int intValue) {
return AnyValueInt.create(intValue);
}

public int getIntValue() {
throw new UnsupportedOperationException(
String.format("This type can only return %s data", getType().name()));
}

/**
* Returns an {@code AnyValue} with a bool value.
*
* @param boolValue The new value.
* @return an {@code AnyValue} with a bool value.
* @since 0.7.0
*/
public static AnyValue boolAnyValue(boolean boolValue) {
return AnyValueBool.create(boolValue);
}

/**
* Returns the boolean value of this {@code AnyValue}. An UnsupportedOperationException will be
* thrown if getType() is not {@link AnyValue.Type#BOOL}.
*
* @return the boolean value of this {@code AttributeValue}.
* @since 0.7.0
*/
public boolean getBoolValue() {
throw new UnsupportedOperationException(
String.format("This type can only return %s data", getType().name()));
}

/**
* Returns an {@code AnyValue} with a double value.
*
* @param doubleValue The new value.
* @return an {@code AnyValue} with a double value.
* @since 0.7.0
*/
public static AnyValue doubleAnyValue(double doubleValue) {
return AnyValueDouble.create(doubleValue);
}

/**
* Returns the double value of this {@code AnyValue}. An UnsupportedOperationException will be
* thrown if getType() is not {@link AnyValue.Type#DOUBLE}.
*
* @return the double value of this {@code AttributeValue}.
* @since 0.7.0
*/
public double getDoubleValue() {
throw new UnsupportedOperationException(
String.format("This type can only return %s data", getType().name()));
}

/**
* Returns an {@code AnyValue} with a array value.
*
* @param values The new value.
* @return an {@code AnyValue} with a array value.
* @since 0.7.0
*/
public static AnyValue arrayAnyValue(List<AnyValue> values) {
return AnyValueArray.create(values);
}

/**
* Returns the array value of this {@code AnyValue}. An UnsupportedOperationException will be
* thrown if getType() is not {@link AnyValue.Type#ARRAY}.
*
* @return the array value of this {@code AttributeValue}.
* @since 0.7.0
*/
public List<AnyValue> getArrayValue() {
throw new UnsupportedOperationException(
String.format("This type can only return %s data", getType().name()));
}

/**
* Returns an {@code AnyValue} with a kvlist value.
*
* @param values The new value.
* @return an {@code AnyValue} with a kvlist value.
* @since 0.7.0
*/
public static AnyValue kvlistAnyValue(Map<String, AnyValue> values) {
return AnyValueKvlist.create(values);
}

/**
* Returns the string value of this {@code AnyValue}. An UnsupportedOperationException will be
* thrown if getType() is not {@link AnyValue.Type#STRING}.
*
* @return the string value of this {@code AttributeValue}.
* @since 0.7.0
*/
public Map<String, AnyValue> getKvlistValue() {
throw new UnsupportedOperationException(
String.format("This type can only return %s data", getType().name()));
}

public abstract Type getType();

@Immutable
@AutoValue
abstract static class AnyValueString extends AnyValue {
AnyValueString() {}

static AnyValue create(String stringValue) {
return new AutoValue_AnyValue_AnyValueString(stringValue);
}

@Override
public final Type getType() {
return Type.STRING;
}

@Override
@Nullable
public abstract String getStringValue();
}

@Immutable
@AutoValue
abstract static class AnyValueInt extends AnyValue {
AnyValueInt() {}

static AnyValue create(int intValue) {
return new AutoValue_AnyValue_AnyValueInt(intValue);
}

@Override
public final Type getType() {
return Type.INT;
}

@Override
public abstract int getIntValue();
}

@Immutable
@AutoValue
abstract static class AnyValueBool extends AnyValue {
AnyValueBool() {}

static AnyValue create(boolean boolValue) {
return new AutoValue_AnyValue_AnyValueBool(boolValue);
}

@Override
public final Type getType() {
return Type.BOOL;
}

@Override
public abstract boolean getBoolValue();
}

@Immutable
@AutoValue
abstract static class AnyValueDouble extends AnyValue {
AnyValueDouble() {}

static AnyValue create(double doubleValue) {
return new AutoValue_AnyValue_AnyValueDouble(doubleValue);
}

@Override
public final Type getType() {
return Type.DOUBLE;
}

@Override
public abstract double getDoubleValue();
}

@Immutable
@AutoValue
abstract static class AnyValueArray extends AnyValue {
AnyValueArray() {}

static AnyValue create(List<AnyValue> arrayValue) {
return new AutoValue_AnyValue_AnyValueArray(arrayValue);
}

@Override
public final Type getType() {
return Type.ARRAY;
}

@Override
public abstract List<AnyValue> getArrayValue();
}

@Immutable
@AutoValue
abstract static class AnyValueKvlist extends AnyValue {
AnyValueKvlist() {}

static AnyValue create(Map<String, AnyValue> kvlistValue) {
return new AutoValue_AnyValue_AnyValueKvlist(kvlistValue);
}

@Override
public final Type getType() {
return Type.KVLIST;
}

@Override
public abstract Map<String, AnyValue> getKvlistValue();
}
}
Loading