Ver código fonte

日志仍未被采集vector配置文件仍有问题

demo-user 2 meses atrás
pai
commit
d5e6445896

+ 72 - 9
k8s/helm/logsys/templates/configmap-vector.yaml

@@ -7,18 +7,81 @@ metadata:
     app: vector
 data:
   vector.toml: |
-    # Simple Vector config - read and forward Docker logs
-    [sources.docker_logs]
-    type = "file"
-    include = ["/var/lib/docker/containers/*/*-json.log"]
+    [sources.kubernetes_logs]
+    type = "kubernetes_logs"
+    read_from = "beginning"
+    fingerprint_lines = 0
+    max_read_bytes = 262144
+    glob_minimum_cooldown_ms = 100
+    max_line_bytes = 102400
+    ignore_older_secs = 0
     data_dir = "/var/lib/vector"
 
-    # Send directly to Loki without transformations
+    [transforms.parse_and_enrich]
+    type = "remap"
+    inputs = ["kubernetes_logs"]
+    drop_on_abort = false
+    source = """
+    # Extract Kubernetes metadata
+    .pod_name = .kubernetes.pod_name
+    .namespace = .kubernetes.namespace_name
+    .container = .kubernetes.container_name
+    
+    # Initialize app with unknown
+    .app = "unknown"
+    
+    # Debug trace fields
+    .debug_pod = .kubernetes.pod_name
+    .debug_k8s_label_app = .kubernetes.labels.app
+    
+    # Check if this is a JSON log (Java/Logstash format with nested JSON)
+    if exists(.log) {
+      log_str = string!(.log)
+      .debug_log_length = length(log_str)
+      .debug_has_timestamp = contains(log_str, "@timestamp")
+      
+      if contains(log_str, "@timestamp") {
+        # Try to parse the nested JSON
+        parsed = parse_json(log_str) ?? null
+        if parsed != null {
+          .debug_parse_success = true
+          if exists(parsed.app) {
+            .app = parsed.app
+            .debug_source = "parsed_json"
+            .debug_parsed_app = parsed.app
+          } else {
+            .debug_parse_no_app = "app_field_missing_in_parsed_json"
+          }
+        } else {
+          .debug_parse_success = false
+          .debug_parse_error = "parse_json_returned_null"
+        }
+      } else {
+        .debug_no_timestamp = "log_does_not_contain_@timestamp"
+      }
+    }
+    
+    # Fallback to Kubernetes labels if we still don't have app name
+    if .app == "unknown" && exists(.kubernetes.labels.app) {
+      .app = .kubernetes.labels.app
+      .debug_source = "k8s_labels"
+      .debug_fallback_app = .kubernetes.labels.app
+    }
+    
+    .debug_final_app = .app
+    .env = "kubernetes"
+    """
+
     [sinks.loki]
     type = "loki"
-    inputs = ["docker_logs"]
+    inputs = ["parse_and_enrich"]
     endpoint = "{{ .Values.vector.loki.endpoint }}"
-    [sinks.loki.encoding]
-    codec = "json"
+    encoding.codec = "json"
+    buffer.type = "memory"
+    buffer.max_events = 10000
+    # Use only templating for labels - Vector Loki sink needs raw field access
     [sinks.loki.labels]
-    environment = "docker"
+    namespace = "{{ namespace }}"
+    app = "{{ app }}"
+    pod = "{{ pod_name }}"
+    container = "{{ container }}"

+ 4 - 0
k8s/helm/logsys/templates/daemonset-vector.yaml

@@ -36,6 +36,10 @@ spec:
               valueFrom:
                 fieldRef:
                   fieldPath: spec.nodeName
+            - name: VECTOR_LOG_LEVEL
+              value: trace
+            - name: VECTOR_INTERNAL_LOG_RATE_SECS
+              value: "0"
           volumeMounts:
             - name: varlog
               mountPath: /var/log

+ 1 - 1
k8s/helm/logsys/values.yaml

@@ -32,7 +32,7 @@ vector:
   namespace: shoprecycle
   image:
     repository: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/timberio/vector
-    tag: 0.26.0-alpine
+    tag: nightly-alpine
   loki:
     endpoint: http://loki:3100
   prometheus:

