1 /* 2 * Copyright 2021 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 io.grpc.BindableService; 20 import io.grpc.ExperimentalApi; 21 import io.grpc.ServerServiceDefinition; 22 import io.grpc.protobuf.services.ChannelzService; 23 import java.lang.reflect.InvocationTargetException; 24 import java.lang.reflect.Method; 25 import java.util.ArrayList; 26 import java.util.Collections; 27 import java.util.List; 28 import java.util.logging.Level; 29 import java.util.logging.Logger; 30 import javax.annotation.concurrent.ThreadSafe; 31 32 /** 33 * Admin Interface provides a class of services for exposing the overall state of gRPC 34 * activity in a given binary. It aims to be a convenient API that provides available admin 35 * services. 36 */ 37 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/7929") 38 @ThreadSafe 39 public final class AdminInterface { 40 private static final int DEFAULT_CHANNELZ_MAX_PAGE_SIZE = 100; 41 private static final Logger logger = Logger.getLogger(AdminInterface.class.getName()); 42 43 // Do not instantiate. AdminInterface()44 private AdminInterface() {} 45 46 /** 47 * Returns a list of gRPC's built-in admin services. 48 * 49 * @return list of standard admin services 50 */ getStandardServices()51 public static List<ServerServiceDefinition> getStandardServices() { 52 List<ServerServiceDefinition> services = new ArrayList<>(); 53 services.add(ChannelzService.newInstance(DEFAULT_CHANNELZ_MAX_PAGE_SIZE).bindService()); 54 BindableService csds = null; 55 try { 56 Class<?> clazz = Class.forName("io.grpc.xds.CsdsService"); 57 Method m = clazz.getMethod("newInstance"); 58 csds = (BindableService) m.invoke(null); 59 } catch (ClassNotFoundException e) { 60 logger.log(Level.FINE, "Unable to find CSDS service", e); 61 } catch (NoSuchMethodException e) { 62 logger.log(Level.FINE, "Unable to load CSDS service", e); 63 } catch (IllegalAccessException e) { 64 logger.log(Level.FINE, "Unable to load CSDS service", e); 65 } catch (InvocationTargetException e) { 66 logger.log(Level.FINE, "Unable to load CSDS service", e); 67 } 68 if (csds != null) { 69 services.add(csds.bindService()); 70 } 71 return Collections.unmodifiableList(services); 72 } 73 } 74