Skip to content

Commit

Permalink
using logger for bsp
Browse files Browse the repository at this point in the history
  • Loading branch information
test authored and test committed Oct 7, 2024
1 parent 41267f8 commit 6002521
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
30 changes: 21 additions & 9 deletions instrumentation/opentelemetry/batchspanprocessor/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,47 @@ package batchspanprocessor // import "github.com/hypertrace/goagent/instrumentat

// Adapted from go.opentelemetry.io/otel/internal/global#internal_logging.go
import (
"log"
"os"

"github.com/go-logr/logr"
"github.com/go-logr/stdr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"sync"
)

// The logger uses stdr which is backed by the standard `log.Logger`
// interface. This logger will only show messages at the Error Level.
var logger logr.Logger = stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile))
var (
logger logr.Logger
once sync.Once
)

// Info prints messages about the general state of the API or SDK.
// This should usually be less then 5 messages a minute.
func Info(msg string, keysAndValues ...interface{}) {
logger.V(4).Info(msg, keysAndValues...)
getLogger().V(4).Info(msg, keysAndValues...)
}

// Error prints messages about exceptional states of the API or SDK.
func Error(err error, msg string, keysAndValues ...interface{}) {
logger.Error(err, msg, keysAndValues...)
getLogger().Error(err, msg, keysAndValues...)
}

// Debug prints messages about all internal changes in the API or SDK.
func Debug(msg string, keysAndValues ...interface{}) {
logger.V(8).Info(msg, keysAndValues...)
getLogger().V(8).Info(msg, keysAndValues...)
}

// Warn prints messages about warnings in the API or SDK.
// Not an error but is likely more important than an informational event.
func Warn(msg string, keysAndValues ...interface{}) {
logger.V(1).Info(msg, keysAndValues...)
getLogger().V(1).Info(msg, keysAndValues...)
}

// since global variables init happens on package imports,
// the zap global logger might not be set by then
// hence doing a lazy initialization
func getLogger() logr.Logger {
once.Do(func() {
logger = zapr.NewLogger(zap.L())
})
return logger
}
2 changes: 2 additions & 0 deletions instrumentation/opentelemetry/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ func InitWithSpanProcessorWrapperAndZap(cfg *config.AgentConfig, wrapper SpanPro
}

if logger != nil {
_ = zap.ReplaceGlobals(logger.With(zap.String("service", "hypertrace")))

// initialize opentelemetry's internal logger
logr := zapr.NewLogger(logger)
otel.SetLogger(logr)
Expand Down

0 comments on commit 6002521

Please sign in to comment.