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

sdk/log: Add NoopProcessor #5618

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 51 additions & 0 deletions internal/shared/noop_helper_test.go.tmpl
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
6 changes: 6 additions & 0 deletions log/noop/gen.go
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions log/noop/noop_helper_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
37 changes: 0 additions & 37 deletions log/noop/noop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package noop // import "go.opentelemetry.io/otel/log/noop"

import (
"context"
"reflect"
"testing"

Expand All @@ -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{})
Expand Down
6 changes: 6 additions & 0 deletions metric/noop/gen.go
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions metric/noop/noop_helper_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
30 changes: 0 additions & 30 deletions metric/noop/noop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down
6 changes: 6 additions & 0 deletions sdk/log/gen.go
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions sdk/log/noop.go
Original file line number Diff line number Diff line change
@@ -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
}
51 changes: 51 additions & 0 deletions sdk/log/noop_helper_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
16 changes: 16 additions & 0 deletions sdk/log/noop_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 6 additions & 0 deletions trace/noop/gen.go
Original file line number Diff line number Diff line change
@@ -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
Loading