Skip to content

Commit

Permalink
Use NullAway to prevent NPEs
Browse files Browse the repository at this point in the history
(closes elastic#7)

Signed-off-by: Felix Barnsteiner <felix.barnsteiner@elastic.co>
  • Loading branch information
felixbarny committed Mar 14, 2018
1 parent c9bcec8 commit 32fa7e9
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ public void setUp() throws Exception {
.withRuntime(new RuntimeInfo("Java", "9.0.4"))
.withFramework(new Framework("Servlet API", "3.1"))
.withLanguage(new Language("Java", "9.0.4"));
ProcessInfo process = new ProcessInfo()
ProcessInfo process = new ProcessInfo("/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/bin/java")
.withPid(2103)
.withPpid(403L)
.withTitle("/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/bin/java")
.withArgv(Collections.singletonList("-javaagent:/path/to/elastic-apm-java.jar"));
SystemInfo system = new SystemInfo("x86_64", "Felixs-MBP", "Mac OS X");
ReporterConfiguration reporterConfiguration = new ReporterConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ enum ForLegacyVM implements ProcessFactory {

@Override
public ProcessInfo getProcessInformation() {
ProcessInfo process = new ProcessInfo();
ProcessInfo process = new ProcessInfo(getTitle());
process.withPid(getPid());
process.withArgv(runtimeMXBean.getInputArguments());
process.withTitle(getTitle());
return process;
}

Expand Down Expand Up @@ -143,8 +142,8 @@ public static ProcessFactory make() {
*/
@Override
public ProcessInfo getProcessInformation() {
final ProcessInfo process = new ProcessInfo();
ProcessHandle processHandle = (ProcessHandle) this.processHandle;
final ProcessInfo process = new ProcessInfo(processHandle.info().command().orElse(null));
process.withPid(processHandle.pid());
process.withPpid(processHandle.parent()
.map(new Function<ProcessHandle, Long>() {
Expand All @@ -163,7 +162,6 @@ public List<String> apply(String[] a) {
}
})
.orElse(null));
process.withTitle(processHandle.info().command().orElse(null));

return process;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -28,16 +29,21 @@ public class ProcessInfo {
/**
* Parent process ID of the service
*/
@Nullable
@JsonProperty("ppid")
private Long ppid;
@JsonProperty("title")
private String title;
private final String title;
/**
* Command line arguments used to start this process
*/
@JsonProperty("argv")
private List<String> argv = new ArrayList<String>();

public ProcessInfo(String title) {
this.title = title;
}

/**
* Process ID of the service
* (Required)
Expand All @@ -58,6 +64,7 @@ public ProcessInfo withPid(long pid) {
/**
* Parent process ID of the service
*/
@Nullable
@JsonProperty("ppid")
public Long getPpid() {
return ppid;
Expand All @@ -76,15 +83,6 @@ public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public ProcessInfo withTitle(String title) {
this.title = title;
return this;
}

/**
* Command line arguments used to start this process
*/
Expand All @@ -103,12 +101,20 @@ public ProcessInfo withArgv(List<String> argv) {

@Override
public String toString() {
return new ToStringBuilder(this).append("pid", pid).append("ppid", ppid).append("title", title).append("argv", argv).toString();
return new ToStringBuilder(this)
.append("pid", pid)
.append("ppid", ppid)
.append("title", title)
.append("argv", argv).toString();
}

@Override
public int hashCode() {
return new HashCodeBuilder().append(pid).append(title).append(argv).append(ppid).toHashCode();
return new HashCodeBuilder()
.append(pid)
.append(title)
.append(argv)
.append(ppid).toHashCode();
}

@Override
Expand All @@ -120,7 +126,11 @@ public boolean equals(Object other) {
return false;
}
ProcessInfo rhs = ((ProcessInfo) other);
return new EqualsBuilder().append(pid, rhs.pid).append(title, rhs.title).append(argv, rhs.argv).append(ppid, rhs.ppid).isEquals();
return new EqualsBuilder()
.append(pid, rhs.pid)
.append(title, rhs.title)
.append(argv, rhs.argv)
.append(ppid, rhs.ppid).isEquals();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package co.elastic.apm.impl.payload;

import com.fasterxml.jackson.annotation.JsonInclude;
Expand All @@ -7,56 +6,66 @@
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.annotation.Nullable;


/**
* Information about the instrumented Service
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
// TODO: make immutable
// TODO reduce usage of @Nullable
public class Service {

/**
* Name and version of the Elastic APM agent
* (Required)
*/
@Nullable
@JsonProperty("agent")
private Agent agent;
/**
* Name and version of the web framework used
*/
@Nullable
@JsonProperty("framework")
private Framework framework;
/**
* Name and version of the programming language used
*/
@Nullable
@JsonProperty("language")
private Language language;
/**
* Immutable name of the service emitting this event
* (Required)
*/
@Nullable
@JsonProperty("name")
private String name;
/**
* Environment name of the service, e.g. "production" or "staging"
*/
@Nullable
@JsonProperty("environment")
private String environment;
/**
* Name and version of the language runtime running this service
*/
@Nullable
@JsonProperty("runtime")
private RuntimeInfo runtime;
/**
* Version of the service emitting this event
*/
@Nullable
@JsonProperty("version")
private String version;

/**
* Name and version of the Elastic APM agent
* (Required)
*/
@Nullable
@JsonProperty("agent")
public Agent getAgent() {
return agent;
Expand All @@ -74,6 +83,7 @@ public Service withAgent(Agent agent) {
/**
* Name and version of the web framework used
*/
@Nullable
@JsonProperty("framework")
public Framework getFramework() {
return framework;
Expand All @@ -90,6 +100,7 @@ public Service withFramework(Framework framework) {
/**
* Name and version of the programming language used
*/
@Nullable
@JsonProperty("language")
public Language getLanguage() {
return language;
Expand All @@ -107,6 +118,7 @@ public Service withLanguage(Language language) {
* Immutable name of the service emitting this event
* (Required)
*/
@Nullable
@JsonProperty("name")
public String getName() {
return name;
Expand All @@ -124,6 +136,7 @@ public Service withName(String name) {
/**
* Environment name of the service, e.g. "production" or "staging"
*/
@Nullable
@JsonProperty("environment")
public String getEnvironment() {
return environment;
Expand All @@ -140,6 +153,7 @@ public Service withEnvironment(String environment) {
/**
* Name and version of the language runtime running this service
*/
@Nullable
@JsonProperty("runtime")
public RuntimeInfo getRuntime() {
return runtime;
Expand All @@ -156,6 +170,7 @@ public Service withRuntime(RuntimeInfo runtime) {
/**
* Version of the service emitting this event
*/
@Nullable
@JsonProperty("version")
public String getVersion() {
return version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -69,7 +70,7 @@ public class Span implements Recyclable, co.elastic.apm.api.Span {
@JsonProperty("type")
private String type;

public Span start(ElasticApmTracer tracer, Transaction transaction, Span span, long nanoTime, boolean dropped) {
public Span start(ElasticApmTracer tracer, Transaction transaction, @Nullable Span span, long nanoTime, boolean dropped) {
this.tracer = tracer;
this.id.setToRandomValue();
if (span != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void setUp() {
private TransactionPayload createPayloadWithRequiredValues() {
Service service = new Service().withAgent(new Agent("name", "version")).withName("name");
SystemInfo system = new SystemInfo("", "", "");
return new TransactionPayload(new ProcessInfo(), service, system);
return new TransactionPayload(new ProcessInfo("title"), service, system);
}

private Transaction createTransactionWithRequiredValues() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package co.elastic.apm.report;

import co.elastic.apm.impl.error.ErrorCapture;
import co.elastic.apm.impl.transaction.Transaction;
import co.elastic.apm.impl.payload.ProcessInfo;
import co.elastic.apm.impl.payload.Service;
import co.elastic.apm.impl.payload.SystemInfo;
import co.elastic.apm.impl.transaction.Transaction;
import co.elastic.apm.report.serialize.JacksonPayloadSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
Expand Down Expand Up @@ -59,7 +59,7 @@ void setUp() {
when(reporterConfiguration.getServerUrl()).thenReturn("http://localhost:" + port);
payloadSender = new ApmServerHttpPayloadSender(new OkHttpClient(), new JacksonPayloadSerializer(objectMapper), reporterConfiguration);
SystemInfo system = new SystemInfo("x64", "localhost", "platform");
reporter = new ApmServerReporter(new Service(), new ProcessInfo(), system, payloadSender, false, reporterConfiguration);
reporter = new ApmServerReporter(new Service(), new ProcessInfo("title"), system, payloadSender, false, reporterConfiguration);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void setUp() {
when(reporterConfiguration.getMaxQueueSize()).thenReturn(2);
SystemInfo system = new SystemInfo("x64", "localhost", "platform");
payloadSender = mock(PayloadSender.class);
reporter = new ApmServerReporter(new Service(), new ProcessInfo(), system, payloadSender, true, reporterConfiguration);
reporter = new ApmServerReporter(new Service(), new ProcessInfo("title"), system, payloadSender, true, reporterConfiguration);
}

@Test
Expand Down
15 changes: 13 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@
<testSource>${maven.compiler.testTarget}</testSource>
<testTarget>${maven.compiler.testTarget}</testTarget>
<encoding>UTF-8</encoding>
<annotationProcessorPaths>
<path>
<groupId>com.uber.nullaway</groupId>
<artifactId>nullaway</artifactId>
<version>0.3.0</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Xep:NullAway:ERROR</arg>
<arg>-XepOpt:NullAway:AnnotatedPackages=co.elastic.apm</arg>
</compilerArgs>
</configuration>
<dependencies>
<dependency>
Expand Down Expand Up @@ -158,8 +169,8 @@
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>3.0.1</version>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down

0 comments on commit 32fa7e9

Please sign in to comment.