Skip to content

Commit

Permalink
add LogMessage and move LogStabilityLevel (#5633)
Browse files Browse the repository at this point in the history
* add LogMessage and move LogStabilityLevel

This change will make the func available for extensions as well.

* add docstring

* fix typo

* fix impi

* apply review feedback
  • Loading branch information
Alex Boten authored Jul 13, 2022
1 parent 6417a3a commit 6266e5b
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 51 deletions.
18 changes: 18 additions & 0 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,24 @@ func (sl StabilityLevel) String() string {
return "undefined"
}

func (sl StabilityLevel) LogMessage() string {
switch sl {
case StabilityLevelUnmaintained:
return "Unmaintained component. Actively looking for contributors. Component will become deprecated after 6 months of remaining unmaintained."
case StabilityLevelDeprecated:
return "Deprecated component. Will be removed in future releases."
case StabilityLevelInDevelopment:
return "In development component. May change in the future."
case StabilityLevelAlpha:
return "Alpha component. May change in the future."
case StabilityLevelBeta:
return "Beta component. May change in the future."
case StabilityLevelStable:
return "Stable component."
}
return "Stability level of component is undefined"
}

// Factory is implemented by all component factories.
//
// This interface cannot be directly implemented. Implementations must
Expand Down
32 changes: 32 additions & 0 deletions service/internal/components/components.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 components // import "go.opentelemetry.io/collector/service/internal/components"

import (
"go.uber.org/zap"

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

// LogStabilityLevel logs the stability level of a component. The log level is set to info for
// undefined, unmaintained, deprecated and in development. The log level is set to debug
// for alpha, beta and stable.
func LogStabilityLevel(logger *zap.Logger, sl component.StabilityLevel) {
if sl >= component.StabilityLevelAlpha {
logger.Debug(sl.LogMessage(), zap.String(ZapStabilityKey, sl.String()))
} else {
logger.Info(sl.LogMessage(), zap.String(ZapStabilityKey, sl.String()))
}
}
56 changes: 56 additions & 0 deletions service/internal/components/components_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 components // import "go.opentelemetry.io/collector/service/internal/components"

import (
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"

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

func TestLogStabilityLevel(t *testing.T) {
tests := []struct {
level zapcore.Level
expectedLogs int
}{
{
level: zapcore.DebugLevel,
expectedLogs: 7,
},
{
level: zapcore.InfoLevel,
expectedLogs: 4,
},
}

for _, tt := range tests {
observed, logs := observer.New(tt.level)
logger := zap.New(observed)
// ensure log levels are set correctly for each stability level
LogStabilityLevel(logger, component.StabilityLevelUndefined)
LogStabilityLevel(logger, component.StabilityLevelUnmaintained)
LogStabilityLevel(logger, component.StabilityLevelDeprecated)
LogStabilityLevel(logger, component.StabilityLevelInDevelopment)
LogStabilityLevel(logger, component.StabilityLevelAlpha)
LogStabilityLevel(logger, component.StabilityLevelBeta)
LogStabilityLevel(logger, component.StabilityLevelStable)
require.Equal(t, tt.expectedLogs, logs.Len())
}
}
21 changes: 3 additions & 18 deletions service/internal/pipelines/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,21 +331,6 @@ func Build(ctx context.Context, set Settings) (*Pipelines, error) {
return exps, nil
}

func logStabilityMessage(logger *zap.Logger, sl component.StabilityLevel) {
switch sl {
case component.StabilityLevelDeprecated:
logger.Info("Component has been deprecated and will be removed in future releases.", zap.String(components.ZapStabilityKey, sl.String()))
case component.StabilityLevelUnmaintained:
logger.Info("Component is unmaintained and actively looking for contributors. This component will become deprecated after 6 months of remaining unmaintained", zap.String(components.ZapStabilityKey, sl.String()))
case component.StabilityLevelInDevelopment:
logger.Info("Component is under development.", zap.String(components.ZapStabilityKey, sl.String()))
case component.StabilityLevelAlpha, component.StabilityLevelBeta, component.StabilityLevelStable:
logger.Debug("Stability level", zap.String(components.ZapStabilityKey, sl.String()))
default:
logger.Debug("Stability level of component undefined", zap.String(components.ZapStabilityKey, sl.String()))
}
}

func buildExporter(
ctx context.Context,
settings component.TelemetrySettings,
Expand All @@ -370,7 +355,7 @@ func buildExporter(
BuildInfo: buildInfo,
}
set.TelemetrySettings.Logger = exporterLogger(settings.Logger, id, pipelineID.Type())
logStabilityMessage(set.TelemetrySettings.Logger, factory.StabilityLevel(pipelineID.Type()))
components.LogStabilityLevel(set.TelemetrySettings.Logger, factory.StabilityLevel(pipelineID.Type()))

exp, err := createExporter(ctx, set, cfg, id, pipelineID, factory)
if err != nil {
Expand Down Expand Up @@ -452,7 +437,7 @@ func buildProcessor(ctx context.Context,
BuildInfo: buildInfo,
}
set.TelemetrySettings.Logger = processorLogger(settings.Logger, id, pipelineID)
logStabilityMessage(set.TelemetrySettings.Logger, factory.StabilityLevel(pipelineID.Type()))
components.LogStabilityLevel(set.TelemetrySettings.Logger, factory.StabilityLevel(pipelineID.Type()))

proc, err := createProcessor(ctx, set, procCfg, id, pipelineID, next, factory)
if err != nil {
Expand Down Expand Up @@ -506,7 +491,7 @@ func buildReceiver(ctx context.Context,
BuildInfo: buildInfo,
}
set.TelemetrySettings.Logger = receiverLogger(settings.Logger, id, pipelineID.Type())
logStabilityMessage(set.TelemetrySettings.Logger, factory.StabilityLevel(pipelineID.Type()))
components.LogStabilityLevel(set.TelemetrySettings.Logger, factory.StabilityLevel(pipelineID.Type()))

recv, err := createReceiver(ctx, set, cfg, id, pipelineID, nexts, factory)
if err != nil {
Expand Down
33 changes: 0 additions & 33 deletions service/internal/pipelines/pipelines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (

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

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
Expand Down Expand Up @@ -348,36 +345,6 @@ func TestFailToStartAndShutdown(t *testing.T) {
}
}

func TestLogStabilityLevle(t *testing.T) {
tests := []struct {
level zapcore.Level
expectedLogs int
}{
{
level: zapcore.DebugLevel,
expectedLogs: 7,
},
{
level: zapcore.InfoLevel,
expectedLogs: 3,
},
}

for _, tt := range tests {
observed, logs := observer.New(tt.level)
logger := zap.New(observed)
// ensure log levels are set correctly for each stability level
logStabilityMessage(logger, component.StabilityLevelUndefined)
logStabilityMessage(logger, component.StabilityLevelUnmaintained)
logStabilityMessage(logger, component.StabilityLevelDeprecated)
logStabilityMessage(logger, component.StabilityLevelInDevelopment)
logStabilityMessage(logger, component.StabilityLevelAlpha)
logStabilityMessage(logger, component.StabilityLevelBeta)
logStabilityMessage(logger, component.StabilityLevelStable)
require.Equal(t, tt.expectedLogs, logs.Len())
}
}

func newBadReceiverFactory() component.ReceiverFactory {
return component.NewReceiverFactory("bf", func() config.Receiver {
return &struct {
Expand Down

0 comments on commit 6266e5b

Please sign in to comment.