Skip to content

Commit

Permalink
Identify logger with schemaURL in global logger provider (#5375)
Browse files Browse the repository at this point in the history
Fix #5366
  • Loading branch information
MrAlias authored May 18, 2024
1 parent ebd0ade commit 14441ae
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Comparison of unordered maps in `go.opentelemetry.io/otel/log.KeyValue` and `go.opentelemetry.io/otel/log.Value`. (#5306)
- Fix wrong package name of the error message when parsing endpoint URL in `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#5371)
- Split the behavior of `Recorder` in `go.opentelemetry.io/otel/log/logtest` so it behaves as a `LoggerProvider` only. (#5365)
- Identify the `Logger` returned from the global `LoggerProvider` in `go.opentelemetry.io/otel/log/global` with its schama URL. (#5375)

## [1.26.0/0.48.0/0.2.0-alpha] 2024-04-24

Expand Down
8 changes: 6 additions & 2 deletions log/internal/global/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// 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 string }
type instLib struct{ name, version, schemaURL string }

type loggerProvider struct {
embedded.LoggerProvider
Expand All @@ -37,7 +37,11 @@ func (p *loggerProvider) Logger(name string, options ...log.LoggerOption) log.Lo
}

cfg := log.NewLoggerConfig(options...)
key := instLib{name, cfg.InstrumentationVersion()}
key := instLib{
name: name,
version: cfg.InstrumentationVersion(),
schemaURL: cfg.SchemaURL(),
}

if p.loggers == nil {
l := &logger{name: name, options: options}
Expand Down
36 changes: 36 additions & 0 deletions log/internal/global/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,39 @@ func TestDelegation(t *testing.T) {
assert.Equal(t, 1, post.(*testLogger).enabledN, "Enabled not delegated")
}
}

func TestLoggerIdentity(t *testing.T) {
type id struct{ name, ver, url string }

ids := []id{
{"name-a", "version-a", "url-a"},
{"name-a", "version-a", "url-b"},
{"name-a", "version-b", "url-a"},
{"name-a", "version-b", "url-b"},
{"name-b", "version-a", "url-a"},
{"name-b", "version-a", "url-b"},
{"name-b", "version-b", "url-a"},
{"name-b", "version-b", "url-b"},
}

provider := &loggerProvider{}
newLogger := func(i id) log.Logger {
return provider.Logger(
i.name,
log.WithInstrumentationVersion(i.ver),
log.WithSchemaURL(i.url),
)
}

for i, id0 := range ids {
for j, id1 := range ids {
l0, l1 := newLogger(id0), newLogger(id1)

if i == j {
assert.Samef(t, l0, l1, "logger(%v) != logger(%v)", id0, id1)
} else {
assert.NotSamef(t, l0, l1, "logger(%v) == logger(%v)", id0, id1)
}
}
}
}

0 comments on commit 14441ae

Please sign in to comment.