Skip to content

Commit 649f963

Browse files
authored
Stabilize log any value (#6591)
1 parent d37c1c7 commit 649f963

File tree

65 files changed

+1185
-1156
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1185
-1156
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.api.common;
7+
8+
/**
9+
* Key-value pair of {@link String} key and {@link Value} value.
10+
*
11+
* @see Value#of(KeyValue...)
12+
*/
13+
public interface KeyValue {
14+
15+
/** Returns a {@link KeyValue} for the given {@code key} and {@code value}. */
16+
static KeyValue of(String key, Value<?> value) {
17+
return KeyValueImpl.create(key, value);
18+
}
19+
20+
/** Returns the key. */
21+
String getKey();
22+
23+
/** Returns the value. */
24+
Value<?> getValue();
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.api.common;
7+
8+
import com.google.auto.value.AutoValue;
9+
10+
@AutoValue
11+
abstract class KeyValueImpl implements KeyValue {
12+
13+
KeyValueImpl() {}
14+
15+
static KeyValueImpl create(String key, Value<?> value) {
16+
return new AutoValue_KeyValueImpl(key, value);
17+
}
18+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.api.common;
7+
8+
import static java.util.stream.Collectors.joining;
9+
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.Collections;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.Objects;
16+
17+
final class KeyValueList implements Value<List<KeyValue>> {
18+
19+
private final List<KeyValue> value;
20+
21+
private KeyValueList(List<KeyValue> value) {
22+
this.value = value;
23+
}
24+
25+
static Value<List<KeyValue>> create(KeyValue... value) {
26+
Objects.requireNonNull(value, "value must not be null");
27+
List<KeyValue> list = new ArrayList<>(value.length);
28+
list.addAll(Arrays.asList(value));
29+
return new KeyValueList(Collections.unmodifiableList(list));
30+
}
31+
32+
static Value<List<KeyValue>> createFromMap(Map<String, Value<?>> value) {
33+
Objects.requireNonNull(value, "value must not be null");
34+
KeyValue[] array =
35+
value.entrySet().stream()
36+
.map(entry -> KeyValue.of(entry.getKey(), entry.getValue()))
37+
.toArray(KeyValue[]::new);
38+
return create(array);
39+
}
40+
41+
@Override
42+
public ValueType getType() {
43+
return ValueType.KEY_VALUE_LIST;
44+
}
45+
46+
@Override
47+
public List<KeyValue> getValue() {
48+
return value;
49+
}
50+
51+
@Override
52+
public String asString() {
53+
return value.stream()
54+
.map(item -> item.getKey() + "=" + item.getValue().asString())
55+
.collect(joining(", ", "[", "]"));
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return "KeyValueList{" + asString() + "}";
61+
}
62+
63+
@Override
64+
public boolean equals(Object o) {
65+
if (this == o) {
66+
return true;
67+
}
68+
return (o instanceof Value) && Objects.equals(this.value, ((Value<?>) o).getValue());
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
return value.hashCode();
74+
}
75+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.api.common;
7+
8+
import java.nio.ByteBuffer;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
/**
13+
* Value mirrors the proto <a
14+
* href="https://github.com/open-telemetry/opentelemetry-proto/blob/ac3242b03157295e4ee9e616af53b81517b06559/opentelemetry/proto/common/v1/common.proto#L28">AnyValue</a>
15+
* message type, and is used to model any type.
16+
*
17+
* <p>It can be used to represent:
18+
*
19+
* <ul>
20+
* <li>Primitive values via {@link #of(long)}, {@link #of(String)}, {@link #of(boolean)}, {@link
21+
* #of(double)}.
22+
* <li>String-keyed maps (i.e. associative arrays, dictionaries) via {@link #of(KeyValue...)},
23+
* {@link #of(Map)}. Note, because map values are type {@link Value}, maps can be nested
24+
* within other maps.
25+
* <li>Arrays (heterogeneous or homogenous) via {@link #of(Value[])}. Note, because array values
26+
* are type {@link Value}, arrays can contain primitives, complex types like maps or arrays,
27+
* or any combination.
28+
* <li>Raw bytes via {@link #of(byte[])}
29+
* </ul>
30+
*
31+
* <p>Currently, Value is only used as an argument for {@link
32+
* io.opentelemetry.api.logs.LogRecordBuilder#setBody(Value)}.
33+
*
34+
* @param <T> the type. See {@link #getValue()} for description of types.
35+
*/
36+
public interface Value<T> {
37+
38+
/** Returns an {@link Value} for the {@link String} value. */
39+
static Value<String> of(String value) {
40+
return ValueString.create(value);
41+
}
42+
43+
/** Returns an {@link Value} for the {@code boolean} value. */
44+
static Value<Boolean> of(boolean value) {
45+
return ValueBoolean.create(value);
46+
}
47+
48+
/** Returns an {@link Value} for the {@code long} value. */
49+
static Value<Long> of(long value) {
50+
return ValueLong.create(value);
51+
}
52+
53+
/** Returns an {@link Value} for the {@code double} value. */
54+
static Value<Double> of(double value) {
55+
return ValueDouble.create(value);
56+
}
57+
58+
/** Returns an {@link Value} for the {@code byte[]} value. */
59+
static Value<ByteBuffer> of(byte[] value) {
60+
return ValueBytes.create(value);
61+
}
62+
63+
/** Returns an {@link Value} for the array of {@link Value} values. */
64+
static Value<List<Value<?>>> of(Value<?>... value) {
65+
return ValueArray.create(value);
66+
}
67+
68+
/** Returns an {@link Value} for the list of {@link Value} values. */
69+
static Value<List<Value<?>>> of(List<Value<?>> value) {
70+
return ValueArray.create(value);
71+
}
72+
73+
/**
74+
* Returns an {@link Value} for the array of {@link KeyValue} values. {@link KeyValue#getKey()}
75+
* values should not repeat - duplicates may be dropped.
76+
*/
77+
static Value<List<KeyValue>> of(KeyValue... value) {
78+
return KeyValueList.create(value);
79+
}
80+
81+
/** Returns an {@link Value} for the {@link Map} of key, {@link Value}. */
82+
static Value<List<KeyValue>> of(Map<String, Value<?>> value) {
83+
return KeyValueList.createFromMap(value);
84+
}
85+
86+
/** Returns the type of this {@link Value}. Useful for building switch statements. */
87+
ValueType getType();
88+
89+
/**
90+
* Returns the value for this {@link Value}.
91+
*
92+
* <p>The return type varies by {@link #getType()} as described below:
93+
*
94+
* <ul>
95+
* <li>{@link ValueType#STRING} returns {@link String}
96+
* <li>{@link ValueType#BOOLEAN} returns {@code boolean}
97+
* <li>{@link ValueType#LONG} returns {@code long}
98+
* <li>{@link ValueType#DOUBLE} returns {@code double}
99+
* <li>{@link ValueType#ARRAY} returns {@link List} of {@link Value}
100+
* <li>{@link ValueType#KEY_VALUE_LIST} returns {@link List} of {@link KeyValue}
101+
* <li>{@link ValueType#BYTES} returns read only {@link ByteBuffer}. See {@link
102+
* ByteBuffer#asReadOnlyBuffer()}.
103+
* </ul>
104+
*/
105+
T getValue();
106+
107+
/**
108+
* Return a string encoding of this {@link Value}. This is intended to be a fallback serialized
109+
* representation in case there is no suitable encoding that can utilize {@link #getType()} /
110+
* {@link #getValue()} to serialize specific types.
111+
*
112+
* <p>WARNING: No guarantees are made about the encoding of this string response. It MAY change in
113+
* a future minor release. If you need a reliable string encoding, write your own serializer.
114+
*/
115+
// TODO(jack-berg): Should this be a JSON encoding?
116+
String asString();
117+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.api.common;
7+
8+
import static java.util.stream.Collectors.joining;
9+
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.Collections;
13+
import java.util.List;
14+
import java.util.Objects;
15+
16+
final class ValueArray implements Value<List<Value<?>>> {
17+
18+
private final List<Value<?>> value;
19+
20+
private ValueArray(List<Value<?>> value) {
21+
this.value = value;
22+
}
23+
24+
static Value<List<Value<?>>> create(Value<?>... value) {
25+
Objects.requireNonNull(value, "value must not be null");
26+
List<Value<?>> list = new ArrayList<>(value.length);
27+
list.addAll(Arrays.asList(value));
28+
return new ValueArray(Collections.unmodifiableList(list));
29+
}
30+
31+
static Value<List<Value<?>>> create(List<Value<?>> value) {
32+
return new ValueArray(Collections.unmodifiableList(value));
33+
}
34+
35+
@Override
36+
public ValueType getType() {
37+
return ValueType.ARRAY;
38+
}
39+
40+
@Override
41+
public List<Value<?>> getValue() {
42+
return value;
43+
}
44+
45+
@Override
46+
public String asString() {
47+
return value.stream().map(Value::asString).collect(joining(", ", "[", "]"));
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return "ValueArray{" + asString() + "}";
53+
}
54+
55+
@Override
56+
public boolean equals(Object o) {
57+
if (this == o) {
58+
return true;
59+
}
60+
return (o instanceof Value) && Objects.equals(this.value, ((Value<?>) o).getValue());
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
return value.hashCode();
66+
}
67+
}

api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/AnyValueBoolean.java renamed to api/all/src/main/java/io/opentelemetry/api/common/ValueBoolean.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.api.incubator.logs;
6+
package io.opentelemetry.api.common;
77

88
import java.util.Objects;
99

10-
final class AnyValueBoolean implements AnyValue<Boolean> {
10+
final class ValueBoolean implements Value<Boolean> {
1111

1212
private final boolean value;
1313

14-
private AnyValueBoolean(boolean value) {
14+
private ValueBoolean(boolean value) {
1515
this.value = value;
1616
}
1717

18-
static AnyValue<Boolean> create(boolean value) {
19-
return new AnyValueBoolean(value);
18+
static Value<Boolean> create(boolean value) {
19+
return new ValueBoolean(value);
2020
}
2121

2222
@Override
23-
public AnyValueType getType() {
24-
return AnyValueType.BOOLEAN;
23+
public ValueType getType() {
24+
return ValueType.BOOLEAN;
2525
}
2626

2727
@Override
@@ -36,15 +36,15 @@ public String asString() {
3636

3737
@Override
3838
public String toString() {
39-
return "AnyValueBoolean{" + asString() + "}";
39+
return "ValueBoolean{" + asString() + "}";
4040
}
4141

4242
@Override
4343
public boolean equals(Object o) {
4444
if (this == o) {
4545
return true;
4646
}
47-
return (o instanceof AnyValue) && Objects.equals(this.value, ((AnyValue<?>) o).getValue());
47+
return (o instanceof Value) && Objects.equals(this.value, ((Value<?>) o).getValue());
4848
}
4949

5050
@Override

0 commit comments

Comments
 (0)