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

MAINT: cherry-pick changes on event model from trace migration branch #1216

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 @@ -104,6 +104,10 @@ private JsonNode getInitialJsonNode(final Object data) {
return mapper.valueToTree(data);
}

protected JsonNode getJsonNode() {
return jsonNode;
}

/**
* Adds or updates the key with a given value in the Event.
* @param key where the value will be set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

package com.amazon.dataprepper.model.trace;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
* The default implementation of {@link TraceGroupFields}, the attributes associated with an entire trace.
*
Expand All @@ -24,11 +21,6 @@ public class DefaultTraceGroupFields implements TraceGroupFields {

private DefaultTraceGroupFields(final Builder builder) {

checkNotNull(builder.durationInNanos, "durationInNanos cannot be null");
checkNotNull(builder.statusCode, "statusCode cannot be null");
checkNotNull(builder.endTime, "endTime cannot be null");
checkArgument(!builder.endTime.isEmpty(), "endTime cannot be an empty string");

this.endTime = builder.endTime;
this.durationInNanos = builder.durationInNanos;
this.statusCode = builder.statusCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@

import com.amazon.dataprepper.model.event.EventType;
import com.amazon.dataprepper.model.event.JacksonEvent;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

/**
* A Jackson implementation for {@link Span}. This class extends the {@link JacksonEvent}.
Expand Down Expand Up @@ -43,10 +47,10 @@ public class JacksonSpan extends JacksonEvent implements Span {
private static final String DURATION_IN_NANOS_KEY = "durationInNanos";
private static final String TRACE_GROUP_FIELDS_KEY = "traceGroupFields";

private static final List<String> REQUIRED_KEYS = Arrays.asList(TRACE_GROUP_KEY);
private static final List<String>
REQUIRED_NON_EMPTY_KEYS = Arrays.asList(TRACE_ID_KEY, SPAN_ID_KEY, TRACE_STATE_KEY, PARENT_SPAN_ID_KEY, PARENT_SPAN_ID_KEY,
NAME_KEY, KIND_KEY, START_TIME_KEY, END_TIME_KEY);
private static final List<String> REQUIRED_NON_NULL_KEYS = Arrays.asList(DURATION_IN_NANOS_KEY);
REQUIRED_NON_EMPTY_KEYS = Arrays.asList(TRACE_ID_KEY, SPAN_ID_KEY, NAME_KEY, KIND_KEY, START_TIME_KEY, END_TIME_KEY);
private static final List<String> REQUIRED_NON_NULL_KEYS = Arrays.asList(DURATION_IN_NANOS_KEY, TRACE_GROUP_FIELDS_KEY);

protected JacksonSpan(final Builder builder) {
super(builder);
Expand Down Expand Up @@ -144,10 +148,37 @@ public String getServiceName() {
return this.get(SERVICE_NAME_KEY, String.class);
}

@Override
public void setTraceGroup(final String traceGroup) {
this.put(TRACE_GROUP_KEY, traceGroup);
}

@Override
public void setTraceGroupFields(final TraceGroupFields traceGroupFields) {
this.put(TRACE_GROUP_FIELDS_KEY, traceGroupFields);
}

public static Builder builder() {
return new Builder();
}

@Override
public String toJsonString() {
final ObjectNode attributesNode = (ObjectNode) getJsonNode().get("attributes");
final ObjectNode flattenedJsonNode = getJsonNode().deepCopy();
if (attributesNode != null) {
flattenedJsonNode.remove("attributes");
for (Iterator<Map.Entry<String, JsonNode>> it = attributesNode.fields(); it.hasNext(); ) {
Map.Entry<String, JsonNode> entry = it.next();
String field = entry.getKey();
if (!flattenedJsonNode.has(field)) {
flattenedJsonNode.set(field, entry.getValue());
}
}
}
return flattenedJsonNode.toString();
}

/**
* Builder for creating {@link JacksonSpan}
* @since 1.2
Expand Down Expand Up @@ -345,33 +376,6 @@ public Builder withServiceName(final String serviceName) {
return this;
}

/**
* Sets all attributes by copying over those from another span
* @param span
* @since 1.3
*/
public Builder fromSpan(final Span span) {
this.withSpanId(span.getSpanId())
.withTraceId(span.getTraceId())
.withTraceState(span.getTraceState())
.withParentSpanId(span.getParentSpanId())
.withName(span.getName())
.withServiceName(span.getServiceName())
.withKind(span.getKind())
.withStartTime(span.getStartTime())
.withEndTime(span.getEndTime())
.withAttributes(span.getAttributes())
.withDroppedAttributesCount(span.getDroppedAttributesCount())
.withEvents(span.getEvents())
.withDroppedEventsCount(span.getDroppedEventsCount())
.withLinks(span.getLinks())
.withDroppedLinksCount(span.getDroppedLinksCount())
.withTraceGroup(span.getTraceGroup())
.withDurationInNanos(span.getDurationInNanos())
.withTraceGroupFields(span.getTraceGroupFields());
return this;
}

