• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.services;
18 
19 import com.google.common.base.Preconditions;
20 import io.grpc.CallOptions;
21 import io.grpc.ClientInterceptor;
22 import io.grpc.ServerInterceptor;
23 import java.io.IOException;
24 import java.util.concurrent.atomic.AtomicLong;
25 import javax.annotation.Nullable;
26 
27 /**
28  * The default implementation of a {@link BinaryLogProvider}.
29  */
30 class BinaryLogProviderImpl extends BinaryLogProvider {
31   private final BinlogHelper.Factory factory;
32   private final BinaryLogSink sink;
33   private final AtomicLong counter = new AtomicLong();
34 
BinaryLogProviderImpl()35   public BinaryLogProviderImpl() throws IOException {
36     this(new TempFileSink(), System.getenv("GRPC_BINARY_LOG_CONFIG"));
37   }
38 
BinaryLogProviderImpl(BinaryLogSink sink)39   public BinaryLogProviderImpl(BinaryLogSink sink) throws IOException {
40     this(sink, System.getenv("GRPC_BINARY_LOG_CONFIG"));
41   }
42 
43   /**
44    * Creates an instance.
45    * @param sink ownership is transferred to this class.
46    * @param configStr config string to parse to determine logged methods and msg size limits.
47    * @throws IOException if initialization failed.
48    */
BinaryLogProviderImpl(BinaryLogSink sink, String configStr)49   BinaryLogProviderImpl(BinaryLogSink sink, String configStr) throws IOException {
50     this.sink = Preconditions.checkNotNull(sink);
51     try {
52       factory = new BinlogHelper.FactoryImpl(sink, configStr);
53     } catch (RuntimeException e) {
54       sink.close();
55       // parsing the conf string may throw if it is blank or contains errors
56       throw new IOException(
57           "Can not initialize. The env variable GRPC_BINARY_LOG_CONFIG must be valid.", e);
58     }
59   }
60 
61   @Nullable
62   @Override
getServerInterceptor(String fullMethodName)63   public ServerInterceptor getServerInterceptor(String fullMethodName) {
64     BinlogHelper helperForMethod = factory.getLog(fullMethodName);
65     if (helperForMethod == null) {
66       return null;
67     }
68     return helperForMethod.getServerInterceptor(getServerCallId());
69   }
70 
71   @Nullable
72   @Override
getClientInterceptor( String fullMethodName, CallOptions callOptions)73   public ClientInterceptor getClientInterceptor(
74       String fullMethodName, CallOptions callOptions) {
75     BinlogHelper helperForMethod = factory.getLog(fullMethodName);
76     if (helperForMethod == null) {
77       return null;
78     }
79     return helperForMethod.getClientInterceptor(getClientCallId(callOptions));
80   }
81 
82   @Override
close()83   public void close() throws IOException {
84     sink.close();
85   }
86 
getServerCallId()87   protected CallId getServerCallId() {
88     return new CallId(0, counter.getAndIncrement());
89   }
90 
getClientCallId(CallOptions options)91   protected CallId getClientCallId(CallOptions options) {
92     return new CallId(0, counter.getAndIncrement());
93   }
94 }
95