Skip to content

Commit

Permalink
Logging improvement (#7)
Browse files Browse the repository at this point in the history
* Added generated OpenAPI/Swagger spec

* Added workflow to build the runner and manager

* Trim null bytes in job body

* Added body encoding option

* Fixed workflow to tag with latest on main branch

* Branch rename

* Fixed name in workflow and tagging fixes

* Reworked tagging

* Typo

* Small fix

* Updated the action

* Fixes

* Fixed workflow

* #1 Migration tool (#5)

* Added migration tool to dockerfile, added an example in docker-compose

* Logging:
- changed logger
- log level can be configured via env

---------

Co-authored-by: Gašper Dobrovoljc <[email protected]>
Co-authored-by: Aljaž Markovič <[email protected]>
  • Loading branch information
3 people authored Nov 12, 2023
1 parent 51c3a59 commit 8ec21af
Show file tree
Hide file tree
Showing 23 changed files with 1,388 additions and 150 deletions.
142 changes: 142 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
name: "Build and Push to ECR"
on:
push:
branches:
- main
- staging
tags:
- 'v*'
pull_request:
types: [ opened, synchronize ]

jobs:
build-and-push-manager:
name: "Build and Push the Manager to ECR"
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Configure AWS Credentials
id: aws-config
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
with:
registries: ${{ steps.aws-config.outputs.aws-account-id }}

- name: Cache Docker layers
uses: actions/[email protected]
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: |
${{ steps.login-ecr.outputs.registry }}/distributed-scheduler
flavor: |
prefix=manager-
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v4
with:
file: ./build/manager/Dockerfile
push: ${{ github.event_name != 'pull_request' }} # Don't push on PR
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache


build-and-push-runner:
name: "Build and Push the Runner to ECR"
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Configure AWS Credentials
id: aws-config
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
with:
registries: ${{ steps.aws-config.outputs.aws-account-id }}

- name: Cache Docker layers
uses: actions/[email protected]
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: |
${{ steps.login-ecr.outputs.registry }}/distributed-scheduler
flavor: |
prefix=runner-
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v4
with:
file: ./build/runner/Dockerfile
push: ${{ github.event_name != 'pull_request' }} # Don't push on PR
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
4 changes: 4 additions & 0 deletions build/manager/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ RUN swag init -g handlers/doc.go
# Build the Go application
RUN go build -o bin/manager cmd/manager/main.go

# Build the migration tool
RUN go build -o bin/tooling cmd/tooling/main.go

# Use an official Alpine Linux runtime as a base image
FROM alpine:latest

Expand All @@ -32,6 +35,7 @@ WORKDIR /app

# Copy the binary from the builder stage to the current stage
COPY --from=builder /app/bin/manager /app/manager
COPY --from=builder /app/bin/tooling /app/tooling

# set command to run when starting the container
CMD ["/app/manager"]
28 changes: 15 additions & 13 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/GLCharge/otelzap"
"net/http"
"os"
"os/signal"
Expand All @@ -20,21 +21,22 @@ import (
var build = "develop"

func main() {
log, err := logger.New("MANAGER")
logLevel := os.Getenv("MANAGER_LOG_LEVEL")
log, err := logger.New(logLevel)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer log.Sync()

if err := run(log); err != nil {
log.Errorw("startup", "ERROR", err)
log.Error("startup error", zap.Error(err))
log.Sync()
os.Exit(1)
}
}

func run(log *zap.SugaredLogger) error {
func run(log *otelzap.Logger) error {

// -------------------------------------------------------------------------
// Configuration
Expand Down Expand Up @@ -81,19 +83,19 @@ func run(log *zap.SugaredLogger) error {
// -------------------------------------------------------------------------
// App Starting

log.Infow("starting service", "version", build)
defer log.Infow("shutdown complete")
log.Info("starting service", zap.String("version", build))
defer log.Info("shutdown complete")

out, err := conf.String(&cfg)
if err != nil {
return fmt.Errorf("generating config for output: %w", err)
}
log.Infow("startup", "config", out)
log.Info("startup", zap.String("config", out))

// -------------------------------------------------------------------------
// Database Support

log.Infow("startup", "status", "initializing database support", "host", cfg.DB.Host)
log.Info("startup", zap.String("status", "initializing database support"), zap.String("host", cfg.DB.Host))

db, err := database.Open(database.Config{
User: cfg.DB.User,
Expand All @@ -109,14 +111,14 @@ func run(log *zap.SugaredLogger) error {
}

defer func() {
log.Infow("shutdown", "status", "stopping database support", "host", cfg.DB.Host)
log.Info("shutdown", zap.String("status", "stopping database support"), zap.String("host", cfg.DB.Host))
db.Close()
}()

// -------------------------------------------------------------------------
// Start API Service

log.Infow("startup", "status", "initializing Management API support")
log.Info("startup", zap.String("status", "initializing Management API support"))

shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
Expand All @@ -137,13 +139,13 @@ func run(log *zap.SugaredLogger) error {
ReadTimeout: cfg.Web.ReadTimeout,
WriteTimeout: cfg.Web.WriteTimeout,
IdleTimeout: cfg.Web.IdleTimeout,
ErrorLog: zap.NewStdLog(log.Desugar()),
ErrorLog: zap.NewStdLog(log.Logger),
}

serverErrors := make(chan error, 1)

go func() {
log.Infow("startup", "status", "api router started", "host", api.Addr)
log.Info("startup", zap.String("status", "api router started"), zap.String("host", api.Addr))
serverErrors <- api.ListenAndServe()
}()

Expand All @@ -154,8 +156,8 @@ func run(log *zap.SugaredLogger) error {
case err := <-serverErrors:
return fmt.Errorf("server error: %w", err)
case sig := <-shutdown:
log.Infow("shutdown", "status", "shutdown started", "signal", sig)
defer log.Infow("shutdown", "status", "shutdown complete", "signal", sig)
log.Info("shutdown", zap.String("status", "shutdown started"), zap.Any("signal", sig))
defer log.Info("shutdown", zap.String("status", "shutdown complete"), zap.Any("signal", sig))

ctx, cancel := context.WithTimeout(context.Background(), cfg.Web.ShutdownTimeout)
defer cancel()
Expand Down
24 changes: 13 additions & 11 deletions cmd/runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/GLCharge/otelzap"
"net/http"
"os"
"os/signal"
Expand All @@ -23,21 +24,22 @@ import (
var build = "develop"

func main() {
log, err := logger.New("RUNNER")
logLevel := os.Getenv("RUNNER_LOG_LEVEL")
log, err := logger.New(logLevel)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer log.Sync()

if err := run(log); err != nil {
log.Errorw("startup", "ERROR", err)
log.Error("startup", zap.Error(err))
log.Sync()
os.Exit(1)
}
}

func run(log *zap.SugaredLogger) error {
func run(log *otelzap.Logger) error {

// -------------------------------------------------------------------------
// Configuration
Expand Down Expand Up @@ -77,19 +79,19 @@ func run(log *zap.SugaredLogger) error {
// -------------------------------------------------------------------------
// App Starting

log.Infow("starting service", "version", build)
defer log.Infow("shutdown complete")
log.Info("starting service", zap.String("version", build))
defer log.Info("shutdown complete")

out, err := conf.String(&cfg)
if err != nil {
return fmt.Errorf("generating config for output: %w", err)
}
log.Infow("startup", "config", out)
log.Info("startup", zap.String("config", out))

// -------------------------------------------------------------------------
// Database Support

log.Infow("startup", "status", "initializing database support", "host", cfg.DB.Host)
log.Info("startup", zap.String("status", "initializing database support"), zap.String("host", cfg.DB.Host))

db, err := database.Open(database.Config{
User: cfg.DB.User,
Expand All @@ -104,7 +106,7 @@ func run(log *zap.SugaredLogger) error {
return fmt.Errorf("connecting to db: %w", err)
}
defer func() {
log.Infow("shutdown", "status", "stopping database support", "host", cfg.DB.Host)
log.Info("shutdown", zap.String("status", "stopping database support"), zap.String("host", cfg.DB.Host))
db.Close()
}()

Expand All @@ -114,7 +116,7 @@ func run(log *zap.SugaredLogger) error {
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)

log.Infow("startup", "status", "initializing runner")
log.Info("startup", zap.String("status", "initializing runner"))

store := postgres.New(db, log)

Expand All @@ -139,8 +141,8 @@ func run(log *zap.SugaredLogger) error {

select {
case sig := <-shutdown:
log.Infow("shutdown", "status", "shutdown started", "signal", sig)
defer log.Infow("shutdown", "status", "shutdown complete", "signal", sig)
log.Info("shutdown", zap.String("status", "shutdown started"), zap.Any("signal", sig))
defer log.Info("shutdown", zap.String("status", "shutdown complete"), zap.Any("signal", sig))

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
Expand Down
Loading

0 comments on commit 8ec21af

Please sign in to comment.