Skip to content

Commit bede881

Browse files
authored
OTLP: reuse hasher for creating _metric_names_hash (elastic#133941)
1 parent 7a86ddf commit bede881

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

server/src/main/java/org/elasticsearch/common/hash/MurmurHash3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public boolean equals(Object other) {
6060

6161
@Override
6262
public int hashCode() {
63-
return (int) (h1 ^ h2);
63+
return Long.hashCode(h1 ^ h2);
6464
}
6565

6666
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ public long getStartTimestampUnixNano() {
265265
return dataPoints.getFirst().getStartTimestampUnixNano();
266266
}
267267

268-
public String getMetricNamesHash() {
268+
public String getMetricNamesHash(BufferedMurmur3Hasher hasher) {
269269
if (metricNamesHash == null) {
270-
BufferedMurmur3Hasher hasher = new BufferedMurmur3Hasher(0);
270+
hasher.reset();
271271
for (int i = 0; i < dataPoints.size(); i++) {
272272
hasher.addString(dataPoints.get(i).getMetricName());
273273
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.google.protobuf.ByteString;
1616

1717
import org.elasticsearch.common.Strings;
18+
import org.elasticsearch.common.hash.BufferedMurmur3Hasher;
1819
import org.elasticsearch.xcontent.XContentBuilder;
1920
import org.elasticsearch.xpack.oteldata.otlp.datapoint.DataPoint;
2021
import org.elasticsearch.xpack.oteldata.otlp.datapoint.DataPointGroupingContext;
@@ -32,6 +33,7 @@
3233
public class MetricDocumentBuilder {
3334

3435
private final BufferedByteStringAccessor byteStringAccessor;
36+
private final BufferedMurmur3Hasher hasher = new BufferedMurmur3Hasher(0);
3537

3638
public MetricDocumentBuilder(BufferedByteStringAccessor byteStringAccessor) {
3739
this.byteStringAccessor = byteStringAccessor;
@@ -49,7 +51,7 @@ public HashMap<String, String> buildMetricDocument(XContentBuilder builder, Data
4951
buildResource(dataPointGroup.resource(), dataPointGroup.resourceSchemaUrl(), builder);
5052
buildScope(builder, dataPointGroup.scopeSchemaUrl(), dataPointGroup.scope());
5153
buildDataPointAttributes(builder, dataPointGroup.dataPointAttributes(), dataPointGroup.unit());
52-
builder.field("_metric_names_hash", dataPointGroup.getMetricNamesHash());
54+
builder.field("_metric_names_hash", dataPointGroup.getMetricNamesHash(hasher));
5355

5456
builder.startObject("metrics");
5557
for (int i = 0, dataPointsSize = dataPoints.size(); i < dataPointsSize; i++) {

0 commit comments

Comments
 (0)