From e73dcf4a2fc8e819824a8a0035c220f2f4dd4e47 Mon Sep 17 00:00:00 2001 From: Bjørn Christian Seime Date: Tue, 26 Mar 2019 17:20:55 +0100 Subject: Expose threadId and processId from LogMessage --- .../handlers/archive/ArchiverHandlerTestCase.java | 24 ++++++------ vespalog/abi-spec.json | 2 + .../src/main/java/com/yahoo/log/LogMessage.java | 43 ++++++++++++++++++---- 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/logserver/src/test/java/com/yahoo/logserver/handlers/archive/ArchiverHandlerTestCase.java b/logserver/src/test/java/com/yahoo/logserver/handlers/archive/ArchiverHandlerTestCase.java index e4904728d9c..525d02c4298 100644 --- a/logserver/src/test/java/com/yahoo/logserver/handlers/archive/ArchiverHandlerTestCase.java +++ b/logserver/src/test/java/com/yahoo/logserver/handlers/archive/ArchiverHandlerTestCase.java @@ -1,6 +1,14 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.logserver.handlers.archive; +import com.yahoo.io.IOUtils; +import com.yahoo.log.InvalidLogFormatException; +import com.yahoo.log.LogMessage; +import com.yahoo.plugin.SystemPropertyConfig; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + import java.io.BufferedReader; import java.io.File; import java.io.FileReader; @@ -10,15 +18,9 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; -import com.yahoo.io.IOUtils; -import com.yahoo.log.InvalidLogFormatException; -import com.yahoo.log.LogMessage; -import com.yahoo.plugin.SystemPropertyConfig; - -import org.junit.*; -import org.junit.rules.TemporaryFolder; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author Bjorn Borud @@ -85,8 +87,8 @@ public class ArchiverHandlerTestCase { try { ArchiverHandler a = new ArchiverHandler(tmpDir.getAbsolutePath(), 1024); - LogMessage msg1 = LogMessage.parseNativeFormat("1139322725\thost\tthread\tservice\tcomponent\tinfo\tpayload"); - LogMessage msg2 = LogMessage.parseNativeFormat("1161172200\thost\tthread\tservice\tcomponent\tinfo\tpayload"); + LogMessage msg1 = LogMessage.parseNativeFormat("1139322725\thost\t1/1\tservice\tcomponent\tinfo\tpayload"); + LogMessage msg2 = LogMessage.parseNativeFormat("1161172200\thost\t1/1\tservice\tcomponent\tinfo\tpayload"); assertEquals(tmpDir.getAbsolutePath() + "/2006/02/07/14", a.getPrefix(msg1)); assertEquals(tmpDir.getAbsolutePath() + "/2006/10/18/11", a.getPrefix(msg2)); assertEquals(a.getPrefix(msg1).length(), a.getPrefix(msg2).length()); diff --git a/vespalog/abi-spec.json b/vespalog/abi-spec.json index c5b64b0a66b..3a8d1c8fd49 100644 --- a/vespalog/abi-spec.json +++ b/vespalog/abi-spec.json @@ -121,6 +121,8 @@ "public long getTime()", "public long getTimeInSeconds()", "public java.lang.String getHost()", + "public long getProcessId()", + "public java.util.OptionalLong getThreadId()", "public java.lang.String getThreadProcess()", "public java.lang.String getService()", "public java.lang.String getComponent()", diff --git a/vespalog/src/main/java/com/yahoo/log/LogMessage.java b/vespalog/src/main/java/com/yahoo/log/LogMessage.java index 1e2b9dfdab0..3a675d843bd 100644 --- a/vespalog/src/main/java/com/yahoo/log/LogMessage.java +++ b/vespalog/src/main/java/com/yahoo/log/LogMessage.java @@ -1,14 +1,15 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.log; +import com.yahoo.log.event.Event; +import com.yahoo.log.event.MalformedEventException; + +import java.util.OptionalLong; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.yahoo.log.event.Event; -import com.yahoo.log.event.MalformedEventException; - /** * This class implements the common ground log message used by * the logserver. A LogMessage is immutable. Note that we have @@ -34,7 +35,8 @@ public class LogMessage private long time; private String timeStr; private String host; - private String threadProcess; + private long processId; + private long threadId; private String service; private String component; private Level level; @@ -45,14 +47,15 @@ public class LogMessage * Private constructor. Log messages should never be instantiated * directly; only as the result of a static factory method. */ - private LogMessage (String timeStr, Long time, String host, String threadProcess, + private LogMessage (String timeStr, Long time, String host, long processId, long threadId, String service, String component, Level level, String payload) { this.timeStr = timeStr; this.time = time; this.host = host; - this.threadProcess = threadProcess; + this.processId = processId; + this.threadId = threadId; this.service = service; this.component = component; this.level = level; @@ -62,7 +65,13 @@ public class LogMessage public long getTime () {return time;} public long getTimeInSeconds () {return time / 1000;} public String getHost () {return host;} - public String getThreadProcess () {return threadProcess;} + public long getProcessId() {return processId;} + public OptionalLong getThreadId() {return threadId != -1 ? OptionalLong.of(threadId) : OptionalLong.empty();} + /** + * @deprecated Use {@link #getProcessId()} / {@link #getThreadId()} + */ + @Deprecated(since = "7", forRemoval = true) + public String getThreadProcess () {return VespaFormat.formatThreadProcess(processId, threadId);} public String getService () {return service;} public String getComponent () {return component;} public Level getLevel () {return level;} @@ -86,8 +95,9 @@ public class LogMessage Level msgLevel = LogLevel.parse(m.group(6)); Long timestamp = parseTimestamp(m.group(1)); + String threadProcess = m.group(3); - return new LogMessage(m.group(1), timestamp, m.group(2), m.group(3), + return new LogMessage(m.group(1), timestamp, m.group(2), parseProcessId(threadProcess), parseThreadId(threadProcess), m.group(4), m.group(5), msgLevel, m.group(7)); } @@ -100,6 +110,22 @@ public class LogMessage } } + private static long parseProcessId(String threadProcess) { + int slashIndex = threadProcess.indexOf('/'); + if (slashIndex == -1) { + return Long.parseLong(threadProcess); + } + return Long.parseLong(threadProcess.substring(0, slashIndex)); + } + + private static long parseThreadId(String threadProcess) { + int slashIndex = threadProcess.indexOf('/'); + if (slashIndex == -1) { + return -1; + } + return Long.parseLong(threadProcess.substring(slashIndex + 1)); + } + /** * If the LogMessage was an EVENT then this method can * be used to get the Event instance representing the @@ -131,6 +157,7 @@ public class LogMessage * Return valid representation of log message. */ public String toString () { + String threadProcess = VespaFormat.formatThreadProcess(processId, threadId); return new StringBuilder(timeStr.length() + host.length() + threadProcess.length() -- cgit v1.2.3