Line data Source code
1 : // Copyright (C) 2020 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.sshd; 16 : 17 : import static com.google.gerrit.sshd.SshLog.P_ACCOUNT_ID; 18 : import static com.google.gerrit.sshd.SshLog.P_AGENT; 19 : import static com.google.gerrit.sshd.SshLog.P_EXEC; 20 : import static com.google.gerrit.sshd.SshLog.P_MEMORY; 21 : import static com.google.gerrit.sshd.SshLog.P_MESSAGE; 22 : import static com.google.gerrit.sshd.SshLog.P_SESSION; 23 : import static com.google.gerrit.sshd.SshLog.P_STATUS; 24 : import static com.google.gerrit.sshd.SshLog.P_TOTAL_CPU; 25 : import static com.google.gerrit.sshd.SshLog.P_USER_CPU; 26 : import static com.google.gerrit.sshd.SshLog.P_USER_NAME; 27 : import static com.google.gerrit.sshd.SshLog.P_WAIT; 28 : 29 : import com.google.common.base.Splitter; 30 : import com.google.gerrit.util.logging.JsonLayout; 31 : import com.google.gerrit.util.logging.JsonLogEntry; 32 : import java.util.List; 33 : import org.apache.log4j.spi.LoggingEvent; 34 : 35 0 : public class SshLogJsonLayout extends JsonLayout { 36 0 : private static final Splitter SPLITTER = Splitter.on(" "); 37 : 38 : @Override 39 : public JsonLogEntry toJsonLogEntry(LoggingEvent event) { 40 0 : return new SshJsonLogEntry(event); 41 : } 42 : 43 : @SuppressWarnings("unused") 44 : private class SshJsonLogEntry extends JsonLogEntry { 45 : public String timestamp; 46 : public String session; 47 : public String thread; 48 : public String user; 49 : public String accountId; 50 : public String message; 51 : public String waitTime; 52 : public String execTime; 53 : public String totalCpu; 54 : public String userCpu; 55 : public String memory; 56 : public String status; 57 : public String agent; 58 : public String timeNegotiating; 59 : public String timeSearchReuse; 60 : public String timeSearchSizes; 61 : public String timeCounting; 62 : public String timeCompressing; 63 : public String timeWriting; 64 : public String timeTotal; 65 : public String bitmapIndexMisses; 66 : public String deltasTotal; 67 : public String objectsTotal; 68 : public String bytesTotal; 69 : 70 0 : public SshJsonLogEntry(LoggingEvent event) { 71 0 : this.timestamp = timestampFormatter.format(event.getTimeStamp()); 72 0 : this.session = getMdcString(event, P_SESSION); 73 0 : this.thread = event.getThreadName(); 74 0 : this.user = getMdcString(event, P_USER_NAME); 75 0 : this.accountId = getMdcString(event, P_ACCOUNT_ID); 76 0 : this.message = (String) event.getMessage(); 77 0 : this.waitTime = getMdcString(event, P_WAIT); 78 0 : this.execTime = getMdcString(event, P_EXEC); 79 0 : this.totalCpu = getMdcString(event, P_TOTAL_CPU); 80 0 : this.userCpu = getMdcString(event, P_USER_CPU); 81 0 : this.memory = getMdcString(event, P_MEMORY); 82 0 : this.status = getMdcString(event, P_STATUS); 83 0 : this.agent = getMdcString(event, P_AGENT); 84 : 85 0 : String metricString = getMdcString(event, P_MESSAGE); 86 0 : if (metricString != null && !metricString.isEmpty()) { 87 0 : List<String> ssh_metrics = SPLITTER.splitToList(metricString); 88 0 : this.timeNegotiating = ssh_metrics.get(0); 89 0 : this.timeSearchReuse = ssh_metrics.get(1); 90 0 : this.timeSearchSizes = ssh_metrics.get(2); 91 0 : this.timeCounting = ssh_metrics.get(3); 92 0 : this.timeCompressing = ssh_metrics.get(4); 93 0 : this.timeWriting = ssh_metrics.get(5); 94 0 : this.timeTotal = ssh_metrics.get(6); 95 0 : this.bitmapIndexMisses = ssh_metrics.get(7); 96 0 : this.deltasTotal = ssh_metrics.get(8); 97 0 : this.objectsTotal = ssh_metrics.get(9); 98 0 : this.bytesTotal = ssh_metrics.get(10); 99 : } 100 0 : } 101 : } 102 : }