Skip to content

Commit

Permalink
enhance: add context to Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Sep 1, 2023
1 parent 46337cf commit cf3b544
Show file tree
Hide file tree
Showing 42 changed files with 198 additions and 86 deletions.
4 changes: 2 additions & 2 deletions api/build/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func PlanBuild(ctx context.Context, database database.Interface, p *pipeline.Bui
}

// plan all services for the build
services, err := service.PlanServices(database, p, b)
services, err := service.PlanServices(ctx, database, p, b)
if err != nil {
// clean up the objects from the pipeline in the database
CleanBuild(ctx, database, b, services, nil, err)
Expand All @@ -51,7 +51,7 @@ func PlanBuild(ctx context.Context, database database.Interface, p *pipeline.Bui
}

// plan all steps for the build
steps, err := step.PlanSteps(database, p, b)
steps, err := step.PlanSteps(ctx, database, p, b)
if err != nil {
// clean up the objects from the pipeline in the database
CleanBuild(ctx, database, b, services, steps, err)
Expand Down
3 changes: 2 additions & 1 deletion api/log/create_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func CreateServiceLog(c *gin.Context) {
r := repo.Retrieve(c)
s := service.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber())

Expand Down Expand Up @@ -111,7 +112,7 @@ func CreateServiceLog(c *gin.Context) {
input.SetRepoID(r.GetID())

// send API call to create the logs
err = database.FromContext(c).CreateLog(input)
err = database.FromContext(c).CreateLog(ctx, input)
if err != nil {
retErr := fmt.Errorf("unable to create logs for service %s: %w", entry, err)

Expand Down
3 changes: 2 additions & 1 deletion api/log/create_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func CreateStepLog(c *gin.Context) {
r := repo.Retrieve(c)
s := step.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber())

Expand Down Expand Up @@ -111,7 +112,7 @@ func CreateStepLog(c *gin.Context) {
input.SetRepoID(r.GetID())

// send API call to create the logs
err = database.FromContext(c).CreateLog(input)
err = database.FromContext(c).CreateLog(ctx, input)
if err != nil {
retErr := fmt.Errorf("unable to create logs for step %s: %w", entry, err)

Expand Down
5 changes: 3 additions & 2 deletions api/log/delete_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func DeleteServiceLog(c *gin.Context) {
r := repo.Retrieve(c)
s := service.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber())

Expand All @@ -84,7 +85,7 @@ func DeleteServiceLog(c *gin.Context) {
}).Infof("deleting logs for service %s", entry)

// send API call to capture the service logs
l, err := database.FromContext(c).GetLogForService(s)
l, err := database.FromContext(c).GetLogForService(ctx, s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err)

Expand All @@ -94,7 +95,7 @@ func DeleteServiceLog(c *gin.Context) {
}

// send API call to remove the log
err = database.FromContext(c).DeleteLog(l)
err = database.FromContext(c).DeleteLog(ctx, l)
if err != nil {
retErr := fmt.Errorf("unable to delete logs for service %s: %w", entry, err)

Expand Down
5 changes: 3 additions & 2 deletions api/log/delete_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func DeleteStepLog(c *gin.Context) {
r := repo.Retrieve(c)
s := step.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber())

Expand All @@ -84,7 +85,7 @@ func DeleteStepLog(c *gin.Context) {
}).Infof("deleting logs for step %s", entry)

// send API call to capture the step logs
l, err := database.FromContext(c).GetLogForStep(s)
l, err := database.FromContext(c).GetLogForStep(ctx, s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err)

Expand All @@ -94,7 +95,7 @@ func DeleteStepLog(c *gin.Context) {
}

// send API call to remove the log
err = database.FromContext(c).DeleteLog(l)
err = database.FromContext(c).DeleteLog(ctx, l)
if err != nil {
retErr := fmt.Errorf("unable to delete logs for step %s: %w", entry, err)

Expand Down
3 changes: 2 additions & 1 deletion api/log/get_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func GetServiceLog(c *gin.Context) {
r := repo.Retrieve(c)
s := service.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber())

Expand All @@ -84,7 +85,7 @@ func GetServiceLog(c *gin.Context) {
}).Infof("reading logs for service %s", entry)

// send API call to capture the service logs
l, err := database.FromContext(c).GetLogForService(s)
l, err := database.FromContext(c).GetLogForService(ctx, s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err)

Expand Down
3 changes: 2 additions & 1 deletion api/log/get_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func GetStepLog(c *gin.Context) {
r := repo.Retrieve(c)
s := step.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber())

Expand All @@ -85,7 +86,7 @@ func GetStepLog(c *gin.Context) {
}).Infof("reading logs for step %s", entry)

// send API call to capture the step logs
l, err := database.FromContext(c).GetLogForStep(s)
l, err := database.FromContext(c).GetLogForStep(ctx, s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err)

Expand Down
3 changes: 2 additions & 1 deletion api/log/list_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func ListLogsForBuild(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber())

Expand Down Expand Up @@ -112,7 +113,7 @@ func ListLogsForBuild(c *gin.Context) {
perPage = util.MaxInt(1, util.MinInt(100, perPage))

// send API call to capture the list of logs for the build
l, t, err := database.FromContext(c).ListLogsForBuild(b, page, perPage)
l, t, err := database.FromContext(c).ListLogsForBuild(ctx, b, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to list logs for build %s: %w", entry, err)

Expand Down
5 changes: 3 additions & 2 deletions api/log/update_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func UpdateServiceLog(c *gin.Context) {
r := repo.Retrieve(c)
s := service.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber())

Expand All @@ -96,7 +97,7 @@ func UpdateServiceLog(c *gin.Context) {
}).Infof("updating logs for service %s", entry)

// send API call to capture the service logs
l, err := database.FromContext(c).GetLogForService(s)
l, err := database.FromContext(c).GetLogForService(ctx, s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err)

Expand Down Expand Up @@ -124,7 +125,7 @@ func UpdateServiceLog(c *gin.Context) {
}

// send API call to update the log
err = database.FromContext(c).UpdateLog(l)
err = database.FromContext(c).UpdateLog(ctx, l)
if err != nil {
retErr := fmt.Errorf("unable to update logs for service %s: %w", entry, err)

Expand Down
5 changes: 3 additions & 2 deletions api/log/update_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func UpdateStepLog(c *gin.Context) {
r := repo.Retrieve(c)
s := step.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d/%d", r.GetFullName(), b.GetNumber(), s.GetNumber())

Expand All @@ -96,7 +97,7 @@ func UpdateStepLog(c *gin.Context) {
}).Infof("updating logs for step %s", entry)

// send API call to capture the step logs
l, err := database.FromContext(c).GetLogForStep(s)
l, err := database.FromContext(c).GetLogForStep(ctx, s)
if err != nil {
retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err)

Expand Down Expand Up @@ -124,7 +125,7 @@ func UpdateStepLog(c *gin.Context) {
}

// send API call to update the log
err = database.FromContext(c).UpdateLog(l)
err = database.FromContext(c).UpdateLog(ctx, l)
if err != nil {
retErr := fmt.Errorf("unable to update logs for step %s: %w", entry, err)

Expand Down
5 changes: 3 additions & 2 deletions api/service/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package service

import (
"context"
"fmt"
"time"

Expand All @@ -17,7 +18,7 @@ import (
// PlanServices is a helper function to plan all services
// in the build for execution. This creates the services
// for the build in the configured backend.
func PlanServices(database database.Interface, p *pipeline.Build, b *library.Build) ([]*library.Service, error) {
func PlanServices(ctx context.Context, database database.Interface, p *pipeline.Build, b *library.Build) ([]*library.Service, error) {
// variable to store planned services
services := []*library.Service{}

Expand Down Expand Up @@ -55,7 +56,7 @@ func PlanServices(database database.Interface, p *pipeline.Build, b *library.Bui
l.SetData([]byte{})

// send API call to create the service logs
err = database.CreateLog(l)
err = database.CreateLog(ctx, l)
if err != nil {
return services, fmt.Errorf("unable to create service logs for service %s: %w", s.GetName(), err)
}
Expand Down
11 changes: 6 additions & 5 deletions api/step/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package step

import (
"context"
"fmt"
"time"

Expand All @@ -17,7 +18,7 @@ import (
// PlanSteps is a helper function to plan all steps
// in the build for execution. This creates the steps
// for the build in the configured backend.
func PlanSteps(database database.Interface, p *pipeline.Build, b *library.Build) ([]*library.Step, error) {
func PlanSteps(ctx context.Context, database database.Interface, p *pipeline.Build, b *library.Build) ([]*library.Step, error) {
// variable to store planned steps
steps := []*library.Step{}

Expand All @@ -26,7 +27,7 @@ func PlanSteps(database database.Interface, p *pipeline.Build, b *library.Build)
// iterate through all steps for each pipeline stage
for _, step := range stage.Steps {
// create the step object
s, err := planStep(database, b, step, stage.Name)
s, err := planStep(ctx, database, b, step, stage.Name)
if err != nil {
return steps, err
}
Expand All @@ -37,7 +38,7 @@ func PlanSteps(database database.Interface, p *pipeline.Build, b *library.Build)

// iterate through all pipeline steps
for _, step := range p.Steps {
s, err := planStep(database, b, step, "")
s, err := planStep(ctx, database, b, step, "")
if err != nil {
return steps, err
}
Expand All @@ -48,7 +49,7 @@ func PlanSteps(database database.Interface, p *pipeline.Build, b *library.Build)
return steps, nil
}

func planStep(database database.Interface, b *library.Build, c *pipeline.Container, stage string) (*library.Step, error) {
func planStep(ctx context.Context, database database.Interface, b *library.Build, c *pipeline.Container, stage string) (*library.Step, error) {
// create the step object
s := new(library.Step)
s.SetBuildID(b.GetID())
Expand Down Expand Up @@ -82,7 +83,7 @@ func planStep(database database.Interface, b *library.Build, c *pipeline.Contain
l.SetData([]byte{})

// send API call to create the step logs
err = database.CreateLog(l)
err = database.CreateLog(ctx, l)
if err != nil {
return nil, fmt.Errorf("unable to create logs for step %s: %w", s.GetName(), err)
}
Expand Down
20 changes: 10 additions & 10 deletions database/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,15 +582,15 @@ func testLogs(t *testing.T, db Interface, resources *Resources) {

// create the logs
for _, log := range resources.Logs {
err := db.CreateLog(log)
err := db.CreateLog(context.TODO(), log)
if err != nil {
t.Errorf("unable to create log %d: %v", log.GetID(), err)
}
}
methods["CreateLog"] = true

// count the logs
count, err := db.CountLogs()
count, err := db.CountLogs(context.TODO())
if err != nil {
t.Errorf("unable to count logs: %v", err)
}
Expand All @@ -600,7 +600,7 @@ func testLogs(t *testing.T, db Interface, resources *Resources) {
methods["CountLogs"] = true

// count the logs for a build
count, err = db.CountLogsForBuild(resources.Builds[0])
count, err = db.CountLogsForBuild(context.TODO(), resources.Builds[0])
if err != nil {
t.Errorf("unable to count logs for build %d: %v", resources.Builds[0].GetID(), err)
}
Expand All @@ -610,7 +610,7 @@ func testLogs(t *testing.T, db Interface, resources *Resources) {
methods["CountLogsForBuild"] = true

// list the logs
list, err := db.ListLogs()
list, err := db.ListLogs(context.TODO())
if err != nil {
t.Errorf("unable to list logs: %v", err)
}
Expand All @@ -620,7 +620,7 @@ func testLogs(t *testing.T, db Interface, resources *Resources) {
methods["ListLogs"] = true

// list the logs for a build
list, count, err = db.ListLogsForBuild(resources.Builds[0], 1, 10)
list, count, err = db.ListLogsForBuild(context.TODO(), resources.Builds[0], 1, 10)
if err != nil {
t.Errorf("unable to list logs for build %d: %v", resources.Builds[0].GetID(), err)
}
Expand All @@ -635,7 +635,7 @@ func testLogs(t *testing.T, db Interface, resources *Resources) {
// lookup the logs by service
for _, log := range []*library.Log{resources.Logs[0], resources.Logs[1]} {
service := resources.Services[log.GetServiceID()-1]
got, err := db.GetLogForService(service)
got, err := db.GetLogForService(context.TODO(), service)
if err != nil {
t.Errorf("unable to get log %d for service %d: %v", log.GetID(), service.GetID(), err)
}
Expand All @@ -648,7 +648,7 @@ func testLogs(t *testing.T, db Interface, resources *Resources) {
// lookup the logs by service
for _, log := range []*library.Log{resources.Logs[2], resources.Logs[3]} {
step := resources.Steps[log.GetStepID()-1]
got, err := db.GetLogForStep(step)
got, err := db.GetLogForStep(context.TODO(), step)
if err != nil {
t.Errorf("unable to get log %d for step %d: %v", log.GetID(), step.GetID(), err)
}
Expand All @@ -661,13 +661,13 @@ func testLogs(t *testing.T, db Interface, resources *Resources) {
// update the logs
for _, log := range resources.Logs {
log.SetData([]byte("bar"))
err = db.UpdateLog(log)
err = db.UpdateLog(context.TODO(), log)
if err != nil {
t.Errorf("unable to update log %d: %v", log.GetID(), err)
}

// lookup the log by ID
got, err := db.GetLog(log.GetID())
got, err := db.GetLog(context.TODO(), log.GetID())
if err != nil {
t.Errorf("unable to get log %d by ID: %v", log.GetID(), err)
}
Expand All @@ -680,7 +680,7 @@ func testLogs(t *testing.T, db Interface, resources *Resources) {

// delete the logs
for _, log := range resources.Logs {
err = db.DeleteLog(log)
err = db.DeleteLog(context.TODO(), log)
if err != nil {
t.Errorf("unable to delete log %d: %v", log.GetID(), err)
}
Expand Down
4 changes: 3 additions & 1 deletion database/log/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
package log

import (
"context"

"github.com/go-vela/types/constants"
)

// CountLogs gets the count of all logs from the database.
func (e *engine) CountLogs() (int64, error) {
func (e *engine) CountLogs(ctx context.Context) (int64, error) {
e.logger.Tracef("getting count of all logs from the database")

// variable to store query results
Expand Down
Loading

0 comments on commit cf3b544

Please sign in to comment.