Skip to content

[Proposal] [DoNotMerge] log/logtest: Redesign 2nd edition #6464

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

Open
wants to merge 58 commits into
base: main
Choose a base branch
from

Conversation

pellared
Copy link
Member

@pellared pellared commented Mar 18, 2025

A proposal on how to fix #6341

Supersedes #6342 by addressing:

Changes:

  • Remove RecordFactory
    • The function is not used anywhere.
  • Change the result type of Recorder.Result from []*ScopeRecords to Recording et al.
    • The types represent better the data model. For instance it is a map of scope -> records. Previously each Logger (with e.g. with the same name) was producing a new ScopeRecords element.
    • The new types are easier to compare even if one would not like to use AssertEqual
  • Replace AssertRecordEqual with AssertEqual which accepts options and can work not only with Record but most importantly, whole Recording
  • Add testable example

The changes between the previous approach are inspired by the design of https://pkg.go.dev/go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest. Thanks to it we continue to use the "assert" pattern, but are not impacted by downsides of testify. The main issue is that with testify we cannot force []log.KeyValue to sort slices before comparing (unordered collection equality). This can make the tests too much "white-boxed" and can easy break during refactoring. Moreover, empty and nil slices are seen not equal which in this case is not relevant. Ignoring timestamps would also mean changing the "got". More: #6342 (comment).

Here is an example of how the current tests of log bridges can be improved:

I will split this into a few PRs to make reviews easier.
Still, I think that this PR is necessary in order to validate and review the new design before going further.
I added [Proposal] [DoNotMerge] prefix to make it clear that I do not intend to merge this PR.

@pellared pellared marked this pull request as ready for review March 18, 2025 11:07
@pellared pellared mentioned this pull request Mar 18, 2025
@pellared pellared added area:logs Part of OpenTelemetry logs pkg:API Related to an API package labels Mar 18, 2025
@pellared pellared changed the title log/logtest: Redesign 2nd edition [Proposal] [DoNotMerge] log/logtest: Redesign 2nd edition Mar 18, 2025
}

// AssertOption allows for fine grain control over how AssertEqual operates.
type AssertOption interface {
Copy link
Member Author

@pellared pellared Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SIG meeting: There was a proposal by @MrAlias to add an option to add additional failure message.

It would be better to add each option as a separate PR.

Comment on lines +69 to +75
// IgnoreTimestamp disables checking if timestamps are different.
func IgnoreTimestamp() AssertOption {
return fnOption(func(cfg assertConfig) assertConfig {
cfg.ignoreTimestamp = true
return cfg
})
}
Copy link
Member Author

@pellared pellared Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SIG meeting: There was a proposal by @MrAlias to add some more generic function that would transform the Record (or any type?) before comparing. This would provide more flexibility e.g.

logtest.AssertEqual(t, want, got, logtest.Mutate(func (r logtest.Record) logtest.Record {
   r.Timestamp = time.Time{}
   return r
}))

Maybe https://pkg.go.dev/github.com/google/go-cmp/cmp#Transformer could used to achieve it.

It would be better to add each option as a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pellared
Copy link
Member Author

I created sub-issues for #6341

pellared added a commit that referenced this pull request Apr 12, 2025
Fixes #6486

Towards #6341

`Recorder` in `go.opentelemetry.io/otel/log/logtest` no longer
separately stores records emitted by loggers with the same
instrumentation scope.

Prior-art: #6464
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:logs Part of OpenTelemetry logs pkg:API Related to an API package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make log/logtest more convenient
1 participant