Skip to content

Commit

Permalink
feat: add last-run flag to cloud-build (#7)
Browse files Browse the repository at this point in the history
use the flag when you know the trigger name and just want the historical logs for the most recent build for that trigger
  • Loading branch information
owenrumney authored Jan 31, 2023
1 parent 1544894 commit 22c4c72
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
9 changes: 6 additions & 3 deletions docs/services/cloudbuild/cloudbuild.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,28 @@ gtail can get historic logs for a Cloud Build that has already completed
gtail cloud-build historic -h
```
```text
Get the cloud build logs for a trigger that has already completed
Get the Cloud Build logs for a trigger that has already completed
Usage:
gtail cloud-build historic [flags]
Flags:
-h, --help help for historic
--hours-ago int Roughly how many hours ago the build happened. Searches a window of time from then till now (default 24)
--last-run Get the logs for the last run of the trigger
Global Flags:
--build-id string The cloud build ID
-d, --debug Enable debug logging
-o, --output string The output format either json or a template string
-p, --project string The GCP project ID
-p, --project string The GCP project ID (default "gs-app-iac")
-r, --region string The GCP region (default "us-central1")
--severity strings The severity of logs to include
--trigger-name string The name of the cloud build trigger to use
```

The `--hours-ago` flag will search for a build that started within the last `n` hours. If you don't specify this flag it will search for a build that started within the last 24 hours.

| Note: starting a historic build with the `--trigger-name` flag will tail all builds for that trigger in the given time period.
| Note: starting a historic build with the `--trigger-name` flag will tail all builds for that trigger in the given time period.

The `--last-run` flag will get the logs for the last run of the trigger, it needs to be used with the `--trigger-name` flag. The time it ran will be pulled from the build details so `--hours-ago` will be ignored.
2 changes: 2 additions & 0 deletions internal/app/gtail/cmd/cloudbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var historicCloudBuildCmd = &cobra.Command{
lf := logfilter.New(projectID, logfilter.CloudBuildLogFilterType).
WithBuildTriggerName(buildTriggerName).
WithID(logID).
WithLastRun(lastRun).
WithHoursAgo(hoursAgo)
return la.GetHistoricalLogEntries(lf)
},
Expand All @@ -52,6 +53,7 @@ func getCloudBuildCommand() *cobra.Command {
cloudBuild.PersistentFlags().StringVar(&logID, "build-id", logID, "The cloud build ID")
cloudBuild.PersistentFlags().StringSliceVar(&severities, "severity", severities, "The severity of logs to include")
cloudBuild.PersistentFlags().StringVarP(&outputFormat, "output", "o", outputFormat, "The output format either json or a template string")
historicCloudBuildCmd.Flags().BoolVar(&lastRun, "last-run", lastRun, "Get the logs for the last run of the trigger")
historicCloudBuildCmd.Flags().IntVar(&hoursAgo, "hours-ago", hoursAgo, "Roughly how many hours ago the build happened. Searches a window of time from then till now")

return cloudBuild
Expand Down
7 changes: 4 additions & 3 deletions internal/app/gtail/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (
)

var (
debug bool
lastRun bool
hoursAgo int = 24
projectID string = os.Getenv("GCP_PROJECT_ID")
region string = os.Getenv("GCP_REGION")
logID string
severities []string
outputFormat string
debug bool
hoursAgo int = 24
buildTriggerName string
functionName string
serviceName string
clusterName string
tailDuration string = "10m"
tailTopic string
severities []string
)

func init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logfilter
import (
"context"
"fmt"
"time"

cloudbuild "cloud.google.com/go/cloudbuild/apiv1/v2"
"cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb"
Expand All @@ -26,3 +27,27 @@ func resolveBuildTriggerID(projectID, triggerName string) (string, error) {
logger.Debug("Resolved trigger ID for %s: %s", trigger, trigger.Id)
return trigger.Id, nil
}

func getLatestBuildID(projectID, triggerID string) (string, *time.Time, error) {
client, err := cloudbuild.NewClient(context.Background())
if err != nil {
return "", nil, fmt.Errorf("NewClient error: %v", err)
}
defer func() { _ = client.Close() }()

logger.Debug("Getting latest build for trigger %s", triggerID)
builds := client.ListBuilds(context.Background(), &cloudbuildpb.ListBuildsRequest{
ProjectId: projectID,
Filter: fmt.Sprintf("trigger_id=%s", triggerID),
PageSize: 1,
})

build, err := builds.Next()
if err != nil {
return "", nil, fmt.Errorf("ListBuilds error: %v", err)
}
createTime := build.GetCreateTime().AsTime()

return build.Id, &createTime, nil

}
25 changes: 21 additions & 4 deletions internal/pkg/logfilter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ const (
)

type LogFilter struct {
filterType LogFilterType
filterType LogFilterType
lastRun bool
historic bool
hoursAgo int

projectID string
historic bool
hoursAgo int
logID string
buildTriggerName string
functionName string
Expand Down Expand Up @@ -50,6 +52,11 @@ func (lf *LogFilter) WithHistoric(historic bool) *LogFilter {
return lf
}

func (lf *LogFilter) WithLastRun(lastRun bool) *LogFilter {
lf.lastRun = lastRun
return lf
}

func (lf *LogFilter) WithHoursAgo(hoursAgo int) *LogFilter {
lf.hoursAgo = hoursAgo
return lf
Expand Down Expand Up @@ -113,7 +120,7 @@ func (lf *LogFilter) GetFilterString() string {
filters = append(filters, `resource.type="k8s_cluster"`, fmt.Sprintf(`resource.labels.project_id="%s"`, lf.projectID))
}

if lf.historic {
if lf.historic && !lf.lastRun {
start := time.Now().Add(time.Duration(-lf.hoursAgo) * time.Hour)
end := start.Add(time.Duration(lf.hoursAgo) * time.Hour)

Expand All @@ -139,6 +146,16 @@ func (lf *LogFilter) GetFilterString() string {
logger.Error("could not resolve the trigger named %s: %v", lf.buildTriggerName, err)
}
filters = append(filters, fmt.Sprintf(`resource.labels.build_trigger_id="%s"`, triggerID))

if lf.lastRun {
buildID, createTime, err := getLatestBuildID(lf.projectID, triggerID)
if err != nil {
logger.Error("could not resolve the build ID for trigger %s: %v", lf.buildTriggerName, err)
}
filters = append(filters, fmt.Sprintf(`resource.labels.build_id="%s"`, buildID))
filters = append(filters, fmt.Sprintf(`timestamp>="%s"`, createTime.Format(time.RFC3339)))
filters = append(filters, fmt.Sprintf(`timestamp<="%s"`, createTime.Add(time.Duration(1)*time.Hour).Format(time.RFC3339)))
}
}

filterString := strings.Join(filters, " ")
Expand Down

0 comments on commit 22c4c72

Please sign in to comment.