Skip to content

Commit

Permalink
tracing: wire our logger into otel's logging
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
  • Loading branch information
srenatus committed Dec 13, 2021
1 parent dc3c933 commit e186d6e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/run.go
Expand Up @@ -282,7 +282,7 @@ func initRuntime(ctx context.Context, params runCmdParams, args []string) (*runt
return nil, err
}

rt.SetDistributedTracingErrorHandler()
rt.SetDistributedTracingLogging()

return rt, nil
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -13,6 +13,7 @@ require (
github.com/fsnotify/fsnotify v1.5.1
github.com/ghodss/yaml v1.0.0
github.com/go-ini/ini v1.66.2
github.com/go-logr/logr v1.2.1
github.com/gobwas/glob v0.2.3
github.com/golang/glog v1.0.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand Down
42 changes: 38 additions & 4 deletions internal/distributedtracing/distributedtracing.go
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"io/ioutil"

"github.com/go-logr/logr"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
Expand Down Expand Up @@ -124,10 +125,9 @@ func Init(ctx context.Context, raw []byte, id string) (*otlptrace.Exporter, trac
return traceExporter, options, nil
}

func SetErrorHandler(logger logging.Logger) {
otel.SetErrorHandler(&errorHandler{
logger: logger,
})
func SetupLogging(logger logging.Logger) {
otel.SetErrorHandler(&errorHandler{logger: logger})
otel.SetLogger(logr.New(&sink{logger: logger}))
}

func parseDistributedTracingConfig(raw []byte) (*distributedTracingConfig, error) {
Expand Down Expand Up @@ -252,3 +252,37 @@ type errorHandler struct {
func (e *errorHandler) Handle(err error) {
e.logger.Warn("Distributed tracing: " + err.Error())
}

// NOTE(sr): This adapter code is used to ensure that whatever otel logs, now or
// in the future, will end up in "our" logs, and not go through whatever defaults
// it has set up with its global logger. As such, it's to a full-featured
// implementation fo the logr.LogSink interface, but a rather minimal one. Notably,
// fields are no supported, the initial runtime time info is ignored, and there is
// no support for different verbosity level is "info" logs: they're all printed
// as-is.

type sink struct {
logger logging.Logger
}

func (s *sink) Enabled(level int) bool {
return int(s.logger.GetLevel()) >= level
}

func (*sink) Init(logr.RuntimeInfo) {} // ignored

func (s *sink) Info(_ int, msg string, _ ...interface{}) {
s.logger.Info(msg)
}

func (s *sink) Error(err error, msg string, _ ...interface{}) {
s.logger.WithFields(map[string]interface{}{"err": err}).Error(msg)
}

func (s *sink) WithName(name string) logr.LogSink {
return &sink{s.logger.WithFields(map[string]interface{}{"name": name})}
}

func (s *sink) WithValues(...interface{}) logr.LogSink { // ignored
return s
}
7 changes: 4 additions & 3 deletions runtime/runtime.go
Expand Up @@ -586,9 +586,10 @@ func (rt *Runtime) StartREPL(ctx context.Context) {
repl.Loop(ctx)
}

// SetDistributedTracingErrorHandler configures the distributed tracing's ErrorHandler.
func (rt *Runtime) SetDistributedTracingErrorHandler() {
internal_tracing.SetErrorHandler(rt.logger)
// SetDistributedTracingLogging configures the distributed tracing's ErrorHandler,
// and logger instances.
func (rt *Runtime) SetDistributedTracingLogging() {
internal_tracing.SetupLogging(rt.logger)
}

func (rt *Runtime) checkOPAUpdate(ctx context.Context) *report.DataResponse {
Expand Down
1 change: 1 addition & 0 deletions vendor/modules.txt
Expand Up @@ -60,6 +60,7 @@ github.com/ghodss/yaml
## explicit
github.com/go-ini/ini
# github.com/go-logr/logr v1.2.1
## explicit
github.com/go-logr/logr
github.com/go-logr/logr/funcr
# github.com/go-logr/stdr v1.2.0
Expand Down

0 comments on commit e186d6e

Please sign in to comment.