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

log/logtest: provide record with their context #5468

Merged
merged 11 commits into from
Jun 6, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- The `IsEmpty` method is added to the `Instrument` type in `go.opentelemetry.io/otel/sdk/metric`.
This method is used to check if an `Instrument` instance is a zero-value. (#5431)
- Store and provide the emitted `context.Context` in `ScopeRecords` of `go.opentelemetry.io/otel/sdk/log/logtest`. (#5468)

### Fixed

Expand Down
19 changes: 14 additions & 5 deletions log/logtest/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,17 @@ type ScopeRecords struct {
// SchemaURL of the telemetry emitted by the scope.
SchemaURL string

// Records are the log records this instrumentation scope recorded.
Records []log.Record
// RecordsWithContext are the log records, and their associated context this
// instrumentation scope recorded.
RecordsWithContext []RecordWithContext
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
}

// RecordWithContext holds a log record the instrumentation received, alongside
// its context.
type RecordWithContext struct {
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
log.Record

Context context.Context
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
}

// Recorder is a recorder that stores all received log records
Expand Down Expand Up @@ -150,17 +159,17 @@ func (l *logger) Enabled(ctx context.Context, record log.Record) bool {
}

// Emit stores the log record.
func (l *logger) Emit(_ context.Context, record log.Record) {
func (l *logger) Emit(ctx context.Context, record log.Record) {
l.mu.Lock()
defer l.mu.Unlock()

l.scopeRecord.Records = append(l.scopeRecord.Records, record)
l.scopeRecord.RecordsWithContext = append(l.scopeRecord.RecordsWithContext, RecordWithContext{record, ctx})
}

// Reset clears the in-memory log records.
func (l *logger) Reset() {
l.mu.Lock()
defer l.mu.Unlock()

l.scopeRecord.Records = nil
l.scopeRecord.RecordsWithContext = nil
}
30 changes: 21 additions & 9 deletions log/logtest/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,37 @@ func TestLoggerEnabledFnUnset(t *testing.T) {
func TestRecorderEmitAndReset(t *testing.T) {
r := NewRecorder()
l := r.Logger("test")
assert.Len(t, r.Result()[0].Records, 0)
assert.Len(t, r.Result()[0].RecordsWithContext, 0)

r1 := log.Record{}
r1.SetSeverity(log.SeverityInfo)
l.Emit(context.Background(), r1)
assert.Equal(t, r.Result()[0].Records, []log.Record{r1})
ctx := context.Background()

l.Emit(ctx, r1)
assert.Equal(t, r.Result()[0].RecordsWithContext, []RecordWithContext{
{r1, ctx},
})

nl := r.Logger("test")
assert.Empty(t, r.Result()[1].Records)
assert.Empty(t, r.Result()[1].RecordsWithContext)

r2 := log.Record{}
r2.SetSeverity(log.SeverityError)
nl.Emit(context.Background(), r2)
assert.Equal(t, r.Result()[0].Records, []log.Record{r1})
assert.Equal(t, r.Result()[1].Records, []log.Record{r2})
// We want a non-background context here so it's different from `ctx`.
ctx2, cancel := context.WithCancel(ctx)
defer cancel()

nl.Emit(ctx2, r2)
assert.Equal(t, r.Result()[0].RecordsWithContext, []RecordWithContext{
{r1, ctx},
})
assert.Equal(t, r.Result()[1].RecordsWithContext, []RecordWithContext{
{r2, ctx2},
})

r.Reset()
assert.Empty(t, r.Result()[0].Records)
assert.Empty(t, r.Result()[1].Records)
assert.Empty(t, r.Result()[0].RecordsWithContext)
assert.Empty(t, r.Result()[1].RecordsWithContext)
}

func TestRecorderConcurrentSafe(t *testing.T) {
Expand Down