/** * Hook method for overriding how the template creates {@link TransactionMonitor}s * * @param cls class to use (may be null) * @param monitorName should not be null * @return {@link TransactionMonitor} to use */ protected TransactionMonitor createMonitor(Class cls, String monitorName) { TransactionMonitor monitor; if (cls == null) { monitor = new TransactionMonitor(monitorName); } else { monitor = new TransactionMonitor(cls, monitorName); } return monitor; }
/** * Create a new transaction monitor using the provided * class and method to generate a name. The monitor is * marked as failed by default. Also, the start time of * this transaction is noted, thereby starting the stop * watch. * * @param klass the class that we're monitoring * @param method a string containing the method name that we're monitoring */ public TransactionMonitor(Class klass, String method) { this(formatName(klass, method)); }
monitor.set(MONITOR_ARGUMENTS_NAME, invocation.getArguments()); monitor.set(MONITOR_RESULT_NAME, result); monitor.succeeded(); monitor.failedDueTo(t); throw t; monitor.done();
/** * Override this method to override how the Template executes the callback. * You probably shouldn't need to override this; but who am I to judge? * * @param callback {@link TransactionMonitorCallback} to execute * @param monitor {@link TransactionMonitor} to use * @return anything the callback returns */ protected Object executeCallback(TransactionMonitorCallback callback, TransactionMonitor monitor) { try { // This looks wrong but it's intentional. We want to allow the callback to // mark the monitor as failed in its code, even if it didn't throw an exception. // Therefore, we're turning the fail-by-default TransactionMonitor into // a succeeded-by-default object. monitor.succeeded(); return callback.doInMonitor(monitor); } catch (RuntimeException e) { monitor.failedDueTo(e); throw e; } catch (Error e) { monitor.failedDueTo(e); throw e; } finally { monitor.done(); } }
/** * Stops the stop watch for this monitor. Delegates to AbstractMonitor.process(). */ public void done() { Date endTime = new Date(); set(Attribute.END_TIME, endTime).serializable().lock(); Date startTime = (Date) get(Attribute.START_TIME); set(Attribute.LATENCY, endTime.getTime() - startTime.getTime()).serializable().lock(); process(); }
if (TransactionMonitor.class.isAssignableFrom(monitor.getClass())) { TransactionMonitor parent = (TransactionMonitor) monitor; long latency = parent.getAsLong(Attribute.LATENCY); if (latency > threshold) { ArrayList list = new ArrayList(); Iterator iter = parent.getChildMonitors().iterator(); while (iter.hasNext()) { Object next = iter.next(); fireMonitoringGapEvent(parent, leftChild, rightChild, latency); } else { Date parentStartDate = (Date) parent.get(Attribute.START_TIME); Date parentEndDate = (Date) parent.get(Attribute.END_TIME); if (listSize == 1) { rightChild = (TransactionMonitor) list.get(0); leftChild = rightChild; Date rightChildStartDate = (Date) rightChild.get(Attribute.START_TIME); Date leftChildEndDate = (Date) leftChild.get(Attribute.END_TIME); latency = rightChildStartDate.getTime() - parentStartDate.getTime(); if (latency > threshold) { Date startDateB = (Date) rightChild.get(Attribute.START_TIME); if (startDateB.getTime() - parentStartDate.getTime() > threshold) { fireMonitoringGapEvent(parent, null, rightChild, latency); leftChild = (TransactionMonitor) list.get(aIdx); rightChild = (TransactionMonitor) list.get(bIdx); Date endDateA = (Date) leftChild.get(Attribute.END_TIME); startDateB = (Date) rightChild.get(Attribute.START_TIME);
public void process(Monitor monitor) { if (enabled && TransactionMonitor.class.isAssignableFrom(monitor.getClass())) { TransactionMonitor transactionMonitor = (TransactionMonitor) monitor; ThreadMXBean tmxbean = ManagementFactory.getThreadMXBean(); long id = Thread.currentThread().getId(); ThreadInfo threadInfo = tmxbean.getThreadInfo(id); transactionMonitor.set("blockedCount", threadInfo.getBlockedCount()-transactionMonitor.getAsLong("startBlockedCount")); transactionMonitor.set("blockedTime", threadInfo.getBlockedTime()-transactionMonitor.getAsLong("startBlockedTime")); transactionMonitor.set("waitedCount", threadInfo.getWaitedCount()-transactionMonitor.getAsLong("startWaitedCount")); transactionMonitor.set("waitedTime", threadInfo.getWaitedTime()-transactionMonitor.getAsLong("startWaitedTime")); } }
/** * Marks this transaction as having succeeded. */ public void succeeded() { set(Attribute.FAILED, false); }
private void fireMonitoringGapEvent(TransactionMonitor tMon, TransactionMonitor leftChild, TransactionMonitor rightChild, long latency) { EventMonitor eventMonitor = new EventMonitor("MonitoringCoverageGap"); eventMonitor.set("monitorName", tMon.get(Attribute.NAME)); eventMonitor.set("leftChild", leftChild == null ? null : leftChild.get(Attribute.NAME)); eventMonitor.set("rightChild", rightChild == null ? null : rightChild.get(Attribute.NAME)); eventMonitor.set(Attribute.LATENCY, latency); eventMonitor.fire(); }
/** * Marks this transaction as having failed. */ public void failed() { set(Attribute.FAILED, true); }
@VisibleForTesting TransactionMonitor createTransactionMonitor(final MethodInvocation invocation) { final MonitoringLevel level = getMonitoringLevel(invocation); final String nameFromAnnotation = getNameFromAnnotation(invocation); if (this.prependClassName || (nameFromAnnotation == null)) { final String name = nameFromAnnotation == null ? invocation.getMethod().getName() : nameFromAnnotation; return new TransactionMonitor(invocation.getMethod().getDeclaringClass(), name, level); } else { return new TransactionMonitor(nameFromAnnotation, level); } }
/** * Create a new transaction monitor using the provided * class and method to generate a name, with the provided * inherited attributes. The monitor is markedas failed * by default. Also, the start time of this transaction * is noted, thereby starting the stop watch. * * @param klass the class that we're monitoring * @param method a string containing the method name that we're monitoring * @param inheritedAttributes the collection of inherited attributes */ public TransactionMonitor(Class klass, String method, Map inheritedAttributes) { this(formatName(klass, method), inheritedAttributes); }
/** * Create a new transaction monitor using the provided * class and method to generate a name, with the provided * monitoring level. The monitor is markedas failed by * default. Also, the start time of this transaction is * noted, thereby starting the stop watch. * * @param klass the class that we're monitoring * @param method a string containing the method name that we're monitoring * @param monitoringLevel the monitoring level */ public TransactionMonitor(Class klass, String method, MonitoringLevel monitoringLevel) { this(formatName(klass, method), monitoringLevel); }