|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.oteldata.otlp.docbuilder; |
| 9 | + |
| 10 | +import io.opentelemetry.proto.common.v1.AnyValue; |
| 11 | +import io.opentelemetry.proto.common.v1.InstrumentationScope; |
| 12 | +import io.opentelemetry.proto.common.v1.KeyValue; |
| 13 | +import io.opentelemetry.proto.resource.v1.Resource; |
| 14 | + |
| 15 | +import com.google.protobuf.ByteString; |
| 16 | + |
| 17 | +import org.elasticsearch.common.Strings; |
| 18 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 19 | +import org.elasticsearch.xpack.oteldata.otlp.datapoint.DataPoint; |
| 20 | +import org.elasticsearch.xpack.oteldata.otlp.datapoint.DataPointGroupingContext; |
| 21 | +import org.elasticsearch.xpack.oteldata.otlp.proto.BufferedByteStringAccessor; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
| 28 | +/** |
| 29 | + * This class constructs an Elasticsearch document representation of a metric data point group. |
| 30 | + * It also handles dynamic templates for metrics based on their attributes. |
| 31 | + */ |
| 32 | +public class MetricDocumentBuilder { |
| 33 | + |
| 34 | + private final BufferedByteStringAccessor byteStringAccessor; |
| 35 | + |
| 36 | + public MetricDocumentBuilder(BufferedByteStringAccessor byteStringAccessor) { |
| 37 | + this.byteStringAccessor = byteStringAccessor; |
| 38 | + } |
| 39 | + |
| 40 | + public HashMap<String, String> buildMetricDocument(XContentBuilder builder, DataPointGroupingContext.DataPointGroup dataPointGroup) |
| 41 | + throws IOException { |
| 42 | + HashMap<String, String> dynamicTemplates = new HashMap<>(); |
| 43 | + List<DataPoint> dataPoints = dataPointGroup.dataPoints(); |
| 44 | + builder.startObject(); |
| 45 | + builder.field("@timestamp", TimeUnit.NANOSECONDS.toMillis(dataPointGroup.getTimestampUnixNano())); |
| 46 | + if (dataPointGroup.getStartTimestampUnixNano() != 0) { |
| 47 | + builder.field("start_timestamp", TimeUnit.NANOSECONDS.toMillis(dataPointGroup.getStartTimestampUnixNano())); |
| 48 | + } |
| 49 | + buildResource(dataPointGroup.resource(), dataPointGroup.resourceSchemaUrl(), builder); |
| 50 | + buildScope(builder, dataPointGroup.scopeSchemaUrl(), dataPointGroup.scope()); |
| 51 | + buildDataPointAttributes(builder, dataPointGroup.dataPointAttributes(), dataPointGroup.unit()); |
| 52 | + builder.field("_metric_names_hash", dataPointGroup.getMetricNamesHash()); |
| 53 | + |
| 54 | + builder.startObject("metrics"); |
| 55 | + for (int i = 0, dataPointsSize = dataPoints.size(); i < dataPointsSize; i++) { |
| 56 | + DataPoint dataPoint = dataPoints.get(i); |
| 57 | + builder.field(dataPoint.getMetricName()); |
| 58 | + dataPoint.buildMetricValue(builder); |
| 59 | + String dynamicTemplate = dataPoint.getDynamicTemplate(); |
| 60 | + if (dynamicTemplate != null) { |
| 61 | + dynamicTemplates.put("metrics." + dataPoint.getMetricName(), dynamicTemplate); |
| 62 | + } |
| 63 | + } |
| 64 | + builder.endObject(); |
| 65 | + builder.endObject(); |
| 66 | + return dynamicTemplates; |
| 67 | + } |
| 68 | + |
| 69 | + private void buildResource(Resource resource, ByteString schemaUrl, XContentBuilder builder) throws IOException { |
| 70 | + builder.startObject("resource"); |
| 71 | + addFieldIfNotEmpty(builder, "schema_url", schemaUrl); |
| 72 | + if (resource.getDroppedAttributesCount() > 0) { |
| 73 | + builder.field("dropped_attributes_count", resource.getDroppedAttributesCount()); |
| 74 | + } |
| 75 | + builder.startObject("attributes"); |
| 76 | + buildAttributes(builder, resource.getAttributesList()); |
| 77 | + builder.endObject(); |
| 78 | + builder.endObject(); |
| 79 | + } |
| 80 | + |
| 81 | + private void buildScope(XContentBuilder builder, ByteString schemaUrl, InstrumentationScope scope) throws IOException { |
| 82 | + builder.startObject("scope"); |
| 83 | + addFieldIfNotEmpty(builder, "schema_url", schemaUrl); |
| 84 | + if (scope.getDroppedAttributesCount() > 0) { |
| 85 | + builder.field("dropped_attributes_count", scope.getDroppedAttributesCount()); |
| 86 | + } |
| 87 | + addFieldIfNotEmpty(builder, "name", scope.getNameBytes()); |
| 88 | + addFieldIfNotEmpty(builder, "version", scope.getVersionBytes()); |
| 89 | + builder.startObject("attributes"); |
| 90 | + buildAttributes(builder, scope.getAttributesList()); |
| 91 | + builder.endObject(); |
| 92 | + builder.endObject(); |
| 93 | + } |
| 94 | + |
| 95 | + private void addFieldIfNotEmpty(XContentBuilder builder, String name, ByteString value) throws IOException { |
| 96 | + if (value != null && value.isEmpty() == false) { |
| 97 | + builder.field(name); |
| 98 | + byteStringAccessor.utf8Value(builder, value); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void buildDataPointAttributes(XContentBuilder builder, List<KeyValue> attributes, String unit) throws IOException { |
| 103 | + builder.startObject("attributes"); |
| 104 | + buildAttributes(builder, attributes); |
| 105 | + builder.endObject(); |
| 106 | + if (Strings.hasLength(unit)) { |
| 107 | + builder.field("unit", unit); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private void buildAttributes(XContentBuilder builder, List<KeyValue> attributes) throws IOException { |
| 112 | + for (int i = 0, size = attributes.size(); i < size; i++) { |
| 113 | + KeyValue attribute = attributes.get(i); |
| 114 | + builder.field(attribute.getKey()); |
| 115 | + attributeValue(builder, attribute.getValue()); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private void attributeValue(XContentBuilder builder, AnyValue value) throws IOException { |
| 120 | + switch (value.getValueCase()) { |
| 121 | + case STRING_VALUE -> byteStringAccessor.utf8Value(builder, value.getStringValueBytes()); |
| 122 | + case BOOL_VALUE -> builder.value(value.getBoolValue()); |
| 123 | + case INT_VALUE -> builder.value(value.getIntValue()); |
| 124 | + case DOUBLE_VALUE -> builder.value(value.getDoubleValue()); |
| 125 | + case ARRAY_VALUE -> { |
| 126 | + builder.startArray(); |
| 127 | + List<AnyValue> valuesList = value.getArrayValue().getValuesList(); |
| 128 | + for (int i = 0, valuesListSize = valuesList.size(); i < valuesListSize; i++) { |
| 129 | + AnyValue arrayValue = valuesList.get(i); |
| 130 | + attributeValue(builder, arrayValue); |
| 131 | + } |
| 132 | + builder.endArray(); |
| 133 | + } |
| 134 | + default -> throw new IllegalArgumentException("Unsupported attribute value type: " + value.getValueCase()); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments