Skip to content

Commit 4c17382

Browse files
authored
services: let BinaryLog factory return null (grpc#3868)
1 parent 4f4cedf commit 4c17382

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

services/src/main/java/io/grpc/services/BinaryLog.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
*/
3535
final class BinaryLog {
3636
private static final Logger logger = Logger.getLogger(BinaryLog.class.getName());
37-
private static final BinaryLog NOOP_LOG =
38-
new BinaryLog(/*maxHeaderBytes=*/ 0, /*maxMessageBytes=*/ 0);
3937
private final int maxHeaderBytes;
4038
private final int maxMessageBytes;
4139

@@ -68,23 +66,18 @@ public String toString() {
6866
}
6967

7068
private static final Factory DEFAULT_FACTORY;
71-
private static final Factory NOOP_FACTORY = new Factory() {
72-
@Override
73-
public BinaryLog getLog(String fullMethodName) {
74-
return NOOP_LOG;
75-
}
76-
};
69+
private static final Factory NULL_FACTORY = new NullFactory();
7770

7871
static {
79-
Factory defaultFactory = NOOP_FACTORY;
72+
Factory defaultFactory = NULL_FACTORY;
8073
try {
8174
String configStr = System.getenv("GRPC_BINARY_LOG_CONFIG");
8275
if (configStr != null && configStr.length() > 0) {
8376
defaultFactory = new FactoryImpl(configStr);
8477
}
8578
} catch (Throwable t) {
8679
logger.log(Level.SEVERE, "Failed to initialize binary log. Disabling binary log.", t);
87-
defaultFactory = NOOP_FACTORY;
80+
defaultFactory = NULL_FACTORY;
8881
}
8982
DEFAULT_FACTORY = defaultFactory;
9083
}
@@ -98,6 +91,7 @@ static BinaryLog getLog(String fullMethodName) {
9891
}
9992

10093
interface Factory {
94+
@Nullable
10195
BinaryLog getLog(String fullMethodName);
10296
}
10397

@@ -167,7 +161,7 @@ static final class FactoryImpl implements Factory {
167161
logger.info(String.format("Method binlog: method=%s log=%s", methodOrSvc, binLog));
168162
}
169163
}
170-
this.globalLog = globalLog == null ? NOOP_LOG : globalLog;
164+
this.globalLog = globalLog;
171165
this.perServiceLogs = Collections.unmodifiableMap(perServiceLogs);
172166
this.perMethodLogs = Collections.unmodifiableMap(perMethodLogs);
173167
}
@@ -243,4 +237,11 @@ static boolean isServiceGlob(String input) {
243237
return input.endsWith("/*");
244238
}
245239
}
240+
241+
private static final class NullFactory implements Factory {
242+
@Override
243+
public BinaryLog getLog(String fullMethodName) {
244+
return null;
245+
}
246+
}
246247
}

services/src/test/java/io/grpc/services/BinaryLogTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
/** Tests for {@link BinaryLog}. */
2828
@RunWith(JUnit4.class)
2929
public final class BinaryLogTest {
30-
private static final BinaryLog NONE = new Builder().build();
3130
private static final BinaryLog HEADER_FULL = new Builder().header(Integer.MAX_VALUE).build();
3231
private static final BinaryLog HEADER_256 = new Builder().header(256).build();
3332
private static final BinaryLog MSG_FULL = new Builder().msg(Integer.MAX_VALUE).build();
@@ -73,7 +72,7 @@ public void configBinLog_method() throws Exception {
7372

7473
@Test
7574
public void configBinLog_method_absent() throws Exception {
76-
assertEquals(NONE, new FactoryImpl("p.s/m").getLog("p.s/absent"));
75+
assertNull(new FactoryImpl("p.s/m").getLog("p.s/absent"));
7776
}
7877

7978
@Test
@@ -95,7 +94,7 @@ public void configBinLog_service() throws Exception {
9594

9695
@Test
9796
public void configBinLog_service_absent() throws Exception {
98-
assertEquals(NONE, new FactoryImpl("p.s/*").getLog("p.other/m"));
97+
assertNull(new FactoryImpl("p.s/*").getLog("p.other/m"));
9998
}
10099

101100
@Test
@@ -157,7 +156,7 @@ public void configBinLog_multiConfig_noGlobal() throws Exception {
157156
"package.both256/*{h:256;m:256},"
158157
+ "package.service1/both128{h:128;m:128},"
159158
+ "package.service2/method_messageOnly{m}");
160-
assertEquals(NONE, factory.getLog("otherpackage.service/method"));
159+
assertNull(factory.getLog("otherpackage.service/method"));
161160

162161
assertEquals(BOTH_256, factory.getLog("package.both256/method1"));
163162
assertEquals(BOTH_256, factory.getLog("package.both256/method2"));
@@ -166,11 +165,11 @@ public void configBinLog_multiConfig_noGlobal() throws Exception {
166165
assertEquals(
167166
new Builder().header(128).msg(128).build(), factory.getLog("package.service1/both128"));
168167
// no global config in effect
169-
assertEquals(NONE, factory.getLog("package.service1/absent"));
168+
assertNull(factory.getLog("package.service1/absent"));
170169

171170
assertEquals(MSG_FULL, factory.getLog("package.service2/method_messageOnly"));
172171
// no global config in effect
173-
assertEquals(NONE, factory.getLog("package.service2/absent"));
172+
assertNull(factory.getLog("package.service2/absent"));
174173
}
175174

176175
@Test

0 commit comments

Comments
 (0)