Skip to content

Commit 95b1bda

Browse files
committed
Add buildMetricValue method to histogram data points
1 parent 2ed63ea commit 95b1bda

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ public String getMetricName() {
184184
return metric.getName();
185185
}
186186

187+
@Override
188+
public void buildMetricValue(XContentBuilder builder) throws IOException {
189+
builder.startObject();
190+
builder.startArray("counts");
191+
HistogramConverter.counts(dataPoint, builder::value);
192+
builder.endArray();
193+
builder.startArray("values");
194+
HistogramConverter.centroidValues(dataPoint, builder::value);
195+
builder.endArray();
196+
builder.endObject();
197+
}
198+
187199
@Override
188200
public String getDynamicTemplate() {
189201
return "histogram";
@@ -225,6 +237,18 @@ public String getMetricName() {
225237
return metric.getName();
226238
}
227239

240+
@Override
241+
public void buildMetricValue(XContentBuilder builder) throws IOException {
242+
builder.startObject();
243+
builder.startArray("counts");
244+
HistogramConverter.counts(dataPoint, builder::value);
245+
builder.endArray();
246+
builder.startArray("values");
247+
HistogramConverter.centroidValues(dataPoint, builder::value);
248+
builder.endArray();
249+
builder.endObject();
250+
}
251+
228252
@Override
229253
public String getDynamicTemplate() {
230254
return "histogram";

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import io.opentelemetry.proto.common.v1.InstrumentationScope;
1111
import io.opentelemetry.proto.common.v1.KeyValue;
1212
import io.opentelemetry.proto.metrics.v1.AggregationTemporality;
13+
import io.opentelemetry.proto.metrics.v1.ExponentialHistogramDataPoint;
14+
import io.opentelemetry.proto.metrics.v1.HistogramDataPoint;
15+
import io.opentelemetry.proto.metrics.v1.Metric;
1316
import io.opentelemetry.proto.metrics.v1.NumberDataPoint;
1417
import io.opentelemetry.proto.resource.v1.Resource;
1518

@@ -32,8 +35,11 @@
3235
import java.util.List;
3336
import java.util.concurrent.TimeUnit;
3437

38+
import static io.opentelemetry.proto.metrics.v1.AggregationTemporality.AGGREGATION_TEMPORALITY_DELTA;
3539
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createDoubleDataPoint;
40+
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createExponentialHistogramMetric;
3641
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createGaugeMetric;
42+
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createHistogramMetric;
3743
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createLongDataPoint;
3844
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createSumMetric;
3945
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.keyValue;
@@ -191,4 +197,71 @@ public void testEmptyFields() throws IOException {
191197
assertThat(doc.evaluate("unit"), is(nullValue()));
192198
}
193199

200+
public void testExponentialHistogram() throws Exception{
201+
Resource resource = Resource.newBuilder().build();
202+
InstrumentationScope scope = InstrumentationScope.newBuilder().build();
203+
204+
ExponentialHistogramDataPoint dataPoint = ExponentialHistogramDataPoint.newBuilder()
205+
.setTimeUnixNano(timestamp)
206+
.setStartTimeUnixNano(startTimestamp)
207+
.setZeroCount(1)
208+
.setPositive(ExponentialHistogramDataPoint.Buckets.newBuilder().setOffset(0).addAllBucketCounts(List.of(1L, 1L)))
209+
.setNegative(ExponentialHistogramDataPoint.Buckets.newBuilder().setOffset(0).addAllBucketCounts(List.of(1L, 1L)))
210+
.build();
211+
Metric metric = createExponentialHistogramMetric("exponential_histogram", "", List.of(), AGGREGATION_TEMPORALITY_DELTA);
212+
List<DataPoint> dataPoints = List.of(new DataPoint.ExponentialHistogram(dataPoint, metric));
213+
214+
DataPointGroupingContext.DataPointGroup dataPointGroup = new DataPointGroupingContext.DataPointGroup(
215+
resource,
216+
null,
217+
scope,
218+
null,
219+
List.of(),
220+
"",
221+
dataPoints,
222+
"metrics-generic.otel-default"
223+
);
224+
225+
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
226+
HashMap<String, String> dynamicTemplates = documentBuilder.buildMetricDocument(builder, dataPointGroup);
227+
228+
ObjectPath doc = ObjectPath.createFromXContent(JsonXContent.jsonXContent, BytesReference.bytes(builder));
229+
assertThat(doc.evaluate("metrics.exponential_histogram.values"), equalTo(List.of(-3.0, -1.5, 0.0, 1.5, 3.0)));
230+
assertThat(doc.evaluate("metrics.exponential_histogram.counts"), equalTo(List.of(1, 1, 1, 1, 1)));
231+
assertThat(dynamicTemplates, hasEntry("metrics.exponential_histogram", "histogram"));
232+
}
233+
234+
public void testHistogram() throws Exception{
235+
Resource resource = Resource.newBuilder().build();
236+
InstrumentationScope scope = InstrumentationScope.newBuilder().build();
237+
238+
HistogramDataPoint dataPoint = HistogramDataPoint.newBuilder()
239+
.setTimeUnixNano(timestamp)
240+
.setStartTimeUnixNano(startTimestamp)
241+
.addBucketCounts(10L)
242+
.addExplicitBounds(5.0)
243+
.build();
244+
Metric metric = createHistogramMetric("histogram", "", List.of(), AGGREGATION_TEMPORALITY_DELTA);
245+
List<DataPoint> dataPoints = List.of(new DataPoint.Histogram(dataPoint, metric));
246+
247+
DataPointGroupingContext.DataPointGroup dataPointGroup = new DataPointGroupingContext.DataPointGroup(
248+
resource,
249+
null,
250+
scope,
251+
null,
252+
List.of(),
253+
"",
254+
dataPoints,
255+
"metrics-generic.otel-default"
256+
);
257+
258+
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
259+
HashMap<String, String> dynamicTemplates = documentBuilder.buildMetricDocument(builder, dataPointGroup);
260+
261+
ObjectPath doc = ObjectPath.createFromXContent(JsonXContent.jsonXContent, BytesReference.bytes(builder));
262+
assertThat(doc.evaluate("metrics.histogram.values"), equalTo(List.of(2.5)));
263+
assertThat(doc.evaluate("metrics.histogram.counts"), equalTo(List.of(10)));
264+
assertThat(dynamicTemplates, hasEntry("metrics.histogram", "histogram"));
265+
}
266+
194267
}

0 commit comments

Comments
 (0)