Skip to content

Commit e634a67

Browse files
committed
feat(telemetry): add gitte.env (injected env KEY=VALUE) to action task spans
1 parent b904ccb commit e634a67

2 files changed

Lines changed: 47 additions & 10 deletions

File tree

actions/features_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,20 @@ func TestEnabledFeaturesForProject(t *testing.T) {
2929
t.Fatalf("enabledFeaturesForProject = %v, want %v", got, want)
3030
}
3131
}
32+
33+
func TestInjectedEnv(t *testing.T) {
34+
cfg := &config.GitteConfig{
35+
FeatureGates: map[string]config.FeatureGate{
36+
"feat-on": {Effects: config.FeatureEffects{Env: map[string]string{"FEAT_VAR": "1"}}},
37+
},
38+
}
39+
st := &state.GitteState{Features: map[string]state.FeatureState{"feat-on": {Enabled: true}}}
40+
proj := config.ProjectConfig{
41+
Remote: "git@github.com:example/myproj.git",
42+
Env: map[string]string{"PROJ_VAR": "x"},
43+
}
44+
got := injectedEnv(cfg, st, "myproj", proj)
45+
if got["PROJ_VAR"] != "x" || got["FEAT_VAR"] != "1" {
46+
t.Fatalf("injectedEnv = %v, want PROJ_VAR=x and FEAT_VAR=1", got)
47+
}
48+
}

actions/runner.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,18 @@ func runGroupTask(
294294
if feats := enabledFeaturesForProject(cfg, st, projName, proj); len(feats) > 0 {
295295
span.SetAttributes(attribute.StringSlice("gitte.features", feats))
296296
}
297+
if env := injectedEnv(cfg, st, projName, proj); len(env) > 0 {
298+
keys := make([]string, 0, len(env))
299+
for k := range env {
300+
keys = append(keys, k)
301+
}
302+
sort.Strings(keys)
303+
kvs := make([]string, 0, len(keys))
304+
for _, k := range keys {
305+
kvs = append(kvs, k+"="+env[k])
306+
}
307+
span.SetAttributes(attribute.StringSlice("gitte.env", kvs))
308+
}
297309
defer func() {
298310
if err != nil {
299311
span.RecordError(err)
@@ -367,16 +379,7 @@ func emitTaskPreamble(
367379
emit(" cmd: " + strings.Join(cmds, " "))
368380

369381
// Collect only the vars gitte injects (not all of os.Environ).
370-
injected := make(map[string]string)
371-
for k, v := range proj.Env {
372-
injected[k] = v
373-
}
374-
for k, v := range config.ResolveEnvWhen(proj.EnvWhen, runtime.GOARCH) {
375-
injected[k] = v
376-
}
377-
for k, v := range extraEnvForProject(cfg, st, projName, proj) {
378-
injected[k] = v
379-
}
382+
injected := injectedEnv(cfg, st, projName, proj)
380383
if len(injected) > 0 {
381384
keys := make([]string, 0, len(injected))
382385
for k := range injected {
@@ -391,6 +394,23 @@ func emitTaskPreamble(
391394
}
392395
}
393396

397+
// injectedEnv returns the env vars gitte injects for a project's task — project
398+
// env, arch-conditional env_when, and enabled feature-gate env — excluding the
399+
// inherited process environment (os.Environ).
400+
func injectedEnv(cfg *config.GitteConfig, st *state.GitteState, projName string, proj config.ProjectConfig) map[string]string {
401+
injected := make(map[string]string)
402+
for k, v := range proj.Env {
403+
injected[k] = v
404+
}
405+
for k, v := range config.ResolveEnvWhen(proj.EnvWhen, runtime.GOARCH) {
406+
injected[k] = v
407+
}
408+
for k, v := range extraEnvForProject(cfg, st, projName, proj) {
409+
injected[k] = v
410+
}
411+
return injected
412+
}
413+
394414
// enabledFeaturesForProject returns the sorted names of feature gates that are
395415
// enabled and in scope for the given project (the gates that actually inject
396416
// env into the project's tasks).

0 commit comments

Comments
 (0)