mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-02-01 13:07:51 +02:00
Implement the LoggerConfig
and add the LoggerOption
s (#4937)
* Implement the LoggerConfig * Add the LoggerOptions * Add NewLoggerConfig test
This commit is contained in:
parent
6e2bfb69ed
commit
e3e8879eb3
11
log/go.mod
11
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
|
||||
|
||||
|
@ -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=
|
||||
|
@ -52,18 +52,69 @@ type LoggerOption interface {
|
||||
type LoggerConfig struct {
|
||||
// Ensure forward compatibility by explicitly making this not comparable.
|
||||
noCmp [0]func() //nolint: unused // This is indeed used.
|
||||
|
||||
version string
|
||||
schemaURL string
|
||||
attrs attribute.Set
|
||||
}
|
||||
|
||||
// NewLoggerConfig returns a new [LoggerConfig] with all the opts applied.
|
||||
func NewLoggerConfig(opts ...LoggerOption) LoggerConfig { return LoggerConfig{} } // TODO (#4911): implement.
|
||||
// NewLoggerConfig returns a new [LoggerConfig] with all the options applied.
|
||||
func NewLoggerConfig(options ...LoggerOption) LoggerConfig {
|
||||
var c LoggerConfig
|
||||
for _, opt := range options {
|
||||
c = opt.applyLogger(c)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// InstrumentationVersion returns the version of the library providing
|
||||
// instrumentation.
|
||||
func (cfg LoggerConfig) InstrumentationVersion() string { return "" } // TODO (#4911): implement.
|
||||
func (cfg LoggerConfig) InstrumentationVersion() string {
|
||||
return cfg.version
|
||||
}
|
||||
|
||||
// InstrumentationAttributes returns the attributes associated with the library
|
||||
// providing instrumentation.
|
||||
func (cfg LoggerConfig) InstrumentationAttributes() attribute.Set { return attribute.NewSet() } // TODO (#4911): implement.
|
||||
func (cfg LoggerConfig) InstrumentationAttributes() attribute.Set {
|
||||
return cfg.attrs
|
||||
}
|
||||
|
||||
// SchemaURL returns the schema URL of the library providing instrumentation.
|
||||
func (cfg LoggerConfig) SchemaURL() string { return "" } // TODO (#4911): implement.
|
||||
func (cfg LoggerConfig) SchemaURL() string {
|
||||
return cfg.schemaURL
|
||||
}
|
||||
|
||||
type loggerOptionFunc func(LoggerConfig) LoggerConfig
|
||||
|
||||
func (fn loggerOptionFunc) applyLogger(cfg LoggerConfig) LoggerConfig {
|
||||
return fn(cfg)
|
||||
}
|
||||
|
||||
// WithInstrumentationVersion returns a [LoggerOption] that sets the
|
||||
// instrumentation version of a [Logger].
|
||||
func WithInstrumentationVersion(version string) LoggerOption {
|
||||
return loggerOptionFunc(func(config LoggerConfig) LoggerConfig {
|
||||
config.version = version
|
||||
return config
|
||||
})
|
||||
}
|
||||
|
||||
// WithInstrumentationAttributes returns a [LoggerOption] that sets the
|
||||
// instrumentation attributes of a [Logger].
|
||||
//
|
||||
// The passed attributes will be de-duplicated.
|
||||
func WithInstrumentationAttributes(attr ...attribute.KeyValue) LoggerOption {
|
||||
return loggerOptionFunc(func(config LoggerConfig) LoggerConfig {
|
||||
config.attrs = attribute.NewSet(attr...)
|
||||
return config
|
||||
})
|
||||
}
|
||||
|
||||
// WithSchemaURL returns a [LoggerOption] that sets the schema URL for a
|
||||
// [Logger].
|
||||
func WithSchemaURL(schemaURL string) LoggerOption {
|
||||
return loggerOptionFunc(func(config LoggerConfig) LoggerConfig {
|
||||
config.schemaURL = schemaURL
|
||||
return config
|
||||
})
|
||||
}
|
||||
|
43
log/logger_test.go
Normal file
43
log/logger_test.go
Normal file
@ -0,0 +1,43 @@
|
||||
// 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 log_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/log"
|
||||
)
|
||||
|
||||
func TestNewLoggerConfig(t *testing.T) {
|
||||
version := "v1.1.1"
|
||||
schemaURL := "https://opentelemetry.io/schemas/1.0.0"
|
||||
attr := attribute.NewSet(
|
||||
attribute.String("user", "alice"),
|
||||
attribute.Bool("admin", true),
|
||||
)
|
||||
|
||||
c := log.NewLoggerConfig(
|
||||
log.WithInstrumentationVersion(version),
|
||||
log.WithSchemaURL(schemaURL),
|
||||
log.WithInstrumentationAttributes(attr.ToSlice()...),
|
||||
)
|
||||
|
||||
assert.Equal(t, version, c.InstrumentationVersion(), "instrumentation version")
|
||||
assert.Equal(t, schemaURL, c.SchemaURL(), "schema URL")
|
||||
assert.Equal(t, attr, c.InstrumentationAttributes(), "instrumentation attributes")
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user