Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.opentelemetry.proto.common.v1.KeyValue;
import io.opentelemetry.proto.metrics.v1.Metric;
import io.opentelemetry.proto.metrics.v1.NumberDataPoint;
import io.opentelemetry.proto.metrics.v1.SummaryDataPoint;

import org.elasticsearch.xcontent.XContentBuilder;

Expand Down Expand Up @@ -153,4 +154,51 @@ public boolean isValid(Set<String> errors) {
return true;
}
}

record Summary(SummaryDataPoint dataPoint, Metric metric) implements DataPoint {

@Override
public long getTimestampUnixNano() {
return dataPoint.getTimeUnixNano();
}

@Override
public List<KeyValue> getAttributes() {
return dataPoint.getAttributesList();
}

@Override
public long getStartTimestampUnixNano() {
return dataPoint.getStartTimeUnixNano();
}

@Override
public String getUnit() {
return metric.getUnit();
}

@Override
public String getMetricName() {
return metric.getName();
}

@Override
public void buildMetricValue(XContentBuilder builder) throws IOException {
// TODO: Add support for quantiles
builder.startObject();
builder.field("sum", dataPoint.getSum());
builder.field("value_count", dataPoint.getCount());
builder.endObject();
}

@Override
public String getDynamicTemplate() {
return "summary";
}

@Override
public boolean isValid(Set<String> errors) {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public void groupDataPoints(ExportMetricsServiceRequest exportMetricsServiceRequ
ignoredDataPointMessages.add("Histogram is not supported yet. Dropping " + metric.getName());
break;
case SUMMARY:
ignoredDataPoints += metric.getSummary().getDataPointsList().size();
ignoredDataPointMessages.add("Summary is not supported yet. Dropping " + metric.getName());
scopeGroup.addDataPoints(metric, metric.getSummary().getDataPointsList(), DataPoint.Summary::new);
break;
default:
ignoredDataPoints++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import io.opentelemetry.proto.metrics.v1.ResourceMetrics;
import io.opentelemetry.proto.metrics.v1.ScopeMetrics;
import io.opentelemetry.proto.metrics.v1.Sum;
import io.opentelemetry.proto.metrics.v1.Summary;
import io.opentelemetry.proto.metrics.v1.SummaryDataPoint;
import io.opentelemetry.proto.resource.v1.Resource;

import java.util.ArrayList;
Expand Down Expand Up @@ -105,6 +107,14 @@ public static Metric createSumMetric(
.build();
}

public static Metric createSummaryMetric(String name, String unit, List<SummaryDataPoint> dataPoints) {
return Metric.newBuilder()
.setName(name)
.setUnit(unit)
.setSummary(Summary.newBuilder().addAllDataPoints(dataPoints).build())
.build();
}

public static NumberDataPoint createDoubleDataPoint(long timestamp) {
return createDoubleDataPoint(timestamp, timestamp, List.of());
}
Expand All @@ -131,6 +141,16 @@ public static NumberDataPoint createLongDataPoint(long timeUnixNano, long startT
.build();
}

public static SummaryDataPoint createSummaryDataPoint(long timestamp, List<KeyValue> attributes) {
return SummaryDataPoint.newBuilder()
.setTimeUnixNano(timestamp)
.setStartTimeUnixNano(timestamp)
.addAllAttributes(attributes)
.setCount(randomLong())
.setSum(randomDouble())
.build();
}

public static ExportMetricsServiceRequest createMetricsRequest(List<Metric> metrics) {

List<ResourceMetrics> resourceMetrics = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createResourceMetrics;
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createScopeMetrics;
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createSumMetric;
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createSummaryDataPoint;
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createSummaryMetric;
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.keyValue;

public class DataPointGroupingContextTests extends ESTestCase {
Expand All @@ -43,11 +45,12 @@ public void testGroupingSameGroup() throws Exception {
List.of(createLongDataPoint(nowUnixNanos)),
true,
AGGREGATION_TEMPORALITY_CUMULATIVE
)
),
createSummaryMetric("summary", "", List.of(createSummaryDataPoint(nowUnixNanos, List.of())))
)
);
context.groupDataPoints(metricsRequest);
assertEquals(3, context.totalDataPoints());
assertEquals(4, context.totalDataPoints());
assertEquals(0, context.getIgnoredDataPoints());
assertEquals("", context.getIgnoredDataPointsMessage());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import io.opentelemetry.proto.common.v1.InstrumentationScope;
import io.opentelemetry.proto.common.v1.KeyValue;
import io.opentelemetry.proto.metrics.v1.AggregationTemporality;
import io.opentelemetry.proto.metrics.v1.Metric;
import io.opentelemetry.proto.metrics.v1.NumberDataPoint;
import io.opentelemetry.proto.metrics.v1.SummaryDataPoint;
import io.opentelemetry.proto.resource.v1.Resource;

import com.google.protobuf.ByteString;
Expand All @@ -36,6 +38,7 @@
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createGaugeMetric;
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createLongDataPoint;
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createSumMetric;
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.createSummaryMetric;
import static org.elasticsearch.xpack.oteldata.otlp.OtlpUtils.keyValue;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
Expand Down Expand Up @@ -191,4 +194,36 @@ public void testEmptyFields() throws IOException {
assertThat(doc.evaluate("unit"), is(nullValue()));
}

public void testSummary() throws Exception {
Resource resource = Resource.newBuilder().build();
InstrumentationScope scope = InstrumentationScope.newBuilder().build();

SummaryDataPoint dataPoint = SummaryDataPoint.newBuilder()
.setTimeUnixNano(timestamp)
.setStartTimeUnixNano(startTimestamp)
.setCount(1)
.setSum(42.0)
.build();
Metric metric = createSummaryMetric("summary", "", List.of());
List<DataPoint> dataPoints = List.of(new DataPoint.Summary(dataPoint, metric));

DataPointGroupingContext.DataPointGroup dataPointGroup = new DataPointGroupingContext.DataPointGroup(
resource,
null,
scope,
null,
List.of(),
"",
dataPoints,
"metrics-generic.otel-default"
);

XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
HashMap<String, String> dynamicTemplates = documentBuilder.buildMetricDocument(builder, dataPointGroup);

ObjectPath doc = ObjectPath.createFromXContent(JsonXContent.jsonXContent, BytesReference.bytes(builder));
assertThat(doc.evaluate("metrics.summary.sum"), equalTo(42.0));
assertThat(doc.evaluate("metrics.summary.value_count"), equalTo(1));
assertThat(dynamicTemplates, hasEntry("metrics.summary", "summary"));
}
}