Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
feat: configure OTEL logger (#57)
Browse files Browse the repository at this point in the history
Signed-off-by: Mateusz Urbanek <mateusz.urbanek.98@gmail.com>
  • Loading branch information
shanduur authored Oct 19, 2023
1 parent c003326 commit a830ba0
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## What this PR does / why we need it:
<!--
What code changes are made?
What problem does this PR addresses, or what what feature this PR adds?
What code changes are made?
What problem does this PR addresses, or what feature this PR adds?
-->
<!-- REPLACE WITH CONTENT -->

Expand Down
2 changes: 1 addition & 1 deletion cmd/bootstrap/template/main.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {
gocosi.SetLogger(log)
if err := realMain(context.Background()); err != nil {
log.Error(err, "critical failure")
log.Error(err, "Critical failure.")
os.Exit(1)
}
}
Expand Down
16 changes: 8 additions & 8 deletions endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ func (e *Endpoint) getCOSIEndpoint() {

u, err := url.Parse(env)
if err != nil {
log.Error(err, "failed to parse COSI Endpoint from environment")
log.Error(err, "Failed to parse COSI Endpoint from environment.")
return
}

log.V(8).Info("replacing default", EnvCOSIEndpoint, env)
log.V(8).Info("Replacing default.", EnvCOSIEndpoint, env)

e.address = u
}
Expand All @@ -160,11 +160,11 @@ func (e *Endpoint) getCOSIEndpointUser() {
}

if err != nil {
log.Error(err, "failed to parse COSI Endpoint Permissions from environment")
log.Error(err, "Failed to parse COSI Endpoint Permissions from environment.")
return
}

log.V(8).Info("replacing default", EnvCOSIEndpointUser, env)
log.V(8).Info("Replacing default.", EnvCOSIEndpointUser, env)

e.user = newUser
}
Expand All @@ -187,11 +187,11 @@ func (e *Endpoint) getCOSIEndpointGroup() {
}

if err != nil {
log.Error(err, "failed to parse COSI Endpoint Permissions from environment")
log.Error(err, "Failed to parse COSI Endpoint Permissions from environment.")
return
}

log.V(8).Info("replacing default", EnvCOSIEndpointGroup, env)
log.V(8).Info("Replacing default.", EnvCOSIEndpointGroup, env)

e.group = newGroup
}
Expand All @@ -204,11 +204,11 @@ func (e *Endpoint) getCOSIEndpointPermissions() {

perms, err := strconv.ParseInt(env, 0, 0)
if err != nil {
log.Error(err, "failed to parse COSI Endpoint Permissions from environment")
log.Error(err, "Failed to parse COSI Endpoint Permissions from environment.")
return
}

log.V(8).Info("replacing default", EnvCOSIEndpointPerms, env)
log.V(8).Info("Replacing default.", EnvCOSIEndpointPerms, env)

e.permissions = os.FileMode(perms)
}
17 changes: 11 additions & 6 deletions gocosi.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import (
"net/url"
"time"

ilog "github.com/doomshrine/gocosi/internal/log"
"github.com/doomshrine/must"
"github.com/go-logr/logr"
"github.com/hellofresh/health-go/v5"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/resource"
"google.golang.org/grpc"
cosi "sigs.k8s.io/container-object-storage-interface-spec"
Expand Down Expand Up @@ -96,6 +98,9 @@ func New(identity cosi.IdentityServer, provisioner cosi.ProvisionerServer, res *
// SetLogger is used to set the default global logger for the gocosi library.
func SetLogger(l logr.Logger) {
log = l

otel.SetLogger(l)
otel.SetErrorHandler(&ilog.Logger{LoggerImpl: log})
}

// Run starts the COSI driver and serves requests.
Expand All @@ -115,7 +120,7 @@ func (d *Driver) Run(ctx context.Context) error {

go shutdown(ctx, srv, d.server)

log.V(4).Info("starting driver", "address", lis.Addr())
log.V(4).Info("Starting driver.", "address", lis.Addr())

err = srv.Serve(lis)
if err != nil {
Expand All @@ -126,9 +131,9 @@ func (d *Driver) Run(ctx context.Context) error {
}

func shutdown(ctx context.Context, g *grpc.Server, h *http.Server) {
log.V(8).Info("shutdown watcher started")
log.V(8).Info("Shutdown watcher started.")
<-ctx.Done()
log.Info("starting shutdown")
log.Info("Starting shutdown.")

if g != nil {
go g.GracefulStop()
Expand All @@ -141,18 +146,18 @@ func shutdown(ctx context.Context, g *grpc.Server, h *http.Server) {
go func() {
err := h.Shutdown(shutdownCtx)
if err != nil {
log.Error(err, "error during HTTP server shutdown")
log.Error(err, "Error during HTTP server shutdown.")
}
}()
}
}

func (d *Driver) serveHTTP() {
log.V(8).Info("http server started", "address", d.server.Addr)
log.V(8).Info("HTTP server started.", "address", d.server.Addr)

err := d.server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Error(err, "failed to serve HTTP server", "address", d.server.Addr)
log.Error(err, "Failed to serve HTTP server.", "address", d.server.Addr)
}
}

Expand Down
4 changes: 2 additions & 2 deletions grpc/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"context"
"runtime/debug"

"github.com/doomshrine/gocosi/grpc/log"
"github.com/doomshrine/gocosi/internal/log"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
)

Expand All @@ -35,7 +35,7 @@ func PanicRecovery(logger *log.Logger, callbacks ...func(context.Context)) recov

if logger != nil {
logger.Log(ctx, 0,
"recovered from panic",
"Recovered from panic.",
"panic", p,
"stack", debug.Stack())
}
Expand Down
2 changes: 1 addition & 1 deletion healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func HealthcheckFunc(ctx context.Context, addr string) error {
return fmt.Errorf("unable to decode healthcheck response: %w", err)
}

log.Info("healthcheck finished",
log.Info("Healthcheck finished.",
"status", c.Status,
"system", c.System,
"failures", c.Failures,
Expand Down
12 changes: 10 additions & 2 deletions grpc/log/log.go → internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ import (

"github.com/go-logr/logr"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"go.opentelemetry.io/otel"
)

type Logger struct {
LoggerImpl logr.Logger
}

// Interface guard.
var _ logging.Logger = (*Logger)(nil)
// Interface guards.
var (
_ logging.Logger = (*Logger)(nil)
_ otel.ErrorHandler = (*Logger)(nil)
)

func (l *Logger) Log(_ context.Context, level logging.Level, msg string, keysAndValues ...any) {
l.LoggerImpl.V(int(level)).Info(msg, keysAndValues...)
}

func (l *Logger) Handle(err error) {
l.LoggerImpl.Error(err, "Caught error.")
}
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"os/user"

grpchandlers "github.com/doomshrine/gocosi/grpc/handlers"
grpclog "github.com/doomshrine/gocosi/grpc/log"
grpclog "github.com/doomshrine/gocosi/internal/log"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
"github.com/hellofresh/health-go/v5"
Expand Down

0 comments on commit a830ba0

Please sign in to comment.