14 changed files with 59 additions and 227 deletions
@ -1,40 +0,0 @@ |
|||||
package org.nl.modules.loki.rest; |
|
||||
|
|
||||
import com.alibaba.fastjson.JSONObject; |
|
||||
import io.swagger.annotations.Api; |
|
||||
import io.swagger.annotations.ApiOperation; |
|
||||
import lombok.RequiredArgsConstructor; |
|
||||
import lombok.extern.slf4j.Slf4j; |
|
||||
import org.nl.modules.common.annotation.RateLimiter; |
|
||||
import org.nl.modules.loki.service.LokiService; |
|
||||
import org.springframework.http.HttpStatus; |
|
||||
import org.springframework.http.ResponseEntity; |
|
||||
import org.springframework.web.bind.annotation.*; |
|
||||
|
|
||||
/** |
|
||||
* @Author: lyd |
|
||||
* @Description: 日志监控 |
|
||||
* @Date: 2022-08-15 |
|
||||
*/ |
|
||||
@RestController |
|
||||
@RequiredArgsConstructor |
|
||||
@Api(tags = "日志监控") |
|
||||
@RequestMapping("/api/loki") |
|
||||
@Slf4j |
|
||||
public class LokiController { |
|
||||
|
|
||||
private final LokiService lokiService; |
|
||||
|
|
||||
@GetMapping("/labels/values") |
|
||||
@ApiOperation("获取标签") |
|
||||
public ResponseEntity<Object> labelsValues() { |
|
||||
return new ResponseEntity<>(lokiService.getLabelsValues(), HttpStatus.OK); |
|
||||
} |
|
||||
|
|
||||
@PostMapping("/logs") |
|
||||
@ApiOperation("获取日志") |
|
||||
@RateLimiter(value = 1, timeout = 300) // 限流
|
|
||||
public ResponseEntity<Object> getLogData(@RequestBody JSONObject json) { |
|
||||
return new ResponseEntity<>(lokiService.getLogData(json), HttpStatus.OK); |
|
||||
} |
|
||||
} |
|
@ -1,25 +0,0 @@ |
|||||
package org.nl.modules.loki.service; |
|
||||
|
|
||||
import com.alibaba.fastjson.JSONArray; |
|
||||
import com.alibaba.fastjson.JSONObject; |
|
||||
|
|
||||
/** |
|
||||
* @Author: lyd |
|
||||
* @Description: 服务类 |
|
||||
* @Date: 2022-08-15 |
|
||||
*/ |
|
||||
public interface LokiService { |
|
||||
|
|
||||
/** |
|
||||
* 获取日志信息 |
|
||||
* @param json |
|
||||
* @return |
|
||||
*/ |
|
||||
JSONObject getLogData(JSONObject json); |
|
||||
|
|
||||
/** |
|
||||
* 获取labels和values树 |
|
||||
* @return |
|
||||
*/ |
|
||||
JSONArray getLabelsValues(); |
|
||||
} |
|
@ -1,114 +0,0 @@ |
|||||
package org.nl.modules.loki.service.impl; |
|
||||
|
|
||||
import cn.hutool.core.util.CharsetUtil; |
|
||||
import cn.hutool.core.util.ObjectUtil; |
|
||||
import cn.hutool.http.HttpUtil; |
|
||||
import com.alibaba.fastjson.JSONArray; |
|
||||
import com.alibaba.fastjson.JSONObject; |
|
||||
import lombok.RequiredArgsConstructor; |
|
||||
import org.nl.modules.loki.service.LokiService; |
|
||||
import org.springframework.beans.factory.annotation.Value; |
|
||||
import org.springframework.stereotype.Service; |
|
||||
|
|
||||
/** |
|
||||
* @Author: lyd |
|
||||
* @Description: 实现类 |
|
||||
* @Date: 2022-08-15 |
|
||||
*/ |
|
||||
@Service |
|
||||
@RequiredArgsConstructor |
|
||||
public class LokiServiceImpl implements LokiService { |
|
||||
|
|
||||
@Value("${loki.url}") |
|
||||
private String lokiUrl; |
|
||||
|
|
||||
@Value("${loki.systemName}") |
|
||||
private String systemName; |
|
||||
|
|
||||
@Override |
|
||||
public JSONObject getLogData(JSONObject json) { |
|
||||
String logLabel = ""; |
|
||||
String logLabelValue = ""; |
|
||||
Long start = 0L; |
|
||||
Long end = 0L; |
|
||||
String text = ""; |
|
||||
String limit = "100"; |
|
||||
String direction = "backward"; |
|
||||
if (ObjectUtil.isNotEmpty(json.get("logLabel"))) logLabel = json.getString("logLabel"); |
|
||||
if (ObjectUtil.isNotEmpty(json.get("logLabelValue"))) logLabelValue = json.getString("logLabelValue"); |
|
||||
if (ObjectUtil.isNotEmpty(json.get("text"))) text = json.getString("text"); |
|
||||
if (ObjectUtil.isNotEmpty(json.get("start"))) start = json.getLong("start"); |
|
||||
if (ObjectUtil.isNotEmpty(json.get("end"))) end = json.getLong("end"); |
|
||||
if (ObjectUtil.isNotEmpty(json.get("limits"))) limit = json.getString("limits"); |
|
||||
if (ObjectUtil.isNotEmpty(json.get("direction"))) direction = json.getString("direction"); |
|
||||
/** |
|
||||
* 组织参数 |
|
||||
* 纳秒数 |
|
||||
* 1660037391880000000 |
|
||||
* 1641453208415000000 |
|
||||
* http://localhost:3100/loki/api/v1/query_range?query={host="localhost"} |= ``&limit=1500&start=1641453208415000000&end=1660027623419419002
|
|
||||
*/ |
|
||||
JSONObject parse = null; |
|
||||
String query = lokiUrl + "/query_range?query={system=\"" + systemName + "\", " + logLabel + "=\"" + logLabelValue + "\"} |= `" + text + "`"; |
|
||||
String result = ""; |
|
||||
if (start==0L) { |
|
||||
result = HttpUtil.get(query + "&limit=" + limit + "&direction=" + direction, CharsetUtil.CHARSET_UTF_8); |
|
||||
} else { |
|
||||
result = HttpUtil.get(query + "&limit=" + limit + "&start=" + start + "&end=" + end + "&direction=" + direction, CharsetUtil.CHARSET_UTF_8); |
|
||||
} |
|
||||
try { |
|
||||
parse = (JSONObject) JSONObject.parse(result); |
|
||||
} catch (Exception e) { |
|
||||
// reslut的值可能为:too many outstanding requests,无法转化成Json
|
|
||||
System.out.println("reslut:" + result); |
|
||||
// e.printStackTrace();
|
|
||||
} |
|
||||
return parse; |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 获取labels和values树 |
|
||||
* |
|
||||
* @return |
|
||||
*/ |
|
||||
@Override |
|
||||
public JSONArray getLabelsValues() { |
|
||||
/** |
|
||||
* [{ |
|
||||
* label: |
|
||||
* value: |
|
||||
* children:[{ |
|
||||
* label |
|
||||
* value |
|
||||
* }] |
|
||||
* }] |
|
||||
*/ |
|
||||
JSONArray result = new JSONArray(); |
|
||||
// 获取所有标签
|
|
||||
String labelString = HttpUtil.get(lokiUrl + "/labels", CharsetUtil.CHARSET_UTF_8); |
|
||||
JSONObject parse = (JSONObject) JSONObject.parse(labelString); |
|
||||
JSONArray labels = parse.getJSONArray("data"); |
|
||||
for (int i=0; i<labels.size(); i++) { |
|
||||
// 获取标签下的所有值
|
|
||||
String valueString = HttpUtil.get(lokiUrl + "/label/" + labels.getString(i) + "/values", CharsetUtil.CHARSET_UTF_8); |
|
||||
JSONObject parse2 = (JSONObject) JSONObject.parse(valueString); |
|
||||
JSONArray values = parse2.getJSONArray("data"); |
|
||||
JSONArray children = new JSONArray(); |
|
||||
// 组成树形状态 两级
|
|
||||
for (int j=0; j<values.size(); j++) { |
|
||||
JSONObject leaf = new JSONObject(); |
|
||||
leaf.put("label", values.getString(j)); |
|
||||
leaf.put("value", values.getString(j)); |
|
||||
children.add(leaf); |
|
||||
} |
|
||||
|
|
||||
JSONObject node = new JSONObject(); |
|
||||
node.put("label", labels.getString(i)); |
|
||||
node.put("value", labels.getString(i)); |
|
||||
node.put("children", children); |
|
||||
result.add(node); |
|
||||
} |
|
||||
return result; |
|
||||
} |
|
||||
|
|
||||
} |
|
Binary file not shown.
Loading…
Reference in new issue