Skip to content

Commit 2b3bfad

Browse files
committed
Add support for mapping hints
1 parent defda7e commit 2b3bfad

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

x-pack/plugin/otel-data/src/main/java/org/elasticsearch/xpack/oteldata/otlp/datapoint/DataPoint.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ public interface DataPoint {
7373
/**
7474
* Builds the metric value for the data point and writes it to the provided XContentBuilder.
7575
*
76+
* @param mappingHints hints for building the metric value
7677
* @param builder the XContentBuilder to write the metric value to
7778
* @throws IOException if an I/O error occurs while writing to the builder
7879
*/
79-
void buildMetricValue(XContentBuilder builder) throws IOException;
80+
void buildMetricValue(MappingHints mappingHints, XContentBuilder builder) throws IOException;
8081

8182
/**
8283
* Returns the dynamic template name for the data point based on its type and value.
@@ -131,7 +132,7 @@ public String getMetricName() {
131132
}
132133

133134
@Override
134-
public void buildMetricValue(XContentBuilder builder) throws IOException {
135+
public void buildMetricValue(MappingHints mappingHints, XContentBuilder builder) throws IOException {
135136
switch (dataPoint.getValueCase()) {
136137
case AS_DOUBLE -> builder.value(dataPoint.getAsDouble());
137138
case AS_INT -> builder.value(dataPoint.getAsInt());
@@ -198,16 +199,18 @@ public String getMetricName() {
198199
}
199200

200201
@Override
201-
public void buildMetricValue(XContentBuilder builder) throws IOException {
202+
public void buildMetricValue(MappingHints mappingHints, XContentBuilder builder) throws IOException {
202203
// TODO: Add support for quantiles
203-
builder.startObject();
204-
builder.field("sum", dataPoint.getSum());
205-
builder.field("value_count", dataPoint.getCount());
206-
builder.endObject();
204+
buildAggregateMetricDouble(builder, dataPoint.getSum(), dataPoint.getCount());
207205
}
208206

209207
@Override
210-
public String getDynamicTemplate() {
208+
public long getDocCount() {
209+
return dataPoint.getCount();
210+
}
211+
212+
@Override
213+
public String getDynamicTemplate(MappingHints mappingHints) {
211214
return "summary";
212215
}
213216

@@ -216,4 +219,11 @@ public boolean isValid(Set<String> errors) {
216219
return true;
217220
}
218221
}
222+
223+
private static void buildAggregateMetricDouble(XContentBuilder builder, double sum, long valueCount) throws IOException {
224+
builder.startObject();
225+
builder.field("sum", sum);
226+
builder.field("value_count", valueCount);
227+
builder.endObject();
228+
}
219229
}

x-pack/plugin/otel-data/src/main/java/org/elasticsearch/xpack/oteldata/otlp/docbuilder/MappingHints.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
*/
2929
public record MappingHints(boolean aggregateMetricDouble, boolean docCount) {
3030
public static final String MAPPING_HINTS = "elasticsearch.mapping.hints";
31+
public static final String AGGREGATE_METRIC_DOUBLE = "aggregate_metric_double";
32+
public static final String DOC_COUNT = "_doc_count";
3133

3234
private static final MappingHints EMPTY = new MappingHints(false, false);
33-
private static final String AGGREGATE_METRIC_DOUBLE = "aggregate_metric_double";
34-
private static final String DOC_COUNT = "_doc_count";
3535

3636
public static MappingHints fromAttributes(List<KeyValue> attributes) {
3737
boolean aggregateMetricDouble = false;

x-pack/plugin/otel-data/src/main/java/org/elasticsearch/xpack/oteldata/otlp/docbuilder/MetricDocumentBuilder.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77

88
package org.elasticsearch.xpack.oteldata.otlp.docbuilder;
99

10+
import com.google.protobuf.ByteString;
1011
import io.opentelemetry.proto.common.v1.AnyValue;
1112
import io.opentelemetry.proto.common.v1.InstrumentationScope;
1213
import io.opentelemetry.proto.common.v1.KeyValue;
1314
import io.opentelemetry.proto.resource.v1.Resource;
14-
15-
import com.google.protobuf.ByteString;
16-
1715
import org.elasticsearch.common.Strings;
1816
import org.elasticsearch.xcontent.XContentBuilder;
1917
import org.elasticsearch.xpack.oteldata.otlp.datapoint.DataPoint;
@@ -51,17 +49,25 @@ public HashMap<String, String> buildMetricDocument(XContentBuilder builder, Data
5149
buildDataPointAttributes(builder, dataPointGroup.dataPointAttributes(), dataPointGroup.unit());
5250
builder.field("_metric_names_hash", dataPointGroup.getMetricNamesHash());
5351

52+
long docCount = 0;
5453
builder.startObject("metrics");
5554
for (int i = 0, dataPointsSize = dataPoints.size(); i < dataPointsSize; i++) {
5655
DataPoint dataPoint = dataPoints.get(i);
5756
builder.field(dataPoint.getMetricName());
58-
dataPoint.buildMetricValue(builder);
59-
String dynamicTemplate = dataPoint.getDynamicTemplate(MappingHints.empty());
57+
MappingHints mappingHints = MappingHints.fromAttributes(dataPoint.getAttributes());
58+
dataPoint.buildMetricValue(mappingHints, builder);
59+
String dynamicTemplate = dataPoint.getDynamicTemplate(mappingHints);
6060
if (dynamicTemplate != null) {
6161
dynamicTemplates.put("metrics." + dataPoint.getMetricName(), dynamicTemplate);
6262
}
63+
if (mappingHints.docCount()) {
64+
docCount = dataPoint.getDocCount();
65+
}
6366
}
6467
builder.endObject();
68+
if (docCount > 0) {
69+
builder.field("_doc_count", docCount);
70+
}
6571
builder.endObject();
6672
return dynamicTemplates;
6773
}

x-pack/plugin/otel-data/src/test/java/org/elasticsearch/xpack/oteldata/otlp/OtlpUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import io.opentelemetry.proto.metrics.v1.SummaryDataPoint;
2424
import io.opentelemetry.proto.resource.v1.Resource;
2525

26+
import org.elasticsearch.xpack.oteldata.otlp.docbuilder.MappingHints;
27+
2628
import java.util.ArrayList;
2729
import java.util.Arrays;
2830
import java.util.List;
@@ -40,6 +42,10 @@ public static KeyValue keyValue(String key, String value) {
4042
return KeyValue.newBuilder().setKey(key).setValue(AnyValue.newBuilder().setStringValue(value).build()).build();
4143
}
4244

45+
public static List<KeyValue> mappingHints(String... mappingHints) {
46+
return List.of(keyValue(MappingHints.MAPPING_HINTS, mappingHints));
47+
}
48+
4349
public static KeyValue keyValue(String key, String... values) {
4450
return KeyValue.newBuilder()
4551
.setKey(key)

x-pack/plugin/otel-data/src/test/java/org/elasticsearch/xpack/oteldata/otlp/docbuilder/MetricDocumentBuilderTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createSumMetric;
4141
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createSummaryMetric;
4242
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.keyValue;
43+
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.mappingHints;
4344
import static org.hamcrest.Matchers.equalTo;
4445
import static org.hamcrest.Matchers.hasEntry;
4546
import static org.hamcrest.Matchers.is;
@@ -203,6 +204,7 @@ public void testSummary() throws Exception {
203204
.setStartTimeUnixNano(startTimestamp)
204205
.setCount(1)
205206
.setSum(42.0)
207+
.addAllAttributes(mappingHints(MappingHints.DOC_COUNT))
206208
.build();
207209
Metric metric = createSummaryMetric("summary", "", List.of());
208210
List<DataPoint> dataPoints = List.of(new DataPoint.Summary(dataPoint, metric));
@@ -224,6 +226,7 @@ public void testSummary() throws Exception {
224226
ObjectPath doc = ObjectPath.createFromXContent(JsonXContent.jsonXContent, BytesReference.bytes(builder));
225227
assertThat(doc.evaluate("metrics.summary.sum"), equalTo(42.0));
226228
assertThat(doc.evaluate("metrics.summary.value_count"), equalTo(1));
229+
assertThat(doc.evaluate("_doc_count"), equalTo(1));
227230
assertThat(dynamicTemplates, hasEntry("metrics.summary", "summary"));
228231
}
229232
}

0 commit comments

Comments
 (0)