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(); } }
@Override public void handle(Request request, Response response) throws Exception { request.attribute(METRICS, metricFactory.timer("webAdmin")); } }
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(); } }
private void setupExpectations() { when(metricFactory.timer(SAVE_BYTES_TIMER_NAME)) .thenReturn(saveBytesTimeMetric); when(metricFactory.timer(SAVE_INPUT_STREAM_TIMER_NAME)) .thenReturn(saveInputStreamTimeMetric); when(metricFactory.timer(READ_BYTES_TIMER_NAME)) .thenReturn(readBytesTimeMetric); when(metricFactory.timer(READ_TIMER_NAME)) .thenReturn(readTimeMetric); } }
@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(); } }
@VisibleForTesting void createAccountIfNeeded(MailboxSession session) { TimeMetric timeMetric = metricFactory.timer("JMAP-user-provisioning"); try { User user = session.getUser(); if (needsAccountCreation(user)) { createAccount(user); } } catch (AlreadyExistInUsersRepositoryException e) { // Ignore } catch (UsersRepositoryException e) { throw new RuntimeException(e); } 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(); }
@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(); } }
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(); }
@Before public void setUp() { FakeMailetConfig mailetConfig = FakeMailetConfig.builder() .setProperty(RemoteDeliveryConfiguration.DELIVERY_THREADS, "1") .setProperty(RemoteDeliveryConfiguration.DEBUG, "true") .setProperty(RemoteDeliveryConfiguration.DELAY_TIME, "1000,2000,3000,4000,5000") .build(); RemoteDeliveryConfiguration configuration = new RemoteDeliveryConfiguration(mailetConfig, mock(DomainList.class)); outgoingMailsMetric = mock(Metric.class); MetricFactory mockMetricFactory = mock(MetricFactory.class); when(mockMetricFactory.generate(anyString())).thenReturn(outgoingMailsMetric); when(mockMetricFactory.timer(anyString())).thenReturn(new NoopMetricFactory.NoopTimeMetric()); bouncer = mock(Bouncer.class); mailDelivrer = mock(MailDelivrer.class); mailQueue = mock(MailQueue.class); testee = new DeliveryRunnable(mailQueue, configuration, mockMetricFactory, bouncer, mailDelivrer, DeliveryRunnable.DEFAULT_NOT_STARTED, FIXED_DATE_SUPPLIER); }