pcacc 3 месяцев назад
Родитель
Сommit
a0c95eac30
3 измененных файлов с 40 добавлено и 3 удалено
  1. 28 0
      loki_query.py
  2. 11 0
      loki_test.py
  3. 1 3
      requirements.txt

+ 28 - 0
loki_query.py

@@ -0,0 +1,28 @@
+import requests
+import json
+
+def test_loki_connection():
+    url = "http://192.168.50.108:3100/loki/api/v1/query_range"
+    headers = {"Content-Type": "application/json"}
+    
+    # 最小化测试查询
+    payload = {
+        "query": "{}",
+        "limit": 1,
+        "start": "2023-07-20T00:00:00Z",
+        "end": "2023-07-20T01:00:00Z"
+    }
+    
+    try:
+        r = requests.post(url, data=json.dumps(payload), headers=headers)
+        print(f"状态码: {r.status_code}")
+        print(f"响应: {r.text[:200]}...")  # 截断长响应
+        return r.status_code == 200
+    except Exception as e:
+        print(f"连接失败: {e}")
+        return False
+
+if test_loki_connection():
+    print("Loki连接测试成功!")
+else:
+    print("请检查:1. Loki服务状态 2. 网络连接 3. 防火墙设置")

+ 11 - 0
loki_test.py

@@ -0,0 +1,11 @@
+import requests
+import time
+
+log_data = {
+    "streams": [{
+        "stream": {"app": "myapp", "env": "dev"},
+        "values": [[str(int(time.time()*1e9)), "This is a log line"]]
+    }]
+}
+
+requests.post("http://192.168.50.108:3100/loki/api/v1/push", json=log_data)

+ 1 - 3
requirements.txt

@@ -20,6 +20,4 @@ simple-websocket
 paddleocr
 paddlepaddle
 setuptools
-pytest
-allure-pytest
-redis
+python-logging-loki[async]