Skip to content

Commit a2505aa

Browse files
dmathieupellaredXSAM
authored
log/logtest: provide record with their context (#5468)
So the instrumentation tests can check the provided context. See open-telemetry/opentelemetry-go-contrib#5707 --------- Co-authored-by: Robert Pająk <[email protected]> Co-authored-by: Sam Xie <[email protected]>
1 parent 59bbe11 commit a2505aa

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1414
The package contains semantic conventions from the `v1.26.0` version of the OpenTelemetry Semantic Conventions. (#5476)
1515
- The `IsEmpty` method is added to the `Instrument` type in `go.opentelemetry.io/otel/sdk/metric`.
1616
This method is used to check if an `Instrument` instance is a zero-value. (#5431)
17+
- Store and provide the emitted `context.Context` in `ScopeRecords` of `go.opentelemetry.io/otel/sdk/log/logtest`. (#5468)
1718

1819
### Changed
1920

log/logtest/factory_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,31 @@ func TestRecordFactoryMultiple(t *testing.T) {
6767
assertAttributes(t, attrs, record1)
6868
}
6969

70+
func assertRecord(t *testing.T, want log.Record, got log.Record) {
71+
t.Helper()
72+
73+
if !want.Timestamp().Equal(got.Timestamp()) {
74+
t.Errorf("Timestamp value is not equal:\nwant: %v\ngot: %v", want.Timestamp(), got.Timestamp())
75+
}
76+
if !want.ObservedTimestamp().Equal(got.ObservedTimestamp()) {
77+
t.Errorf("ObservedTimestamp value is not equal:\nwant: %v\ngot: %v", want.ObservedTimestamp(), got.ObservedTimestamp())
78+
}
79+
if want.Severity() != got.Severity() {
80+
t.Errorf("Severity value is not equal:\nwant: %v\ngot: %v", want.Severity(), got.Severity())
81+
}
82+
if want.SeverityText() != got.SeverityText() {
83+
t.Errorf("SeverityText value is not equal:\nwant: %v\ngot: %v", want.SeverityText(), got.SeverityText())
84+
}
85+
assertBody(t, want.Body(), got)
86+
87+
var attrs []log.KeyValue
88+
want.WalkAttributes(func(kv log.KeyValue) bool {
89+
attrs = append(attrs, kv)
90+
return true
91+
})
92+
assertAttributes(t, attrs, got)
93+
}
94+
7095
func assertBody(t *testing.T, want log.Value, r log.Record) {
7196
t.Helper()
7297
got := r.Body()

log/logtest/recorder.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,22 @@ type ScopeRecords struct {
6767
// SchemaURL of the telemetry emitted by the scope.
6868
SchemaURL string
6969

70-
// Records are the log records this instrumentation scope recorded.
71-
Records []log.Record
70+
// Records are the log records, and their associated context this
71+
// instrumentation scope recorded.
72+
Records []EmittedRecord
73+
}
74+
75+
// EmittedRecord holds a log record the instrumentation received, alongside its
76+
// context.
77+
type EmittedRecord struct {
78+
log.Record
79+
80+
ctx context.Context
81+
}
82+
83+
// Context provides the context emitted with the record.
84+
func (rwc EmittedRecord) Context() context.Context {
85+
return rwc.ctx
7286
}
7387

7488
// Recorder is a recorder that stores all received log records
@@ -150,11 +164,11 @@ func (l *logger) Enabled(ctx context.Context, record log.Record) bool {
150164
}
151165

152166
// Emit stores the log record.
153-
func (l *logger) Emit(_ context.Context, record log.Record) {
167+
func (l *logger) Emit(ctx context.Context, record log.Record) {
154168
l.mu.Lock()
155169
defer l.mu.Unlock()
156170

157-
l.scopeRecord.Records = append(l.scopeRecord.Records, record)
171+
l.scopeRecord.Records = append(l.scopeRecord.Records, EmittedRecord{record, ctx})
158172
}
159173

160174
// Reset clears the in-memory log records.

log/logtest/recorder_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,30 @@ func TestRecorderEmitAndReset(t *testing.T) {
115115

116116
r1 := log.Record{}
117117
r1.SetSeverity(log.SeverityInfo)
118-
l.Emit(context.Background(), r1)
119-
assert.Equal(t, r.Result()[0].Records, []log.Record{r1})
118+
ctx := context.Background()
119+
120+
l.Emit(ctx, r1)
121+
assert.Equal(t, r.Result()[0].Records, []EmittedRecord{
122+
{r1, ctx},
123+
})
120124

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

124128
r2 := log.Record{}
125129
r2.SetSeverity(log.SeverityError)
126-
nl.Emit(context.Background(), r2)
127-
assert.Equal(t, r.Result()[0].Records, []log.Record{r1})
128-
assert.Equal(t, r.Result()[1].Records, []log.Record{r2})
130+
// We want a non-background context here so it's different from `ctx`.
131+
ctx2, cancel := context.WithCancel(ctx)
132+
defer cancel()
133+
134+
nl.Emit(ctx2, r2)
135+
assert.Len(t, r.Result()[0].Records, 1)
136+
assertRecord(t, r.Result()[0].Records[0].Record, r1)
137+
assert.Equal(t, r.Result()[0].Records[0].Context(), ctx)
138+
139+
assert.Len(t, r.Result()[1].Records, 1)
140+
assertRecord(t, r.Result()[1].Records[0].Record, r2)
141+
assert.Equal(t, r.Result()[1].Records[0].Context(), ctx2)
129142

130143
r.Reset()
131144
assert.Empty(t, r.Result()[0].Records)

0 commit comments

Comments
 (0)