Skip to content

Commit

Permalink
Merge pull request #117 from zalando-nakadi/116
Browse files Browse the repository at this point in the history
#116: create a new trace if none has been started yet during snapshot…
  • Loading branch information
BGehrels authored Mar 12, 2019
2 parents 463240e + aa8c1ee commit 74d7f15
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 12 deletions.
2 changes: 1 addition & 1 deletion nakadi-producer-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>org.zalando</groupId>
<artifactId>nakadi-producer-reactor</artifactId>
<version>20.1.0</version>
<version>20.2.0</version>
</parent>

<artifactId>nakadi-producer-spring-boot-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import java.util.List;
import java.util.Optional;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.flyway.FlywayProperties;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand Down Expand Up @@ -45,6 +45,7 @@
@AutoConfigureAfter(name="org.zalando.tracer.spring.TracerAutoConfiguration")
@EnableScheduling
@EnableConfigurationProperties({ DataSourceProperties.class, FlywayProperties.class })
@Slf4j
public class NakadiProducerAutoConfiguration {

@ConditionalOnMissingBean({NakadiPublishingClient.class, NakadiClient.class})
Expand Down Expand Up @@ -111,8 +112,8 @@ public FlowIdComponent flowIdComponent() {
@Bean
@ConditionalOnMissingBean
public SnapshotEventCreationEndpoint snapshotEventCreationEndpoint(
SnapshotCreationService snapshotCreationService) {
return new SnapshotEventCreationEndpoint(snapshotCreationService);
SnapshotCreationService snapshotCreationService, FlowIdComponent flowIdComponent) {
return new SnapshotEventCreationEndpoint(snapshotCreationService, flowIdComponent);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ public class NoopFlowIdComponent implements FlowIdComponent {

@Override
public String getXFlowIdValue() {
log.debug("No bean of class FlowIdComponent was found. Returning null.");
return null;
}

@Override
public void startTraceIfNoneExists() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,21 @@ public String getXFlowIdValue() {
}
return null;
}

@Override
public void startTraceIfNoneExists() {
if (tracer != null) {
try {
tracer.get(X_FLOW_ID).getValue();
} catch (IllegalArgumentException e) {
log.warn("No trace was configured for the name {}. Returning null. " +
"To configure Tracer provide an application property: " +
"tracer.traces.X-Flow-ID=flow-id", X_FLOW_ID);
} catch (IllegalStateException e) {
tracer.start();
}
} else {
log.warn("No bean of class Tracer was found.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.lang.Nullable;
import org.zalando.nakadiproducer.flowid.FlowIdComponent;

@Endpoint(id = "snapshot-event-creation")
public class SnapshotEventCreationEndpoint {
private final SnapshotCreationService snapshotCreationService;
private final FlowIdComponent flowIdComponent;

public SnapshotEventCreationEndpoint(SnapshotCreationService snapshotCreationService) {
public SnapshotEventCreationEndpoint(SnapshotCreationService snapshotCreationService, FlowIdComponent flowIdComponent) {
this.snapshotCreationService = snapshotCreationService;
this.flowIdComponent = flowIdComponent;
}

@ReadOperation
Expand All @@ -31,6 +34,7 @@ public void createFilteredSnapshotEvents(
// Test in the IDE. So let's stick with arg0 for now.
@Selector String arg0,
@Nullable String filter) {
flowIdComponent.startTraceIfNoneExists();
snapshotCreationService.createSnapshotEvents(arg0, filter);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.zalando.nakadiproducer.flowid;

import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.zalando.nakadiproducer.flowid.TracerFlowIdComponent;
import org.mockito.junit.MockitoJUnitRunner;
import org.zalando.tracer.Tracer;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -21,10 +22,40 @@ public class TracerFlowIdComponentTest {
@Test
public void makeSureItWorks() {
TracerFlowIdComponent flowIdComponent = new TracerFlowIdComponent(tracer);
Mockito.when(tracer.get("X-Flow-ID").getValue()).thenReturn("A_FUNKY_VALUE");
when(tracer.get("X-Flow-ID").getValue()).thenReturn("A_FUNKY_VALUE");

assertThat(flowIdComponent.getXFlowIdKey(), Matchers.equalTo("X-Flow-ID"));
assertThat(flowIdComponent.getXFlowIdValue(), Matchers.equalTo("A_FUNKY_VALUE"));
}

@Test
public void makeSureTraceWillBeStartedIfNoneHasBeenStartedBefore() {
TracerFlowIdComponent flowIdComponent = new TracerFlowIdComponent(tracer);
when(tracer.get("X-Flow-ID").getValue()).thenThrow(new IllegalStateException());

flowIdComponent.startTraceIfNoneExists();

verify(tracer).start();
}

@Test
public void wontFailIfTraceHasNotBeenConfiguredInStartTrace() {
TracerFlowIdComponent flowIdComponent = new TracerFlowIdComponent(tracer);
when(tracer.get("X-Flow-ID")).thenThrow(new IllegalArgumentException());

flowIdComponent.startTraceIfNoneExists();

// then no exception is thrown
}

@Test
public void makeSureTraceWillNotStartedIfOneExists() {
TracerFlowIdComponent flowIdComponent = new TracerFlowIdComponent(tracer);
when(tracer.get("X-Flow-ID").getValue()).thenReturn("A_FUNKY_VALUE");

flowIdComponent.startTraceIfNoneExists();

verify(tracer, never()).start();
}

}
2 changes: 1 addition & 1 deletion nakadi-producer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>org.zalando</groupId>
<artifactId>nakadi-producer-reactor</artifactId>
<version>20.1.0</version>
<version>20.2.0</version>
</parent>

<artifactId>nakadi-producer</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

public interface FlowIdComponent {
String getXFlowIdValue();

void startTraceIfNoneExists();
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<artifactId>nakadi-producer-reactor</artifactId>
<groupId>org.zalando</groupId>
<version>20.1.0</version>
<version>20.2.0</version>
<packaging>pom</packaging>
<name>Nakadi Event Producer Reactor</name>

Expand Down

0 comments on commit 74d7f15

Please sign in to comment.