Skip to content

Commit fe05536

Browse files
committed
chore(cloud): add cloud host value to log labels
1 parent 60cd1ac commit fe05536

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

internal/controller/testrun_controller.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,15 +432,10 @@ func (r *TestRunReconciler) ShouldAbort(ctx context.Context, k6 *v1alpha1.TestRu
432432

433433
status, err := cloud.GetTestRunState(r.k6CloudClient, k6.TestRunID(), log)
434434
if err != nil {
435-
log.Error(err, "Failed to get test run state.")
436435
return false
437436
}
438437

439-
isAborted := status.Aborted()
440-
441-
log.Info(fmt.Sprintf("Received test run status %v", status))
442-
443-
return isAborted
438+
return status.Aborted()
444439
}
445440

446441
func (r *TestRunReconciler) createClient(ctx context.Context, k6 *v1alpha1.TestRun, log logr.Logger) (bool, error) {

pkg/cloud/cloud_output.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ type TestRun struct {
2424
Instances int32 `json:"instances"`
2525
}
2626

27-
func NewClient(log logr.Logger, token, host string) *cloudapi.Client {
28-
logger := &logrus.Logger{
27+
// logger is currently unused, because of logrus dependency in cloudapi.
28+
// This will have a re-visit during or after https://github.com/grafana/k6-operator/issues/571
29+
func NewClient(logger logr.Logger, token, host string) *cloudapi.Client {
30+
l := &logrus.Logger{
2931
Out: os.Stdout,
3032
Formatter: new(logrus.TextFormatter),
3133
Hooks: make(logrus.LevelHooks),
@@ -38,15 +40,13 @@ func NewClient(log logr.Logger, token, host string) *cloudapi.Client {
3840
host = cloudConfig.Host.String
3941
}
4042

43+
logrusLogger := l.WithFields(logrus.Fields{"k6_cloud_host": host})
44+
4145
// TODO: how to get the version now?
42-
return cloudapi.NewClient(logger, token, host, "1.2.3", time.Duration(time.Minute))
46+
return cloudapi.NewClient(logrusLogger, token, host, "1.2.3", time.Duration(time.Minute))
4347
}
4448

4549
func CreateTestRun(opts InspectOutput, instances int32, host, token string, log logr.Logger) (*cloudapi.CreateTestRunResponse, error) {
46-
if client == nil {
47-
client = NewClient(log, token, host)
48-
}
49-
5050
cloudConfig := cloudapi.NewConfig()
5151

5252
if opts.ProjectID() > 0 {

pkg/cloud/test_runs.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ type TestRunPoller struct {
2424
}
2525

2626
func NewTestRunPoller(host, token, plzName string, logger logr.Logger) *TestRunPoller {
27-
logrusLogger := &logrus.Logger{
27+
// We need two loggers here because of logrus dependency in cloudapi.
28+
// This will have a re-visit during or after https://github.com/grafana/k6-operator/issues/571
29+
l := &logrus.Logger{
2830
Out: os.Stdout,
2931
Formatter: new(logrus.TextFormatter),
3032
Hooks: make(logrus.LevelHooks),
3133
Level: logrus.InfoLevel,
3234
}
35+
logrusLogger := l.WithFields(logrus.Fields{"k6_cloud_host": host})
36+
logger = logger.WithValues("k6_cloud_host", host)
3337

3438
testRunsCh := make(chan string)
3539

@@ -110,35 +114,46 @@ func GetTestRunData(client *cloudapi.Client, refID string) (*TestRunData, error)
110114
}
111115

112116
// called by TestRun controller
113-
func GetTestRunState(client *cloudapi.Client, refID string, log logr.Logger) (TestRunStatus, error) {
114-
url := fmt.Sprintf("%s/loadtests/v4/test_runs(%s)?$select=id,run_status", ApiURL(client.BaseURL()), refID)
117+
// If there's an error, it'll be logged.
118+
func GetTestRunState(client *cloudapi.Client, refID string, logger logr.Logger) (TestRunStatus, error) {
119+
host := ApiURL(client.BaseURL())
120+
logger = logger.WithValues("k6_cloud_host", host)
121+
122+
url := fmt.Sprintf("%s/loadtests/v4/test_runs(%s)?$select=id,run_status", host, refID)
115123
trData, err := getTestRun(client, url)
116124
if err != nil {
125+
logger.Error(err, "Failed to get test run state.")
117126
return TestRunStatus(cloudapi.RunStatusRunning), err
118127
}
119128

120-
return TestRunStatus(trData.RunStatus), nil
129+
status := TestRunStatus(trData.RunStatus)
130+
logger.Info(fmt.Sprintf("Received test run status %v", status))
131+
132+
return status, nil
121133
}
122134

123135
// called by TestRun controller
124136
// If there's an error, it'll be logged.
125-
func SendTestRunEvents(client *cloudapi.Client, refID string, log logr.Logger, events *Events) {
137+
func SendTestRunEvents(client *cloudapi.Client, refID string, logger logr.Logger, events *Events) {
126138
if len(*events) == 0 {
127139
return
128140
}
129141

130-
url := fmt.Sprintf("%s/orchestrator/v1/testruns/%s/events", strings.TrimSuffix(client.BaseURL(), "/v1"), refID)
142+
host := strings.TrimSuffix(client.BaseURL(), "/v1")
143+
logger = logger.WithValues("k6_cloud_host", host)
144+
145+
url := fmt.Sprintf("%s/orchestrator/v1/testruns/%s/events", host, refID)
131146
req, err := client.NewRequest("POST", url, events)
132147

133148
if err != nil {
134-
log.Error(err, fmt.Sprintf("Failed to create events HTTP request %+v", events))
149+
logger.Error(err, fmt.Sprintf("Failed to create events HTTP request %+v", events))
135150
return
136151
}
137152

138-
log.Info(fmt.Sprintf("Sending events to k6 Cloud %+v", *events))
153+
logger.Info(fmt.Sprintf("Sending events to k6 Cloud %+v", *events))
139154

140155
// status code is checked in Do
141156
if err = client.Do(req, nil); err != nil {
142-
log.Error(err, fmt.Sprintf("Failed to send events %+v", events))
157+
logger.Error(err, fmt.Sprintf("Failed to send events %+v", events))
143158
}
144159
}

0 commit comments

Comments
 (0)