Skip to content
Merged
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 @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add `WithAttributes` option to set instrumentation scope attributes on the created `log.Logger` in `go.opentelemetry.io/contrib/bridges/otelzap`. (#6962)
- Add `WithAttributes` option to set instrumentation scope attributes on the created `log.Logger` in `go.opentelemetry.io/contrib/bridges/otelslog`. (#6965)
- Add `WithAttributes` option to set instrumentation scope attributes on the created `log.Logger` in `go.opentelemetry.io/contrib/bridges/otellogrus`. (#6966)
- Add `WithAttributes` option to set instrumentation scope attributes on the created `log.Logger` in `go.opentelemetry.io/contrib/bridges/otellogr`. (#6967)

### Changed

Expand Down
20 changes: 17 additions & 3 deletions bridges/otellogr/logsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ import (

"github.com/go-logr/logr"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/global"
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
)

type config struct {
provider log.LoggerProvider
version string
schemaURL string
provider log.LoggerProvider
version string
schemaURL string
attributes []attribute.KeyValue

levelSeverity func(int) log.Severity
}
Expand Down Expand Up @@ -127,6 +129,15 @@ func WithSchemaURL(schemaURL string) Option {
})
}

// WithAttributes returns an [Option] that configures the instrumentation scope
// attributes of the [log.Logger] used by a [LogSink].
func WithAttributes(attributes ...attribute.KeyValue) Option {
return optFunc(func(c config) config {
c.attributes = attributes
return c
})
}

// WithLoggerProvider returns an [Option] that configures [log.LoggerProvider]
// used by a [LogSink] to create its [log.Logger].
//
Expand Down Expand Up @@ -169,6 +180,9 @@ func NewLogSink(name string, options ...Option) *LogSink {
if c.schemaURL != "" {
opts = append(opts, log.WithSchemaURL(c.schemaURL))
}
if c.attributes != nil {
opts = append(opts, log.WithInstrumentationAttributes(c.attributes...))
}

return &LogSink{
name: name,
Expand Down
11 changes: 7 additions & 4 deletions bridges/otellogr/logsink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/embedded"
"go.opentelemetry.io/otel/log/global"
Expand Down Expand Up @@ -85,15 +86,17 @@ func TestNewLogSink(t *testing.T) {
wantScopeRecords: &logtest.ScopeRecords{Name: name},
},
{
name: "with version and schema URL",
name: "with custom options",
options: []Option{
WithVersion("1.0"),
WithSchemaURL("https://example.com"),
WithAttributes(attribute.String("testattr", "testval")),
},
wantScopeRecords: &logtest.ScopeRecords{
Name: name,
Version: "1.0",
SchemaURL: "https://example.com",
Name: name,
Version: "1.0",
SchemaURL: "https://example.com",
Attributes: attribute.NewSet(attribute.String("testattr", "testval")),
},
},
} {
Expand Down