Skip to content

strc: add ability to override time in Span.Event and Span.End#51

Merged
lzap merged 4 commits intoosbuild:mainfrom
lzap:new-span
Oct 1, 2025
Merged

strc: add ability to override time in Span.Event and Span.End#51
lzap merged 4 commits intoosbuild:mainfrom
lzap:new-span

Conversation

@lzap
Copy link
Copy Markdown
Collaborator

@lzap lzap commented Sep 23, 2025

strc: add ability to override time in Span.Event and Span.End

Span start, event and end time is automatically taken via time.Now()
call but there are some use cases when this needs to be overridden to a
specific time. Use special attributes to do that:

span := strc.Start(ctx, "span name", "started", time.Now())
span.Event("an event", "at", time.Now())
span.End("finished", time.Now())

Also adds TraceID() method to Span to return the trace ID. This will be
usefil in images/ library to collect logs from JSONSeq osbuild monitor and
keep them in a LRU cache.


This small feature is needed for composer worker reading events from JSON Monitor coming out of osbuild so events can be emitted via the strc API with correct timing information. @croissanne @schuellerf thanks for review!

While working on it linter complained a lot so I added configuration, fixed many linter issues mostly related to closing and then this revealed one flaky test which I also fixed.

@lzap lzap requested a review from a team as a code owner September 23, 2025 15:11
@lzap lzap requested review from croissanne and schuellerf and removed request for a team September 23, 2025 15:11
Linter was not configured previously, this commit adds
.golangci.yml file and fixes all the issues reported by it.
The retry test was flaky due to improper synchronization. The return statement
was missing and it could not be done in sync.Once effectively. Replaced with atomic
counter which is more straightforward.
Previously, the send frequency was hardcoded, which made tests slower.
Now, it can be set via a parameter, allowing tests to run faster.
Span start, event and end time is automatically taken via `time.Now()`
call but there are some use cases when this needs to be overridden to a
specific time. Use special attributes to do that:

    span := strc.Start(ctx, "span name", "started", time.Now())
    span.Event("an event", "at", time.Now())
    span.End("finished", time.Now())

Also adds TraceID() method to Span to return the trace ID. This will be
usefil in images/ library to collect logs from JSONSeq osbuild monitor and
keep them in a LRU cache.
Copy link
Copy Markdown
Contributor

@schuellerf schuellerf left a comment

Choose a reason for hiding this comment

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

Thanks!
If there are more "custom attributes", it would be cool to collect them in consts and/or a list in the documentation, like an index, to find faster what's possible.
For now, it's great that there is documentation 🥳

Comment thread internal/example_splunk/main.go
Comment thread pkg/sinit/flush_test.go
Copy link
Copy Markdown
Member

@croissanne croissanne left a comment

Choose a reason for hiding this comment

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

LGTM, but I think an example of the use case can't hurt. Either in the README or the commit message.

Comment thread pkg/strc/README.md
@lzap lzap merged commit c705c32 into osbuild:main Oct 1, 2025
2 checks passed
@lzap lzap deleted the new-span branch October 1, 2025 12:16
@lzap
Copy link
Copy Markdown
Collaborator Author

lzap commented Oct 1, 2025

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants