From 5d2b616052fa6e477c4e6164a67a5334ef897afe Mon Sep 17 00:00:00 2001 From: B Date: Thu, 17 Oct 2024 15:10:58 +0200 Subject: [PATCH] Copy some diverged changes over (#31) Co-authored-by: @lstout --- Dockerfile | 2 +- cmd/gitlab-honeycomb-buildevents/main.go | 3 +- go.mod | 4 +- internal/hook/hook.go | 23 ++++++++---- internal/hook/hook_test.go | 8 +++- internal/hook/parse.go | 2 +- internal/hook/types/job.go | 47 ++++++++++++------------ internal/hook/types/mergerequest.go | 8 ++-- internal/hook/types/pipeline.go | 10 ++--- internal/hook/types/types.go | 12 +++--- 10 files changed, 66 insertions(+), 53 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7ad0282..2fab4b1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.18 +FROM golang:1.23 WORKDIR /usr/src/app diff --git a/cmd/gitlab-honeycomb-buildevents/main.go b/cmd/gitlab-honeycomb-buildevents/main.go index afa9aa8..bceffc7 100644 --- a/cmd/gitlab-honeycomb-buildevents/main.go +++ b/cmd/gitlab-honeycomb-buildevents/main.go @@ -5,10 +5,9 @@ import ( "os" "strconv" - "github.com/zoidbergwill/gitlab-honeycomb-buildevents-webhooks-sink/internal/hook" - "github.com/honeycombio/libhoney-go" "github.com/spf13/cobra" + "github.com/zoidyzoidzoid/gitlab-honeycomb-buildevents-webhooks-sink/internal/hook" ) // Version is the default value that should be overridden in the diff --git a/go.mod b/go.mod index 0ddf3ba..5b9fd86 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/zoidyzoidzoid/gitlab-honeycomb-buildevents-webhooks-sink -// +heroku goVersion 1.18 -go 1.18 +// +heroku goVersion 1.23 +go 1.23 require ( github.com/honeycombio/libhoney-go v1.20.0 diff --git a/internal/hook/hook.go b/internal/hook/hook.go index e0fcbe7..f5ff66a 100644 --- a/internal/hook/hook.go +++ b/internal/hook/hook.go @@ -12,7 +12,7 @@ import ( "github.com/honeycombio/libhoney-go" "github.com/honeycombio/libhoney-go/transmission" - "github.com/zoidbergwill/gitlab-honeycomb-buildevents-webhooks-sink/internal/hook/types" + "github.com/zoidyzoidzoid/gitlab-honeycomb-buildevents-webhooks-sink/internal/hook/types" ) type Listener struct { @@ -48,7 +48,7 @@ func New(cfg Config) (*Listener, error) { mux.HandleFunc("/", l.Home) srv := &http.Server{ - Addr: fmt.Sprintf(cfg.ListenAddr), + Addr: cfg.ListenAddr, Handler: mux, ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, @@ -129,6 +129,13 @@ func (l *Listener) HandleRequest(w http.ResponseWriter, r *http.Request) { } } +func SendEvent(e *libhoney.Event) { + err := e.Send() + if err != nil { + fmt.Printf("failed to send event: %s", err) + } +} + func (l *Listener) handlePipeline(p types.PipelineEventPayload) error { if p.ObjectAttributes.Duration == 0 || p.ObjectAttributes.Status == "running" { return nil @@ -140,7 +147,7 @@ func (l *Listener) handlePipeline(p types.PipelineEventPayload) error { return err } - defer ev.Send() + defer SendEvent(ev) buildURL := fmt.Sprintf("%s/-/pipelines/%d", p.Project.WebURL, p.ObjectAttributes.ID) err = ev.Add(map[string]interface{}{ // Basic trace information @@ -194,14 +201,14 @@ func (l *Listener) handleJob(j types.JobEventPayload) error { return err } - defer ev.Send() + defer SendEvent(ev) err = ev.Add(map[string]interface{}{ // Basic trace information "service_name": "job", "trace.span_id": spanID, "trace.trace_id": parentTraceID, "trace.parent_id": parentTraceID, - "name": fmt.Sprintf(j.BuildName), + "name": j.BuildName, // CI information "ci_provider": "GitLab-CI", @@ -210,12 +217,14 @@ func (l *Listener) handleJob(j types.JobEventPayload) error { "build_id": j.BuildID, "repo": j.Repository.Homepage, // TODO: Something with job status - "status": j.BuildStatus, + "status": j.BuildStatus, + "queued_duration_ms": j.BuildQueuedDuration * 1000, + "queued_duration_min": j.BuildQueuedDuration / 60, // Runner information "ci_runner": j.Runner.Description, "ci_runner_id": j.Runner.ID, - //"ci_runner_tags": strings.Join(j.Runner.Tags, ","), + // "ci_runner_tags": strings.Join(j.Runner.Tags, ","), "duration_ms": j.BuildDuration * 1000, }) diff --git a/internal/hook/hook_test.go b/internal/hook/hook_test.go index 1493d8b..8798c12 100644 --- a/internal/hook/hook_test.go +++ b/internal/hook/hook_test.go @@ -1,8 +1,9 @@ package hook import ( - "github.com/honeycombio/libhoney-go" "testing" + + "github.com/honeycombio/libhoney-go" ) func Test_createEvent(t *testing.T) { @@ -19,6 +20,9 @@ func Test_createEvent(t *testing.T) { HookSecret: "", HoneycombConfig: &config, }) + if err != nil { + t.Errorf("failed to create config: %s", err) + } got, err := l.createEvent() if err != nil { t.Errorf("failed to create event: %s", err) @@ -35,7 +39,7 @@ func Test_createEvent(t *testing.T) { }) } -//func Test_HandlePipeline(t *testing.T) { +// func Test_HandlePipeline(t *testing.T) { // defer libhoney.Close() // var config libhoney.Config // tests := []struct { diff --git a/internal/hook/parse.go b/internal/hook/parse.go index 5d7dc7f..aefca5a 100644 --- a/internal/hook/parse.go +++ b/internal/hook/parse.go @@ -8,7 +8,7 @@ import ( "log" "net/http" - "github.com/zoidbergwill/gitlab-honeycomb-buildevents-webhooks-sink/internal/hook/types" + "github.com/zoidyzoidzoid/gitlab-honeycomb-buildevents-webhooks-sink/internal/hook/types" ) var ( diff --git a/internal/hook/types/job.go b/internal/hook/types/job.go index aa60ef2..58cbe20 100644 --- a/internal/hook/types/job.go +++ b/internal/hook/types/job.go @@ -1,31 +1,32 @@ package types -// JobEventPayload contains the information for GitLab's Job status change +// JobEventPayload contains the information for GitLab's Job status change. type JobEventPayload struct { - ObjectKind string `json:"object_kind"` - Ref string `json:"ref"` - Tag bool `json:"tag"` - BeforeSHA string `json:"before_sha"` - SHA string `json:"sha"` - BuildID int64 `json:"build_id"` - BuildName string `json:"build_name"` - BuildStage string `json:"build_stage"` - BuildStatus string `json:"build_status"` - BuildStartedAt GitLabTimestamp `json:"build_started_at,omitempty"` - BuildFinishedAt GitLabTimestamp `json:"build_finished_at,omitempty"` - BuildDuration float64 `json:"build_duration"` - BuildAllowFailure bool `json:"build_allow_failure"` - BuildFailureReason string `json:"build_failure_reason"` - PipelineID int64 `json:"pipeline_id"` - ProjectID int64 `json:"project_id"` - ProjectName string `json:"project_name"` - User User `json:"user"` - Commit BuildCommit `json:"commit"` - Repository Repository `json:"repository"` - Runner Runner `json:"runner"` + ObjectKind string `json:"object_kind"` + Ref string `json:"ref"` + Tag bool `json:"tag"` + BeforeSHA string `json:"before_sha"` + SHA string `json:"sha"` + BuildID int64 `json:"build_id"` + BuildName string `json:"build_name"` + BuildStage string `json:"build_stage"` + BuildStatus string `json:"build_status"` + BuildStartedAt GitLabTimestamp `json:"build_started_at,omitempty"` + BuildFinishedAt GitLabTimestamp `json:"build_finished_at,omitempty"` + BuildDuration float64 `json:"build_duration"` + BuildAllowFailure bool `json:"build_allow_failure"` + BuildFailureReason string `json:"build_failure_reason"` + BuildQueuedDuration float64 `json:"build_queued_duration"` + PipelineID int64 `json:"pipeline_id"` + ProjectID int64 `json:"project_id"` + ProjectName string `json:"project_name"` + User User `json:"user"` + Commit BuildCommit `json:"commit"` + Repository Repository `json:"repository"` + Runner Runner `json:"runner"` } -// BuildCommit contains all of the GitLab build commit information +// BuildCommit contains all of the GitLab build commit information. type BuildCommit struct { ID int64 `json:"id"` SHA string `json:"sha"` diff --git a/internal/hook/types/mergerequest.go b/internal/hook/types/mergerequest.go index dcad477..b3d1c30 100644 --- a/internal/hook/types/mergerequest.go +++ b/internal/hook/types/mergerequest.go @@ -2,7 +2,7 @@ package types import "time" -// MergeRequest contains all the GitLab merge request information +// MergeRequest contains all the GitLab merge request information. type MergeRequest struct { ID int64 `json:"id"` TargetBranch string `json:"target_branch"` @@ -29,7 +29,7 @@ type MergeRequest struct { URL string `json:"url"` } -// Source contains all the GitLab source information +// Source contains all the GitLab source information. type Source struct { Name string `json:"name"` Description string `json:"description"` @@ -47,7 +47,7 @@ type Source struct { HTTPURL string `json:"http_url"` } -// Target contains all the GitLab target information +// Target contains all the GitLab target information. type Target struct { Name string `json:"name"` Description string `json:"description"` @@ -65,7 +65,7 @@ type Target struct { HTTPURL string `json:"http_url"` } -// LastCommit contains all the GitLab last commit information +// LastCommit contains all the GitLab last commit information. type LastCommit struct { ID string `json:"id"` Message string `json:"message"` diff --git a/internal/hook/types/pipeline.go b/internal/hook/types/pipeline.go index deec64f..79c895e 100644 --- a/internal/hook/types/pipeline.go +++ b/internal/hook/types/pipeline.go @@ -1,6 +1,6 @@ package types -// PipelineEventPayload contains the information for GitLab's pipeline status change event +// PipelineEventPayload contains the information for GitLab's pipeline status change event. type PipelineEventPayload struct { ObjectKind string `json:"object_kind"` User User `json:"user"` @@ -11,7 +11,7 @@ type PipelineEventPayload struct { Builds []Build `json:"builds"` } -// PipelineObjectAttributes contains pipeline specific GitLab object attributes information +// PipelineObjectAttributes contains pipeline specific GitLab object attributes information. type PipelineObjectAttributes struct { ID int64 `json:"id"` Ref string `json:"ref"` @@ -27,13 +27,13 @@ type PipelineObjectAttributes struct { Variables []Variable `json:"variables"` } -// Variable contains pipeline variables +// Variable contains pipeline variables. type Variable struct { Key string `json:"key"` Value string `json:"value"` } -// Build contains all of the GitLab Build information +// Build contains all of the GitLab Build information. type Build struct { ID int64 `json:"id"` Stage string `json:"stage"` @@ -49,7 +49,7 @@ type Build struct { ArtifactsFile ArtifactsFile `json:"artifactsfile"` } -// ArtifactsFile contains all of the GitLab artifact information +// ArtifactsFile contains all of the GitLab artifact information. type ArtifactsFile struct { Filename string `json:"filename"` Size string `json:"size"` diff --git a/internal/hook/types/types.go b/internal/hook/types/types.go index 3f2658f..09a2ef8 100644 --- a/internal/hook/types/types.go +++ b/internal/hook/types/types.go @@ -6,7 +6,7 @@ import ( "time" ) -// Commit contains all of the GitLab commit information +// Commit contains all of the GitLab commit information. type Commit struct { ID string `json:"id"` Message string `json:"message"` @@ -19,13 +19,13 @@ type Commit struct { Removed []string `json:"removed"` } -// Author contains all of the GitLab author information +// Author contains all of the GitLab author information. type Author struct { Name string `json:"name"` Email string `json:"email"` } -// Project contains all of the GitLab project information +// Project contains all of the GitLab project information. type Project struct { ID int64 `json:"id"` Name string `json:"name"` @@ -44,7 +44,7 @@ type Project struct { HTTPURL string `json:"http_url"` } -// Repository contains all of the GitLab repository information +// Repository contains all of the GitLab repository information. type Repository struct { Name string `json:"name"` URL string `json:"url"` @@ -55,7 +55,7 @@ type Repository struct { VisibilityLevel int64 `json:"visibility_level"` } -// User contains all of the GitLab user information +// User contains all of the GitLab user information. type User struct { ID int64 `json:"id"` Name string `json:"name"` @@ -64,7 +64,7 @@ type User struct { Email string `json:"email"` } -// Runner represents a runner agent +// Runner represents a runner agent. type Runner struct { ID int64 `json:"id"` Description string `json:"description"`