default <T> CompletableFuture<T> runPublishingTimerMetric(String name, CompletableFuture<T> future) { TimeMetric timer = timer(name); return future.whenComplete((result, throwable) -> timer.stopAndPublish()); } }
default <T> T runPublishingTimerMetric(String name, Supplier<T> operation) { TimeMetric timer = timer(name); try { return operation.get(); } finally { timer.stopAndPublish(); } }
default <T> T runPublishingTimerMetric(String name, Supplier<T> operation) { TimeMetric timer = timer(name); try { return operation.get(); } finally { timer.stopAndPublish(); } }
@Override public void deliver(MailboxListener mailboxListener, Event event) { TimeMetric timer = metricFactory.timer("mailbox-listener-" + mailboxListener.getClass().getSimpleName()); try { mailboxListener.event(event); } catch (Throwable throwable) { LOGGER.error("Error while processing listener {} for {}", mailboxListener.getClass().getCanonicalName(), event.getClass().getCanonicalName(), throwable); } finally { timer.stopAndPublish(); } } }
private HttpServletRequest authenticate(HttpServletRequest httpRequest) { TimeMetric timeMetric = metricFactory.timer("JMAP-authentication-filter"); try { return authMethods.stream() .flatMap(auth -> createSession(auth, httpRequest)) .findFirst() .map(mailboxSession -> addSessionToRequest(httpRequest, mailboxSession)) .orElseThrow(UnauthorizedException::new); } finally { timeMetric.stopAndPublish(); } }
@Override public void handle(Request request, Response response) throws Exception { if (request.attribute(MetricPreFilter.METRICS) instanceof TimeMetric) { TimeMetric timeMetric = request.attribute(MetricPreFilter.METRICS); timeMetric.stopAndPublish(); } } }
@VisibleForTesting void createMailboxesIfNeeded(MailboxSession session) { TimeMetric timeMetric = metricFactory.timer("JMAP-mailboxes-provisioning"); try { User user = session.getUser(); createDefaultMailboxes(user); } catch (MailboxException e) { throw new RuntimeException(e); } finally { timeMetric.stopAndPublish(); } }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException { TimeMetric timeMetric = metricFactory.timer("JMAP-download-post"); String pathInfo = req.getPathInfo(); try { respondAttachmentAccessToken(getMailboxSession(req), DownloadPath.from(pathInfo), resp); } catch (IllegalArgumentException e) { LOGGER.error("Error while generating attachment access token '{}'", pathInfo, e); resp.setStatus(SC_BAD_REQUEST); } finally { timeMetric.stopAndPublish(); } }
@Override public SetMailboxesResponse process(SetMailboxesRequest request, MailboxSession mailboxSession) { TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + "SetMailboxesCreationProcessor"); SetMailboxesResponse.Builder builder = SetMailboxesResponse.builder(); try { Map<MailboxCreationId, MailboxId> creationIdsToCreatedMailboxId = new HashMap<>(); sortingHierarchicalCollections.sortFromRootToLeaf(request.getCreate().entrySet()) .forEach(entry -> createMailbox(entry.getKey(), entry.getValue(), mailboxSession, creationIdsToCreatedMailboxId, builder)); } catch (CycleDetectedException e) { markRequestsAsNotCreatedDueToCycle(request, builder); } timeMetric.stopAndPublish(); return builder.build(); }
@Override public SetMessagesResponse process(SetMessagesRequest request, MailboxSession mailboxSession) { TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + "SetMessageCreationProcessor"); Builder responseBuilder = SetMessagesResponse.builder(); request.getCreate() .forEach(create -> handleCreate(create, responseBuilder, mailboxSession)); timeMetric.stopAndPublish(); return responseBuilder.build(); }
@Override public SetMailboxesResponse process(SetMailboxesRequest request, MailboxSession mailboxSession) { TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + "SetMailboxesDestructionProcessor"); ImmutableMap<MailboxId, Mailbox> idToMailbox = mapDestroyRequests(request, mailboxSession); SetMailboxesResponse.Builder builder = SetMailboxesResponse.builder(); sortingHierarchicalCollections.sortFromLeafToRoot(idToMailbox.entrySet()) .forEach(entry -> destroyMailbox(entry, mailboxSession, builder)); notDestroyedRequests(request, idToMailbox, builder); timeMetric.stopAndPublish(); return builder.build(); }
@Override public String getHostName(InetAddress addr) { TimeMetric timeMetric = metricFactory.timer("getHostName"); String result; Name name = ReverseMap.fromAddress(addr); Record[] records = lookupNoException(name.toString(), Type.PTR, "PTR"); try { if (records == null) { result = addr.getHostAddress(); } else { PTRRecord ptr = (PTRRecord) records[0]; result = ptr.getTarget().toString(); } return result; } finally { timeMetric.stopAndPublish(); } }
protected final void process(M message, Responder responder, ImapSession session) { ImapCommand command = message.getCommand(); String tag = message.getTag(); TimeMetric timeMetric = metricFactory.timer(IMAP_PREFIX + command.getName()); doProcess(message, command, tag, responder, session); timeMetric.stopAndPublish(); }
@Test default void saveInputStreamShouldPublishSaveInputStreamTimerMetrics() { testee().save(new ByteArrayInputStream(BYTES_CONTENT)).join(); testee().save(new ByteArrayInputStream(BYTES_CONTENT)).join(); testee().save(new ByteArrayInputStream(BYTES_CONTENT)).join(); verify(metricsTestExtension.saveInputStreamTimeMetric, times(3)).stopAndPublish(); }
@Test default void saveBytesShouldPublishSaveBytesTimerMetrics() { testee().save(BYTES_CONTENT).join(); testee().save(BYTES_CONTENT).join(); verify(metricsTestExtension.saveBytesTimeMetric, times(2)).stopAndPublish(); }
private void doDeliverToListener(MailboxListener mailboxListener, Event event) { TimeMetric timer = metricFactory.timer(timerName(mailboxListener)); try (Closeable mdc = MDCBuilder.create() .addContext(EventBus.StructuredLoggingFields.EVENT_ID, event.getEventId()) .addContext(EventBus.StructuredLoggingFields.EVENT_CLASS, event.getClass()) .addContext(EventBus.StructuredLoggingFields.USER, event.getUser()) .addContext(EventBus.StructuredLoggingFields.LISTENER_CLASS, mailboxListener.getClass()) .build()) { mailboxListener.event(event); } catch (Exception e) { throw new RuntimeException(e); } finally { timer.stopAndPublish(); } }
@Override public SetMailboxesResponse process(SetMailboxesRequest request, MailboxSession mailboxSession) { TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + "mailboxUpdateProcessor"); SetMailboxesResponse.Builder responseBuilder = SetMailboxesResponse.builder(); request.getUpdate() .forEach((key, value) -> handleUpdate(key, value, responseBuilder, mailboxSession)); timeMetric.stopAndPublish(); return responseBuilder.build(); }
@Override public SetMessagesResponse process(SetMessagesRequest request, MailboxSession mailboxSession) { TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + "SetMessagesUpdateProcessor"); SetMessagesResponse.Builder responseBuilder = SetMessagesResponse.builder(); request.buildUpdatePatches(updatePatchConverter).forEach((id, patch) -> { if (patch.isValid()) { update(id, patch, mailboxSession, responseBuilder); } else { handleInvalidRequest(responseBuilder, id, patch.getValidationErrors(), patch); } } ); timeMetric.stopAndPublish(); return responseBuilder.build(); }
@Test default void readBytesShouldPublishReadBytesTimerMetrics() { BlobId blobId = testee().save(BYTES_CONTENT).join(); testee().readBytes(blobId).join(); testee().readBytes(blobId).join(); verify(metricsTestExtension.readBytesTimeMetric, times(2)).stopAndPublish(); }
@Test default void readShouldPublishReadTimerMetrics() { BlobId blobId = testee().save(BYTES_CONTENT).join(); testee().read(blobId); testee().read(blobId); verify(metricsTestExtension.readTimeMetric, times(2)).stopAndPublish(); } }