diff --git a/CHANGELOG.md b/CHANGELOG.md index 76a355194da..679a64d8aee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added - Add macOS ARM64 platform to the compatibility testing suite. (#5577) +- Add `NoopProcessor` in `go.opentelemetry.io/otel/sdk/log`. (#5618) ### Fixed diff --git a/internal/shared/noop_helper_test.go.tmpl b/internal/shared/noop_helper_test.go.tmpl new file mode 100644 index 00000000000..6d619d88bed --- /dev/null +++ b/internal/shared/noop_helper_test.go.tmpl @@ -0,0 +1,51 @@ +// Code created by gotmpl. DO NOT MODIFY. +// source: internal/shared/noop_helper_test.go.tmpl + +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package {{ .packageName }} + +import ( + "context" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) { + return func(t *testing.T) { + for n := 0; n < rType.NumMethod(); n++ { + mType := rType.Method(n) + if !mType.IsExported() { + t.Logf("ignoring unexported %s", mType.Name) + continue + } + m := rVal.MethodByName(mType.Name) + if !m.IsValid() { + t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name) + } + + numIn := mType.Type.NumIn() + if mType.Type.IsVariadic() { + numIn-- + } + args := make([]reflect.Value, numIn) + ctx := context.Background() + for i := range args { + aType := mType.Type.In(i) + if aType.Name() == "Context" { + // Do not panic on a nil context. + args[i] = reflect.ValueOf(ctx) + } else { + args[i] = reflect.New(aType).Elem() + } + } + + assert.NotPanicsf(t, func() { + _ = m.Call(args) + }, "%s.%s", rVal.Type().Name(), mType.Name) + } + } +} diff --git a/log/noop/gen.go b/log/noop/gen.go new file mode 100644 index 00000000000..2f5f41aa370 --- /dev/null +++ b/log/noop/gen.go @@ -0,0 +1,6 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package noop // import "go.opentelemetry.io/otel/log/noop" + +//go:generate gotmpl --body=../../internal/shared/noop_helper_test.go.tmpl "--data={\"packageName\": \"noop\"}" --out=noop_helper_test.go diff --git a/log/noop/noop_helper_test.go b/log/noop/noop_helper_test.go new file mode 100644 index 00000000000..ec22c433f04 --- /dev/null +++ b/log/noop/noop_helper_test.go @@ -0,0 +1,51 @@ +// Code created by gotmpl. DO NOT MODIFY. +// source: internal/shared/noop_helper_test.go.tmpl + +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package noop + +import ( + "context" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) { + return func(t *testing.T) { + for n := 0; n < rType.NumMethod(); n++ { + mType := rType.Method(n) + if !mType.IsExported() { + t.Logf("ignoring unexported %s", mType.Name) + continue + } + m := rVal.MethodByName(mType.Name) + if !m.IsValid() { + t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name) + } + + numIn := mType.Type.NumIn() + if mType.Type.IsVariadic() { + numIn-- + } + args := make([]reflect.Value, numIn) + ctx := context.Background() + for i := range args { + aType := mType.Type.In(i) + if aType.Name() == "Context" { + // Do not panic on a nil context. + args[i] = reflect.ValueOf(ctx) + } else { + args[i] = reflect.New(aType).Elem() + } + } + + assert.NotPanicsf(t, func() { + _ = m.Call(args) + }, "%s.%s", rVal.Type().Name(), mType.Name) + } + } +} diff --git a/log/noop/noop_test.go b/log/noop/noop_test.go index 61a4f54de7b..0d7bdcac4d3 100644 --- a/log/noop/noop_test.go +++ b/log/noop/noop_test.go @@ -4,7 +4,6 @@ package noop // import "go.opentelemetry.io/otel/log/noop" import ( - "context" "reflect" "testing" @@ -26,42 +25,6 @@ func TestImplementationNoPanics(t *testing.T) { )) } -func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) { - return func(t *testing.T) { - for n := 0; n < rType.NumMethod(); n++ { - mType := rType.Method(n) - if !mType.IsExported() { - t.Logf("ignoring unexported %s", mType.Name) - continue - } - m := rVal.MethodByName(mType.Name) - if !m.IsValid() { - t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name) - } - - numIn := mType.Type.NumIn() - if mType.Type.IsVariadic() { - numIn-- - } - args := make([]reflect.Value, numIn) - ctx := context.Background() - for i := range args { - aType := mType.Type.In(i) - if aType.Name() == "Context" { - // Do not panic on a nil context. - args[i] = reflect.ValueOf(ctx) - } else { - args[i] = reflect.New(aType).Elem() - } - } - - assert.NotPanicsf(t, func() { - _ = m.Call(args) - }, "%s.%s", rVal.Type().Name(), mType.Name) - } - } -} - func TestNewTracerProvider(t *testing.T) { provider := NewLoggerProvider() assert.Equal(t, provider, LoggerProvider{}) diff --git a/metric/noop/gen.go b/metric/noop/gen.go new file mode 100644 index 00000000000..eb8e5ac0a3e --- /dev/null +++ b/metric/noop/gen.go @@ -0,0 +1,6 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package noop // import "go.opentelemetry.io/otel/metric/noop" + +//go:generate gotmpl --body=../../internal/shared/noop_helper_test.go.tmpl "--data={\"packageName\": \"noop\"}" --out=noop_helper_test.go diff --git a/metric/noop/noop_helper_test.go b/metric/noop/noop_helper_test.go new file mode 100644 index 00000000000..ec22c433f04 --- /dev/null +++ b/metric/noop/noop_helper_test.go @@ -0,0 +1,51 @@ +// Code created by gotmpl. DO NOT MODIFY. +// source: internal/shared/noop_helper_test.go.tmpl + +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package noop + +import ( + "context" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) { + return func(t *testing.T) { + for n := 0; n < rType.NumMethod(); n++ { + mType := rType.Method(n) + if !mType.IsExported() { + t.Logf("ignoring unexported %s", mType.Name) + continue + } + m := rVal.MethodByName(mType.Name) + if !m.IsValid() { + t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name) + } + + numIn := mType.Type.NumIn() + if mType.Type.IsVariadic() { + numIn-- + } + args := make([]reflect.Value, numIn) + ctx := context.Background() + for i := range args { + aType := mType.Type.In(i) + if aType.Name() == "Context" { + // Do not panic on a nil context. + args[i] = reflect.ValueOf(ctx) + } else { + args[i] = reflect.New(aType).Elem() + } + } + + assert.NotPanicsf(t, func() { + _ = m.Call(args) + }, "%s.%s", rVal.Type().Name(), mType.Name) + } + } +} diff --git a/metric/noop/noop_test.go b/metric/noop/noop_test.go index 4770fb45fd2..3b7149aab5d 100644 --- a/metric/noop/noop_test.go +++ b/metric/noop/noop_test.go @@ -97,36 +97,6 @@ func TestImplementationNoPanics(t *testing.T) { )) } -func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) { - return func(t *testing.T) { - for n := 0; n < rType.NumMethod(); n++ { - mType := rType.Method(n) - if !mType.IsExported() { - t.Logf("ignoring unexported %s", mType.Name) - continue - } - m := rVal.MethodByName(mType.Name) - if !m.IsValid() { - t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name) - } - - numIn := mType.Type.NumIn() - if mType.Type.IsVariadic() { - numIn-- - } - args := make([]reflect.Value, numIn) - for i := range args { - aType := mType.Type.In(i) - args[i] = reflect.New(aType).Elem() - } - - assert.NotPanicsf(t, func() { - _ = m.Call(args) - }, "%s.%s", rVal.Type().Name(), mType.Name) - } - } -} - func TestNewMeterProvider(t *testing.T) { mp := NewMeterProvider() assert.Equal(t, mp, MeterProvider{}) diff --git a/sdk/log/gen.go b/sdk/log/gen.go new file mode 100644 index 00000000000..12ccb2b7a22 --- /dev/null +++ b/sdk/log/gen.go @@ -0,0 +1,6 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package log // import "go.opentelemetry.io/otel/sdk/log" + +//go:generate gotmpl --body=../../internal/shared/noop_helper_test.go.tmpl "--data={\"packageName\": \"log\"}" --out=noop_helper_test.go diff --git a/sdk/log/noop.go b/sdk/log/noop.go new file mode 100644 index 00000000000..d4a3e1ce671 --- /dev/null +++ b/sdk/log/noop.go @@ -0,0 +1,39 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package log // import "go.opentelemetry.io/otel/sdk/log" + +import "context" + +// Compile-time check NoopProcessor implements Processor. +var _ Processor = (*NoopProcessor)(nil) + +var noopProcessorInstance = &NoopProcessor{} + +// NoopProcessor is a [Processor] that does nothing. +type NoopProcessor struct{} + +// NewNoopProcessor returns a [Processor] that does nothing. +func NewNoopProcessor() *NoopProcessor { + return noopProcessorInstance +} + +// Enabled returns true. +func (p *NoopProcessor) Enabled(context.Context, Record) bool { + return true +} + +// OnEmit does nothing and returns nil. +func (p *NoopProcessor) OnEmit(ctx context.Context, r Record) error { + return nil +} + +// Shutdown does nothing and returns nil. +func (p *NoopProcessor) Shutdown(ctx context.Context) error { + return nil +} + +// ForceFlush does nothing and returns nil. +func (panic *NoopProcessor) ForceFlush(ctx context.Context) error { + return nil +} diff --git a/sdk/log/noop_helper_test.go b/sdk/log/noop_helper_test.go new file mode 100644 index 00000000000..ab45ebe27b7 --- /dev/null +++ b/sdk/log/noop_helper_test.go @@ -0,0 +1,51 @@ +// Code created by gotmpl. DO NOT MODIFY. +// source: internal/shared/noop_helper_test.go.tmpl + +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package log + +import ( + "context" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) { + return func(t *testing.T) { + for n := 0; n < rType.NumMethod(); n++ { + mType := rType.Method(n) + if !mType.IsExported() { + t.Logf("ignoring unexported %s", mType.Name) + continue + } + m := rVal.MethodByName(mType.Name) + if !m.IsValid() { + t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name) + } + + numIn := mType.Type.NumIn() + if mType.Type.IsVariadic() { + numIn-- + } + args := make([]reflect.Value, numIn) + ctx := context.Background() + for i := range args { + aType := mType.Type.In(i) + if aType.Name() == "Context" { + // Do not panic on a nil context. + args[i] = reflect.ValueOf(ctx) + } else { + args[i] = reflect.New(aType).Elem() + } + } + + assert.NotPanicsf(t, func() { + _ = m.Call(args) + }, "%s.%s", rVal.Type().Name(), mType.Name) + } + } +} diff --git a/sdk/log/noop_test.go b/sdk/log/noop_test.go new file mode 100644 index 00000000000..97a34b77a68 --- /dev/null +++ b/sdk/log/noop_test.go @@ -0,0 +1,16 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package log + +import ( + "reflect" + "testing" +) + +func TestNoopProcessorNoPanics(t *testing.T) { + assertAllExportedMethodNoPanic( + reflect.ValueOf(NewNoopProcessor()), + reflect.TypeOf((*Processor)(nil)).Elem(), + )(t) +} diff --git a/trace/noop/gen.go b/trace/noop/gen.go new file mode 100644 index 00000000000..8e3278ae846 --- /dev/null +++ b/trace/noop/gen.go @@ -0,0 +1,6 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package noop // import "go.opentelemetry.io/otel/trace/noop" + +//go:generate gotmpl --body=../../internal/shared/noop_helper_test.go.tmpl "--data={\"packageName\": \"noop\"}" --out=noop_helper_test.go diff --git a/trace/noop/noop_helper_test.go b/trace/noop/noop_helper_test.go new file mode 100644 index 00000000000..ec22c433f04 --- /dev/null +++ b/trace/noop/noop_helper_test.go @@ -0,0 +1,51 @@ +// Code created by gotmpl. DO NOT MODIFY. +// source: internal/shared/noop_helper_test.go.tmpl + +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package noop + +import ( + "context" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) { + return func(t *testing.T) { + for n := 0; n < rType.NumMethod(); n++ { + mType := rType.Method(n) + if !mType.IsExported() { + t.Logf("ignoring unexported %s", mType.Name) + continue + } + m := rVal.MethodByName(mType.Name) + if !m.IsValid() { + t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name) + } + + numIn := mType.Type.NumIn() + if mType.Type.IsVariadic() { + numIn-- + } + args := make([]reflect.Value, numIn) + ctx := context.Background() + for i := range args { + aType := mType.Type.In(i) + if aType.Name() == "Context" { + // Do not panic on a nil context. + args[i] = reflect.ValueOf(ctx) + } else { + args[i] = reflect.New(aType).Elem() + } + } + + assert.NotPanicsf(t, func() { + _ = m.Call(args) + }, "%s.%s", rVal.Type().Name(), mType.Name) + } + } +} diff --git a/trace/noop/noop_test.go b/trace/noop/noop_test.go index a8eedb777c0..d05aa2d2706 100644 --- a/trace/noop/noop_test.go +++ b/trace/noop/noop_test.go @@ -30,42 +30,6 @@ func TestImplementationNoPanics(t *testing.T) { )) } -func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) { - return func(t *testing.T) { - for n := 0; n < rType.NumMethod(); n++ { - mType := rType.Method(n) - if !mType.IsExported() { - t.Logf("ignoring unexported %s", mType.Name) - continue - } - m := rVal.MethodByName(mType.Name) - if !m.IsValid() { - t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name) - } - - numIn := mType.Type.NumIn() - if mType.Type.IsVariadic() { - numIn-- - } - args := make([]reflect.Value, numIn) - ctx := context.Background() - for i := range args { - aType := mType.Type.In(i) - if aType.Name() == "Context" { - // Do not panic on a nil context. - args[i] = reflect.ValueOf(ctx) - } else { - args[i] = reflect.New(aType).Elem() - } - } - - assert.NotPanicsf(t, func() { - _ = m.Call(args) - }, "%s.%s", rVal.Type().Name(), mType.Name) - } - } -} - func TestNewTracerProvider(t *testing.T) { tp := NewTracerProvider() assert.Equal(t, tp, TracerProvider{})