Search in sources :

Example 1 with PerformanceStatMessage

use of core.framework.log.message.PerformanceStatMessage in project core-ng-project by neowu.

the class ActionLogMessageFactoryTest method actionLog.

@Test
void actionLog() {
    var log = new ActionLog("begin", null);
    log.action("action");
    log.process(new LogEvent("logger", Markers.errorCode("ERROR_CODE"), LogLevel.WARN, "message", null, null));
    log.track("db", 1000, 1, 2);
    log.track("http", 2000, 0, 0);
    log.context("order_id", "id1", "id2");
    ActionLogMessage message = factory.create(log);
    assertThat(message).isNotNull();
    assertThat(message.app).isEqualTo(LogManager.APP_NAME);
    assertThat(message.action).isEqualTo("action");
    assertThat(message.errorCode).isEqualTo("ERROR_CODE");
    assertThat(message.traceLog).isNotEmpty();
    PerformanceStatMessage stats = message.performanceStats.get("db");
    assertThat(stats.totalElapsed).isEqualTo(1000);
    assertThat(stats.count).isEqualTo(1);
    assertThat(stats.readEntries).isEqualTo(1);
    assertThat(stats.writeEntries).isEqualTo(2);
    stats = message.performanceStats.get("http");
    assertThat(stats.totalElapsed).isEqualTo(2000);
    assertThat(stats.count).isEqualTo(1);
    assertThat(stats.readEntries).isNull();
    assertThat(stats.writeEntries).isNull();
}
Also used : PerformanceStatMessage(core.framework.log.message.PerformanceStatMessage) ActionLogMessage(core.framework.log.message.ActionLogMessage) Test(org.junit.jupiter.api.Test)

Example 2 with PerformanceStatMessage

use of core.framework.log.message.PerformanceStatMessage in project core-ng-project by neowu.

the class ConsoleAppenderTest method perf.

private PerformanceStatMessage perf(long elapsed, int read, int write) {
    PerformanceStatMessage stat = new PerformanceStatMessage();
    stat.count = 1;
    stat.totalElapsed = elapsed;
    stat.readEntries = read;
    stat.writeEntries = write;
    return stat;
}
Also used : PerformanceStatMessage(core.framework.log.message.PerformanceStatMessage)

Example 3 with PerformanceStatMessage

use of core.framework.log.message.PerformanceStatMessage in project core-ng-project by neowu.

the class ConsoleAppender method message.

String message(ActionLogMessage log) {
    // according to benchmark, create DecimalFormat every time is faster than String.format("%.9", value)
    var format = new DecimalFormat();
    var builder = new StringBuilder(512);
    builder.append(DateTimeFormatter.ISO_INSTANT.format(log.date)).append(LOG_SPLITTER).append(log.result).append(LOG_SPLITTER).append("elapsed=").append(format.format(log.elapsed.longValue())).append(LOG_SPLITTER).append("id=").append(log.id).append(LOG_SPLITTER).append("action=").append(log.action);
    if (log.correlationIds != null) {
        builder.append(LOG_SPLITTER).append("correlation_id=");
        appendList(builder, log.correlationIds);
    }
    String errorCode = log.errorCode;
    if (errorCode != null) {
        builder.append(LOG_SPLITTER).append("error_code=").append(errorCode).append(LOG_SPLITTER).append("error_message=").append(filterLineSeparator(log.errorMessage));
    }
    for (Map.Entry<String, List<String>> entry : log.context.entrySet()) {
        String key = entry.getKey();
        List<String> values = entry.getValue();
        for (String value : values) {
            builder.append(LOG_SPLITTER).append(key).append('=').append(filterLineSeparator(value));
        }
    }
    if (log.clients != null) {
        builder.append(LOG_SPLITTER).append("client=");
        appendList(builder, log.clients);
    }
    if (log.refIds != null) {
        builder.append(LOG_SPLITTER).append("ref_id=");
        appendList(builder, log.refIds);
    }
    for (Map.Entry<String, Double> entry : log.stats.entrySet()) {
        builder.append(LOG_SPLITTER).append(entry.getKey()).append('=').append(format.format(entry.getValue()));
    }
    for (Map.Entry<String, PerformanceStatMessage> entry : log.performanceStats.entrySet()) {
        String key = entry.getKey();
        PerformanceStatMessage stat = entry.getValue();
        builder.append(LOG_SPLITTER).append(key).append("_count=").append(stat.count);
        if (stat.readEntries != null)
            builder.append(LOG_SPLITTER).append(key).append("_reads=").append(stat.readEntries);
        if (stat.writeEntries != null)
            builder.append(LOG_SPLITTER).append(key).append("_writes=").append(stat.writeEntries);
        builder.append(LOG_SPLITTER).append(key).append("_elapsed=").append(format.format(stat.totalElapsed));
    }
    return builder.toString();
}
Also used : PerformanceStatMessage(core.framework.log.message.PerformanceStatMessage) DecimalFormat(java.text.DecimalFormat) List(java.util.List) Map(java.util.Map)

