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

Make scope attributes as identifying for Logger #5925

Merged
merged 6 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -29,6 +29,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc` now keeps the metadata already present in the context when `WithHeaders` is used. (#5892)
- `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc` now keeps the metadata already present in the context when `WithHeaders` is used. (#5911)
- `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc` now keeps the metadata already present in the context when `WithHeaders` is used. (#5915)
- Support scope attributes and make them as identifying for `Logger` in `go.opentelemetry.io/otel` and `go.opentelemetry.io/otel/sdk/log`. (#5925)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down
9 changes: 8 additions & 1 deletion log/internal/global/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ import (
"sync"
"sync/atomic"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/embedded"
)

// instLib defines the instrumentation library a logger is created for.
//
// Do not use sdk/instrumentation (API cannot depend on the SDK).
type instLib struct{ name, version, schemaURL string }
type instLib struct {
name string
version string
schemaURL string
attrs attribute.Set
}

type loggerProvider struct {
embedded.LoggerProvider
Expand All @@ -41,6 +47,7 @@ func (p *loggerProvider) Logger(name string, options ...log.LoggerOption) log.Lo
name: name,
version: cfg.InstrumentationVersion(),
schemaURL: cfg.SchemaURL(),
attrs: cfg.InstrumentationAttributes(),
}

if p.loggers == nil {
Expand Down
4 changes: 4 additions & 0 deletions log/internal/global/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/embedded"
"go.opentelemetry.io/otel/log/noop"
Expand Down Expand Up @@ -128,6 +129,9 @@ func TestDelegation(t *testing.T) {
alt := provider.Logger("alt")
assert.NotSame(t, pre0, alt)

alt2 := provider.Logger(preName, log.WithInstrumentationAttributes(attribute.String("k", "v")))
assert.NotSame(t, pre0, alt2)

delegate := &testLoggerProvider{}
provider.setDelegate(delegate)

Expand Down
7 changes: 4 additions & 3 deletions sdk/log/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ func (p *LoggerProvider) Logger(name string, opts ...log.LoggerOption) log.Logge

cfg := log.NewLoggerConfig(opts...)
scope := instrumentation.Scope{
Name: name,
Version: cfg.InstrumentationVersion(),
SchemaURL: cfg.SchemaURL(),
Name: name,
Version: cfg.InstrumentationVersion(),
SchemaURL: cfg.SchemaURL(),
Attributes: cfg.InstrumentationAttributes(),
}

p.loggersMu.Lock()
Expand Down
2 changes: 1 addition & 1 deletion sdk/log/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func TestLoggerProviderLogger(t *testing.T) {

l0, l1, l2 := p.Logger("l0"), p.Logger("l1"), p.Logger("l0", log.WithInstrumentationAttributes(attribute.String("foo", "bar")))
assert.NotSame(t, l0, l1)
assert.Same(t, l0, l2) // TODO (#3368): Change to assert.NotSame.
assert.NotSame(t, l0, l2)
assert.NotSame(t, l1, l2)

l3, l4, l5 := p.Logger("l0"), p.Logger("l1"), p.Logger("l0", log.WithInstrumentationAttributes(attribute.String("foo", "bar")))
Expand Down
Loading