> ## Documentation Index
> Fetch the complete documentation index at: https://docs.molesignal.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 配置

> 配置当前单 Crate 服务、端口、存储、Worker、安全、遥测与可选运行时。

使用一个 TOML 文件启动 MoleSignal：

```bash theme={null}
molesignal --config /etc/molesignal/config.toml
```

配置按以下顺序应用，后面的来源覆盖前面的来源：

1. Rust 内置默认值；
2. `--config` 指定的文件；
3. `MS_` 环境变量。

结构化环境变量在分区之间使用点号：

```bash theme={null}
env 'MS_HTTP.PORT=5081' \
    'MS_STORE.META.DSN=postgres://user:pass@db:5432/molesignal' \
    molesignal --config /etc/molesignal/config.toml
```

配置结构定义在 `src/config/`。仓库维护的生产向示例是 `conf/config.toml`；升级时请检查该文件，因为未填写的新设置会使用代码默认值。

## 仅 Secret 的值

以下值应尽量保留在 TOML 外：

| 环境变量                              | 用途                            |
| --------------------------------- | ----------------------------- |
| `MS_AUTH_JWT_SECRET_OVERRIDE`     | 部署策略要求时固定引导 JWT 签名材料。         |
| `MS_CIPHER_KEY`                   | 组织加密密钥存储的根材料。                 |
| `MS_LICENSE_FILE`                 | 明确启用后用于初始或灾难回退的签名许可证包。        |
| `MS_SELF_TELEMETRY_CLUSTER_TOKEN` | 验证拆分角色之间的自身遥测转发。              |
| `MS_AGENT_OPENAI_*`               | 开发环境回退 OpenAI Key 与基础 URL。    |
| `MS_AGENT_ANTHROPIC_*`            | 开发环境回退 Anthropic Key 与基础 URL。 |
| `MS_AGENT_COMPATIBLE_*`           | 开发环境回退 OpenAI 兼容 Key 与基础 URL。 |

生产环境通常应在 **Mole Agent → Agent 设置**中创建模型供应商。供应商 Key 会加密、只写保存，并优先于环境变量回退从 PostgreSQL 解析。

## 节点角色

```toml theme={null}
[node]
roles = ["standalone"]
id = ""
drain_timeout_secs = 30
```

角色可选 `standalone`、`router`、`intake`、`querier`、`compactor` 与 `alert_manager`。`standalone` 会组合完整服务。`id` 为空时在启动时生成。关闭进程会等待最多 `drain_timeout_secs`，让待处理采集数据刷写完成。

## 网络监听器

| 默认端口   | 分区             | 暴露范围                            |
| ------ | -------------- | ------------------------------- |
| `5080` | `[http]`       | 产品 HTTP API、UI 代理目标、健康与指标。      |
| `5082` | `[grpc]`       | 可信内部节点、扫描与私有采集协议。               |
| `5083` | `[flight_sql]` | 对外需身份验证的 Arrow Flight SQL；默认关闭。 |
| `4317` | `[otlp_grpc]`  | 对外标准 OTLP gRPC；默认开启。            |
| `5084` | `[profiling]`  | 节点本地 pprof 诊断；默认关闭且只绑定回环。       |

### HTTP 与 TLS

```toml theme={null}
[http]
bind = "0.0.0.0"
port = 5080
gzip = true
external_url = ""

[http.tls]
enabled = false
plain_port = 80
port = 443
acme_directory = "production"
account_email = ""
key_storage_dir = "/var/lib/molesignal/acme"
issue_poll_secs = 60
renewal_retry_secs = 21600
```

如果反向代理改变了主机或协议，请将 `external_url` 设置为公开 Origin。启用 TLS 后会启动 ACME HTTP-01 Listener 与 rustls SNI 端点。

### 内部 gRPC、Flight SQL 与 OTLP

```toml theme={null}
[grpc]
bind = "0.0.0.0"
port = 5082
max_message_size_mb = 32

[flight_sql]
enabled = false
bind = "0.0.0.0"
port = 5083
default_lookback_hours = 24
max_message_size_mb = 32

[otlp_grpc]
enabled = true
bind = "0.0.0.0"
port = 4317
max_message_size_mb = 32
```

不要把 `5082` 暴露到不可信网络。对外 OTLP 与 Flight SQL 要求 Bearer 身份验证；公开暴露前请终止 TLS。

## 元数据与对象存储

内置开发默认值使用 SQLite 与本地文件系统。仓库示例使用 PostgreSQL 保存元数据。

```toml theme={null}
[store.meta]
backend = "postgres"
dsn = "postgres://molesignal:molesignal@db:5432/molesignal"
max_connections = 16

[store.object]
backend = "s3"
root = ""
bucket = "molesignal"
region = "ap-southeast-1"
endpoint = ""
credentials_file = "/etc/molesignal/object-store.toml"
multipart_threshold_mb = 32
multipart_part_size_mb = 8
range_threshold_mb = 16
range_chunk_mb = 8
max_concurrency = 8
op_timeout_secs = 30
health_probe_interval_secs = 30

[store.object.retry]
max_attempts = 4
base_backoff_ms = 100
max_backoff_ms = 5000
jitter_ratio = 0.2
```

对象存储后端包括 `local`、`s3`、`azure` 与 `gcs`。凭据优先级是环境变量、凭据文件、TOML 内联。多节点部署请使用共享对象存储与 PostgreSQL。

## 采集、WAL、查询与保留

