api: implement admin interface API (#7928)

Adds an AdminInterface API that automatically loads available admin services (currently, only supports Channelz and CSDS).
This commit is contained in:
Chengyuan Zhang 2021-03-04 16:53:03 -08:00 committed by GitHub
parent 37b94b3074
commit 18f5fc2d7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,72 @@
/*
* Copyright 2021 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 io.grpc.BindableService;
import io.grpc.ExperimentalApi;
import io.grpc.ServerServiceDefinition;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.concurrent.ThreadSafe;
/**
* Admin Interface provides a class of services for exposing the overall state of gRPC
* activity in a given binary. It aims to be a convenient API that provides available admin
* services.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/7929")
@ThreadSafe
public final class AdminInterface {
private static final int DEFAULT_CHANNELZ_MAX_PAGE_SIZE = 100;
private static final Logger logger = Logger.getLogger(AdminInterface.class.getName());
// Do not instantiate.
private AdminInterface() {}
/**
* Returns a list of gRPC's built-in admin services.
*
* @return list of standard admin services
*/
public static List<ServerServiceDefinition> getStandardServices() {
List<ServerServiceDefinition> services = new ArrayList<>();
services.add(ChannelzService.newInstance(DEFAULT_CHANNELZ_MAX_PAGE_SIZE).bindService());
BindableService csds = null;
try {
Class<?> clazz = Class.forName("io.grpc.xds.CsdsService");
Method m = clazz.getMethod("newInstance");
csds = (BindableService) m.invoke(null);
} catch (ClassNotFoundException e) {
logger.log(Level.FINE, "Unable to find CSDS service", e);
} catch (NoSuchMethodException e) {
logger.log(Level.FINE, "Unable to load CSDS service", e);
} catch (IllegalAccessException e) {
logger.log(Level.FINE, "Unable to load CSDS service", e);
} catch (InvocationTargetException e) {
logger.log(Level.FINE, "Unable to load CSDS service", e);
}
if (csds != null) {
services.add(csds.bindService());
}
return Collections.unmodifiableList(services);
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2021 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 static com.google.common.truth.Truth.assertThat;
import com.google.common.collect.Iterables;
import io.grpc.ServerServiceDefinition;
import io.grpc.channelz.v1.ChannelzGrpc;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class AdminInterfaceChannelzTest {
@Test
public void autoChannelz() {
ServerServiceDefinition channelz =
Iterables.getOnlyElement(AdminInterface.getStandardServices());
assertThat(channelz.getServiceDescriptor().getName()).isEqualTo(ChannelzGrpc.SERVICE_NAME);
}
}