Skip to content

Commit cb39ec0

Browse files
committed
Fix nullability problems
1 parent a78ac2c commit cb39ec0

File tree

9 files changed

+30
-22
lines changed

9 files changed

+30
-22
lines changed

apm-agent-api/src/main/java/co/elastic/apm/api/ElasticApm.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ public static ElasticApm get() {
8686
* @param tracer The tracer implementation to register.
8787
*/
8888
void register(Tracer tracer) {
89-
if (tracer == null) {
90-
INSTANCE.tracer = NoopTracer.INSTANCE;
91-
}
9289
INSTANCE.tracer = tracer;
9390
}
9491

apm-agent-core/src/main/java/co/elastic/apm/impl/ElasticApmTracer.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public static ElasticApmTracer get() {
139139
*/
140140
public static void unregister() {
141141
synchronized (ElasticApmTracer.class) {
142-
instance = null;
142+
instance = ElasticApmTracer.builder().build().register();
143143
TracerRegisterer.unregister();
144144
}
145145
}
@@ -153,7 +153,6 @@ public ElasticApmTracer register() {
153153
return this;
154154
}
155155

156-
@Nonnull
157156
@Override
158157
public Transaction startTransaction() {
159158
Transaction transaction;
@@ -176,14 +175,13 @@ public Span currentSpan() {
176175
return currentSpan.get();
177176
}
178177

179-
@Nonnull
180178
@Override
181179
public Span startSpan() {
182180
Transaction transaction = currentTransaction();
183181
final Span span;
184182
// makes sure that the active setting is consistent during a transaction
185183
// even when setting active=false mid-transaction
186-
if (isNoop(transaction)) {
184+
if (isNoop(transaction) || transaction == null) {
187185
span = noopSpan;
188186
} else {
189187
span = createRealSpan(transaction);

apm-agent-core/src/main/java/co/elastic/apm/impl/payload/ProcessFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -161,7 +161,7 @@ public static ProcessFactory make() {
161161
@Override
162162
public ProcessInfo getProcessInformation() {
163163
ProcessHandle processHandle = (ProcessHandle) this.processHandle;
164-
final ProcessInfo process = new ProcessInfo(processHandle.info().command().orElse(null));
164+
final ProcessInfo process = new ProcessInfo(processHandle.info().command().orElse("unknown"));
165165
process.withPid(processHandle.pid());
166166
process.withPpid(processHandle.parent()
167167
.map(new Function<ProcessHandle, Long>() {

apm-agent-core/src/main/java/co/elastic/apm/impl/payload/ProcessInfo.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public Long getPpid() {
8787
/**
8888
* Parent process ID of the service
8989
*/
90-
public ProcessInfo withPpid(Long ppid) {
90+
public ProcessInfo withPpid(@Nullable Long ppid) {
9191
this.ppid = ppid;
9292
return this;
9393
}
@@ -108,8 +108,10 @@ public List<String> getArgv() {
108108
/**
109109
* Command line arguments used to start this process
110110
*/
111-
public ProcessInfo withArgv(List<String> argv) {
112-
this.argv = argv;
111+
public ProcessInfo withArgv(@Nullable List<String> argv) {
112+
if (argv != null) {
113+
this.argv.addAll(argv);
114+
}
113115
return this;
114116
}
115117

apm-agent-core/src/main/java/co/elastic/apm/matcher/WildcardMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public static boolean anyMatch(Collection<WildcardMatcher> matchers, String s) {
149149
* @return {@code true}, if any of the matchers match the provided partitioned string
150150
* @see #matches(String, String)
151151
*/
152-
public static boolean anyMatch(Collection<WildcardMatcher> matchers, @Nullable String firstPart, String secondPart) {
152+
public static boolean anyMatch(Collection<WildcardMatcher> matchers, String firstPart, @Nullable String secondPart) {
153153
for (WildcardMatcher matcher : matchers) {
154154
if (matcher.matches(firstPart, secondPart)) {
155155
return true;

apm-agent-core/src/main/java/co/elastic/apm/objectpool/impl/AbstractObjectPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public T createInstance() {
4949
@Override
5050
public void fillFromOtherPool(ObjectPool<T> otherPool, int maxElements) {
5151
for (int i = 0; i < maxElements; i++) {
52-
T obj = createInstance();
52+
T obj = tryCreateInstance();
5353
if (obj == null) {
5454
return;
5555
}

apm-agent-core/src/main/java/co/elastic/apm/report/ApmServerHttpPayloadSender.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -35,10 +35,11 @@
3535
import org.slf4j.LoggerFactory;
3636

3737
import java.io.IOException;
38+
import java.util.Objects;
3839

3940
public class ApmServerHttpPayloadSender implements PayloadSender {
4041
private static final Logger logger = LoggerFactory.getLogger(ApmServerHttpPayloadSender.class);
41-
private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json");
42+
private static final MediaType MEDIA_TYPE_JSON = Objects.requireNonNull(MediaType.parse("application/json"));
4243
private static final int GZIP_COMPRESSION_LEVEL = 3;
4344

4445
private final OkHttpClient httpClient;

apm-agent-core/src/main/java/co/elastic/apm/report/ReporterConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -23,6 +23,7 @@
2323
import org.stagemonitor.configuration.ConfigurationOptionProvider;
2424
import org.stagemonitor.configuration.converter.UrlValueConverter;
2525

26+
import javax.annotation.Nullable;
2627
import java.net.URL;
2728

2829
public class ReporterConfiguration extends ConfigurationOptionProvider {
@@ -81,6 +82,7 @@ public class ReporterConfiguration extends ConfigurationOptionProvider {
8182
.dynamic(true)
8283
.buildWithDefault(500);
8384

85+
@Nullable
8486
public String getSecretToken() {
8587
return secretToken.get();
8688
}

apm-agent-core/src/test/java/co/elastic/apm/impl/ElasticApmTracerTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -259,4 +259,12 @@ public void stop() {
259259
tracer.stop();
260260
assertThat(stopCalled).isTrue();
261261
}
262+
263+
@Test
264+
void testSpanWithoutTransaction() {
265+
try (Span span = tracerImpl.startSpan()) {
266+
assertThat(span.isSampled()).isFalse();
267+
}
268+
269+
}
262270
}

0 commit comments

Comments
 (0)