Skip to content

feat: default node name to hostname when NODE_NAME unset - #5659

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
feitianbubu:feat/node-name-default-hostname
Jun 22, 2026
Merged

feat: default node name to hostname when NODE_NAME unset#5659
seefs001 merged 1 commit into
QuantumNous:mainfrom
feitianbubu:feat/node-name-default-hostname

Conversation

@feitianbubu

@feitianbubu feitianbubu commented Jun 22, 2026

Copy link
Copy Markdown
Member

📝 变更描述 / Description

NodeName(写入审计/用量日志,用于标识执行节点)此前仅从 NODE_NAME 环境变量读取,未配置时为空字符串。

本 PR 在未配置 NODE_NAME 时回退到操作系统主机名:

hostname, _ := os.Hostname()
NodeName = GetEnvOrDefaultString("NODE_NAME", hostname)

优先级为 NODE_NAME → hostname → 空。显式配置始终优先,只在缺省时兜底,不改变已配置场景的行为。

为什么这样改能生效: 在容器 / Kubernetes 部署中,容器主机名默认等于容器 ID 或 Pod 名,在自动扩容场景下每个实例天然唯一,因此日志能自动标识出是哪个实例执行,无需对每个动态实例手动配置 NODE_NAMEos.Hostname() 出错时返回空串,退回原有行为,不会 panic。

📸 运行证明 / Proof of Work

优化前:
image
优化后:
image

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced node name initialization with automatic fallback to system hostname when environment configuration is unavailable, improving system resilience in containerized deployments.

Fall back to the OS hostname when NODE_NAME is not configured, so node
identity in audit/usage logs is populated automatically. In container and
Kubernetes deployments the hostname equals the container ID or Pod name,
which stays unique under autoscaling without any manual per-instance config.
@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

In common/init.go, the NodeName initialization inside InitEnv is changed from os.Getenv("NODE_NAME") to GetEnvOrDefaultString("NODE_NAME", hostname), where hostname is obtained via os.Hostname(). This provides a fallback value when NODE_NAME is not set in the environment.

Changes

NodeName Hostname Fallback

Layer / File(s) Summary
NodeName fallback to os.Hostname()
common/init.go
NodeName now calls GetEnvOrDefaultString("NODE_NAME", hostname) with hostname from os.Hostname(), replacing the unconditional os.Getenv("NODE_NAME") that would return an empty string when the env var is absent.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐇 A hostname to fall back upon,
When NODE_NAME has up and gone,
The rabbit hops from env to host,
No empty name shall haunt this post,
With os.Hostname() the gap is bridged! 🌟

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: defaulting node name to hostname when NODE_NAME is unset, which directly matches the primary modification in common/init.go.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@seefs001
seefs001 merged commit 354d0fe into QuantumNous:main Jun 22, 2026
2 checks passed
jahnli added a commit to jahnli/new-api that referenced this pull request Jun 22, 2026
Merge upstream changes including:
- ClickHouse log database support (QuantumNous#5663)
- System task for persistent log cleanup progress
- Passive channel monitoring mode (QuantumNous#5592)
- Channel test environment toggle
- Node name defaults to hostname when NODE_NAME unset (QuantumNous#5659)
- tsgo for web type checking
- Refactored database type detection API (UsingMainDatabase/UsingLogDatabase)

Conflict resolution:
- AGENTS.md, CLAUDE.md: kept local Chinese version, added UsingMainDatabase/UsingLogDatabase docs
- controller/channel-test.go: kept AIHubError naming, adopted allowDisable + localErr checks
- docker-compose.yml: kept ai-hub naming, added ClickHouse config
- model/redemption.go: kept local deletion (feature removed)
- web/default/package.json: adopted upstream tsgo/oxlint/oxfmt toolchain
- use-sidebar-data.ts: kept local (no redemption codes)
- i18n locale files: kept local translations, added 8 new upstream keys
ruanhangjian pushed a commit to ruanhangjian/new-api that referenced this pull request Jul 11, 2026
…#5659)

Fall back to the OS hostname when NODE_NAME is not configured, so node
identity in audit/usage logs is populated automatically. In container and
Kubernetes deployments the hostname equals the container ID or Pod name,
which stays unique under autoscaling without any manual per-instance config.
zhaodechao2008 pushed a commit to zhaodechao2008/new-api that referenced this pull request Jul 27, 2026
…#5659)

Fall back to the OS hostname when NODE_NAME is not configured, so node
identity in audit/usage logs is populated automatically. In container and
Kubernetes deployments the hostname equals the container ID or Pod name,
which stays unique under autoscaling without any manual per-instance config.
330079598 pushed a commit to 330079598/new-api that referenced this pull request Aug 19, 2026
…#5659)

Fall back to the OS hostname when NODE_NAME is not configured, so node
identity in audit/usage logs is populated automatically. In container and
Kubernetes deployments the hostname equals the container ID or Pod name,
which stays unique under autoscaling without any manual per-instance config.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants