Search in sources :

Example 1 with DatarouterDuration

use of io.datarouter.util.duration.DatarouterDuration in project datarouter by hotpads.

the class JobWrapper method logSuccess.

protected void logSuccess() {
    long startDelayMs = Duration.between(scheduledTime, tracker.getStartTime()).toMillis();
    Duration elapsedTime = Duration.between(tracker.getStartTime(), tracker.getFinishTime());
    jobCounters.duration(jobClass, elapsedTime);
    Optional<Instant> nextJobTriggerTime = jobPackage.getNextValidTimeAfter(Date.from(scheduledTime)).map(Date::toInstant);
    String jobCompletionLog = "finished in " + new DatarouterDuration(elapsedTime) + " jobName=" + jobClass.getSimpleName() + " durationMs=" + elapsedTime.toMillis();
    if (startDelayMs > 1000) {
        jobCounters.startedAfterLongDelay(jobClass);
        jobCompletionLog += " startDelayMs= " + startDelayMs;
    }
    if (nextJobTriggerTime.isPresent() && Instant.now().isAfter(nextJobTriggerTime.get())) {
        jobCounters.missedNextTrigger(jobClass);
        jobCompletionLog += " missed next trigger";
    }
    if (ComparableTool.gt(elapsedTime, Duration.ofHours(3))) {
        logger.warn("long-running job " + jobCompletionLog);
    } else if (ComparableTool.gt(elapsedTime, Duration.ofMillis(500))) {
        logger.warn(jobCompletionLog);
    } else {
        logger.info(jobCompletionLog);
    }
}
Also used : Instant(java.time.Instant) DatarouterDuration(io.datarouter.util.duration.DatarouterDuration) DatarouterDuration(io.datarouter.util.duration.DatarouterDuration) Duration(java.time.Duration) Date(java.util.Date)

Example 2 with DatarouterDuration

use of io.datarouter.util.duration.DatarouterDuration in project datarouter by hotpads.

the class WebappInstanceAlertJob method run.

@Override
public void run(TaskTracker tracker) {
    WebappInstance webappInstance = webappInstanceService.buildCurrentWebappInstance();
    Instant build = webappInstance.getBuildInstant();
    DatarouterDuration buildAge = DatarouterDuration.age(build);
    if (buildAge.isLongerThan(settings.staleWebappInstanceThreshold.get())) {
        sendEmail(webappInstance, buildAge);
    }
}
Also used : Instant(java.time.Instant) DatarouterDuration(io.datarouter.util.duration.DatarouterDuration) WebappInstance(io.datarouter.webappinstance.storage.webappinstance.WebappInstance)

Example 3 with DatarouterDuration

use of io.datarouter.util.duration.DatarouterDuration in project datarouter by hotpads.

the class WebappInstanceAlertJob method makeContent.

