Skip to content

Commit 709c28c

Browse files
igorwwwwwwwwwwwwwwwwwwwwAxel von Bertoldi
authored andcommitted
Add pod/container name to build logger fields
Adds PodName field to Build struct and includes it in log messages after the script stage. This helps identify the specific container or pod executing a job, appearing in messages like 'Job succeeded' alongside hostname and other metadata. Implemented across executors: - Docker: uses container name - Kubernetes: uses pod name - Docker Machine: tracks in machine details - Custom: supports pod_name in config output
1 parent 2dcf7de commit 709c28c

6 files changed

Lines changed: 110 additions & 15 deletions

File tree

common/build.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ func (b *Build) Log() *logrus.Entry {
227227
l = l.WithField("gitlab_scoped_user_id", *b.JobInfo.ScopedUserID)
228228
}
229229

230+
// this is only set after the prepare stage has run
231+
if b.Hostname != "" {
232+
l = l.WithField("name", b.Hostname)
233+
}
234+
235+
// executor-specific log fields
236+
for k, v := range GetExecutorLogFields(b.ExecutorData) {
237+
l = l.WithField(k, v)
238+
}
239+
230240
return l
231241
}
232242

common/executor.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ import (
1515
// executor will use. Meant to be casted, e.g. virtual machine details.
1616
type ExecutorData interface{}
1717

18+
// ExecutorDataLogger is an optional interface that ExecutorData implementations
19+
// can implement to provide executor-specific fields for structured logging.
20+
type ExecutorDataLogger interface {
21+
LogFields() map[string]string
22+
}
23+
24+
// GetExecutorLogFields extracts log fields from ExecutorData if it implements
25+
// ExecutorDataLogger, otherwise returns nil.
26+
func GetExecutorLogFields(data ExecutorData) map[string]string {
27+
if l, ok := data.(ExecutorDataLogger); ok {
28+
return l.LogFields()
29+
}
30+
return nil
31+
}
32+
1833
// ExecutorCommand stores the script executor will run on a given stage.
1934
// If Predefined it will try to use already allocated resources.
2035
type ExecutorCommand struct {

executors/docker/docker_command.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ func (s *commandExecutor) requestHelperContainer() (*container.InspectResponse,
178178
return nil, err
179179
}
180180

181+
if data, ok := s.Build.ExecutorData.(*executorData); ok {
182+
data.ContainerName = s.helperContainer.Name
183+
}
184+
181185
return s.helperContainer, nil
182186
}
183187

@@ -217,6 +221,10 @@ func (s *commandExecutor) requestBuildContainer() (*container.InspectResponse, e
217221
return nil, err
218222
}
219223

224+
if data, ok := s.Build.ExecutorData.(*executorData); ok {
225+
data.ContainerName = s.buildContainer.Name
226+
}
227+
220228
err = s.changeFilesOwnership()
221229
if err != nil {
222230
return nil, err
@@ -388,22 +396,26 @@ func init() {
388396
features.Variables = true
389397
}
390398

391-
common.RegisterExecutorProvider("docker", executors.DefaultExecutorProvider{
392-
Creator: creator,
393-
FeaturesUpdater: featuresUpdater,
394-
ConfigUpdater: configUpdater,
395-
DefaultShellName: options.Shell.Shell,
399+
common.RegisterExecutorProvider("docker", executorProvider{
400+
DefaultExecutorProvider: executors.DefaultExecutorProvider{
401+
Creator: creator,
402+
FeaturesUpdater: featuresUpdater,
403+
ConfigUpdater: configUpdater,
404+
DefaultShellName: options.Shell.Shell,
405+
},
396406
})
397407

398408
windowsFeaturesUpdater := func(features *common.FeaturesInfo) {
399409
featuresUpdater(features)
400410
features.NativeStepsIntegration = false
401411
}
402412

403-
common.RegisterExecutorProvider("docker-windows", executors.DefaultExecutorProvider{
404-
Creator: creator,
405-
FeaturesUpdater: windowsFeaturesUpdater,
406-
ConfigUpdater: configUpdater,
407-
DefaultShellName: options.Shell.Shell,
413+
common.RegisterExecutorProvider("docker-windows", executorProvider{
414+
DefaultExecutorProvider: executors.DefaultExecutorProvider{
415+
Creator: creator,
416+
FeaturesUpdater: windowsFeaturesUpdater,
417+
ConfigUpdater: configUpdater,
418+
DefaultShellName: options.Shell.Shell,
419+
},
408420
})
409421
}

executors/docker/provider.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package docker
2+
3+
import (
4+
"strings"
5+
6+
"gitlab.com/gitlab-org/gitlab-runner/common"
7+
"gitlab.com/gitlab-org/gitlab-runner/executors"
8+
)
9+
10+
type executorData struct {
11+
ContainerName string
12+
}
13+
14+
func (d *executorData) LogFields() map[string]string {
15+
if d.ContainerName == "" {
16+
return nil
17+
}
18+
return map[string]string{"container_name": strings.TrimPrefix(d.ContainerName, "/")}
19+
}
20+
21+
type executorProvider struct {
22+
executors.DefaultExecutorProvider
23+
}
24+
25+
func (p executorProvider) Acquire(config *common.RunnerConfig) (common.ExecutorData, error) {
26+
return &executorData{}, nil
27+
}

executors/kubernetes/kubernetes.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,10 @@ func (s *executor) setupBuildPod(ctx context.Context, initContainers []api.Conta
21292129
return err
21302130
}
21312131

2132+
if data, ok := s.Build.ExecutorData.(*executorData); ok {
2133+
data.PodName = s.pod.GetName()
2134+
}
2135+
21322136
ownerReferences := s.buildPodReferences()
21332137
err = s.setOwnerReferencesForResources(ctx, ownerReferences)
21342138
if err != nil {
@@ -3533,11 +3537,13 @@ func featuresFn(features *common.FeaturesInfo) {
35333537
}
35343538

35353539
func init() {
3536-
common.RegisterExecutorProvider(common.ExecutorKubernetes, executors.DefaultExecutorProvider{
3537-
Creator: func() common.Executor {
3538-
return newExecutor()
3540+
common.RegisterExecutorProvider(common.ExecutorKubernetes, executorProvider{
3541+
DefaultExecutorProvider: executors.DefaultExecutorProvider{
3542+
Creator: func() common.Executor {
3543+
return newExecutor()
3544+
},
3545+
FeaturesUpdater: featuresFn,
3546+
DefaultShellName: executorOptions.Shell.Shell,
35393547
},
3540-
FeaturesUpdater: featuresFn,
3541-
DefaultShellName: executorOptions.Shell.Shell,
35423548
})
35433549
}

executors/kubernetes/provider.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package kubernetes
2+
3+
import (
4+
"gitlab.com/gitlab-org/gitlab-runner/common"
5+
"gitlab.com/gitlab-org/gitlab-runner/executors"
6+
)
7+
8+
type executorData struct {
9+
PodName string
10+
}
11+
12+
func (d *executorData) LogFields() map[string]string {
13+
if d.PodName == "" {
14+
return nil
15+
}
16+
return map[string]string{"pod_name": d.PodName}
17+
}
18+
19+
type executorProvider struct {
20+
executors.DefaultExecutorProvider
21+
}
22+
23+
func (p executorProvider) Acquire(config *common.RunnerConfig) (common.ExecutorData, error) {
24+
return &executorData{}, nil
25+
}

0 commit comments

Comments
 (0)