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] How to support complex attributes in logs/events? (Option B) #6959

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ static AttributeKey<List<Long>> longArrayKey(String key) {
static AttributeKey<List<Double>> doubleArrayKey(String key) {
return InternalAttributeKeyImpl.create(key, AttributeType.DOUBLE_ARRAY);
}

/** Returns a new AttributeKey for Value valued attributes. */
static AttributeKey<Value> valueKey(String key) {
return InternalAttributeKeyImpl.create(key, AttributeType.VALUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public enum AttributeType {
STRING_ARRAY,
BOOLEAN_ARRAY,
LONG_ARRAY,
DOUBLE_ARRAY
DOUBLE_ARRAY,
VALUE
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.api.common.AttributeKey.valueKey;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -86,6 +87,21 @@ default AttributesBuilder put(String key, boolean value) {
return put(booleanKey(key), value);
}

/**
* Puts a {@link Value} attribute into this.
*
* <p>Note: It is strongly recommended to use {@link #put(AttributeKey, Object)}, and pre-allocate
* your keys, if possible.
*
* <p>IMPORTANT: {@link Value} attributes are only supported by Logs. Spans and Metrics do not
* support {@link Value} attributes.
*
* @return this Builder
*/
default AttributesBuilder put(String key, Value<?> value) {
return put(valueKey(key), value);
}

/**
* Puts a String array attribute into this.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,8 @@ static Value<List<KeyValue>> of(Map<String, Value<?>> value) {
*/
// TODO(jack-berg): Should this be a JSON encoding?
String asString();

static ValueBuilder builder() {
Copy link
Member Author

Choose a reason for hiding this comment

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

this is specifically for building "maps"

another option is in #6761 that has separate map and list builders:

https://github.com/open-telemetry/opentelemetry-java/pull/6761/files#diff-11421657bbb9763b8bd1579fe3bc147d5955abc85a01a94eb567f641cdbb17a7R87-R93

return new ValueBuilderImpl();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.common;

import static java.util.stream.Collectors.toList;

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

public interface ValueBuilder {

Value<?> build();

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

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

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

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

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

default ValueBuilder put(String key, String... value) {
put(key, Value.of(Arrays.stream(value).map(Value::of).collect(toList())));
return this;
}

default ValueBuilder put(String key, long... value) {
put(key, Value.of(Arrays.stream(value).mapToObj(Value::of).collect(toList())));
return this;
}

default ValueBuilder put(String key, double... value) {
put(key, Value.of(Arrays.stream(value).mapToObj(Value::of).collect(toList())));
return this;
}

default ValueBuilder put(String key, boolean... value) {
List<Value<?>> values = new ArrayList<>(value.length);
for (boolean val : value) {
values.add(Value.of(val));
}
put(key, Value.of(values));
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.opentelemetry.api.common;

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

class ValueBuilderImpl implements ValueBuilder {

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

@Override
public ValueBuilder 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]));
}
}
Loading