Search in sources :

Example 1 with AnyValue

use of io.opentelemetry.proto.common.v1.AnyValue in project data-prepper by opensearch-project.

the class OTelProtoHelperTest method testArrayOfValueAsResourceAttributes.

/**
 * Below object has a KeyValue with a key mapped to KeyValueList and is part of the span attributes
 *
 * @throws JsonProcessingException
 */
@Test
public void testArrayOfValueAsResourceAttributes() throws JsonProcessingException {
    final KeyValue childAttr1 = KeyValue.newBuilder().setKey("ec2.instances").setValue(AnyValue.newBuilder().setIntValue(20).build()).build();
    final KeyValue childAttr2 = KeyValue.newBuilder().setKey("ec2.instance.az").setValue(AnyValue.newBuilder().setStringValue("us-east-1").build()).build();
    final AnyValue anyValue1 = AnyValue.newBuilder().setStringValue(UUID.randomUUID().toString()).build();
    final AnyValue anyValue2 = AnyValue.newBuilder().setDoubleValue(2000.123).build();
    final AnyValue anyValue3 = AnyValue.newBuilder().setKvlistValue(KeyValueList.newBuilder().addAllValues(Arrays.asList(childAttr1, childAttr2))).build();
    final ArrayValue arrayValue = ArrayValue.newBuilder().addAllValues(Arrays.asList(anyValue1, anyValue2, anyValue3)).build();
    final KeyValue spanAttribute1 = KeyValue.newBuilder().setKey("aws.details").setValue(AnyValue.newBuilder().setArrayValue(arrayValue)).build();
    final Map<String, Object> actual = OTelProtoHelper.getResourceAttributes(Resource.newBuilder().addAllAttributes(Collections.singletonList(spanAttribute1)).build());
    assertThat(actual.containsKey(OTelProtoHelper.RESOURCE_ATTRIBUTES_REPLACE_DOT_WITH_AT.apply(spanAttribute1.getKey()))).isTrue();
    final List<Object> actualValue = returnList((String) actual.get(OTelProtoHelper.RESOURCE_ATTRIBUTES_REPLACE_DOT_WITH_AT.apply(spanAttribute1.getKey())));
    assertThat((actualValue.get(0)).equals(anyValue1.getStringValue())).isTrue();
    assertThat(((Double) actualValue.get(1)) == (anyValue2.getDoubleValue())).isTrue();
    final Map<String, Object> map = returnMap((String) actualValue.get(2));
    assertThat((Integer) map.get(OTelProtoHelper.REPLACE_DOT_WITH_AT.apply(childAttr1.getKey())) == childAttr1.getValue().getIntValue()).isTrue();
    assertThat(map.get(OTelProtoHelper.REPLACE_DOT_WITH_AT.apply(childAttr2.getKey())).equals(childAttr2.getValue().getStringValue())).isTrue();
    assertThat((Integer) map.get(OTelProtoHelper.REPLACE_DOT_WITH_AT.apply(childAttr1.getKey())) == (childAttr1.getValue().getIntValue())).isTrue();
}
Also used : KeyValue(io.opentelemetry.proto.common.v1.KeyValue) AnyValue(io.opentelemetry.proto.common.v1.AnyValue) ByteString(com.google.protobuf.ByteString) ArrayValue(io.opentelemetry.proto.common.v1.ArrayValue) Test(org.junit.Test)

Example 2 with AnyValue

use of io.opentelemetry.proto.common.v1.AnyValue in project opentelemetry-java-instrumentation by open-telemetry.

the class AgentTestingExporterAccess method fromProto.

private static Attributes fromProto(List<KeyValue> attributes) {
    AttributesBuilder converted = Attributes.builder();
    for (KeyValue attribute : attributes) {
        String key = attribute.getKey();
        AnyValue value = attribute.getValue();
        switch(value.getValueCase()) {
            case STRING_VALUE:
                converted.put(key, value.getStringValue());
                break;
            case BOOL_VALUE:
                converted.put(key, value.getBoolValue());
                break;
            case INT_VALUE:
                converted.put(key, value.getIntValue());
                break;
            case DOUBLE_VALUE:
                converted.put(key, value.getDoubleValue());
                break;
            case ARRAY_VALUE:
                ArrayValue array = value.getArrayValue();
                if (array.getValuesCount() != 0) {
                    switch(array.getValues(0).getValueCase()) {
                        case STRING_VALUE:
                            converted.put(stringArrayKey(key), array.getValuesList().stream().map(AnyValue::getStringValue).collect(toList()));
                            break;
                        case BOOL_VALUE:
                            converted.put(booleanArrayKey(key), array.getValuesList().stream().map(AnyValue::getBoolValue).collect(toList()));
                            break;
                        case INT_VALUE:
                            converted.put(longArrayKey(key), array.getValuesList().stream().map(AnyValue::getIntValue).collect(toList()));
                            break;
                        case DOUBLE_VALUE:
                            converted.put(doubleArrayKey(key), array.getValuesList().stream().map(AnyValue::getDoubleValue).collect(toList()));
                            break;
                        case VALUE_NOT_SET:
                            break;
                        default:
                            throw new IllegalStateException("Unexpected attribute: " + array.getValues(0).getValueCase());
                    }
                }
                break;
            case VALUE_NOT_SET:
                break;
            default:
                throw new IllegalStateException("Unexpected attribute: " + value.getValueCase());
        }
    }
    return converted.build();
}
Also used : KeyValue(io.opentelemetry.proto.common.v1.KeyValue) AnyValue(io.opentelemetry.proto.common.v1.AnyValue) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) ArrayValue(io.opentelemetry.proto.common.v1.ArrayValue)

Aggregations

AnyValue (io.opentelemetry.proto.common.v1.AnyValue)2 ArrayValue (io.opentelemetry.proto.common.v1.ArrayValue)2 KeyValue (io.opentelemetry.proto.common.v1.KeyValue)2 ByteString (com.google.protobuf.ByteString)1 AttributesBuilder (io.opentelemetry.api.common.AttributesBuilder)1 Test (org.junit.Test)1