Lotty是一个将命令输出通过HTTP API发送到Grafana Loki的Go程序,支持PTY能力、内存缓存和批量发送。
git clone https://github.com/yourname/lotty.git
cd lotty
make build
docker build -t lotty:latest . docker run -e LOKI_ENDPOINT=http://loki:3100 \ -e LOKI_USERNAME=admin \ -e LOKI_PASSWORD=password \ lotty:latest -- your-command
所有配置项都支持环境变量,优先级:命令行参数 > 环境变量 > 默认值
| 变量名 | 描述 | 默认值 |
|---|---|---|
LOKI_ENDPOINT | Loki服务地址 | 无(必需) |
LOKI_USERNAME | Basic Auth用户名 | 无 |
LOKI_PASSWORD | Basic Auth密码 | 无 |
LOKI_LABELS | Loki标签,格式key=value | 无 |
BUFFER_SIZE | 缓冲区大小 | 10000 |
BATCH_INTERVAL | 批量发送间隔 | 900ms |
BATCH_SIZE | 批量大小 | 1000 |
RETRY_COUNT | 重试次数 | 3 |
INPUT_MODE | 输入模式(pty/pipe) | pty |
LOG_LEVEL | 日志级别 | info |
lotty [flags] -- command [args...]
Flags:
--config string 配置文件 (默认是 $HOME/.lotty.yaml)
--input-mode string 输入模式 (pty, pipe) (默认 "pty")
--log-level string 日志级别 (debug, info, warn, error) (默认 "info")
-h, --help 显示帮助信息
# 使用环境变量配置
export LOKI_ENDPOINT="http://localhost:3100/loki/api/v1/push"
export LOKI_USERNAME="admin"
export LOKI_PASSWORD="password"
export LOKI_LABELS="service=myapp,env=prod"
# 发送ping输出到Loki
lotty -- ping google.com
# 监控日志文件
lotty --input-mode pipe -- tail -f /var/log/app.log
# 监控应用程序输出(支持彩色输出和交互式程序)
lotty -- my-app --arg1 --arg2
命令输出 → PTY/管道捕获 → 行解析 → 内存缓存 → 批量处理器 → HTTP客户端 → Loki
# 运行所有测试
make test
# 生成测试覆盖率报告
make test-coverage
# 运行linter
make lint
# 格式化代码
make fmt
# 构建二进制文件
make build
# 交叉编译
make build-all
lotty/ ├── cmd/lotty/main.go # 主入口 ├── internal/ │ ├── config/ # 配置管理 │ ├── input/ # 输入处理(pty + reader) │ ├── buffer/ # 环形缓冲区 │ ├── loki/ # HTTP客户端 + Auth │ ├── batcher/ # 批量处理器 │ └── retry/ # 重试机制 ├── pkg/ │ └── types/ # 公共类型 ├── go.mod ├── go.sum ├── Makefile ├── Dockerfile └── README.md
MIT License
欢迎提交Pull Request或Issue。