Skip to content

Commit

Permalink
Merge branch 'main' into issue-5617
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Boten authored Jul 13, 2022
2 parents b3af22c + a831d51 commit e06cf6b
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 78 deletions.
11 changes: 10 additions & 1 deletion .github/ISSUE_TEMPLATE/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ assignees: ''

Like #4522, but for vX.X.X

**Performed by collector release manager**

- [ ] Prepare core release vX.X.X
- [ ] Tag and release core vX.X.X
- [ ] Prepare contrib release vX.X.X
- [ ] Tag and release contrib vX.X.X
- [ ] Prepare otelcol-releases vX.X.X
- [ ] Release binaries and container images vX.X.X
- [ ] Release the operator vX.X.X

**Performed by operator maintainers**

- [ ] Release the operator vX.X.X

**Performed by helm chart maintainers**

- [ ] Update the opentelemetry-collector helm chart to use vX.X.X docker image
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### 💡 Enhancements 💡

- Add `linux-ppc64le` architecture to cross build tests in CI
- `client`: perform case insensitive lookups in case the requested metadata value isn't found (#5646)
- `loggingexporter`: Decouple `loglevel` field from level of logged messages (#5678)
- Expose `pcommon.NewSliceFromRaw` function (#5679)

### 🧰 Bug fixes 🧰
Expand Down
15 changes: 14 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ package client // import "go.opentelemetry.io/collector/client"
import (
"context"
"net"
"strings"
)

type ctxKey struct{}
Expand Down Expand Up @@ -160,7 +161,19 @@ func NewMetadata(md map[string][]string) Metadata {
func (m Metadata) Get(key string) []string {
vals := m.data[key]
if len(vals) == 0 {
return nil
// we didn't find the key, but perhaps it just has different cases?
for k, v := range m.data {
if strings.EqualFold(key, k) {
vals = v
// we optimize for the next lookup
m.data[key] = v
}
}

// if it's still not found, it's really not here
if len(vals) == 0 {
return nil
}
}

ret := make([]string, len(vals))
Expand Down
1 change: 1 addition & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func TestMetadata(t *testing.T) {
source := map[string][]string{"test-key": {"test-val"}}
md := NewMetadata(source)
assert.Equal(t, []string{"test-val"}, md.Get("test-key"))
assert.Equal(t, []string{"test-val"}, md.Get("test-KEY")) // case insensitive lookup

// test if copy. In regular use, source cannot change
val := md.Get("test-key")
Expand Down
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
6 changes: 3 additions & 3 deletions exporter/loggingexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func createTracesExporter(_ context.Context, set component.ExporterCreateSetting
return nil, err
}

return newTracesExporter(config, exporterLogger, set)
return newTracesExporter(cfg, exporterLogger, set)
}

func createMetricsExporter(_ context.Context, set component.ExporterCreateSettings, config config.Exporter) (component.MetricsExporter, error) {
Expand All @@ -70,7 +70,7 @@ func createMetricsExporter(_ context.Context, set component.ExporterCreateSettin
return nil, err
}

return newMetricsExporter(config, exporterLogger, set)
return newMetricsExporter(cfg, exporterLogger, set)
}

func createLogsExporter(_ context.Context, set component.ExporterCreateSettings, config config.Exporter) (component.LogsExporter, error) {
Expand All @@ -81,7 +81,7 @@ func createLogsExporter(_ context.Context, set component.ExporterCreateSettings,
return nil, err
}

return newLogsExporter(config, exporterLogger, set)
return newLogsExporter(cfg, exporterLogger, set)
}

func createLogger(cfg *Config) (*zap.Logger, error) {
Expand Down
35 changes: 18 additions & 17 deletions exporter/loggingexporter/logging_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"go.uber.org/zap/zapcore"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/exporter/loggingexporter/internal/otlptext"
Expand All @@ -33,6 +32,7 @@ import (
)

type loggingExporter struct {
logLevel zapcore.Level
logger *zap.Logger
logsMarshaler plog.Marshaler
metricsMarshaler pmetric.Marshaler
Expand All @@ -41,50 +41,51 @@ type loggingExporter struct {

func (s *loggingExporter) pushTraces(_ context.Context, td ptrace.Traces) error {
s.logger.Info("TracesExporter", zap.Int("#spans", td.SpanCount()))
if !s.logger.Core().Enabled(zapcore.DebugLevel) {
if s.logLevel != zapcore.DebugLevel {
return nil
}

buf, err := s.tracesMarshaler.MarshalTraces(td)
if err != nil {
return err
}
s.logger.Debug(string(buf))
s.logger.Info(string(buf))
return nil
}

func (s *loggingExporter) pushMetrics(_ context.Context, md pmetric.Metrics) error {
s.logger.Info("MetricsExporter", zap.Int("#metrics", md.MetricCount()))

if !s.logger.Core().Enabled(zapcore.DebugLevel) {
if s.logLevel != zapcore.DebugLevel {
return nil
}

buf, err := s.metricsMarshaler.MarshalMetrics(md)
if err != nil {
return err
}
s.logger.Debug(string(buf))
s.logger.Info(string(buf))
return nil
}

func (s *loggingExporter) pushLogs(_ context.Context, ld plog.Logs) error {
s.logger.Info("LogsExporter", zap.Int("#logs", ld.LogRecordCount()))

if !s.logger.Core().Enabled(zapcore.DebugLevel) {
if s.logLevel != zapcore.DebugLevel {
return nil
}

buf, err := s.logsMarshaler.MarshalLogs(ld)
if err != nil {
return err
}
s.logger.Debug(string(buf))
s.logger.Info(string(buf))
return nil
}

func newLoggingExporter(logger *zap.Logger) *loggingExporter {
func newLoggingExporter(logger *zap.Logger, logLevel zapcore.Level) *loggingExporter {
return &loggingExporter{
logLevel: logLevel,
logger: logger,
logsMarshaler: otlptext.NewTextLogsMarshaler(),
metricsMarshaler: otlptext.NewTextMetricsMarshaler(),
Expand All @@ -94,10 +95,10 @@ func newLoggingExporter(logger *zap.Logger) *loggingExporter {

// newTracesExporter creates an exporter.TracesExporter that just drops the
// received data and logs debugging messages.
func newTracesExporter(config config.Exporter, logger *zap.Logger, set component.ExporterCreateSettings) (component.TracesExporter, error) {
s := newLoggingExporter(logger)
func newTracesExporter(cfg *Config, logger *zap.Logger, set component.ExporterCreateSettings) (component.TracesExporter, error) {
s := newLoggingExporter(logger, cfg.LogLevel)
return exporterhelper.NewTracesExporter(
config,
cfg,
set,
s.pushTraces,
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
Expand All @@ -111,10 +112,10 @@ func newTracesExporter(config config.Exporter, logger *zap.Logger, set component

// newMetricsExporter creates an exporter.MetricsExporter that just drops the
// received data and logs debugging messages.
func newMetricsExporter(config config.Exporter, logger *zap.Logger, set component.ExporterCreateSettings) (component.MetricsExporter, error) {
s := newLoggingExporter(logger)
func newMetricsExporter(cfg *Config, logger *zap.Logger, set component.ExporterCreateSettings) (component.MetricsExporter, error) {
s := newLoggingExporter(logger, cfg.LogLevel)
return exporterhelper.NewMetricsExporter(
config,
cfg,
set,
s.pushMetrics,
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
Expand All @@ -128,10 +129,10 @@ func newMetricsExporter(config config.Exporter, logger *zap.Logger, set componen

// newLogsExporter creates an exporter.LogsExporter that just drops the
// received data and logs debugging messages.
func newLogsExporter(config config.Exporter, logger *zap.Logger, set component.ExporterCreateSettings) (component.LogsExporter, error) {
s := newLoggingExporter(logger)
func newLogsExporter(cfg *Config, logger *zap.Logger, set component.ExporterCreateSettings) (component.LogsExporter, error) {
s := newLoggingExporter(logger, cfg.LogLevel)
return exporterhelper.NewLogsExporter(
config,
cfg,
set,
s.pushLogs,
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
Expand Down
13 changes: 8 additions & 5 deletions exporter/loggingexporter/logging_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ 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"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
)

func TestLoggingTracesExporterNoErrors(t *testing.T) {
lte, err := newTracesExporter(&config.ExporterSettings{}, zap.NewNop(), componenttest.NewNopExporterCreateSettings())
f := NewFactory()
lte, err := newTracesExporter(f.CreateDefaultConfig().(*Config), zap.NewNop(), componenttest.NewNopExporterCreateSettings())
require.NotNil(t, lte)
assert.NoError(t, err)

Expand All @@ -43,7 +44,8 @@ func TestLoggingTracesExporterNoErrors(t *testing.T) {
}

func TestLoggingMetricsExporterNoErrors(t *testing.T) {
lme, err := newMetricsExporter(&config.ExporterSettings{}, zap.NewNop(), componenttest.NewNopExporterCreateSettings())
f := NewFactory()
lme, err := newMetricsExporter(f.CreateDefaultConfig().(*Config), zap.NewNop(), componenttest.NewNopExporterCreateSettings())
require.NotNil(t, lme)
assert.NoError(t, err)

Expand All @@ -57,7 +59,8 @@ func TestLoggingMetricsExporterNoErrors(t *testing.T) {
}

func TestLoggingLogsExporterNoErrors(t *testing.T) {
lle, err := newLogsExporter(&config.ExporterSettings{}, zap.NewNop(), componenttest.NewNopExporterCreateSettings())
f := NewFactory()
lle, err := newLogsExporter(f.CreateDefaultConfig().(*Config), zap.NewNop(), componenttest.NewNopExporterCreateSettings())
require.NotNil(t, lle)
assert.NoError(t, err)

Expand All @@ -68,7 +71,7 @@ func TestLoggingLogsExporterNoErrors(t *testing.T) {
}

func TestLoggingExporterErrors(t *testing.T) {
le := newLoggingExporter(zaptest.NewLogger(t))
le := newLoggingExporter(zaptest.NewLogger(t), zapcore.DebugLevel)
require.NotNil(t, le)

errWant := errors.New("my error")
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())
}
}
Loading

0 comments on commit e06cf6b

Please sign in to comment.