|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_42.logs; |
| 7 | + |
| 8 | +import application.io.opentelemetry.api.common.KeyValue; |
| 9 | +import application.io.opentelemetry.api.common.Value; |
| 10 | +import application.io.opentelemetry.api.logs.LogRecordBuilder; |
| 11 | +import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_27.logs.ApplicationLogRecordBuilder; |
| 12 | +import java.nio.ByteBuffer; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +class ApplicationLogRecordBuilder142 extends ApplicationLogRecordBuilder |
| 17 | + implements LogRecordBuilder { |
| 18 | + |
| 19 | + private final io.opentelemetry.api.logs.LogRecordBuilder agentLogRecordBuilder; |
| 20 | + |
| 21 | + ApplicationLogRecordBuilder142(io.opentelemetry.api.logs.LogRecordBuilder agentLogRecordBuilder) { |
| 22 | + super(agentLogRecordBuilder); |
| 23 | + this.agentLogRecordBuilder = agentLogRecordBuilder; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public LogRecordBuilder setBody(Value<?> body) { |
| 28 | + agentLogRecordBuilder.setBody(convertValue(body)); |
| 29 | + return this; |
| 30 | + } |
| 31 | + |
| 32 | + @SuppressWarnings("unchecked") |
| 33 | + private static io.opentelemetry.api.common.Value<?> convertValue(Value<?> value) { |
| 34 | + if (value == null) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + switch (value.getType()) { |
| 39 | + case STRING: |
| 40 | + return io.opentelemetry.api.common.Value.of((String) value.getValue()); |
| 41 | + case BOOLEAN: |
| 42 | + return io.opentelemetry.api.common.Value.of((Boolean) value.getValue()); |
| 43 | + case LONG: |
| 44 | + return io.opentelemetry.api.common.Value.of((Long) value.getValue()); |
| 45 | + case DOUBLE: |
| 46 | + return io.opentelemetry.api.common.Value.of((Double) value.getValue()); |
| 47 | + case ARRAY: |
| 48 | + List<Value<?>> values = (List<Value<?>>) value.getValue(); |
| 49 | + List<io.opentelemetry.api.common.Value<?>> convertedValues = new ArrayList<>(); |
| 50 | + for (Value<?> source : values) { |
| 51 | + convertedValues.add(convertValue(source)); |
| 52 | + } |
| 53 | + return io.opentelemetry.api.common.Value.of(convertedValues); |
| 54 | + case KEY_VALUE_LIST: |
| 55 | + List<KeyValue> keyValueList = (List<KeyValue>) value.getValue(); |
| 56 | + io.opentelemetry.api.common.KeyValue[] convertedKeyValueList = |
| 57 | + new io.opentelemetry.api.common.KeyValue[keyValueList.size()]; |
| 58 | + int i = 0; |
| 59 | + for (KeyValue source : keyValueList) { |
| 60 | + convertedKeyValueList[i++] = |
| 61 | + io.opentelemetry.api.common.KeyValue.of( |
| 62 | + source.getKey(), convertValue(source.getValue())); |
| 63 | + } |
| 64 | + return io.opentelemetry.api.common.Value.of(convertedKeyValueList); |
| 65 | + case BYTES: |
| 66 | + ByteBuffer byteBuffer = (ByteBuffer) value.getValue(); |
| 67 | + byte[] bytes = new byte[byteBuffer.remaining()]; |
| 68 | + byteBuffer.get(bytes); |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + throw new IllegalStateException("Unhandled value type: " + value.getType()); |
| 73 | + } |
| 74 | +} |
0 commit comments