diff --git a/log/go.mod b/log/go.mod index 715553c4576..2dfceaf6ed3 100644 --- a/log/go.mod +++ b/log/go.mod @@ -2,7 +2,16 @@ module go.opentelemetry.io/otel/log go 1.20 -require go.opentelemetry.io/otel v1.23.1 +require ( + github.com/stretchr/testify v1.8.4 + go.opentelemetry.io/otel v1.23.1 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) replace go.opentelemetry.io/otel/metric => ../metric diff --git a/log/go.sum b/log/go.sum index 2d8ac4840ed..a6bcd03a15e 100644 --- a/log/go.sum +++ b/log/go.sum @@ -1,5 +1,11 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/log/noop/noop_test.go b/log/noop/noop_test.go new file mode 100644 index 00000000000..079f8f2888f --- /dev/null +++ b/log/noop/noop_test.go @@ -0,0 +1,81 @@ +// 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 noop // import "go.opentelemetry.io/otel/log/noop" + +import ( + "context" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/otel/log" +) + +func TestImplementationNoPanics(t *testing.T) { + // Check that if type has an embedded interface and that interface has + // methods added to it than the No-Op implementation implements them. + t.Run("LoggerProvider", assertAllExportedMethodNoPanic( + reflect.ValueOf(LoggerProvider{}), + reflect.TypeOf((*log.LoggerProvider)(nil)).Elem(), + )) + t.Run("Logger", assertAllExportedMethodNoPanic( + reflect.ValueOf(Logger{}), + reflect.TypeOf((*log.Logger)(nil)).Elem(), + )) +} + +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{}) + logger := provider.Logger("") + assert.Equal(t, logger, Logger{}) +}