Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set grpc logger to collector #4501

Merged
merged 11 commits into from
Dec 13, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Remove `pdata.AttributeMap.InitFromMap` (#4429)
- Updated configgrpc `ToDialOptions` to support passing providers to instrumentation library (#4451)
- Make state information propagation non-blocking on the collector (#4460)
- Added support to expose gRPC framework's logs as part of collector logs (#4501)

## 💡 Enhancements 💡

Expand Down
2 changes: 2 additions & 0 deletions service/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
return fmt.Errorf("failed to get logger: %w", err)
}

telemetrylogs.NewColGRPCLogger(col.logger, col.cfgW.cfg.Service.Telemetry.Logs.Level).SetGRPCLogger()

col.logger.Info("Applying configuration...")

col.service, err = newService(&svcSettings{
Expand Down
7 changes: 5 additions & 2 deletions service/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/http"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -102,10 +103,12 @@ func TestCollector_StartAsGoRoutine(t *testing.T) {
func TestCollector_Start(t *testing.T) {
factories, err := defaultcomponents.Components()
require.NoError(t, err)

var once sync.Once
loggingHookCalled := false
hook := func(entry zapcore.Entry) error {
loggingHookCalled = true
once.Do(func() {
loggingHookCalled = true
})
return nil
}

Expand Down
41 changes: 41 additions & 0 deletions service/internal/telemetrylogs/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
package telemetrylogs // import "go.opentelemetry.io/collector/service/internal/telemetrylogs"

import (
"sync"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zapgrpc"
"google.golang.org/grpc/grpclog"

"go.opentelemetry.io/collector/config"
)
Expand Down Expand Up @@ -45,5 +49,42 @@ func NewLogger(cfg config.ServiceTelemetryLogs, options []zap.Option) (*zap.Logg
if err != nil {
return nil, err
}

return logger, nil
}

// SettableGRPCLoggerV2 sets grpc framework's logger with internal logger.
type SettableGRPCLoggerV2 interface {
SetGRPCLogger()
}

type colGRPCLogger struct {
setOnce sync.Once
loggerV2 grpclog.LoggerV2
}

// NewColGRPCLogger constructs a grpclog.LoggerV2 instance cloned from baseLogger with exact configuration.
// The minimum level of gRPC logs is set to WARN should the loglevel of the collector is set to INFO to avoid
// copious logging from grpc framework.
func NewColGRPCLogger(baseLogger *zap.Logger, loglevel zapcore.Level) SettableGRPCLoggerV2 {
logger := baseLogger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
var c zapcore.Core
if loglevel == zap.InfoLevel {
c, _ = zapcore.NewIncreaseLevelCore(core, zap.WarnLevel)
jpkrohling marked this conversation as resolved.
Show resolved Hide resolved
} else {
c = core
}
return c.With([]zapcore.Field{zap.Bool("grpc_log", true)})
}))
return &colGRPCLogger{
loggerV2: zapgrpc.NewLogger(logger),
}
}

// SetGRPCLogger needs to be run before any grpc calls and this implementation requires it to be run
// only once.
func (gl *colGRPCLogger) SetGRPCLogger() {
gl.setOnce.Do(func() {
grpclog.SetLoggerV2(gl.loggerV2)
})
}
94 changes: 94 additions & 0 deletions service/internal/telemetrylogs/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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 telemetrylogs

import (
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"go.opentelemetry.io/collector/config"
)

func TestGRPCLogger(t *testing.T) {
tests := []struct {
name string
cfg config.ServiceTelemetryLogs
infoLogged bool
warnLogged bool
}{
{
"collector_info_level_grpc_log_warn",
config.ServiceTelemetryLogs{
Level: zapcore.InfoLevel,
Encoding: "console",
},
false,
true,
},
{
"collector_debug_level_grpc_log_debug",
config.ServiceTelemetryLogs{
Level: zapcore.DebugLevel,
Encoding: "console",
},
true,
true,
},
{
"collector_warn_level_grpc_log_warn",
config.ServiceTelemetryLogs{
Development: false, // this must set the grpc loggerV2 to loggerV2
Level: zapcore.WarnLevel,
Encoding: "console",
},
false,
true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
obsInfo, obsWarn := false, false
hook := zap.Hooks(func(entry zapcore.Entry) error {
switch entry.Level {
case zapcore.InfoLevel:
obsInfo = true
case zapcore.WarnLevel:
obsWarn = true
}
return nil
})

// create new collector zap logger
logger, err := NewLogger(test.cfg, []zap.Option{hook})
assert.NoError(t, err)

// create colGRPCLogger
glogger := NewColGRPCLogger(logger, test.cfg.Level)
assert.NotNil(t, glogger)

zapGRPCLogger, ok := glogger.(*colGRPCLogger)
assert.True(t, ok)

zapGRPCLogger.loggerV2.Info(test.name)
zapGRPCLogger.loggerV2.Warning(test.name)

assert.Equal(t, obsInfo, test.infoLogged)
assert.Equal(t, obsWarn, test.warnLogged)
})
}
}