xds: Add counter and gauge metrics (#11661) · grpc/grpc-java@20d09ce

@@ -21,6 +21,7 @@

21212222

import com.google.common.annotations.VisibleForTesting;

2323

import com.google.common.collect.ImmutableList;

24+

import io.grpc.MetricRecorder;

2425

import io.grpc.internal.ExponentialBackoffPolicy;

2526

import io.grpc.internal.GrpcUtil;

2627

import io.grpc.internal.ObjectPool;

@@ -51,6 +52,8 @@ final class SharedXdsClientPoolProvider implements XdsClientPoolFactory {

5152

private static final boolean LOG_XDS_NODE_ID = Boolean.parseBoolean(

5253

System.getenv("GRPC_LOG_XDS_NODE_ID"));

5354

private static final Logger log = Logger.getLogger(XdsClientImpl.class.getName());

55+

private static final ExponentialBackoffPolicy.Provider BACKOFF_POLICY_PROVIDER =

56+

new ExponentialBackoffPolicy.Provider();

54575558

private final Bootstrapper bootstrapper;

5659

private 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 {

8690

ObjectPool<XdsClient> ref = targetToXdsClientMap.get(target);

8791

if (ref == null) {

8892

synchronized (lock) {

@@ -98,7 +102,7 @@ public ObjectPool<XdsClient> getOrCreate(String target) throws XdsInitialization

98102

if (bootstrapInfo.servers().isEmpty()) {

99103

throw new XdsInitializationException("No xDS server provided");

100104

}

101-

ref = new RefCountedXdsClientObjectPool(bootstrapInfo, target);

105+

ref = new RefCountedXdsClientObjectPool(bootstrapInfo, target, metricRecorder);

102106

targetToXdsClientMap.put(target, ref);

103107

}

104108

}

@@ -111,31 +115,33 @@ public ImmutableList<String> getTargets() {

111115

return ImmutableList.copyOf(targetToXdsClientMap.keySet());

112116

}

113117114-115118

private static class SharedXdsClientPoolProviderHolder {

116119

private 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();

125126

private final BootstrapInfo bootstrapInfo;

126127

private final String target; // The target associated with the xDS client.

128+

private final MetricRecorder metricRecorder;

127129

private final Object lock = new Object();

128130

@GuardedBy("lock")

129131

private ScheduledExecutorService scheduler;

130132

@GuardedBy("lock")

131133

private XdsClient xdsClient;

132134

@GuardedBy("lock")

133135

private 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) {

137142

this.bootstrapInfo = checkNotNull(bootstrapInfo);

138143

this.target = target;

144+

this.metricRecorder = metricRecorder;

139145

}

140146141147

@Override

@@ -146,6 +152,7 @@ public XdsClient getObject() {

146152

log.log(Level.INFO, "xDS node ID: {0}", bootstrapInfo.node().getId());

147153

}

148154

scheduler = SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE);

155+

metricReporter = new XdsClientMetricReporterImpl(metricRecorder, target);

149156

xdsClient = new XdsClientImpl(

150157

DEFAULT_XDS_TRANSPORT_FACTORY,

151158

bootstrapInfo,

@@ -154,7 +161,9 @@ public XdsClient getObject() {

154161

GrpcUtil.STOPWATCH_SUPPLIER,

155162

TimeProvider.SYSTEM_TIME_PROVIDER,

156163

MessagePrinter.INSTANCE,

157-

new TlsContextManagerImpl(bootstrapInfo));

164+

new TlsContextManagerImpl(bootstrapInfo),

165+

metricReporter);

166+

metricReporter.setXdsClient(xdsClient);

158167

}

159168

refCount++;

160169

return xdsClient;

@@ -168,6 +177,9 @@ public XdsClient returnObject(Object object) {

168177

if (refCount == 0) {

169178

xdsClient.shutdown();

170179

xdsClient = null;

180+

metricReporter.close();

181+

metricReporter = null;

182+

targetToXdsClientMap.remove(target);

171183

scheduler = SharedResourceHolder.release(GrpcUtil.TIMER_SERVICE, scheduler);

172184

}

173185

return null;