Skip to content

[Repo Assist] fix(cmd): apply OTEL_SERVICE_NAME env var override to tracing config#6344

Merged
lpcox merged 3 commits into
mainfrom
repo-assist/fix-tracing-service-name-env-052b3dee06ceaf6d
May 23, 2026
Merged

[Repo Assist] fix(cmd): apply OTEL_SERVICE_NAME env var override to tracing config#6344
lpcox merged 3 commits into
mainfrom
repo-assist/fix-tracing-service-name-env-052b3dee06ceaf6d

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Problem

OTEL_SERVICE_NAME environment variable was silently ignored when configuring the OTLP tracing service name.

Root cause

In internal/cmd/root.go, the three tracing flags were applied inconsistently:

// ✅ Endpoint: correctly applies env-var override
if otlpEndpoint != "" || cmd.Flags().Changed("otlp-endpoint") {
    ensureTracingConfig(cfg).Endpoint = otlpEndpoint
}
// ❌ Service name: only applied when flag was explicitly set on CLI
if cmd.Flags().Changed("otlp-service-name") {
    ensureTracingConfig(cfg).ServiceName = otlpServiceName
}

When OTEL_SERVICE_NAME=my-service is set in the environment, otlpServiceName picks up the value via the flag's default (envutil.GetEnvString("OTEL_SERVICE_NAME", ...)), but cmd.Flags().Changed("otlp-service-name") is false since no explicit CLI flag was passed. The env var value was therefore never written to the config, and resolveServiceName fell back to "mcp-gateway".

The config documentation explicitly states OTEL_SERVICE_NAME — overrides ServiceName, so this is a bug.

Fix

Use applyFlagOrEnv for both otlp-endpoint and otlp-service-name, consistent with the payload flag block (lines 256–258). Pre-fetch the TracingConfig pointer once to avoid repeated ensureTracingConfig(cfg) calls:

tc := ensureTracingConfig(cfg)
applyFlagOrEnv(cmd, "otlp-endpoint", &tc.Endpoint, otlpEndpoint, "")
applyFlagOrEnv(cmd, "otlp-service-name", &tc.ServiceName, otlpServiceName, config.DefaultTracingServiceName)
if cmd.Flags().Changed("otlp-sample-rate") {
    tc.SampleRate = &otlpSampleRate
}

otlp-sample-rate retains the Changed-only check since it has no env var override.

Tests added

Two new tests in internal/cmd/flags_tracing_test.go:

  • TestApplyTracingFlags_ServiceNameEnvVar — verifies OTEL_SERVICE_NAME propagates to tracing config when flag is not explicitly set
  • TestApplyTracingFlags_ServiceNameDefaultDoesNotOverrideConfig — verifies TOML config service name is preserved when env var is unset and flag is unchanged

Test Status

⚠️ Infrastructure limitation: The sandbox network firewall blocks downloading Go 1.25.0 (required by go.mod). Build and tests could not be executed locally. The changes are syntactically verified; the logic follows the same pattern as the existing applyFlagOrEnv calls on lines 256–258 which are already covered by passing tests.

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • proxy.golang.org

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "proxy.golang.org"

See Network Configuration for more information.

Generated by Repo Assist · ● 3.4M ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@851905c06e905bf362a9f6cc54f912e3df747d55

When OTEL_EXPORTER_OTLP_ENDPOINT is set in the environment, the
otlp-endpoint flag correctly propagates that value into the tracing
config via the 'otlpEndpoint != ""' check. However, OTEL_SERVICE_NAME
was only applied when --otlp-service-name was explicitly passed on the
CLI (cmd.Flags().Changed), meaning the env var was silently ignored.

Fix: use applyFlagOrEnv for both otlp-endpoint and otlp-service-name,
consistent with the payload flags (lines 256-258). Pre-fetch the
TracingConfig pointer once to avoid repeated ensureTracingConfig calls.

Add two new tests in flags_tracing_test.go:
- TestApplyTracingFlags_ServiceNameEnvVar: OTEL_SERVICE_NAME overrides config
- TestApplyTracingFlags_ServiceNameDefaultDoesNotOverrideConfig: TOML
  config is preserved when neither env var nor CLI flag is set

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review May 23, 2026 16:38
Copilot AI review requested due to automatic review settings May 23, 2026 16:38
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug in the gateway CLI config merge where OTEL_SERVICE_NAME (picked up via the --otlp-service-name flag default) was not being written into cfg.Gateway.Tracing.ServiceName unless the CLI flag was explicitly passed, causing tracing to fall back to config.DefaultTracingServiceName even when the env var was set.

Changes:

  • Update tracing flag application in internal/cmd/root.go to use applyFlagOrEnv for otlp-service-name (and refactor to reuse a single tracing config pointer).
  • Add tests to verify OTEL_SERVICE_NAME propagates into config when the flag is not explicitly set, and that defaults do not overwrite an existing config value.
Show a summary per file
File Description
internal/cmd/root.go Uses applyFlagOrEnv to correctly apply env-var overrides to tracing config (service name).
internal/cmd/flags_tracing_test.go Adds regression tests covering env-var override propagation and default non-overwrite behavior.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 2

Comment thread internal/cmd/flags_tracing_test.go Outdated
Comment thread internal/cmd/root.go Outdated
lpcox and others added 2 commits May 23, 2026 09:52
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@lpcox lpcox merged commit aac5c74 into main May 23, 2026
16 checks passed
@lpcox lpcox deleted the repo-assist/fix-tracing-service-name-env-052b3dee06ceaf6d branch May 23, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants