aboutsummaryrefslogtreecommitdiff
path: root/services
diff options
context:
space:
mode:
authorzpencer <spencerfang@google.com>2018-04-19 17:51:49 -0700
committerGitHub <noreply@github.com>2018-04-19 17:51:49 -0700
commitf5e8ec18b71c6e626ec3f669446aecac43e40740 (patch)
tree5385ae47bf3557042b8ffb519a69f11ecb5dd541 /services
parentddf77f59ccf2e7501fb0dcefbc30cef7b977b377 (diff)
downloadgrpc-grpc-java-f5e8ec18b71c6e626ec3f669446aecac43e40740.tar.gz
services: avoid static initialization for BinaryLog.java (#4363)
The code that uses this will create an instance.
Diffstat (limited to 'services')
-rw-r--r--services/src/main/java/io/grpc/services/BinaryLog.java35
-rw-r--r--services/src/main/java/io/grpc/services/BinaryLogProviderImpl.java70
2 files changed, 71 insertions, 34 deletions
diff --git a/services/src/main/java/io/grpc/services/BinaryLog.java b/services/src/main/java/io/grpc/services/BinaryLog.java
index a414dda22..f51da4eb9 100644
--- a/services/src/main/java/io/grpc/services/BinaryLog.java
+++ b/services/src/main/java/io/grpc/services/BinaryLog.java
@@ -248,33 +248,6 @@ final class BinaryLog implements ServerInterceptor, ClientInterceptor {
abstract int getMaxMessageBytes();
}
- private static final Factory DEFAULT_FACTORY;
- private static final Factory NULL_FACTORY = new NullFactory();
-
- static {
- Factory defaultFactory = NULL_FACTORY;
- try {
- String configStr = System.getenv("GRPC_BINARY_LOG_CONFIG");
- // TODO(zpencer): make BinaryLog.java implement isAvailable, and put this check there
- BinaryLogSink sink = BinaryLogSinkProvider.provider();
- if (sink != null && configStr != null && configStr.length() > 0) {
- defaultFactory = new FactoryImpl(sink, configStr);
- }
- } catch (Throwable t) {
- logger.log(Level.SEVERE, "Failed to initialize binary log. Disabling binary log.", t);
- defaultFactory = NULL_FACTORY;
- }
- DEFAULT_FACTORY = defaultFactory;
- }
-
- /**
- * Accepts the fullMethodName and returns the binary log that should be used. The log may be
- * a log that does nothing.
- */
- static BinaryLog getLog(String fullMethodName) {
- return DEFAULT_FACTORY.getLog(fullMethodName);
- }
-
static CallId getCallIdForServer(Context context) {
CallId callId = BinaryLogProvider.SERVER_CALL_ID_CONTEXT_KEY.get(context);
if (callId == null) {
@@ -423,6 +396,7 @@ final class BinaryLog implements ServerInterceptor, ClientInterceptor {
*/
@VisibleForTesting
FactoryImpl(BinaryLogSink sink, String configurationString) {
+ Preconditions.checkNotNull(sink);
Preconditions.checkState(configurationString != null && configurationString.length() > 0);
BinaryLog globalLog = null;
Map<String, BinaryLog> perServiceLogs = new HashMap<String, BinaryLog>();
@@ -542,13 +516,6 @@ final class BinaryLog implements ServerInterceptor, ClientInterceptor {
}
}
- private static final class NullFactory implements Factory {
- @Override
- public BinaryLog getLog(String fullMethodName) {
- return null;
- }
- }
-
/**
* Returns a {@link Uint128} from a CallId.
*/
diff --git a/services/src/main/java/io/grpc/services/BinaryLogProviderImpl.java b/services/src/main/java/io/grpc/services/BinaryLogProviderImpl.java
new file mode 100644
index 000000000..8833c561b
--- /dev/null
+++ b/services/src/main/java/io/grpc/services/BinaryLogProviderImpl.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2018, gRPC Authors All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.grpc.services;
+
+import io.grpc.ClientInterceptor;
+import io.grpc.ServerInterceptor;
+import io.grpc.internal.BinaryLogProvider;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.annotation.Nullable;
+
+/**
+ * The default implementation of a {@link BinaryLogProvider}.
+ */
+public class BinaryLogProviderImpl extends BinaryLogProvider {
+ private static final Logger logger = Logger.getLogger(BinaryLogProviderImpl.class.getName());
+ private final BinaryLog.Factory factory;
+
+ public BinaryLogProviderImpl() {
+ this(BinaryLogSinkProvider.provider(), System.getenv("GRPC_BINARY_LOG_CONFIG"));
+ }
+
+ BinaryLogProviderImpl(BinaryLogSink sink, String configStr) {
+ BinaryLog.Factory factory = null;
+ try {
+ factory = new BinaryLog.FactoryImpl(sink, configStr);
+ } catch (RuntimeException e) {
+ logger.log(Level.SEVERE, "Caught exception, binary log will be disabled", e);
+ } catch (Error err) {
+ logger.log(Level.SEVERE, "Caught exception, binary log will be disabled", err);
+ }
+ this.factory = factory;
+ }
+
+ @Nullable
+ @Override
+ public ServerInterceptor getServerInterceptor(String fullMethodName) {
+ return null;
+ }
+
+ @Nullable
+ @Override
+ public ClientInterceptor getClientInterceptor(String fullMethodName) {
+ return null;
+ }
+
+ @Override
+ protected int priority() {
+ return 5;
+ }
+
+ @Override
+ protected boolean isAvailable() {
+ return factory != null;
+ }
+}