configmap-vector.yaml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: vector-config
  5. namespace: {{ .Release.Namespace }}
  6. labels:
  7. app: vector
  8. data:
  9. vector.toml: |
  10. data_dir = "/var/lib/vector"
  11. [sources.kubernetes_logs]
  12. type = "kubernetes_logs"
  13. read_from = "end"
  14. [transforms.parse_and_enrich]
  15. type = "remap"
  16. inputs = ["kubernetes_logs"]
  17. drop_on_abort = true
  18. source = """
  19. ns = to_string(.kubernetes.pod_namespace) ?? ""
  20. pod_labels = .kubernetes.pod_labels
  21. if pod_labels == null { abort }
  22. app_name = to_string(.kubernetes.pod_labels."app.kubernetes.io/name") ?? ""
  23. if ns != "shop-recycle" || app_name != "shop-recycle" { abort }
  24. log_line = .log
  25. if log_line == null { log_line = .message }
  26. if log_line == null { log_line = .msg }
  27. log_line = to_string(log_line) ?? ""
  28. parsed = null
  29. if starts_with(log_line, "{") {
  30. parsed = parse_json(log_line) ?? null
  31. }
  32. if parsed != null && is_object(parsed) {
  33. if parsed.ts != null { .ts = parsed.ts }
  34. msg_val = parsed.message
  35. if msg_val == null { msg_val = parsed.msg }
  36. if msg_val != null { .message = msg_val }
  37. if parsed.level != null { .level = parsed.level }
  38. if parsed.app != null { .app = parsed.app }
  39. if parsed.env != null { .env = parsed.env }
  40. if parsed.uri != null { .uri = parsed.uri }
  41. if parsed.status != null { .status = parsed.status }
  42. if parsed.event_class != null { .event_class = parsed.event_class }
  43. if parsed.event != null && .event_class == null { .event_class = parsed.event }
  44. if parsed.traceId != null { .traceId = parsed.traceId }
  45. if parsed.userId != null { .userId = parsed.userId }
  46. if parsed.orderId != null { .orderId = parsed.orderId }
  47. if parsed.error != null { .error = parsed.error }
  48. if parsed.stack_trace != null { .stack_trace = parsed.stack_trace }
  49. if parsed.duration_ms != null { .duration_ms = parsed.duration_ms }
  50. if parsed.duration != null { .duration = parsed.duration }
  51. }
  52. if .message == null { .message = log_line }
  53. if .app == null {
  54. app_val = .kubernetes.pod_labels.app
  55. if app_val == null { app_val = .kubernetes.container_name }
  56. if app_val == null { app_val = "unknown" }
  57. .app = app_val
  58. }
  59. env_str = to_string(.env) ?? ""
  60. if env_str == "" { .env = "unknown" } else { .env = env_str }
  61. level_str = to_string(.level) ?? ""
  62. if level_str == "" { .level = "INFO" } else { .level = level_str }
  63. uri_str = to_string(.uri) ?? ""
  64. if uri_str == "" { .uri = "-" } else { .uri = uri_str }
  65. status_str = to_string(.status) ?? ""
  66. if status_str == "" { .status = "unknown" } else { .status = status_str }
  67. event_class_str = to_string(.event_class) ?? ""
  68. if event_class_str == "" {
  69. if contains(uri_str, "order") {
  70. .event_class = "order"
  71. } else if contains(uri_str, "payment") {
  72. .event_class = "payment"
  73. } else if contains(uri_str, "login") {
  74. .event_class = "auth"
  75. } else {
  76. .event_class = "api"
  77. }
  78. } else {
  79. .event_class = event_class_str
  80. }
  81. if .ts == null { .ts = .timestamp }
  82. .ts = to_string(.ts) ?? ""
  83. if .duration_ms == null {
  84. duration_str = to_string(.duration) ?? ""
  85. duration_str = replace(duration_str, "ms", "")
  86. .duration_ms = to_int(duration_str) ?? null
  87. } else {
  88. .duration_ms = to_int(.duration_ms) ?? null
  89. }
  90. app_str = to_string(.app) ?? "unknown"
  91. if app_str == "" { .app = "unknown" } else { .app = app_str }
  92. msg_str = to_string(.message) ?? log_line
  93. .message = msg_str
  94. # 从业务日志文本兜底提取 orderId,修复面板按 orderId 过滤无数据问题
  95. order_probe = to_string(.orderId) ?? ""
  96. if order_probe == "" {
  97. order_match = parse_regex(msg_str, r'orderId=(?P<oid>[A-Za-z0-9-]+)') ?? null
  98. if order_match != null && order_match.oid != null {
  99. .orderId = order_match.oid
  100. }
  101. }
  102. msg_lower = downcase(msg_str)
  103. uri_lower = downcase(uri_str)
  104. # 过滤健康检查探针日志,避免污染业务检索视图
  105. if contains(msg_lower, "kube-probe/") { abort }
  106. if uri_lower == "/actuator/health" || starts_with(uri_lower, "/actuator/health/") { abort }
  107. if uri_lower == "/actuator/prometheus" || starts_with(uri_lower, "/actuator/prometheus/") { abort }
  108. if uri_lower == "/health" || uri_lower == "/healthz" || uri_lower == "/readyz" || uri_lower == "/livez" { abort }
  109. if contains(msg_lower, "/actuator/prometheus") { abort }
  110. pod_name = to_string(.kubernetes.pod_name) ?? "-"
  111. image_name = to_string(.kubernetes.container_image) ?? "-"
  112. if pod_name == "" { pod_name = "-" }
  113. if image_name == "" { image_name = "-" }
  114. trace_val = to_string(.traceId) ?? ""
  115. user_val = to_string(.userId) ?? ""
  116. order_val = to_string(.orderId) ?? ""
  117. error_val = to_string(.error) ?? ""
  118. if trace_val == "" { .traceId = null } else { .traceId = trace_val }
  119. if user_val == "" { .userId = null } else { .userId = user_val }
  120. if order_val == "" { .orderId = null } else { .orderId = order_val }
  121. if error_val == "" { .error = null } else { .error = error_val }
  122. if .level != "ERROR" || !is_string(.stack_trace) || .stack_trace == "" {
  123. .stack_trace = null
  124. } else {
  125. .stack_trace = truncate!(string!(.stack_trace), 8192)
  126. }
  127. . = {
  128. "ts": .ts,
  129. "level": .level,
  130. "app": .app,
  131. "env": .env,
  132. "event_class": .event_class,
  133. "uri": .uri,
  134. "status": .status,
  135. "duration_ms": .duration_ms,
  136. "traceId": .traceId,
  137. "userId": .userId,
  138. "orderId": .orderId,
  139. "kubernetes_pod_name": pod_name,
  140. "kubernetes_container_image": image_name,
  141. "message": .message,
  142. "error": .error,
  143. "stack_trace": .stack_trace
  144. }
  145. if .duration_ms == null { del(.duration_ms) }
  146. if .traceId == null { del(.traceId) }
  147. if .userId == null { del(.userId) }
  148. if .orderId == null { del(.orderId) }
  149. if .error == null { del(.error) }
  150. if .stack_trace == null { del(.stack_trace) }
  151. """
  152. [sinks.loki]
  153. type = "loki"
  154. inputs = ["parse_and_enrich"]
  155. endpoint = "{{ .Values.vector.loki.endpoint }}"
  156. encoding.codec = "json"
  157. buffer.type = "memory"
  158. buffer.max_events = 10000
  159. [sinks.loki.labels]
  160. env = '{{ "{{ env }}" }}'
  161. app = '{{ "{{ app }}" }}'
  162. level = '{{ "{{ level }}" }}'
  163. status = '{{ "{{ status }}" }}'
  164. event_class = '{{ "{{ event_class }}" }}'