+ 98 - 0
web-log-generator-simple.sh

@@ -0,0 +1,98 @@
+#!/bin/bash
+
+# Web API 日志生成脚本 - 简易版
+# 通过shop-recycle-web前端快速生成日志
+
+WEB_URL="${1:-http://192.168.1.203}"
+API_URL="$WEB_URL/api"
+
+echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
+echo "  Web API 日志生成 (Simple Mode)"
+echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
+echo "URL: $WEB_URL"
+echo ""
+
+STATS_FILE="/tmp/web-log-gen-$$.txt"
+echo "0 0 0" > "$STATS_FILE"
+
+cleanup() {
+    echo ""
+    read create payment failed < "$STATS_FILE"
+    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
+    echo "已停止 | 创建=$create 支付=$payment 失败=$failed"
+    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
+    rm -f "$STATS_FILE"
+    exit 0
+}
+
+trap cleanup SIGINT
+
+USERS=("alice" "bob" "charlie" "david" "emma" "frank" "grace" "henry")
+DESCRIPTIONS=("回收电子产品" "二手家电处理" "旧家具回收" "废金属回收" "包装材料" "废纸回收" "塑料回收" "玻璃回收")
+METHODS=("CARD" "WECHAT" "ALIPAY")
+
+# 创建订单
+(
+    count=0
+    while true; do
+        user="${USERS[$((RANDOM % ${#USERS[@]}))]}-$((RANDOM % 10000))"
+        price="$((RANDOM % 500 + 10)).$(printf "%02d" $((RANDOM % 100)))"
+        desc="${DESCRIPTIONS[$((RANDOM % ${#DESCRIPTIONS[@]}))]}"
+        
+        resp=$(curl -s -X POST "$API_URL/order/create" \
+          -H "Content-Type: application/json" \
+          -d "{\"userId\":\"$user\",\"price\":$price,\"description\":\"$desc\"}" 2>/dev/null)
+        
+        if echo "$resp" | grep -q '"code":0'; then
+            order_id=$(echo "$resp" | grep -o '"orderId":"[^"]*' | cut -d':' -f2 | tr -d '"')
+            if [ ! -z "$order_id" ]; then
+                ((count++))
+                echo "$count" > /tmp/web-log-gen-create-$$.txt
+                echo "[$(date '+%H:%M:%S')] CREATE #$count"
+                sleep 0.5
+                continue
+            fi
+        fi
+        failed=$(<"$STATS_FILE" cut -d' ' -f3)
+        echo "0 0 $((failed+1))" > "$STATS_FILE"
+        sleep 0.5
+    done
+) &
+
+# 支付订单
+(
+    count=0
+    while true; do
+        sleep 1.2
+        order_id=$(<"/tmp/web-log-gen-create-$$.txt" head -1 2>/dev/null)
+        if [ ! -z "$order_id" ] && [ "$order_id" != "0" ]; then
+            price="$((RANDOM % 500 + 10)).$(printf "%02d" $((RANDOM % 100)))"
+            method="${METHODS[$((RANDOM % ${#METHODS[@]}))]}"
+            
+            resp=$(curl -s -X POST "$API_URL/payment/pay" \
+              -H "Content-Type: application/json" \
+              -d "{\"orderId\":\"$order_id\",\"amount\":$price,\"paymentMethod\":\"$method\"}" 2>/dev/null)
+            
+            if echo "$resp" | grep -q '"code":0'; then
+                ((count++))
+                echo "$count"
+            fi
+        fi
+        sleep 0.8
+    done
+) > /tmp/web-log-gen-payment-$$.txt 2>&1 &
+
+# 监控
+echo "运行中 (Ctrl+C 停止)..."
+echo ""
+
+while true; do
+    create=$(cat /tmp/web-log-gen-create-$$.txt 2>/dev/null || echo "0")
+    payment=$(cat /tmp/web-log-gen-payment-$$.txt 2>/dev/null | tail -1 || echo "0")
+    failed=$(<"$STATS_FILE" cut -d' ' -f3)
+    
+    total=$((create + payment))
+    echo -ne "\r创建: $create | 支付: $payment | 失败: $failed | 总计: $total   "
+    
+    sleep 2
+done