-
Notifications
You must be signed in to change notification settings - Fork 25.6k
OTLP: determine target data stream based on attributes and scope name #133903
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
Changes from 1 commit
e766658
8a95e5d
ec56b19
39fe446
dd0cf6f
f811ab1
6f196b1
4883108
ec8ef6e
4147742
38b41d1
0a022b4
051723c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /* | ||
| * 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.KeyValue; | ||
|
|
||
| import org.elasticsearch.cluster.metadata.DataStream; | ||
| import org.elasticsearch.core.Nullable; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Represents the target index for a data point, which can be either a specific index or a data stream. | ||
| * The index is determined based on attributes, scope name, and default values. | ||
| */ | ||
| public final class TargetIndex { | ||
|
|
||
| public static final String ELASTICSEARCH_INDEX = "elasticsearch.index"; | ||
| public static final String DATA_STREAM_DATASET = "data_stream.dataset"; | ||
| public static final String DATA_STREAM_NAMESPACE = "data_stream.namespace"; | ||
|
|
||
| private String index; | ||
| private String type; | ||
| private String dataset; | ||
| private String namespace; | ||
|
|
||
| /** | ||
| * Determines the target index for a data point. | ||
| * | ||
| * @param type The data stream type (e.g., "metrics", "logs"). | ||
| * @param attributes The attributes associated with the data point. | ||
| * @param receiverName The name of the receiver, which may influence the dataset (receiver-based routing). | ||
| * @param scopeAttributes Attributes associated with the scope. | ||
| * @param resourceAttributes Attributes associated with the resource. | ||
| * @return A TargetIndex instance representing the target index for the data point. | ||
| */ | ||
| public static TargetIndex route( | ||
felixbarny marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| String type, | ||
| List<KeyValue> attributes, | ||
| @Nullable String receiverName, | ||
| List<KeyValue> scopeAttributes, | ||
| List<KeyValue> resourceAttributes | ||
| ) { | ||
| // Order: | ||
| // 1. elasticsearch.index from attributes, scope.attributes, resource.attributes | ||
| // 2. read data_stream.* from attributes, scope.attributes, resource.attributes | ||
| // 3. receiver-based routing based on scope.name | ||
| // 4. use default hardcoded data_stream.* (<type>-generic-default) | ||
| TargetIndex target = new TargetIndex(); | ||
| target.populateFrom(attributes); | ||
| target.populateFrom(scopeAttributes); | ||
| target.populateFrom(resourceAttributes); | ||
| if (target.index == null) { | ||
| target.type = type; | ||
| if (target.dataset == null && receiverName != null) { | ||
| target.dataset = receiverName; | ||
| } | ||
| target.dataset = DataStream.sanitizeDataset(target.dataset); | ||
| if (target.dataset == null) { | ||
| target.dataset = "generic"; | ||
felixbarny marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| // add otel suffix to match OTel index template | ||
| target.dataset = target.dataset + ".otel"; | ||
| target.namespace = DataStream.sanitizeNamespace(target.namespace); | ||
|
|
||
| if (target.namespace == null) { | ||
| target.namespace = "default"; | ||
| } | ||
| target.index = target.type + "-" + target.dataset + "-" + target.namespace; | ||
| } | ||
| return target; | ||
| } | ||
|
|
||
| private void populateFrom(List<KeyValue> attributes) { | ||
| if (isPopulated()) { | ||
| return; | ||
| } | ||
| for (int i = 0, size = attributes.size(); i < size; i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if these should be ordered e.g. lexicographically, so that naming is deterministic in the presence of multiple matching attributes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorting may be pretty expensive. But I see what you mean. If both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't this still use the first match on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand. The |
||
| KeyValue attr = attributes.get(i); | ||
| if (attr.getKey().equals(ELASTICSEARCH_INDEX)) { | ||
| index = attr.getValue().getStringValue(); | ||
felixbarny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| if (isPopulated() == false && dataset == null && attr.getKey().equals(DATA_STREAM_DATASET)) { | ||
| dataset = attr.getValue().getStringValue(); | ||
| } | ||
| if (isPopulated() == false && namespace == null && attr.getKey().equals(DATA_STREAM_NAMESPACE)) { | ||
| namespace = attr.getValue().getStringValue(); | ||
| } | ||
| if (isPopulated()) { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private boolean isPopulated() { | ||
| return (dataset != null && namespace != null) || index != null; | ||
| } | ||
|
|
||
| public boolean isDataStream() { | ||
| return type != null && dataset != null && namespace != null; | ||
kkrik-es marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public String index() { | ||
| return index; | ||
| } | ||
|
|
||
| public String type() { | ||
| return type; | ||
| } | ||
|
|
||
| public String dataset() { | ||
| return dataset; | ||
| } | ||
|
|
||
| public String namespace() { | ||
| return namespace; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return index; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
||
| TargetIndex that = (TargetIndex) o; | ||
| return index.equals(that.index); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return index.hashCode(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.