private ContainerTag<?> makeContent(WebappInstance webappInstance, DatarouterDuration buildAge) {
    ZoneId zoneId = defaultDistributionListZoneId.get();
    var rows = List.of(new Twin<>("webapp", webappInstance.getKey().getWebappName()), new Twin<>("build date", ZonedDateFormatterTool.formatInstantWithZone(webappInstance.getBuildInstant(), zoneId)), new Twin<>("build age", buildAge.toString()), new Twin<>("startup date", ZonedDateFormatterTool.formatInstantWithZone(webappInstance.getStartupInstant(), zoneId)), new Twin<>("commitId", webappInstance.getCommitId()));
    return new J2HtmlEmailTable<Twin<String>>().withColumn(new J2HtmlEmailTableColumn<>(null, row -> makeDivBoldRight(row.getLeft()))).withColumn(new J2HtmlEmailTableColumn<>(null, row -> text(row.getRight()))).build(rows);
}
Also used : Twin(io.datarouter.util.tuple.Twin) TagCreator.text(j2html.TagCreator.text) TagCreator.body(j2html.TagCreator.body) DatarouterHtmlEmailService(io.datarouter.web.email.DatarouterHtmlEmailService) DatarouterWebappInstanceSettingRoot(io.datarouter.webappinstance.config.DatarouterWebappInstanceSettingRoot) J2HtmlEmailTableColumn(io.datarouter.email.html.J2HtmlEmailTable.J2HtmlEmailTableColumn) DatarouterWebappInstancePaths(io.datarouter.webappinstance.config.DatarouterWebappInstancePaths) Inject(javax.inject.Inject) WebappInstanceService(io.datarouter.webappinstance.service.WebappInstanceService) ServerTypeDetector(io.datarouter.storage.servertype.ServerTypeDetector) J2HtmlEmailTable(io.datarouter.email.html.J2HtmlEmailTable) BaseJob(io.datarouter.job.BaseJob) StandardDatarouterEmailHeaderService(io.datarouter.web.email.StandardDatarouterEmailHeaderService) TaskTracker(io.datarouter.instrumentation.task.TaskTracker) WebappInstance(io.datarouter.webappinstance.storage.webappinstance.WebappInstance) Instant(java.time.Instant) DatarouterDuration(io.datarouter.util.duration.DatarouterDuration) ZoneId(java.time.ZoneId) ContainerTag(j2html.tags.ContainerTag) ZonedDateFormatterTool(io.datarouter.util.time.ZonedDateFormatterTool) DefaultEmailDistributionListZoneId(io.datarouter.web.config.properties.DefaultEmailDistributionListZoneId) WebappInstanceAlertEmailType(io.datarouter.email.type.DatarouterEmailTypes.WebappInstanceAlertEmailType) DomContent(j2html.tags.DomContent) List(java.util.List) TagCreator.div(j2html.TagCreator.div) ZoneId(java.time.ZoneId) DefaultEmailDistributionListZoneId(io.datarouter.web.config.properties.DefaultEmailDistributionListZoneId) J2HtmlEmailTableColumn(io.datarouter.email.html.J2HtmlEmailTable.J2HtmlEmailTableColumn) Twin(io.datarouter.util.tuple.Twin)

Example 4 with DatarouterDuration

use of io.datarouter.util.duration.DatarouterDuration in project datarouter by hotpads.

the class ResponseTool method sendJson.

public static void sendJson(HttpServletResponse response, String body) throws IOException {
    response.setContentType(ResponseTool.CONTENT_TYPE_APPLICATION_JSON);
    // close the writer before the trace to be able to include the close() duration in the measure
    long start = System.currentTimeMillis();
    try (var $ = TracerTool.startSpan("ResponseTool sendJson", TraceSpanGroupType.HTTP);
        OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8)) {
        writer.append(body);
    }
    DatarouterDuration duration = DatarouterDuration.ageMs(start);
    if (duration.isLongerThan(LOG_THRESHOLD)) {
        logger.warn("duration={} durationMs={} size={} traceContext={}", duration, duration.toMillis(), body.length(), TracerTool.getCurrentTraceparent().orElse(null));
    }
}
Also used : DatarouterDuration(io.datarouter.util.duration.DatarouterDuration) OutputStreamWriter(java.io.OutputStreamWriter)

Example 5 with DatarouterDuration

use of io.datarouter.util.duration.DatarouterDuration in project datarouter by hotpads.

the class WebappInstanceServersHandler method makeContent.

