mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-11-28 08:38:51 +02:00
Fixes race, moved into global
This commit is contained in:
parent
8901b22c4f
commit
d467ac529a
@ -12,11 +12,12 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package debug // import "go.opentelemetry.io/otel/internal/debug"
|
||||
package global // import "go.opentelemetry.io/otel/internal/global"
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/go-logr/stdr"
|
||||
@ -26,28 +27,37 @@ import (
|
||||
//
|
||||
// The default logger uses stdr which is backed by the standard `log.Logger`
|
||||
// interface. This logger will only show messages at the Error Level.
|
||||
var globalLoggger logr.Logger = stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile))
|
||||
var globalLoggerLock = &sync.RWMutex{}
|
||||
var globalLogger logr.Logger = stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile))
|
||||
|
||||
// SetLogger overrides the globalLogger with l.
|
||||
//
|
||||
// To see Info messages use a logger with `l.V(1).Enabled() == true`
|
||||
// To see Debug messages use a logger with `l.V(5).Enabled() == true`
|
||||
func SetLogger(l logr.Logger) {
|
||||
globalLoggger = l
|
||||
globalLoggerLock.Lock()
|
||||
defer globalLoggerLock.Unlock()
|
||||
globalLogger = l
|
||||
}
|
||||
|
||||
// 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{}) {
|
||||
globalLoggger.V(1).Info(msg, keysAndValues...)
|
||||
globalLoggerLock.RLock()
|
||||
defer globalLoggerLock.RUnlock()
|
||||
globalLogger.V(1).Info(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
// Error prints messages about exceptional states of the API or SDK.
|
||||
func Error(err error, msg string, keysAndValues ...interface{}) {
|
||||
globalLoggger.Error(err, msg, keysAndValues...)
|
||||
globalLoggerLock.RLock()
|
||||
defer globalLoggerLock.RUnlock()
|
||||
globalLogger.Error(err, msg, keysAndValues...)
|
||||
}
|
||||
|
||||
// Debug prints messages about all internal changes in the API or SDK.
|
||||
func Debug(msg string, keysAndValues ...interface{}) {
|
||||
globalLoggger.V(5).Info(msg, keysAndValues...)
|
||||
globalLoggerLock.RLock()
|
||||
defer globalLoggerLock.RUnlock()
|
||||
globalLogger.V(5).Info(msg, keysAndValues...)
|
||||
}
|
@ -12,7 +12,17 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/*
|
||||
Package debug provides access to enable internal debugging of opentelemetry.
|
||||
*/
|
||||
package debug // import "go.opentelemetry.io/otel/internal/debug"
|
||||
package global
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/go-logr/stdr"
|
||||
)
|
||||
|
||||
func TestRace(t *testing.T) {
|
||||
go SetLogger(stdr.New(log.New(os.Stderr, "", 0)))
|
||||
go Info("")
|
||||
}
|
@ -17,10 +17,10 @@ package otel // import "go.opentelemetry.io/otel"
|
||||
import (
|
||||
"github.com/go-logr/logr"
|
||||
|
||||
"go.opentelemetry.io/otel/internal/debug"
|
||||
"go.opentelemetry.io/otel/internal/global"
|
||||
)
|
||||
|
||||
// SetLogger configures the logger used internally to opentelemetry.
|
||||
func SetLogger(logger logr.Logger) {
|
||||
debug.SetLogger(logger)
|
||||
global.SetLogger(logger)
|
||||
}
|
||||
|
29
internal_logging_test.go
Normal file
29
internal_logging_test.go
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright The OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package otel_test
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-logr/stdr"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
)
|
||||
|
||||
func ExampleSetLogger() {
|
||||
logger := stdr.New(log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile))
|
||||
otel.SetLogger(logger)
|
||||
}
|
@ -22,7 +22,7 @@ import (
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/internal/debug"
|
||||
"go.opentelemetry.io/otel/internal/global"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
@ -238,7 +238,7 @@ func (bsp *batchSpanProcessor) exportSpans(ctx context.Context) error {
|
||||
}
|
||||
|
||||
if l := len(bsp.batch); l > 0 {
|
||||
debug.Info("exporting spans", "count", len(bsp.batch))
|
||||
global.Info("exporting spans", "count", len(bsp.batch))
|
||||
err := bsp.e.ExportSpans(ctx, bsp.batch)
|
||||
|
||||
// A new batch is always created after exporting, even if the batch failed to be exported.
|
||||
|
Loading…
Reference in New Issue
Block a user