Skip to content

Commit 8263207

Browse files
committed
Add buildMetricValue method to DataPoint.Summary
1 parent afc77da commit 8263207

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ public String getMetricName() {
182182
return metric.getName();
183183
}
184184

185+
@Override
186+
public void buildMetricValue(XContentBuilder builder) throws IOException {
187+
// TODO: Add support for quantiles
188+
builder.startObject();
189+
builder.field("sum", dataPoint.getSum());
190+
builder.field("value_count", dataPoint.getCount());
191+
builder.endObject();
192+
}
193+
185194
@Override
186195
public String getDynamicTemplate() {
187196
return "summary";

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

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

10+
import com.google.protobuf.ByteString;
1011
import io.opentelemetry.proto.common.v1.InstrumentationScope;
1112
import io.opentelemetry.proto.common.v1.KeyValue;
1213
import io.opentelemetry.proto.metrics.v1.AggregationTemporality;
14+
import io.opentelemetry.proto.metrics.v1.Metric;
1315
import io.opentelemetry.proto.metrics.v1.NumberDataPoint;
16+
import io.opentelemetry.proto.metrics.v1.SummaryDataPoint;
1417
import io.opentelemetry.proto.resource.v1.Resource;
15-
16-
import com.google.protobuf.ByteString;
17-
1818
import org.elasticsearch.common.bytes.BytesReference;
1919
import org.elasticsearch.test.ESTestCase;
2020
import org.elasticsearch.test.rest.ObjectPath;
@@ -36,6 +36,7 @@
3636
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createGaugeMetric;
3737
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createLongDataPoint;
3838
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createSumMetric;
39+
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createSummaryMetric;
3940
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.keyValue;
4041
import static org.hamcrest.Matchers.equalTo;
4142
import static org.hamcrest.Matchers.hasEntry;
@@ -191,4 +192,36 @@ public void testEmptyFields() throws IOException {
191192
assertThat(doc.evaluate("unit"), is(nullValue()));
192193
}
193194

195+
public void testSummary() throws Exception {
196+
Resource resource = Resource.newBuilder().build();
197+
InstrumentationScope scope = InstrumentationScope.newBuilder().build();
198+
199+
SummaryDataPoint dataPoint = SummaryDataPoint.newBuilder()
200+
.setTimeUnixNano(timestamp)
201+
.setStartTimeUnixNano(startTimestamp)
202+
.setCount(1)
203+
.setSum(42.0)
204+
.build();
205+
Metric metric = createSummaryMetric("summary", "", List.of());
206+
List<DataPoint> dataPoints = List.of(new DataPoint.Summary(dataPoint, metric));
207+
208+
DataPointGroupingContext.DataPointGroup dataPointGroup = new DataPointGroupingContext.DataPointGroup(
209+
resource,
210+
null,
211+
scope,
212+
null,
213+
List.of(),
214+
"",
215+
dataPoints,
216+
"metrics-generic.otel-default"
217+
);
218+
219+
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
220+
HashMap<String, String> dynamicTemplates = documentBuilder.buildMetricDocument(builder, dataPointGroup);
221+
222+
ObjectPath doc = ObjectPath.createFromXContent(JsonXContent.jsonXContent, BytesReference.bytes(builder));
223+
assertThat(doc.evaluate("metrics.summary.sum"), equalTo(42.0));
224+
assertThat(doc.evaluate("metrics.summary.value_count"), equalTo(1));
225+
assertThat(dynamicTemplates, hasEntry("metrics.summary", "summary"));
226+
}
194227
}

0 commit comments

Comments
 (0)