/**
* Returns a newly created {@link JacksonSpan}
* @return a JacksonSpan
Expand All @@ -386,6 +390,10 @@ public JacksonSpan build() {
}

private void validateParameters() {
REQUIRED_KEYS.forEach(key -> {
checkState(data.containsKey(key), key + " need to be assigned");
});

REQUIRED_NON_EMPTY_KEYS.forEach(key -> {
final String value = (String) data.get(key);
checkNotNull(value, key + " cannot be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,18 @@ public interface Span extends Event {
* @since 1.3
*/
String getServiceName();

/**
* Sets the trace group's name for this span.
* @param traceGroup trace group's name
* @since 1.3
*/
void setTraceGroup(String traceGroup);

/**
* Sets the {@link com.amazon.dataprepper.model.trace.TraceGroupFields} for this span.
* @param traceGroupFields trace group related fields
* @since 1.3
*/
void setTraceGroupFields(TraceGroupFields traceGroupFields);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class DefaultTraceGroupFieldsTest {

Expand Down Expand Up @@ -112,51 +111,4 @@ public void testBuilder_withAllParameters_createsTraceGroupFields() {

assertThat(result, is(notNullValue()));
}

@Test
public void testBuilder_withoutParameters_throws_nullPointerException() {
final DefaultTraceGroupFields.Builder builder = DefaultTraceGroupFields.builder();
assertThrows(NullPointerException.class, builder::build);
}

@Test
public void testBuilder_throwsNullPointerException_whenEndTimeIsMissing() {

final DefaultTraceGroupFields.Builder builder = DefaultTraceGroupFields.builder()
.withDurationInNanos(TEST_DURATION)
.withStatusCode(TEST_STATUS_CODE);

assertThrows(NullPointerException.class, builder::build);
}

@Test
public void testBuilder_throwsIllegalArgumentException_whenEndTimeIsEmptyString() {

final DefaultTraceGroupFields.Builder builder = DefaultTraceGroupFields.builder()
.withDurationInNanos(TEST_DURATION)
.withStatusCode(TEST_STATUS_CODE)
.withEndTime("");

assertThrows(IllegalArgumentException.class, builder::build);
}

@Test
public void testBuilder_throwsNullPointerException_whenStatusCodeIsMissing() {

final DefaultTraceGroupFields.Builder builder = DefaultTraceGroupFields.builder()
.withDurationInNanos(123L)
.withEndTime(TEST_END_TIME);

assertThrows(NullPointerException.class, builder::build);
}

@Test
public void testBuilder_throwsNullPointerException_whenDurationIsMissing() {

final DefaultTraceGroupFields.Builder builder = DefaultTraceGroupFields.builder()
.withStatusCode(200)
.withEndTime(TEST_END_TIME);

assertThrows(NullPointerException.class, builder::build);
}
}
Loading