use of com.alibaba.dubbo.monitor.Monitor in project dubbo by alibaba.
the class AbstractMonitorFactory method getMonitor.
public Monitor getMonitor(URL url) {
url = url.setPath(MonitorService.class.getName()).addParameter(Constants.INTERFACE_KEY, MonitorService.class.getName());
String key = url.toServiceStringWithoutResolving();
Monitor monitor = MONITORS.get(key);
Future<Monitor> future = FUTURES.get(key);
if (monitor != null || future != null) {
return monitor;
}
LOCK.lock();
try {
monitor = MONITORS.get(key);
future = FUTURES.get(key);
if (monitor != null || future != null) {
return monitor;
}
final URL monitorUrl = url;
final ListenableFutureTask<Monitor> listenableFutureTask = ListenableFutureTask.create(new MonitorCreator(monitorUrl));
listenableFutureTask.addListener(new MonitorListener(key));
executor.execute(listenableFutureTask);
FUTURES.put(key, listenableFutureTask);
return null;
} finally {
// unlock
LOCK.unlock();
}
}
use of com.alibaba.dubbo.monitor.Monitor in project dubbo by alibaba.
the class MonitorFilter method collect.
// collect info
private void collect(Invoker<?> invoker, Invocation invocation, Result result, String remoteHost, long start, boolean error) {
try {
// ---- service statistics ----
// invocation cost
long elapsed = System.currentTimeMillis() - start;
// current concurrent count
int concurrent = getConcurrent(invoker, invocation).get();
String application = invoker.getUrl().getParameter(Constants.APPLICATION_KEY);
// service name
String service = invoker.getInterface().getName();
// method name
String method = RpcUtils.getMethodName(invocation);
URL url = invoker.getUrl().getUrlParameter(Constants.MONITOR_KEY);
Monitor monitor = monitorFactory.getMonitor(url);
if (monitor == null) {
return;
}
int localPort;
String remoteKey;
String remoteValue;
if (Constants.CONSUMER_SIDE.equals(invoker.getUrl().getParameter(Constants.SIDE_KEY))) {
// ---- for service consumer ----
localPort = 0;
remoteKey = MonitorService.PROVIDER;
remoteValue = invoker.getUrl().getAddress();
} else {
// ---- for service provider ----
localPort = invoker.getUrl().getPort();
remoteKey = MonitorService.CONSUMER;
remoteValue = remoteHost;
}
String input = "", output = "";
if (invocation.getAttachment(Constants.INPUT_KEY) != null) {
input = invocation.getAttachment(Constants.INPUT_KEY);
}
if (result != null && result.getAttachment(Constants.OUTPUT_KEY) != null) {
output = result.getAttachment(Constants.OUTPUT_KEY);
}
monitor.collect(new URL(Constants.COUNT_PROTOCOL, NetUtils.getLocalHost(), localPort, service + "/" + method, MonitorService.APPLICATION, application, MonitorService.INTERFACE, service, MonitorService.METHOD, method, remoteKey, remoteValue, error ? MonitorService.FAILURE : MonitorService.SUCCESS, "1", MonitorService.ELAPSED, String.valueOf(elapsed), MonitorService.CONCURRENT, String.valueOf(concurrent), Constants.INPUT_KEY, input, Constants.OUTPUT_KEY, output));
} catch (Throwable t) {
logger.error("Failed to monitor count service " + invoker.getUrl() + ", cause: " + t.getMessage(), t);
}
}
use of com.alibaba.dubbo.monitor.Monitor in project dubbo by alibaba.
the class AbstractMonitorFactoryTest method testMonitorFactoryCache.
@Test
public void testMonitorFactoryCache() throws Exception {
URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostAddress() + ":2233");
Monitor monitor1 = monitorFactory.getMonitor(url);
Monitor monitor2 = monitorFactory.getMonitor(url);
if (monitor1 == null || monitor2 == null) {
Thread.sleep(2000);
monitor1 = monitorFactory.getMonitor(url);
monitor2 = monitorFactory.getMonitor(url);
}
Assert.assertEquals(monitor1, monitor2);
}
use of com.alibaba.dubbo.monitor.Monitor in project dubbo by alibaba.
the class MonitorFilter method collect.
// 信息采集
private void collect(Invoker<?> invoker, Invocation invocation, Result result, RpcContext context, long start, boolean error) {
try {
// ---- 服务信息获取 ----
// 计算调用耗时
long elapsed = System.currentTimeMillis() - start;
// 当前并发数
int concurrent = getConcurrent(invoker, invocation).get();
String application = invoker.getUrl().getParameter(Constants.APPLICATION_KEY);
// 获取服务名称
String service = invoker.getInterface().getName();
// 获取方法名
String method = RpcUtils.getMethodName(invocation);
URL url = invoker.getUrl().getUrlParameter(Constants.MONITOR_KEY);
Monitor monitor = monitorFactory.getMonitor(url);
int localPort;
String remoteKey;
String remoteValue;
if (Constants.CONSUMER_SIDE.equals(invoker.getUrl().getParameter(Constants.SIDE_KEY))) {
// ---- 服务消费方监控 ----
// 消费方必须在invoke()之后获取context信息
context = RpcContext.getContext();
localPort = 0;
remoteKey = MonitorService.PROVIDER;
remoteValue = invoker.getUrl().getAddress();
} else {
// ---- 服务提供方监控 ----
localPort = invoker.getUrl().getPort();
remoteKey = MonitorService.CONSUMER;
remoteValue = context.getRemoteHost();
}
String input = "", output = "";
if (invocation.getAttachment(Constants.INPUT_KEY) != null) {
input = invocation.getAttachment(Constants.INPUT_KEY);
}
if (result != null && result.getAttachment(Constants.OUTPUT_KEY) != null) {
output = result.getAttachment(Constants.OUTPUT_KEY);
}
monitor.collect(new URL(Constants.COUNT_PROTOCOL, NetUtils.getLocalHost(), localPort, service + "/" + method, MonitorService.APPLICATION, application, MonitorService.INTERFACE, service, MonitorService.METHOD, method, remoteKey, remoteValue, error ? MonitorService.FAILURE : MonitorService.SUCCESS, "1", MonitorService.ELAPSED, String.valueOf(elapsed), MonitorService.CONCURRENT, String.valueOf(concurrent), Constants.INPUT_KEY, input, Constants.OUTPUT_KEY, output));
} catch (Throwable t) {
logger.error("Failed to monitor count service " + invoker.getUrl() + ", cause: " + t.getMessage(), t);
}
}
use of com.alibaba.dubbo.monitor.Monitor in project dubbo by alibaba.
the class AbstractMonitorFactoryTest method testMonitorFactoryIpCache.
@Test
public void testMonitorFactoryIpCache() throws Exception {
URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":2233");
Monitor monitor1 = monitorFactory.getMonitor(url);
Monitor monitor2 = monitorFactory.getMonitor(url);
if (monitor1 == null || monitor2 == null) {
Thread.sleep(2000);
monitor1 = monitorFactory.getMonitor(url);
monitor2 = monitorFactory.getMonitor(url);
}
Assert.assertEquals(monitor1, monitor2);
}
Aggregations