From 3f04ff2f24462be332f3f652e9a175e3b176ff7d Mon Sep 17 00:00:00 2001 From: luorijun Date: Mon, 15 Jun 2026 17:01:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20otel=20=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 6 ++-- .gitignore | 1 + README.md | 7 +++++ caddy/Caddyfile | 8 ++++-- docker-compose.yaml | 15 ++++++++-- gost/gost.yaml | 9 +++++- gost/logs/record.log | 0 otel/collector.yaml | 67 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 README.md create mode 100644 gost/logs/record.log create mode 100644 otel/collector.yaml diff --git a/.env.example b/.env.example index dd0137b..cbe447a 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,4 @@ -GOST_USERNAME=gost -GOST_PASSWORD=gost +GOST_API_DOMAIN= +GOST_API_USERNAME= +GOST_API_PASSWORD= +GOST_LOG_ENDPOINT= diff --git a/.gitignore b/.gitignore index 4c49bd7..0953651 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .env +dev/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..524f6a2 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# 代理网关 + +## 使用方式 + +复制 `.env.example` 为 `.env`,并根据需要修改环境变量 + +执行 `docker-compose up` 启动容器 diff --git a/caddy/Caddyfile b/caddy/Caddyfile index fa9c8fe..86e3aee 100644 --- a/caddy/Caddyfile +++ b/caddy/Caddyfile @@ -1,3 +1,7 @@ -proxy1.lanhuip.com { - reverse_proxy localhost:9700 +{$GOST_API_DOMAIN} { + basic_auth { + {$GOST_API_USERNAME} {$GOST_API_PASSWORD} + } + + reverse_proxy 127.0.0.1:9700 } diff --git a/docker-compose.yaml b/docker-compose.yaml index 0fed3fe..738259f 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -7,8 +7,15 @@ services: volumes: - ./gost/gost.yaml:/etc/gost/gost.yaml - ./gost/logs:/var/log/gost - command: > - -api $GOST_USERNAME:$GOST_PASSWORD@:9700 + restart: unless-stopped + + otel-collector: + image: otel/opentelemetry-collector-contrib:0.153.0 + network_mode: host + volumes: + - ./otel/collector.yaml:/etc/otelcol-contrib/config.yaml + environment: + GOST_LOG_ENDPOINT: ${GOST_LOG_ENDPOINT} restart: unless-stopped caddy: @@ -16,4 +23,8 @@ services: network_mode: host volumes: - ./caddy/Caddyfile:/etc/caddy/Caddyfile + environment: + GOST_API_DOMAIN: ${GOST_API_DOMAIN} + GOST_API_USERNAME: ${GOST_API_USERNAME} + GOST_API_PASSWORD: ${GOST_API_PASSWORD} restart: unless-stopped diff --git a/gost/gost.yaml b/gost/gost.yaml index f4a3d82..2a09bbe 100644 --- a/gost/gost.yaml +++ b/gost/gost.yaml @@ -1,5 +1,12 @@ +api: + addr: 127.0.0.1:9700 + recorders: - name: record-file file: - path: /var/log/gost/ + path: /var/log/gost/record.log sep: "\n" + - name: record-http-otel + http: + url: http://127.0.0.1:4318/gost/record + timeout: 10s diff --git a/gost/logs/record.log b/gost/logs/record.log new file mode 100644 index 0000000..e69de29 diff --git a/otel/collector.yaml b/otel/collector.yaml new file mode 100644 index 0000000..cf05abb --- /dev/null +++ b/otel/collector.yaml @@ -0,0 +1,67 @@ +receivers: + webhookevent/gost: + endpoint: 0.0.0.0:4318 + path: /gost/record + split_logs_at_newline: false + +processors: + transform/gost: + error_mode: ignore + log_statements: + - context: log + statements: + - set(log.cache["gost"], ParseJSON(log.body)) + - set(log.time, Time(log.cache["gost"]["time"], "%Y-%m-%dT%H:%M:%S.%sZ")) where log.cache["gost"]["time"] != nil + - set(log.severity_text, "INFO") + - set(log.severity_text, "ERROR") where log.cache["gost"]["err"] != nil and log.cache["gost"]["err"] != "" + - set(log.severity_text, "ERROR") where log.cache["gost"]["http"]["statusCode"] != nil and log.cache["gost"]["http"]["statusCode"] >= 500 + - set(log.body, log.cache["gost"]) + - set(log.attributes["gost.service"], log.cache["gost"]["service"]) where log.cache["gost"]["service"] != nil + - set(log.attributes["gost.network"], log.cache["gost"]["network"]) where log.cache["gost"]["network"] != nil + - set(log.attributes["gost.remote"], log.cache["gost"]["remote"]) where log.cache["gost"]["remote"] != nil + - set(log.attributes["gost.local"], log.cache["gost"]["local"]) where log.cache["gost"]["local"] != nil + - set(log.attributes["gost.client"], log.cache["gost"]["client"]) where log.cache["gost"]["client"] != nil + - set(log.attributes["gost.src"], log.cache["gost"]["src"]) where log.cache["gost"]["src"] != nil + - set(log.attributes["gost.dst"], log.cache["gost"]["dst"]) where log.cache["gost"]["dst"] != nil + - set(log.attributes["gost.host"], log.cache["gost"]["host"]) where log.cache["gost"]["host"] != nil + - set(log.attributes["gost.proto"], log.cache["gost"]["proto"]) where log.cache["gost"]["proto"] != nil + - set(log.attributes["gost.clientIP"], log.cache["gost"]["clientIP"]) where log.cache["gost"]["clientIP"] != nil + - set(log.attributes["gost.route"], log.cache["gost"]["route"]) where log.cache["gost"]["route"] != nil + - set(log.attributes["gost.inputBytes"], log.cache["gost"]["inputBytes"]) where log.cache["gost"]["inputBytes"] != nil + - set(log.attributes["gost.outputBytes"], log.cache["gost"]["outputBytes"]) where log.cache["gost"]["outputBytes"] != nil + - set(log.attributes["gost.sid"], log.cache["gost"]["sid"]) where log.cache["gost"]["sid"] != nil + - set(log.attributes["gost.duration"], log.cache["gost"]["duration"]) where log.cache["gost"]["duration"] != nil + - set(log.attributes["gost.http.host"], log.cache["gost"]["http"]["host"]) where log.cache["gost"]["http"]["host"] != nil + - set(log.attributes["gost.http.method"], log.cache["gost"]["http"]["method"]) where log.cache["gost"]["http"]["method"] != nil + - set(log.attributes["gost.http.proto"], log.cache["gost"]["http"]["proto"]) where log.cache["gost"]["http"]["proto"] != nil + - set(log.attributes["gost.http.scheme"], log.cache["gost"]["http"]["scheme"]) where log.cache["gost"]["http"]["scheme"] != nil + - set(log.attributes["gost.http.uri"], log.cache["gost"]["http"]["uri"]) where log.cache["gost"]["http"]["uri"] != nil + - set(log.attributes["gost.http.statusCode"], log.cache["gost"]["http"]["statusCode"]) where log.cache["gost"]["http"]["statusCode"] != nil + - set(log.attributes["gost.err"], log.cache["gost"]["err"]) where log.cache["gost"]["err"] != nil and log.cache["gost"]["err"] != "" + - set(log.attributes["error.message"], log.cache["gost"]["err"]) where log.cache["gost"]["err"] != nil and log.cache["gost"]["err"] != "" + + resource/gost: + attributes: + - key: service.name + value: gost + action: upsert + + batch: + +exporters: + otlp_http/remote: + endpoint: ${env:GOST_LOG_ENDPOINT} + debug: + verbosity: detailed + +service: + pipelines: + logs: + receivers: + - webhookevent/gost + processors: + - transform/gost + - resource/gost + - batch + exporters: + - otlp_http/remote