Skip to content

Commit

Permalink
Merge pull request #711 from ww24/add-source-references
Browse files Browse the repository at this point in the history
Add source references
  • Loading branch information
ww24 authored Sep 26, 2023
2 parents c8e5c9d + 4838669 commit 798e4e3
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion cmd/linebot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions cmd/linebot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion cmd/screenshot/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions cmd/screenshot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ import (
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"

"github.com/ww24/linebot/internal/buildinfo"
"github.com/ww24/linebot/logger"
)

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)
Expand All @@ -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)
Expand All @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion docker/linebot/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion docker/screenshot/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 26 additions & 0 deletions internal/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 30 additions & 1 deletion logger/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit 798e4e3

Please sign in to comment.