Skip to content

Commit

Permalink
Fixes race, moved into global
Browse files Browse the repository at this point in the history
  • Loading branch information
MadVikingGod authored Nov 24, 2021
1 parent 8901b22 commit d467ac5
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 14 deletions.
22 changes: 16 additions & 6 deletions internal/debug/debug.go → internal/global/internal_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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...)
}
18 changes: 14 additions & 4 deletions internal/debug/doc.go → internal/global/internal_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
}
4 changes: 2 additions & 2 deletions internal_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 29 additions & 0 deletions internal_logging_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions sdk/trace/batch_span_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d467ac5

Please sign in to comment.