From 45e401c735ec9c20858bab8f27d4dfd1641d418b Mon Sep 17 00:00:00 2001 From: ww24 Date: Tue, 26 Sep 2023 23:23:00 +0900 Subject: [PATCH 1/2] feat: add buildinfo --- .github/workflows/docker.yml | 4 ++++ cmd/linebot/bot.go | 3 ++- cmd/linebot/main.go | 10 +++------- cmd/screenshot/job.go | 3 ++- cmd/screenshot/main.go | 13 +++++-------- docker/linebot/Dockerfile | 5 ++++- docker/screenshot/Dockerfile | 5 ++++- internal/buildinfo/buildinfo.go | 26 ++++++++++++++++++++++++++ 8 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 internal/buildinfo/buildinfo.go diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index cb2bbb96..a592016f 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -105,6 +105,8 @@ jobs: push: true build-args: | VERSION=${{ steps.params.outputs.version }} + REVISION=${{ github.sha }} + REPOSITORY=${{ github.repositoryUrl }} tags: | ghcr.io/${{ github.repository_owner }}/${{ matrix.target }}:${{ inputs.image_tag }} - name: Run Trivy vulnerability scanner @@ -164,6 +166,8 @@ jobs: push: true build-args: | VERSION=v${{ inputs.image_tag }} + REVISION=${{ github.sha }} + REPOSITORY=${{ github.repositoryUrl }} tags: | ${{ env.GCP_LOCATION }}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ env.GAR_REPOSITORY }}/${{ matrix.target }}:${{ inputs.image_tag }} provenance: false # keep compatibility to run on Cloud Run diff --git a/cmd/linebot/bot.go b/cmd/linebot/bot.go index be4844cd..696361e9 100644 --- a/cmd/linebot/bot.go +++ b/cmd/linebot/bot.go @@ -5,12 +5,13 @@ import ( "go.opentelemetry.io/otel/trace" + "github.com/ww24/linebot/internal/buildinfo" "github.com/ww24/linebot/internal/config" "github.com/ww24/linebot/tracer" ) //nolint:gochecknoglobals -var tc = tracer.NewConfig(serviceName, version) +var tc = tracer.NewConfig(serviceName, buildinfo.Version()) type bot struct { conf *config.LINEBot diff --git a/cmd/linebot/main.go b/cmd/linebot/main.go index 6a0d3ac5..6a606a53 100644 --- a/cmd/linebot/main.go +++ b/cmd/linebot/main.go @@ -14,6 +14,7 @@ import ( "go.uber.org/automaxprocs/maxprocs" "go.uber.org/zap" + "github.com/ww24/linebot/internal/buildinfo" "github.com/ww24/linebot/logger" ) @@ -23,17 +24,12 @@ const ( readHeaderTimeout = 10 * time.Second ) -var ( - // version is set during build - version string -) - func main() { ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() log.SetFlags(0) - if err := logger.SetConfig(serviceName, version); err != nil { + if err := logger.SetConfig(serviceName, buildinfo.Version()); err != nil { log.Printf("ERROR logger.SetMeta: %+v", err) return } @@ -52,7 +48,7 @@ func main() { defer cleanup() // initialize cloud profiler and tracing if build is production - if version != "" { + if version := buildinfo.Version(); version != "" { profilerConfig := profiler.Config{ Service: serviceName, ServiceVersion: version, diff --git a/cmd/screenshot/job.go b/cmd/screenshot/job.go index dfafb6cd..f215b57c 100644 --- a/cmd/screenshot/job.go +++ b/cmd/screenshot/job.go @@ -7,13 +7,14 @@ import ( "go.opentelemetry.io/otel/trace" "golang.org/x/xerrors" + "github.com/ww24/linebot/internal/buildinfo" "github.com/ww24/linebot/internal/config" "github.com/ww24/linebot/tracer" "github.com/ww24/linebot/usecase" ) //nolint:gochecknoglobals -var tc = tracer.NewConfig(serviceName, version) +var tc = tracer.NewConfig(serviceName, buildinfo.Version()) type job struct { config *config.Screenshot diff --git a/cmd/screenshot/main.go b/cmd/screenshot/main.go index 9c21c7bb..062fbcbe 100644 --- a/cmd/screenshot/main.go +++ b/cmd/screenshot/main.go @@ -12,6 +12,7 @@ import ( "go.uber.org/automaxprocs/maxprocs" "go.uber.org/zap" + "github.com/ww24/linebot/internal/buildinfo" "github.com/ww24/linebot/logger" ) @@ -19,12 +20,8 @@ const ( serviceName = "screenshot" ) -var ( - // version is set during build - version string - //nolint:gochecknoglobals - tr = otel.Tracer("github.com/ww24/linebot/cmd/screenshot") -) +//nolint:gochecknoglobals +var tr = otel.Tracer("github.com/ww24/linebot/cmd/screenshot") func main() { ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) @@ -34,7 +31,7 @@ func main() { defer span.End() log.SetFlags(0) - if err := logger.SetConfig(serviceName, version); err != nil { + if err := logger.SetConfig(serviceName, buildinfo.Version()); err != nil { stop() log.Printf("ERROR logger.SetMeta: %+v", err) os.Exit(1) @@ -55,7 +52,7 @@ func main() { defer cleanup() // initialize cloud profiler and tracing if build is production - if version != "" { + if version := buildinfo.Version(); version != "" { profilerConfig := profiler.Config{ Service: serviceName, ServiceVersion: version, diff --git a/docker/linebot/Dockerfile b/docker/linebot/Dockerfile index c816a445..55bff428 100644 --- a/docker/linebot/Dockerfile +++ b/docker/linebot/Dockerfile @@ -1,13 +1,16 @@ FROM golang:1.21 AS build ARG VERSION +ARG REVISION +ARG REPOSITORY WORKDIR $GOPATH/src/github.com/ww24/linebot COPY . . ENV CGO_ENABLED=0 RUN go build -buildmode pie \ - -ldflags "-X main.version=${VERSION} -s -w" -trimpath \ + -ldflags "-X buildinfo.version=${VERSION} -X buildinfo.revision=${REVISION} -X buildinfo.repository=${REPOSITORY} -s -w" \ + -trimpath \ -o /usr/local/bin/linebot ./cmd/linebot FROM gcr.io/distroless/base:nonroot diff --git a/docker/screenshot/Dockerfile b/docker/screenshot/Dockerfile index bcb78922..21964106 100644 --- a/docker/screenshot/Dockerfile +++ b/docker/screenshot/Dockerfile @@ -1,13 +1,16 @@ FROM golang:1.21 AS build ARG VERSION +ARG REVISION +ARG REPOSITORY WORKDIR $GOPATH/src/github.com/ww24/screenshot COPY . . ENV CGO_ENABLED=0 RUN go build -buildmode pie \ - -ldflags "-X main.version=${VERSION} -s -w" -trimpath \ + -ldflags "-X buildinfo.version=${VERSION} -X buildinfo.revision=${REVISION} -X buildinfo.repository=${REPOSITORY} -s -w" \ + -trimpath \ -o /usr/local/bin/screenshot ./cmd/screenshot FROM chromedp/headless-shell:100.0.4896.56 diff --git a/internal/buildinfo/buildinfo.go b/internal/buildinfo/buildinfo.go new file mode 100644 index 00000000..57f18ced --- /dev/null +++ b/internal/buildinfo/buildinfo.go @@ -0,0 +1,26 @@ +package buildinfo + +var ( + // version is set during build + version string + // revision is set during build + //nolint:gochecknoglobals + revision string + // repository is set during build + //nolint:gochecknoglobals + repository string +) + +// Version returns version string. +func Version() string { + return version +} + +// Revision returns revision string. +func Revision() string { + return revision +} + +func Repository() string { + return repository +} From 483866948e3a69394b23795c916464ff6af37fed Mon Sep 17 00:00:00 2001 From: ww24 Date: Tue, 26 Sep 2023 23:23:57 +0900 Subject: [PATCH 2/2] feat(logger): add sourceReferences field --- logger/report.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/logger/report.go b/logger/report.go index 8bc71738..7786c7bc 100644 --- a/logger/report.go +++ b/logger/report.go @@ -45,9 +45,33 @@ func (l *sourceLocation) MarshalLogObject(enc zapcore.ObjectEncoder) error { return nil } +// see: https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorContext#SourceReference +type sourceReference struct { + Repository string + RevisionID string +} + +func (r *sourceReference) MarshalLogObject(enc zapcore.ObjectEncoder) error { + enc.AddString("repository", r.Repository) + enc.AddString("revisionId", r.RevisionID) + return nil +} + +type sourceReferences []*sourceReference + +func (r sourceReferences) MarshalLogArray(enc zapcore.ArrayEncoder) error { + for _, sr := range r { + if err := enc.AppendObject(sr); err != nil { + return fmt.Errorf("failed to append sourceReference: %w", err) + } + } + return nil +} + // see: https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorContext type errorContext struct { - ReportLocation *sourceLocation + ReportLocation *sourceLocation + SourceReferences sourceReferences } func (c *errorContext) MarshalLogObject(enc zapcore.ObjectEncoder) error { @@ -56,6 +80,11 @@ func (c *errorContext) MarshalLogObject(enc zapcore.ObjectEncoder) error { return fmt.Errorf("failed to add reportLocation field: %w", err) } } + if len(c.SourceReferences) > 0 { + if err := enc.AddArray("sourceReferences", c.SourceReferences); err != nil { + return fmt.Errorf("failed to add sourceReferences field: %w", err) + } + } return nil }