Skip to content

Commit

Permalink
Keep channelz and binlog implementations in-place, while mark them as…
Browse files Browse the repository at this point in the history
… deprecated and suggest users to migrate new ones in io.grpc.protobuf.services
  • Loading branch information
voidzcy committed Apr 13, 2021
1 parent d677c84 commit d8412ab
Show file tree
Hide file tree
Showing 8 changed files with 1,951 additions and 0 deletions.
93 changes: 93 additions & 0 deletions services/src/main/java/io/grpc/services/BinaryLogProviderImpl.java
@@ -0,0 +1,93 @@
/*
* Copyright 2018 The gRPC Authors
*
* 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 com.google.common.base.Preconditions;
import io.grpc.CallOptions;
import io.grpc.ClientInterceptor;
import io.grpc.ServerInterceptor;
import io.grpc.protobuf.services.BinaryLogProvider;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nullable;

/**
* The default implementation of a {@link BinaryLogProvider}.
*/
class BinaryLogProviderImpl extends BinaryLogProvider {
// avoid using 0 because proto3 long fields default to 0 when unset
private static final AtomicLong counter = new AtomicLong(1);

private final BinlogHelper.Factory factory;
private final BinaryLogSink sink;

public BinaryLogProviderImpl() throws IOException {
this(new TempFileSink(), System.getenv("GRPC_BINARY_LOG_CONFIG"));
}

/**
* Deprecated and will be removed in a future version of gRPC.
*/
@Deprecated
public BinaryLogProviderImpl(BinaryLogSink sink) throws IOException {
this(sink, System.getenv("GRPC_BINARY_LOG_CONFIG"));
}

/**
* Creates an instance.
* @param sink ownership is transferred to this class.
* @param configStr config string to parse to determine logged methods and msg size limits.
* @throws IOException if initialization failed.
*/
public BinaryLogProviderImpl(BinaryLogSink sink, String configStr) throws IOException {
this.sink = Preconditions.checkNotNull(sink);
try {
factory = new BinlogHelper.FactoryImpl(sink, configStr);
} catch (RuntimeException e) {
sink.close();
// parsing the conf string may throw if it is blank or contains errors
throw new IOException(
"Can not initialize. The env variable GRPC_BINARY_LOG_CONFIG must be valid.", e);
}
}

@Nullable
@Override
public ServerInterceptor getServerInterceptor(String fullMethodName) {
BinlogHelper helperForMethod = factory.getLog(fullMethodName);
if (helperForMethod == null) {
return null;
}
return helperForMethod.getServerInterceptor(counter.getAndIncrement());
}

@Nullable
@Override
public ClientInterceptor getClientInterceptor(
String fullMethodName, CallOptions callOptions) {
BinlogHelper helperForMethod = factory.getLog(fullMethodName);
if (helperForMethod == null) {
return null;
}
return helperForMethod.getClientInterceptor(counter.getAndIncrement());
}

@Override
public void close() throws IOException {
sink.close();
}
}
35 changes: 35 additions & 0 deletions services/src/main/java/io/grpc/services/BinaryLogSink.java
@@ -0,0 +1,35 @@
/*
* Copyright 2018 The gRPC Authors
*
* 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 com.google.protobuf.MessageLite;
import io.grpc.ExperimentalApi;
import java.io.Closeable;

/**
* A class that accepts binary log messages.
*
* @deprecated Use {@link io.grpc.protobuf.services.BinaryLogSink} instead.
*/
@Deprecated
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4017")
public interface BinaryLogSink extends Closeable {
/**
* Writes the {@code message} to the destination.
*/
void write(MessageLite message);
}
57 changes: 57 additions & 0 deletions services/src/main/java/io/grpc/services/BinaryLogs.java
@@ -0,0 +1,57 @@
/*
* 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.BinaryLog;
import io.grpc.ExperimentalApi;
import java.io.IOException;

/**
* @deprecated Use {@link io.grpc.protobuf.services.BinaryLogs} instead.
*/
@Deprecated
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4017")
public final class BinaryLogs {
/**
* Creates a binary log that writes to a temp file. <b>Warning:</b> this implementation is
* not performance optimized, and RPCs will experience back pressure if disk IO does not keep
* up.
*/
public static BinaryLog createBinaryLog() throws IOException {
return new BinaryLogProviderImpl();
}

/**
* Deprecated and will be removed in a future version of gRPC.
*/
@Deprecated
public static BinaryLog createBinaryLog(BinaryLogSink sink) throws IOException {
return new BinaryLogProviderImpl(sink);
}

/**
* Creates a binary log with a custom {@link BinaryLogSink} for receiving the logged data,
* and a config string as defined by
* <a href="https://github.com/grpc/proposal/blob/master/A16-binary-logging.md">
* A16-binary-logging</a>.
*/
public static BinaryLog createBinaryLog(BinaryLogSink sink, String configStr) throws IOException {
return new BinaryLogProviderImpl(sink, configStr);
}

private BinaryLogs() {}
}

0 comments on commit d8412ab

Please sign in to comment.