Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】从 bindCommand 中拆出遥测环境变量注入 bindTelemetryEnvironment (#655) (#656)

fx-kurumi4f6aba76030d
diff --git a/internal/runtime/command_binding.go b/internal/runtime/command_binding.go
index 3739161d..10f32e5e 100644
--- a/internal/runtime/command_binding.go
+++ b/internal/runtime/command_binding.go
@@ -151,21 +151,29 @@ func bindCommand(sandbox Sandbox, workspace string, command CommandSpec, credent
if command.HookOutput != "" {
bound.environment["AGENTFLOW_HOOK_OUTPUT"] = command.HookOutput
}
- if command.Telemetry != nil {
- runID, nodeID := command.Telemetry.RunID, command.Telemetry.NodeID
- bound.environment["AGENTFLOW_RUN_ID"] = runID
- bound.environment["AGENTFLOW_NODE_ID"] = nodeID
- bound.environment["CODEBUDDY_CODE_ENABLE_TELEMETRY"] = "1"
- bound.environment["CLAUDE_CODE_ENABLE_TELEMETRY"] = "1"
- bound.environment["OTEL_TRACES_EXPORTER"] = "otlp"
- bound.environment["OTEL_EXPORTER_OTLP_PROTOCOL"] = "http/protobuf"
- bound.environment["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"] = observability.CollectorTracesURL()
- bound.environment["OTEL_LOG_TOOL_DETAILS"] = "1"
- bound.environment["OTEL_RESOURCE_ATTRIBUTES"] = fmt.Sprintf("agentflow.run_id=%s,agentflow.node_id=%s,service.name=agentflow-codebuddy", runID, nodeID)
- }
+ bindTelemetryEnvironment(bound.environment, command.Telemetry)
return bound, nil
}
+// bindTelemetryEnvironment points the child CLI's OpenTelemetry exporter at
+// the AgentFlow collector and tags its spans with the run and node. A nil
+// telemetry leaves env untouched.
+func bindTelemetryEnvironment(env map[string]string, telemetry *InvocationOptions) {
+ if telemetry == nil {
+ return
+ }
+ runID, nodeID := telemetry.RunID, telemetry.NodeID
+ env["AGENTFLOW_RUN_ID"] = runID
+ env["AGENTFLOW_NODE_ID"] = nodeID
+ env["CODEBUDDY_CODE_ENABLE_TELEMETRY"] = "1"
+ env["CLAUDE_CODE_ENABLE_TELEMETRY"] = "1"
+ env["OTEL_TRACES_EXPORTER"] = "otlp"
+ env["OTEL_EXPORTER_OTLP_PROTOCOL"] = "http/protobuf"
+ env["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"] = observability.CollectorTracesURL()
+ env["OTEL_LOG_TOOL_DETAILS"] = "1"
+ env["OTEL_RESOURCE_ATTRIBUTES"] = fmt.Sprintf("agentflow.run_id=%s,agentflow.node_id=%s,service.name=agentflow-codebuddy", runID, nodeID)
+}
+
// CodexUserProviderArgv inserts a dedicated "agentflow_user" model provider
// before the final argv element (the prompt). An empty baseURL falls back to
// the OpenAI API. The key stays in the process environment as CODEX_API_KEY;
diff --git a/internal/runtime/command_binding_telemetry_test.go b/internal/runtime/command_binding_telemetry_test.go
new file mode 100644
index 00000000..48add7ae
--- /dev/null
+++ b/internal/runtime/command_binding_telemetry_test.go
@@ -0,0 +1,55 @@
+package runtime
+
+import "testing"
+
+func TestBindTelemetryEnvironmentNilLeavesEnvUntouched(t *testing.T) {
+ env := map[string]string{"KEEP": "1"}
+ bindTelemetryEnvironment(env, nil)
+ if len(env) != 1 || env["KEEP"] != "1" {
+ t.Fatalf("nil telemetry changed env: %#v", env)
+ }
+}
+
+func TestBindTelemetryEnvironmentSetsCollectorAndRunTags(t *testing.T) {
+ t.Setenv("AGENTFLOW_OTEL_TRACES_ENDPOINT", "http://collector.test/v1/traces")
+ env := map[string]string{}
+ bindTelemetryEnvironment(env, &InvocationOptions{RunID: "run-1", NodeID: "node-a"})
+ want := map[string]string{
+ "AGENTFLOW_RUN_ID": "run-1",
+ "AGENTFLOW_NODE_ID": "node-a",
+ "CODEBUDDY_CODE_ENABLE_TELEMETRY": "1",
+ "CLAUDE_CODE_ENABLE_TELEMETRY": "1",
+ "OTEL_TRACES_EXPORTER": "otlp",
+ "OTEL_EXPORTER_OTLP_PROTOCOL": "http/protobuf",
+ "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "http://collector.test/v1/traces",
+ "OTEL_LOG_TOOL_DETAILS": "1",
+ "OTEL_RESOURCE_ATTRIBUTES": "agentflow.run_id=run-1,agentflow.node_id=node-a,service.name=agentflow-codebuddy",
+ }
+ if len(env) != len(want) {
+ t.Fatalf("env = %#v, want %#v", env, want)
+ }
+ for key, value := range want {
+ if env[key] != value {
+ t.Fatalf("%s = %q, want %q", key, env[key], value)
+ }
+ }
+}
+
+func TestBindCommandAppliesTelemetryEnvironment(t *testing.T) {
+ t.Setenv("AGENTFLOW_OTEL_TRACES_ENDPOINT", "http://collector.test/v1/traces")
+ workspace := t.TempDir()
+ plain, err := bindCommand(&localSandbox{workspace: workspace}, workspace, CommandSpec{Argv: []string{"true"}}, RunCredentials{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, ok := plain.environment["AGENTFLOW_RUN_ID"]; ok {
+ t.Fatalf("command without telemetry got telemetry env: %#v", plain.environment)
+ }
+ traced, err := bindCommand(&localSandbox{workspace: workspace}, workspace, CommandSpec{Argv: []string{"true"}, Telemetry: &InvocationOptions{RunID: "run-2", NodeID: "node-b"}}, RunCredentials{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if traced.environment["AGENTFLOW_RUN_ID"] != "run-2" || traced.environment["AGENTFLOW_NODE_ID"] != "node-b" || traced.environment["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"] != "http://collector.test/v1/traces" {
+ t.Fatalf("telemetry env not applied: %#v", traced.environment)
+ }
+}