Skip to content

Commit a4e8c25

Browse files
committed
fix: 优化 promtail 配置,调整日志解析阶段和标签提取
1 parent 946f771 commit a4e8c25

File tree

1 file changed

+69
-70
lines changed

1 file changed

+69
-70
lines changed

promtail/config.yaml

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,73 @@ clients:
99
- url: http://loki:3100/loki/api/v1/push
1010

1111
scrape_configs:
12-
- job_name: access
13-
static_configs:
14-
- targets:
15-
- localhost
16-
labels:
17-
job: nginx-access-log
18-
__path__: /var/log/nginx/access.log
19-
pipeline_stages:
20-
- match:
21-
selector: '{job="nginx-access-log"}'
22-
stages:
23-
- json: # 解析 JSON 格式的日志
24-
expressions:
25-
remote_addr:
26-
request_uri:
27-
verb:
28-
httpversion:
29-
response:
30-
body_bytes_sent:
31-
referrer:
32-
user_agent:
33-
http_x_forwarded_for:
34-
server_name:
35-
request_time:
36-
upstream_response_time:
37-
upstream_addr:
38-
realpath_root:
39-
cookie:
40-
nginx_version:
41-
scheme:
42-
time:
12+
- job_name: access
13+
static_configs:
14+
- targets:
15+
- localhost
16+
labels:
17+
job: nginx-access-log
18+
__path__: /var/log/nginx/access.log
19+
pipeline_stages:
20+
- match:
21+
selector: '{job="nginx-access-log"}'
22+
stages:
23+
- json: # 日志为json格式 选择需要的字段
24+
expressions:
25+
remote_addr:
26+
request_uri:
27+
verb:
28+
httpversion:
29+
response:
30+
body_bytes_sent:
31+
referrer:
32+
user_agent:
33+
http_x_forwarded_for:
34+
server_name:
35+
request_time:
36+
upstream_response_time:
37+
upstream_addr:
38+
realpath_root:
39+
cookie:
40+
nginx_version:
41+
scheme:
42+
time:
43+
- labels:
44+
verb:
45+
response:
46+
server_name:
47+
scheme:
48+
- timestamp:
49+
format: '2006-01-02T15:04:05+08:00'
50+
source: time
51+
location: Asia/Shanghai
4352

44-
- labels: # 只选择低基数字段作为标签
45-
verb:
46-
response:
47-
server_name:
48-
scheme:
49-
- timestamp:
50-
format: "2006-01-02T15:04:05+08:00"
51-
source: time
52-
location: Asia/Shanghai
53-
54-
- job_name: nginx-error
55-
static_configs:
56-
- targets:
57-
- localhost
58-
labels:
59-
job: nginx-error-log
60-
__path__: /var/log/nginx/error.log
61-
pipeline_stages:
62-
- match:
63-
selector: '{job="nginx-error-log"}'
64-
stages:
65-
- regex: # 使用正则选择要提取的字段
66-
expression: '^(?P<timestamp>\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}) \[(?P<level>\S+)\] (?P<pid>\d+)#(?P<tid>\d+): \*(?P<cid>\d+) (?P<msg>.*) client: (?P<client_ip>[^,]+), server: (?P<server_name>[^,]+), request: "(?P<request_method>\S+) (?P<request_path>\S+) (?P<request_protocol>\S+)", upstream: "(?P<upstream>[^"]+)", host: "(?P<host>[^"]+)"$'
67-
- labels:
68-
level:
69-
pid:
70-
tid:
71-
cid:
72-
client_ip:
73-
server_name:
74-
request_method:
75-
request_path:
76-
request_protocol:
77-
upstream:
78-
host:
79-
- timestamp:
80-
format: "2006/01/02 15:04:05"
81-
source: time # 正则提取的字段
82-
location: Asia/Shanghai
53+
- job_name: nginx-error
54+
static_configs:
55+
- targets:
56+
- localhost
57+
labels:
58+
job: nginx-error-log
59+
__path__: /var/log/nginx/error.log
60+
pipeline_stages:
61+
- match:
62+
selector: '{job="nginx-error-log"}'
63+
stages:
64+
- regex: # 使用正则选择要提取的字段
65+
expression: '^(?P<timestamp>\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}) \[(?P<level>\S+)\] (?P<pid>\d+)#(?P<tid>\d+): \*(?P<cid>\d+) (?P<msg>.*) client: (?P<client_ip>[^,]+), server: (?P<server_name>[^,]+), request: "(?P<request_method>\S+) (?P<request_path>\S+) (?P<request_protocol>\S+)", upstream: "(?P<upstream>[^"]+)", host: "(?P<host>[^"]+)"$'
66+
- labels:
67+
level:
68+
pid:
69+
tid:
70+
cid:
71+
client_ip:
72+
server_name:
73+
request_method:
74+
request_path:
75+
request_protocol:
76+
upstream:
77+
host:
78+
- timestamp:
79+
format: "2006/01/02 15:04:05"
80+
source: time # 正则提取的字段
81+
location: Asia/Shanghai

0 commit comments

Comments
 (0)