下面列出了怎么用com.mongodb.connection.ServerDescription的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings("unchecked")
@Override
public void preProcess(WriteOperation t, Object proxy, Method method, Object[] args) {
if (!method.getName().equals("execute")) {
return;
}
WriteBinding binding = (WriteBinding) args[0];
ServerDescription serverDesc = binding.getWriteConnectionSource().getServerDescription();
String address = serverDesc.getAddress().toString();
String operationType = t.getClass().getSimpleName();
String url = "mongo://" + address + "/" + dbName;
Map<String, Object> params = new HashMap<String, Object>();
params.put(CaptureConstants.INFO_CLIENT_REQUEST_URL, url);
params.put(CaptureConstants.INFO_CLIENT_REQUEST_ACTION, operationType);
params.put(CaptureConstants.INFO_CLIENT_APPID, applicationId);
params.put(CaptureConstants.INFO_CLIENT_TYPE, "mongo.client");
if (logger.isDebugable()) {
logger.debug("Invoke START:" + url + ",op=" + operationType, null);
}
UAVServer.instance().runMonitorCaptureOnServerCapPoint(CaptureConstants.CAPPOINT_APP_CLIENT,
Monitor.CapturePhase.PRECAP, params);
// register adapter
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter",
"registerAdapter", MongoClientAdapter.class);
ivcContextParams = (Map<String, Object>) UAVServer.instance().runSupporter(
"com.creditease.uav.apm.supporters.InvokeChainSupporter", "runCap",
InvokeChainConstants.CHAIN_APP_CLIENT, InvokeChainConstants.CapturePhase.PRECAP, params,
MongoClientAdapter.class, args);
}
private void doCap(int rc, WriteBinding binding, Throwable e) {
ServerDescription serverDesc = binding.getWriteConnectionSource().getServerDescription();
String targetServer = "Mongo" + toVersionString(serverDesc.getVersion()) + "_"
+ serverDesc.getType().toString();
Map<String, Object> params = new HashMap<String, Object>();
params.put(CaptureConstants.INFO_CLIENT_TARGETSERVER, targetServer);
params.put(CaptureConstants.INFO_CLIENT_RESPONSECODE, rc);
if (logger.isDebugable()) {
logger.debug("Invoke END: rc=" + rc + "," + targetServer, null);
}
UAVServer.instance().runMonitorCaptureOnServerCapPoint(CaptureConstants.CAPPOINT_APP_CLIENT,
Monitor.CapturePhase.DOCAP, params);
if (rc == -1) {
params.put(CaptureConstants.INFO_CLIENT_RESPONSESTATE, e.toString());
}
if (ivcContextParams != null) {
ivcContextParams.putAll(params);
}
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "runCap",
InvokeChainConstants.CHAIN_APP_CLIENT, InvokeChainConstants.CapturePhase.DOCAP, ivcContextParams,
MongoClientAdapter.class, null);
}
@SuppressWarnings("unchecked")
@Override
public void preProcess(ReadOperation t, Object proxy, Method method, Object[] args) {
if (!method.getName().equals("execute")) {
return;
}
ReadBinding binding = (ReadBinding) args[0];
ServerDescription serverDesc = binding.getReadConnectionSource().getServerDescription();
String address = serverDesc.getAddress().toString();
String operationType = t.getClass().getSimpleName();
String url = "mongo://" + address + "/" + dbName;
Map<String, Object> params = new HashMap<String, Object>();
params.put(CaptureConstants.INFO_CLIENT_REQUEST_URL, url);
params.put(CaptureConstants.INFO_CLIENT_REQUEST_ACTION, operationType);
params.put(CaptureConstants.INFO_CLIENT_APPID, applicationId);
params.put(CaptureConstants.INFO_CLIENT_TYPE, "mongo.client");
if (logger.isDebugable()) {
logger.debug("Invoke START:" + url + ",op=" + operationType, null);
}
UAVServer.instance().runMonitorCaptureOnServerCapPoint(CaptureConstants.CAPPOINT_APP_CLIENT,
Monitor.CapturePhase.PRECAP, params);
// register adapter
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter",
"registerAdapter", MongoClientAdapter.class);
ivcContextParams = (Map<String, Object>) UAVServer.instance().runSupporter(
"com.creditease.uav.apm.supporters.InvokeChainSupporter", "runCap",
InvokeChainConstants.CHAIN_APP_CLIENT, InvokeChainConstants.CapturePhase.PRECAP, params,
MongoClientAdapter.class, args);
}
private void doCap(int rc, ReadBinding binding, Throwable e) {
ServerDescription serverDesc = binding.getReadConnectionSource().getServerDescription();
String targetServer = "Mongo" + toVersionString(serverDesc.getVersion()) + "_"
+ serverDesc.getType().toString();
Map<String, Object> params = new HashMap<String, Object>();
params.put(CaptureConstants.INFO_CLIENT_TARGETSERVER, targetServer);
params.put(CaptureConstants.INFO_CLIENT_RESPONSECODE, rc);
if (logger.isDebugable()) {
logger.debug("Invoke END: rc=" + rc + "," + targetServer, null);
}
UAVServer.instance().runMonitorCaptureOnServerCapPoint(CaptureConstants.CAPPOINT_APP_CLIENT,
Monitor.CapturePhase.DOCAP, params);
if (rc == -1) {
params.put(CaptureConstants.INFO_CLIENT_RESPONSESTATE, e.toString());
}
if (ivcContextParams != null) {
ivcContextParams.putAll(params);
}
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "runCap",
InvokeChainConstants.CHAIN_APP_CLIENT, InvokeChainConstants.CapturePhase.DOCAP, ivcContextParams,
MongoClientAdapter.class, null);
}
public static String getRemotePeer(Cluster cluster) {
StringBuilder peersBuilder = new StringBuilder();
for (ServerDescription description : cluster.getDescription().getAll()) {
ServerAddress address = description.getAddress();
peersBuilder.append(address.getHost()).append(":").append(address.getPort()).append(";");
}
return peersBuilder.substring(0, peersBuilder.length() - 1);
}
private List<String> getHostList(Object arg) {
if (!(arg instanceof Cluster)) {
return Collections.emptyList();
}
final Cluster cluster = (Cluster) arg;
final List<String> hostList = new ArrayList<String>();
Collection<ServerDescription> serverDescriptions;// = cluster.getDescription().getAll();//.getServerDescriptions();
try {
ClusterDescription.class.getDeclaredMethod("getServerDescriptions");
serverDescriptions = cluster.getDescription().getServerDescriptions();
} catch (NoSuchMethodException e) {
serverDescriptions = cluster.getDescription().getAll();
}
for (ServerDescription serverDescription : serverDescriptions) {
ServerAddress serverAddress = serverDescription.getAddress();
final String hostAddress = HostAndPort.toHostAndPortString(serverAddress.getHost(), serverAddress.getPort());
hostList.add(hostAddress);
}
return hostList;
}