Skip to content

Commit

Permalink
ipc: Fix NPE from #1150 if IPC logger is not configured. (#1157)
Browse files Browse the repository at this point in the history
Results in a
`java.lang.NullPointerException: Cannot invoke "com.netflix.spectator.ipc.IpcLogger.inflightEnabled()" because "this.logger" is null`
  • Loading branch information
manolama authored and brharrington committed Sep 18, 2024
1 parent c0f3e5f commit c8b4940
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public IpcLogEntry withLatency(long latency, TimeUnit unit) {
* the request completes it is recommended to call {@link #markEnd()}.
*/
public IpcLogEntry markStart() {
if (registry != null && !disableMetrics && logger.inflightEnabled()) {
if (registry != null && !disableMetrics && logger != null && logger.inflightEnabled()) {
inflightId = getInflightId();
int n = logger.inflightRequests(inflightId).incrementAndGet();
registry.distributionSummary(inflightId).record(n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.netflix.spectator.api.DefaultRegistry;
import com.netflix.spectator.api.DistributionSummary;
import com.netflix.spectator.api.ManualClock;
import com.netflix.spectator.api.NoopRegistry;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Utils;
import com.netflix.spectator.api.patterns.CardinalityLimiters;
Expand All @@ -36,6 +37,8 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertSame;

public class IpcLogEntryTest {

private final ManualClock clock = new ManualClock();
Expand Down Expand Up @@ -889,4 +892,12 @@ public void endpointUnknownIfNotSet() {
Assertions.assertEquals("unknown", Utils.getTagValue(c.id(), "ipc.endpoint"));
});
}

@Test
public void markNullLogger() {
IpcLogEntry entry = new IpcLogEntry(clock)
.withRegistry(new NoopRegistry());
entry.markStart();
assertSame(entry, entry.markEnd());
}
}

0 comments on commit c8b4940

Please sign in to comment.