|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hertzbeat.mcp.server.service; |
| 19 | + |
| 20 | +import com.jayway.jsonpath.JsonPath; |
| 21 | +import com.jayway.jsonpath.ReadContext; |
| 22 | +import lombok.extern.slf4j.Slf4j; |
| 23 | +import org.springframework.ai.tool.annotation.Tool; |
| 24 | +import org.springframework.ai.tool.annotation.ToolParam; |
| 25 | +import org.springframework.beans.factory.annotation.Value; |
| 26 | +import org.springframework.http.MediaType; |
| 27 | +import org.springframework.stereotype.Service; |
| 28 | +import org.springframework.util.LinkedMultiValueMap; |
| 29 | +import org.springframework.util.MultiValueMap; |
| 30 | +import org.springframework.web.client.RestClient; |
| 31 | + |
| 32 | +import java.time.Instant; |
| 33 | +import java.time.LocalDateTime; |
| 34 | +import java.time.ZoneId; |
| 35 | +import java.time.format.DateTimeFormatter; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +/** |
| 40 | + * Log query service |
| 41 | + */ |
| 42 | +@Service |
| 43 | +@Slf4j |
| 44 | +public class LogService { |
| 45 | + |
| 46 | + private static final String TIMESTAMP_COLUMN = "timestamp"; |
| 47 | + private static final String SEVERITY_TEXT_COLUMN = "severity_text"; |
| 48 | + private static final String BODY_COLUMN = "body"; |
| 49 | + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| 50 | + |
| 51 | + private final RestClient restClient; |
| 52 | + |
| 53 | + public LogService(@Value("${greptime.url}") String greptimeUrl) { |
| 54 | + this.restClient = RestClient.builder() |
| 55 | + .baseUrl(greptimeUrl) |
| 56 | + .defaultHeader("Accept", "application/json") |
| 57 | + .defaultHeader("Content-Type", "application/x-www-form-urlencoded") |
| 58 | + .build(); |
| 59 | + } |
| 60 | + |
| 61 | + @Tool(description = "System log query tool that supports filtering by time, log level, and content") |
| 62 | + public String getHertzbeatLog( |
| 63 | + @ToolParam(description = """ |
| 64 | + Query system logs with support for filtering by time, log level, and content. |
| 65 | + |
| 66 | + Usage: |
| 67 | + 1. Table name: hzb_log |
| 68 | + 2. Common query examples: |
| 69 | + - Get latest 10 logs: SELECT * FROM hzb_log ORDER BY timestamp DESC LIMIT 10 |
| 70 | + - Query ERROR level logs: SELECT * FROM hzb_log WHERE severity_number=17 |
| 71 | + - Query specific time range: SELECT * FROM hzb_log WHERE timestamp > '2024-01-01 00:00:00' |
| 72 | + |
| 73 | + Field descriptions: |
| 74 | + 1. severity_number (log level): |
| 75 | + - 5: DEBUG |
| 76 | + - 9: INFO |
| 77 | + - 13: WARN |
| 78 | + - 17: ERROR |
| 79 | + 2. timestamp: log timestamp |
| 80 | + 3. body: log content |
| 81 | + """) String querySql) { |
| 82 | + |
| 83 | + if (!isValidQuery(querySql)) { |
| 84 | + return "Invalid query statement"; |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + String response = executeQuery(querySql); |
| 89 | + return formatQueryResults(response); |
| 90 | + } catch (Exception e) { |
| 91 | + log.error("Failed to query logs", e); |
| 92 | + return "Failed to query logs: " + e.getMessage(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private boolean isValidQuery(String sql) { |
| 97 | + return sql != null && sql.toLowerCase().contains("hzb_log"); |
| 98 | + } |
| 99 | + |
| 100 | + private String executeQuery(String sql) { |
| 101 | + MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(); |
| 102 | + formData.add("sql", sql); |
| 103 | + log.debug("Executing SQL query: {}", sql); |
| 104 | + |
| 105 | + return restClient.post() |
| 106 | + .uri("/v1/sql?db=public") |
| 107 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 108 | + .body(formData) |
| 109 | + .retrieve() |
| 110 | + .body(String.class); |
| 111 | + } |
| 112 | + |
| 113 | + private String formatQueryResults(String response) { |
| 114 | + ReadContext ctx = JsonPath.parse(response); |
| 115 | + List<Map<String, Object>> columnSchemas = ctx.read("$.output[0].records.schema.column_schemas"); |
| 116 | + List<List<Object>> rows = ctx.read("$.output[0].records.rows"); |
| 117 | + int totalRows = ctx.read("$.output[0].records.total_rows"); |
| 118 | + |
| 119 | + ColumnIndices indices = findColumnIndices(columnSchemas); |
| 120 | + StringBuilder result = new StringBuilder() |
| 121 | + .append("Query Results:\n\n") |
| 122 | + .append("Log Time\t\t\tLog Level\tLog Content\n") |
| 123 | + .append("----------------------------------------------------\n"); |
| 124 | + |
| 125 | + if (rows != null && !rows.isEmpty()) { |
| 126 | + formatRows(rows, indices, result); |
| 127 | + result.append("\nTotal ").append(totalRows).append(" records"); |
| 128 | + } else { |
| 129 | + result.append("No data"); |
| 130 | + } |
| 131 | + |
| 132 | + return result.toString(); |
| 133 | + } |
| 134 | + |
| 135 | + private record ColumnIndices(int timestamp, int severityText, int body) {} |
| 136 | + |
| 137 | + private ColumnIndices findColumnIndices(List<Map<String, Object>> columnSchemas) { |
| 138 | + int timestampIndex = -1; |
| 139 | + int severityTextIndex = -1; |
| 140 | + int bodyIndex = -1; |
| 141 | + |
| 142 | + for (int i = 0; i < columnSchemas.size(); i++) { |
| 143 | + String columnName = (String) columnSchemas.get(i).get("name"); |
| 144 | + switch (columnName) { |
| 145 | + case TIMESTAMP_COLUMN -> timestampIndex = i; |
| 146 | + case SEVERITY_TEXT_COLUMN -> severityTextIndex = i; |
| 147 | + case BODY_COLUMN -> bodyIndex = i; |
| 148 | + default -> { |
| 149 | + // Ignore other columns |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return new ColumnIndices(timestampIndex, severityTextIndex, bodyIndex); |
| 155 | + } |
| 156 | + |
| 157 | + private void formatRows(List<List<Object>> rows, ColumnIndices indices, StringBuilder result) { |
| 158 | + for (List<Object> row : rows) { |
| 159 | + appendTimestamp(row, indices.timestamp(), result); |
| 160 | + appendSeverity(row, indices.severityText(), result); |
| 161 | + appendBody(row, indices.body(), result); |
| 162 | + result.append("\n"); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private void appendTimestamp(List<Object> row, int index, StringBuilder result) { |
| 167 | + if (index >= 0 && index < row.size()) { |
| 168 | + Object value = row.get(index); |
| 169 | + if (value instanceof Number) { |
| 170 | + long timestamp = ((Number) value).longValue(); |
| 171 | + LocalDateTime dateTime = LocalDateTime.ofInstant( |
| 172 | + Instant.ofEpochMilli(timestamp / 1_000_000), |
| 173 | + ZoneId.systemDefault()); |
| 174 | + result.append(DATE_FORMATTER.format(dateTime)).append("\t"); |
| 175 | + return; |
| 176 | + } |
| 177 | + } |
| 178 | + result.append("Unknown time\t"); |
| 179 | + } |
| 180 | + |
| 181 | + private void appendSeverity(List<Object> row, int index, StringBuilder result) { |
| 182 | + if (index >= 0 && index < row.size()) { |
| 183 | + result.append(row.get(index)).append("\t"); |
| 184 | + } else { |
| 185 | + result.append("Unknown\t"); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private void appendBody(List<Object> row, int index, StringBuilder result) { |
| 190 | + if (index >= 0 && index < row.size()) { |
| 191 | + result.append(row.get(index)); |
| 192 | + } else { |
| 193 | + result.append("No content"); |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments