xds: Add counter and gauge metrics (#11661) · grpc/grpc-java@20d09ce
@@ -21,6 +21,7 @@
21212222import com.google.common.annotations.VisibleForTesting;
2323import com.google.common.collect.ImmutableList;
24+import io.grpc.MetricRecorder;
2425import io.grpc.internal.ExponentialBackoffPolicy;
2526import io.grpc.internal.GrpcUtil;
2627import io.grpc.internal.ObjectPool;
@@ -51,6 +52,8 @@ final class SharedXdsClientPoolProvider implements XdsClientPoolFactory {
5152private static final boolean LOG_XDS_NODE_ID = Boolean.parseBoolean(
5253System.getenv("GRPC_LOG_XDS_NODE_ID"));
5354private static final Logger log = Logger.getLogger(XdsClientImpl.class.getName());
55+private static final ExponentialBackoffPolicy.Provider BACKOFF_POLICY_PROVIDER =
56+new ExponentialBackoffPolicy.Provider();
54575558private final Bootstrapper bootstrapper;
5659private final Object lock = new Object();
@@ -82,7 +85,8 @@ public ObjectPool<XdsClient> get(String target) {
8285 }
83868487@Override
85-public ObjectPool<XdsClient> getOrCreate(String target) throws XdsInitializationException {
88+public ObjectPool<XdsClient> getOrCreate(String target, MetricRecorder metricRecorder)
89+throws XdsInitializationException {
8690ObjectPool<XdsClient> ref = targetToXdsClientMap.get(target);
8791if (ref == null) {
8892synchronized (lock) {
@@ -98,7 +102,7 @@ public ObjectPool<XdsClient> getOrCreate(String target) throws XdsInitialization
98102if (bootstrapInfo.servers().isEmpty()) {
99103throw new XdsInitializationException("No xDS server provided");
100104 }
101-ref = new RefCountedXdsClientObjectPool(bootstrapInfo, target);
105+ref = new RefCountedXdsClientObjectPool(bootstrapInfo, target, metricRecorder);
102106targetToXdsClientMap.put(target, ref);
103107 }
104108 }
@@ -111,31 +115,33 @@ public ImmutableList<String> getTargets() {
111115return ImmutableList.copyOf(targetToXdsClientMap.keySet());
112116 }
113117114-115118private static class SharedXdsClientPoolProviderHolder {
116119private static final SharedXdsClientPoolProvider instance = new SharedXdsClientPoolProvider();
117120 }
118121119122@ThreadSafe
120123@VisibleForTesting
121-static class RefCountedXdsClientObjectPool implements ObjectPool<XdsClient> {
124+class RefCountedXdsClientObjectPool implements ObjectPool<XdsClient> {
122125123-private static final ExponentialBackoffPolicy.Provider BACKOFF_POLICY_PROVIDER =
124-new ExponentialBackoffPolicy.Provider();
125126private final BootstrapInfo bootstrapInfo;
126127private final String target; // The target associated with the xDS client.
128+private final MetricRecorder metricRecorder;
127129private final Object lock = new Object();
128130@GuardedBy("lock")
129131private ScheduledExecutorService scheduler;
130132@GuardedBy("lock")
131133private XdsClient xdsClient;
132134@GuardedBy("lock")
133135private int refCount;
136+@GuardedBy("lock")
137+private XdsClientMetricReporterImpl metricReporter;
134138135139@VisibleForTesting
136-RefCountedXdsClientObjectPool(BootstrapInfo bootstrapInfo, String target) {
140+RefCountedXdsClientObjectPool(BootstrapInfo bootstrapInfo, String target,
141+MetricRecorder metricRecorder) {
137142this.bootstrapInfo = checkNotNull(bootstrapInfo);
138143this.target = target;
144+this.metricRecorder = metricRecorder;
139145 }
140146141147@Override
@@ -146,6 +152,7 @@ public XdsClient getObject() {
146152log.log(Level.INFO, "xDS node ID: {0}", bootstrapInfo.node().getId());
147153 }
148154scheduler = SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE);
155+metricReporter = new XdsClientMetricReporterImpl(metricRecorder, target);
149156xdsClient = new XdsClientImpl(
150157DEFAULT_XDS_TRANSPORT_FACTORY,
151158bootstrapInfo,
@@ -154,7 +161,9 @@ public XdsClient getObject() {
154161GrpcUtil.STOPWATCH_SUPPLIER,
155162TimeProvider.SYSTEM_TIME_PROVIDER,
156163MessagePrinter.INSTANCE,
157-new TlsContextManagerImpl(bootstrapInfo));
164+new TlsContextManagerImpl(bootstrapInfo),
165+metricReporter);
166+metricReporter.setXdsClient(xdsClient);
158167 }
159168refCount++;
160169return xdsClient;
@@ -168,6 +177,9 @@ public XdsClient returnObject(Object object) {
168177if (refCount == 0) {
169178xdsClient.shutdown();
170179xdsClient = null;
180+metricReporter.close();
181+metricReporter = null;
182+targetToXdsClientMap.remove(target);
171183scheduler = SharedResourceHolder.release(GrpcUtil.TIMER_SERVICE, scheduler);
172184 }
173185return null;