Example 4 with PerformanceStatMessage

use of core.framework.log.message.PerformanceStatMessage in project core-ng-project by neowu.

the class ActionServiceTest method index.

@Test
void index() {
    ActionLogMessage message1 = message("1", "OK");
    message1.context = Map.of("key", List.of("value"));
    message1.stats = Map.of("count", 1d);
    message1.correlationIds = List.of("id1", "id2");
    message1.clients = List.of("client");
    var stat = new PerformanceStatMessage();
    stat.count = 1;
    stat.totalElapsed = 10L;
    stat.readEntries = 1;
    stat.writeEntries = 2;
    message1.performanceStats = Map.of("redis", stat);
    ActionLogMessage message2 = message("2", "WARN");
    message2.traceLog = "trace";
    LocalDate now = LocalDate.of(2016, Month.JANUARY, 15);
    actionService.index(List.of(message1, message2), now);
    ActionDocument action = actionDocument(now, message1.id);
    assertThat(action.timestamp).isEqualTo(message1.date);
    assertThat(action.result).isEqualTo(message1.result);
    assertThat(action.correlationIds).isEqualTo(message1.correlationIds);
    assertThat(action.refIds).isEqualTo(message1.refIds);
    assertThat(action.clients).isEqualTo(message1.clients);
    assertThat(action.performanceStats.get("redis")).usingRecursiveComparison().isEqualTo(message1.performanceStats.get("redis"));
    assertThat(action.context).containsEntry("key", List.of("value"));
    TraceDocument trace = traceDocument(now, message2.id);
    assertThat(trace.content).isEqualTo(message2.traceLog);
    elasticSearch.refreshIndex(indexService.indexName("action", now));
    List<ActionDocument> actions = searchActionDocument(now, "context.key", "value");
    assertThat(actions).hasSize(1);
}
Also used : PerformanceStatMessage(core.framework.log.message.PerformanceStatMessage) ActionLogMessage(core.framework.log.message.ActionLogMessage) LocalDate(java.time.LocalDate) ActionDocument(core.log.domain.ActionDocument) TraceDocument(core.log.domain.TraceDocument) Test(org.junit.jupiter.api.Test) IntegrationTest(core.log.IntegrationTest)

Aggregations

PerformanceStatMessage (core.framework.log.message.PerformanceStatMessage)4 ActionLogMessage (core.framework.log.message.ActionLogMessage)2 Test (org.junit.jupiter.api.Test)2 IntegrationTest (core.log.IntegrationTest)1 ActionDocument (core.log.domain.ActionDocument)1 TraceDocument (core.log.domain.TraceDocument)1 DecimalFormat (java.text.DecimalFormat)1 LocalDate (java.time.LocalDate)1 List (java.util.List)1 Map (java.util.Map)1