-
Notifications
You must be signed in to change notification settings - Fork 25.6k
OTLP: add mapping hints #133905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
OTLP: add mapping hints #133905
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...tel-data/src/main/java/org/elasticsearch/xpack/oteldata/otlp/docbuilder/MappingHints.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.oteldata.otlp.docbuilder; | ||
|
||
import io.opentelemetry.proto.common.v1.AnyValue; | ||
import io.opentelemetry.proto.common.v1.KeyValue; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Represents mapping hints that can be used to influence how data is indexed in Elasticsearch. | ||
* These hints can be provided by users via data point attributes. | ||
* | ||
* @param aggregateMetricDouble Indicates that the metric should be mapped as an aggregate_metric_double. | ||
* This hint is available for histogram and exponential histogram metrics. | ||
* @param docCount Indicates that the metric should be mapped with a _doc_count field. | ||
* This hint is available for all metric types. | ||
* When used for a histogram, exponential histogram, or summary metric, | ||
* the _doc_count field will be populated with the number of total counts. | ||
* It is not recommended to use this hint for multiple metrics that are grouped together | ||
* into the same document. | ||
* In these cases, the behavior is undefined but does not lead to data loss. | ||
*/ | ||
public record MappingHints(boolean aggregateMetricDouble, boolean docCount) { | ||
public static final String MAPPING_HINTS = "elasticsearch.mapping.hints"; | ||
|
||
private static final MappingHints EMPTY = new MappingHints(false, false); | ||
private static final String AGGREGATE_METRIC_DOUBLE = "aggregate_metric_double"; | ||
private static final String DOC_COUNT = "_doc_count"; | ||
|
||
public static MappingHints fromAttributes(List<KeyValue> attributes) { | ||
boolean aggregateMetricDouble = false; | ||
boolean docCount = false; | ||
for (int i = 0, attributesSize = attributes.size(); i < attributesSize; i++) { | ||
KeyValue attribute = attributes.get(i); | ||
if (attribute.getKey().equals(MAPPING_HINTS)) { | ||
if (attribute.getValue().hasArrayValue()) { | ||
List<AnyValue> valuesList = attribute.getValue().getArrayValue().getValuesList(); | ||
for (int j = 0, valuesListSize = valuesList.size(); j < valuesListSize; j++) { | ||
AnyValue hint = valuesList.get(j); | ||
if (hint.hasStringValue()) { | ||
String value = hint.getStringValue(); | ||
if (value.equals(AGGREGATE_METRIC_DOUBLE)) { | ||
aggregateMetricDouble = true; | ||
} else if (value.equals(DOC_COUNT)) { | ||
docCount = true; | ||
} | ||
} | ||
} | ||
} | ||
return new MappingHints(aggregateMetricDouble, docCount); | ||
} | ||
} | ||
return EMPTY; | ||
} | ||
|
||
public static MappingHints empty() { | ||
return EMPTY; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...data/src/test/java/org/elasticsearch/xpack/oteldata/otlp/datapoint/MappingHintsTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.oteldata.otlp.datapoint; | ||
|
||
import io.opentelemetry.proto.common.v1.AnyValue; | ||
import io.opentelemetry.proto.common.v1.ArrayValue; | ||
import io.opentelemetry.proto.common.v1.KeyValue; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xpack.oteldata.otlp.docbuilder.MappingHints; | ||
|
||
import java.util.List; | ||
|
||
public class MappingHintsTests extends ESTestCase { | ||
|
||
public void testEmptyAttributes() { | ||
MappingHints hints = MappingHints.fromAttributes(List.of()); | ||
assertFalse(hints.aggregateMetricDouble()); | ||
assertFalse(hints.docCount()); | ||
} | ||
|
||
public void testNoMappingHints() { | ||
KeyValue kv = KeyValue.newBuilder() | ||
.setKey("some.other.key") | ||
.setValue(AnyValue.newBuilder().setStringValue("some_value").build()) | ||
.build(); | ||
MappingHints hints = MappingHints.fromAttributes(List.of(kv)); | ||
assertFalse(hints.aggregateMetricDouble()); | ||
assertFalse(hints.docCount()); | ||
} | ||
|
||
public void testSingleMappingHint() { | ||
// Test with just aggregate_metric_double hint | ||
KeyValue aggregateMetricHint = createMappingHint("aggregate_metric_double"); | ||
MappingHints hints = MappingHints.fromAttributes(List.of(aggregateMetricHint)); | ||
assertTrue(hints.aggregateMetricDouble()); | ||
assertFalse(hints.docCount()); | ||
|
||
// Test with just _doc_count hint | ||
KeyValue docCountHint = createMappingHint("_doc_count"); | ||
hints = MappingHints.fromAttributes(List.of(docCountHint)); | ||
assertFalse(hints.aggregateMetricDouble()); | ||
assertTrue(hints.docCount()); | ||
} | ||
|
||
public void testMultipleMappingHints() { | ||
// Test with both hints | ||
KeyValue bothHints = createMappingHint("aggregate_metric_double", "_doc_count"); | ||
MappingHints hints = MappingHints.fromAttributes(List.of(bothHints)); | ||
assertTrue(hints.aggregateMetricDouble()); | ||
assertTrue(hints.docCount()); | ||
} | ||
|
||
public void testInvalidHints() { | ||
// Test with invalid hint | ||
KeyValue invalidHint = createMappingHint("invalid_hint"); | ||
MappingHints hints = MappingHints.fromAttributes(List.of(invalidHint)); | ||
assertFalse(hints.aggregateMetricDouble()); | ||
assertFalse(hints.docCount()); | ||
|
||
// Test with mix of valid and invalid hints | ||
KeyValue mixedHints = createMappingHint("aggregate_metric_double", "invalid_hint", "_doc_count"); | ||
hints = MappingHints.fromAttributes(List.of(mixedHints)); | ||
assertTrue(hints.aggregateMetricDouble()); | ||
assertTrue(hints.docCount()); | ||
} | ||
|
||
public void testNonArrayValue() { | ||
// Test with non-array value | ||
KeyValue nonArrayHint = KeyValue.newBuilder() | ||
.setKey("elasticsearch.mapping.hints") | ||
.setValue(AnyValue.newBuilder().setStringValue("aggregate_metric_double").build()) | ||
.build(); | ||
MappingHints hints = MappingHints.fromAttributes(List.of(nonArrayHint)); | ||
assertFalse(hints.aggregateMetricDouble()); | ||
assertFalse(hints.docCount()); | ||
} | ||
|
||
public void testNonStringArrayValues() { | ||
// Test with non-string array values | ||
AnyValue numberValue = AnyValue.newBuilder().setIntValue(42).build(); | ||
AnyValue boolValue = AnyValue.newBuilder().setBoolValue(true).build(); | ||
|
||
ArrayValue.Builder arrayBuilder = ArrayValue.newBuilder(); | ||
arrayBuilder.addValues(numberValue); | ||
arrayBuilder.addValues(boolValue); | ||
|
||
KeyValue invalidTypeHints = KeyValue.newBuilder() | ||
.setKey("elasticsearch.mapping.hints") | ||
.setValue(AnyValue.newBuilder().setArrayValue(arrayBuilder).build()) | ||
.build(); | ||
|
||
MappingHints hints = MappingHints.fromAttributes(List.of(invalidTypeHints)); | ||
assertFalse(hints.aggregateMetricDouble()); | ||
assertFalse(hints.docCount()); | ||
} | ||
|
||
private KeyValue createMappingHint(String... hintValues) { | ||
ArrayValue.Builder arrayBuilder = ArrayValue.newBuilder(); | ||
for (String hint : hintValues) { | ||
arrayBuilder.addValues(AnyValue.newBuilder().setStringValue(hint)); | ||
} | ||
|
||
return KeyValue.newBuilder() | ||
.setKey("elasticsearch.mapping.hints") | ||
.setValue(AnyValue.newBuilder().setArrayValue(arrayBuilder)) | ||
.build(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.