private ContainerTag<?> makeContent(Page<WebappInstanceLogDto> page, List<String> activeServerNames) {
    var form = Bootstrap4PagerHtml.renderForm(page).withClass("mt-4");
    var linkBar = Bootstrap4PagerHtml.renderLinkBar(page).withClass("mt-2");
    ZoneId zoneId = getUserZoneId();
    var table = new J2HtmlTable<WebappInstanceLogDto>().withClasses("sortable table table-sm table-striped my-4 border").withHtmlColumn("", row -> {
        if (activeServerNames.contains(row.key.serverName)) {
            if (row.shutdown.isEmpty()) {
                return td();
            }
            if (row.shutdown.get().toEpochMilli() < System.currentTimeMillis()) {
                return td();
            }
            return td(span("Active").withClass("badge badge-success"));
        }
        return td();
    }).withColumn("Server Name", row -> row.key.serverName).withColumn("Private IP", row -> row.key.serverPrivateIp).withColumn("Build Date", row -> ZonedDateFormatterTool.formatInstantWithZone(row.key.buildDate, zoneId)).withColumn("Startup Range", row -> row.startup.map(date -> ZonedDateFormatterTool.formatInstantWithZone(date, zoneId)).orElse("unknown") + " - " + row.shutdown.map(date -> ZonedDateFormatterTool.formatInstantWithZone(date, zoneId)).orElse("unknown")).withColumn("Up Time", row -> {
        if (row.startup.isEmpty() || row.shutdown.isEmpty()) {
            return "unknown";
        }
        var duration = Duration.ofMillis(row.shutdown.get().toEpochMilli() - row.startup.get().toEpochMilli());
        return new DatarouterDuration(duration).toString(TimeUnit.MINUTES);
    }).build(page.rows);
    return div(form, linkBar, table).withClass("container-fluid");
}
Also used : J2HtmlTable(io.datarouter.web.html.j2html.J2HtmlTable) Scanner(io.datarouter.scanner.Scanner) DatarouterWebappInstanceLogDao(io.datarouter.webappinstance.storage.webappinstancelog.DatarouterWebappInstanceLogDao) Bootstrap4PagerHtml(io.datarouter.web.html.pager.Bootstrap4PagerHtml) HashMap(java.util.HashMap) DatarouterWebappInstancePaths(io.datarouter.webappinstance.config.DatarouterWebappInstancePaths) Inject(javax.inject.Inject) Duration(java.time.Duration) WebappInstanceKey(io.datarouter.webappinstance.storage.webappinstance.WebappInstanceKey) J2HtmlTable(io.datarouter.web.html.j2html.J2HtmlTable) Page(io.datarouter.web.html.pager.MemoryPager.Page) WebappInstanceLog(io.datarouter.webappinstance.storage.webappinstancelog.WebappInstanceLog) TagCreator.span(j2html.TagCreator.span) Mav(io.datarouter.web.handler.mav.Mav) WebappInstanceLogKey(io.datarouter.webappinstance.storage.webappinstancelog.WebappInstanceLogKey) Instant(java.time.Instant) DatarouterDuration(io.datarouter.util.duration.DatarouterDuration) ZoneId(java.time.ZoneId) ContainerTag(j2html.tags.ContainerTag) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ZonedDateFormatterTool(io.datarouter.util.time.ZonedDateFormatterTool) List(java.util.List) MemoryPager(io.datarouter.web.html.pager.MemoryPager) TagCreator.td(j2html.TagCreator.td) BaseHandler(io.datarouter.web.handler.BaseHandler) Optional(java.util.Optional) Bootstrap4PageFactory(io.datarouter.web.html.j2html.bootstrap4.Bootstrap4PageFactory) Comparator(java.util.Comparator) TagCreator.div(j2html.TagCreator.div) Collections(java.util.Collections) DatarouterWebappInstanceDao(io.datarouter.webappinstance.storage.webappinstance.DatarouterWebappInstanceDao) MemorySorter(io.datarouter.web.html.pager.MemorySorter) ZoneId(java.time.ZoneId) DatarouterDuration(io.datarouter.util.duration.DatarouterDuration)

Aggregations

DatarouterDuration (io.datarouter.util.duration.DatarouterDuration)8 Instant (java.time.Instant)4 ZonedDateFormatterTool (io.datarouter.util.time.ZonedDateFormatterTool)2 Twin (io.datarouter.util.tuple.Twin)2 BaseHandler (io.datarouter.web.handler.BaseHandler)2 Mav (io.datarouter.web.handler.mav.Mav)2 DatarouterWebappInstancePaths (io.datarouter.webappinstance.config.DatarouterWebappInstancePaths)2 WebappInstance (io.datarouter.webappinstance.storage.webappinstance.WebappInstance)2 TagCreator.div (j2html.TagCreator.div)2 ContainerTag (j2html.tags.ContainerTag)2 Duration (java.time.Duration)2 ZoneId (java.time.ZoneId)2 List (java.util.List)2 Inject (javax.inject.Inject)2 J2HtmlEmailTable (io.datarouter.email.html.J2HtmlEmailTable)1 J2HtmlEmailTableColumn (io.datarouter.email.html.J2HtmlEmailTable.J2HtmlEmailTableColumn)1 WebappInstanceAlertEmailType (io.datarouter.email.type.DatarouterEmailTypes.WebappInstanceAlertEmailType)1 DatarouterChangelogDtoBuilder (io.datarouter.instrumentation.changelog.ChangelogRecorder.DatarouterChangelogDtoBuilder)1 TaskTracker (io.datarouter.instrumentation.task.TaskTracker)1 BaseJob (io.datarouter.job.BaseJob)1