```toml theme={null}
[wal]
dir = "./data/wal"
segment_size_mb = 256
flush_strategy = "batch"
sync_level = "data"
batch_max_pending = 64
batch_max_delay_ms = 50

[intake]
buffer_max_mb = 256
flush_interval_secs = 30
flush_parallelism = 4

[querier]
concurrency = 0
max_scan_rows = 100000000
auto_async_threshold_rows = 50000000
estimate_throughput_per_sec = 1000

[compactor]
interval_secs = 300
target_mb = 512
max_concurrent_groups = 4
retention_days = 30
downsample_after_days = 0
downsample_interval_secs = 3600
```

`concurrency = 0` 使用可用 CPU。查询估算超过 `auto_async_threshold_rows` 时会转为异步搜索任务；将阈值设为 `0` 可关闭自动转换。数据流保留策略会覆盖 Compactor 默认值。

## 集群与限流

```toml theme={null}
[cluster]
advertise_addr = "127.0.0.1:5082"
heartbeat_interval_secs = 5
peer_timeout_secs = 15

[router.rate_limit]
intake_qps = 1000
query_qps = 100
burst_multiplier = 2
```

将 `advertise_addr` 设为所有对等节点都能访问的地址。速率设为 `0` 会关闭对应限制。远程集群定义与组织映射保存在数据库中，不属于该配置块。

## 告警与邮件

```toml theme={null}
[alert_manager]
eval_interval_secs = 30
dispatch_interval_secs = 10
eval_timeout_secs = 10
default_ack_timeout_secs = 300

[notify.smtp]
host = ""
port = 587
username = ""
password = ""
from = "molesignal@example.com"
tls = "starttls"
timeout_secs = 10
```

`host` 非空时启用 SMTP。Slack、Webhook 渠道、通知模板、值班计划与升级策略是组织资源，通过 UI 或 API 管理。

## 身份验证与许可证

```toml theme={null}
[auth]
token_ttl_secs = 86400
issuer = "molesignal"
root_email = ""
root_password = ""

[license]
bootstrap_from_environment = false
disaster_fallback_from_environment = false
```

JWT 签名密钥持久化在 PostgreSQL。旧 `[auth].jwt_secret` 设置会被忽略，应当删除。

许可证内容永远不会嵌入 TOML。两个许可证开关只控制 `MS_LICENSE_FILE` 是否可以初始化空数据库，或从损坏的活动版本恢复。

## 自身可观测性与性能剖析

进程日志使用 `[telemetry]`。尾采样、受保护的 `_sys` 采集、外部链路导出和 pprof Listener 使用以下嵌套分区：

```toml theme={null}
[telemetry]
log_level = "info"
log_format = "text"
log_output = "console"

[telemetry.self_collect]
enabled = false
retention_days = 7
metrics_retention_days = 7
traces_retention_days = 7
profiles_retention_days = 7
metrics_enabled = true
metrics_interval_secs = 15
queue_capacity = 8192

[telemetry.trace]
enabled = true
force_disabled = false
filter = "info"
deployment_environment = "production"
normal_sample_ratio = 0.10
development_sample_ratio = 1.0
decision_window_secs = 30

[telemetry.trace.external]
endpoint = ""
protocol = "grpc"
timeout_ms = 5000
queue_capacity = 8192
batch_size = 256
gzip = false
allow_self_export = false

[profiling]
enabled = false
bind = "127.0.0.1"
port = 5084
allow_remote = false
```

完整尾采样上限、慢操作阈值、各信号保留、导出器 TLS 与 Profile 计划见 `conf/config.toml`。修改前请阅读[自身可观测性](/zh-Hans/self-observability)。

## 搜索任务、缓存与冷元数据

以下分区用于调整查询准入与运行存储：

* `[search.admission]` 按工作组设置节点与集群并发。
* `[search] max_result_rows` 限制无界且非聚合的结果。
* `[search.stream_agg_cache]` 缓存已关闭的 PromQL 窗口；`capacity = 0` 表示关闭。
* `[search_jobs]` 控制 Worker 数量、空闲轮询与清理。
* `[cache.*]` 设置 FileMeta、Parquet 元数据、查询结果、Tantivy、Footer 与冷 Dump 缓存。
* `[cache.disk_cache] max_size_gb = 0` 关闭本地 Parquet 缓存。
* `[storage.file_meta_dump]` 将旧 FileMeta 分区迁移到对象存储。

完整缓存 Key 与默认值请以仓库配置示例为准。

## 函数、报告与 Agent

```toml theme={null}
[functions]
llm_eval_enabled = false

[scheduled_reports.renderer]
enabled = false
base_url = "http://127.0.0.1:5173"
concurrent_renders = 2
render_timeout_secs = 30
viewport_width = 1280
viewport_height = 800

[agent]
enabled = false
default_provider = "openai"
```

VRL 始终可用。JavaScript 需要带 `js-runtime` 构建，并且没有独立运行配置开关。`llm_eval_enabled` 允许数据管道按事件调用模型，可能增加采集延迟与成本。

PDF 与 PNG 报告需要启用渲染器，并配置可访问的 Web `base_url`。

`[agent].enabled` 是本地开发解锁。生产环境使用签名 `agent` 授权与 IAM 权限。模型供应商、提示词、工具、MCP 服务器与审批策略保存在 PostgreSQL 中，并在产品内管理。

## 数据库管理的设置

不要重新添加已经移除的 SSO 或产品策略 TOML 分区。OIDC/SAML 供应商、注册与共享策略、远程集群、域名、加密密钥、模型定价、Agent 设置、通知渠道，以及多数组织配置都保存在 PostgreSQL 中，并通过 UI 或 API 修改。
