Skip to content
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

Set default value of flattenAttributes to true in Otel metrics source #4190

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -32,13 +32,21 @@ public abstract class JacksonMetric extends JacksonEvent implements Metric {
protected static final String SCHEMA_URL_KEY = "schemaUrl";
protected static final String EXEMPLARS_KEY = "exemplars";
protected static final String FLAGS_KEY = "flags";
private final boolean flattenAttributes;
private boolean flattenAttributes;

protected JacksonMetric(Builder builder, boolean flattenAttributes) {
super(builder);
this.flattenAttributes = flattenAttributes;
}

public void setFlattenAttributes(boolean flattenAttributes) {
this.flattenAttributes = flattenAttributes;
}

boolean getFlattenAttributes() {
return flattenAttributes;
}

@Override
public String toJsonString() {
if (!flattenAttributes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ public void testGetScale() {
public void testZeroCount() {
Long zeroCount = histogram.getZeroCount();
assertThat(zeroCount, is(equalTo(TEST_ZERO_COUNT)));
assertThat(((JacksonMetric)histogram).getFlattenAttributes(), equalTo(true));
((JacksonMetric)histogram).setFlattenAttributes(false);
assertThat(((JacksonMetric)histogram).getFlattenAttributes(), equalTo(false));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import static org.opensearch.dataprepper.model.metric.JacksonExponentialHistogram.POSITIVE_BUCKETS_KEY;
import static org.opensearch.dataprepper.model.metric.JacksonExponentialHistogram.NEGATIVE_BUCKETS_KEY;
import static org.opensearch.dataprepper.model.metric.JacksonHistogram.BUCKETS_KEY;
import org.opensearch.dataprepper.model.metric.JacksonMetric;
import org.opensearch.dataprepper.model.metric.Metric;
import static org.opensearch.dataprepper.model.metric.JacksonMetric.ATTRIBUTES_KEY;
import org.opensearch.dataprepper.model.processor.AbstractProcessor;
import org.opensearch.dataprepper.model.processor.Processor;
import org.opensearch.dataprepper.model.record.Record;
Expand All @@ -25,7 +25,6 @@
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -55,13 +54,8 @@ private void modifyRecord(Record<? extends Metric> record,
boolean calcualteExponentialHistogramBuckets) {
Event event = (Event)record.getData();

if (flattenAttributes) {
Map<String, Object> attributes = event.get(ATTRIBUTES_KEY, Map.class);

for (Map.Entry<String, Object> entry : attributes.entrySet()) {
event.put(entry.getKey(), entry.getValue());
}
event.delete(ATTRIBUTES_KEY);
if (!flattenAttributes) {
((JacksonMetric)record.getData()).setFlattenAttributes(false);
}
if (!calcualteHistogramBuckets && event.get(BUCKETS_KEY, List.class) != null) {
event.delete(BUCKETS_KEY);
Expand All @@ -85,7 +79,7 @@ public Collection<Record<? extends Metric>> doExecute(Collection<Record<?>> reco
for (Record<?> rec : records) {
if ((rec.getData() instanceof Event)) {
Record<? extends Metric> newRecord = (Record<? extends Metric>)rec;
if (otelMetricsRawProcessorConfig.getFlattenAttributesFlag() ||
if (!otelMetricsRawProcessorConfig.getFlattenAttributesFlag() ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this is being negated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's true by default. Only when it is set to false in the config, we call JacksonMetric.setFlattenAttributes(false)

!otelMetricsRawProcessorConfig.getCalculateHistogramBuckets() ||
!otelMetricsRawProcessorConfig.getCalculateExponentialHistogramBuckets()) {
modifyRecord(newRecord, otelMetricsRawProcessorConfig.getFlattenAttributesFlag(), otelMetricsRawProcessorConfig.getCalculateHistogramBuckets(), otelMetricsRawProcessorConfig.getCalculateExponentialHistogramBuckets());
Expand Down
Loading