Skip to content

Add helpers to construct a Trace backed by a Ref#723

Closed
mrdziuban wants to merge 2 commits intotrace4cats:masterfrom
mrdziuban:trace-ref
Closed

Add helpers to construct a Trace backed by a Ref#723
mrdziuban wants to merge 2 commits intotrace4cats:masterfrom
mrdziuban:trace-ref

Conversation

@mrdziuban
Copy link
Copy Markdown
Contributor

Given a Ref[F, Span[F]] we can product an instance of Local[F, Span[F]], which can be used to provide an implementation of Trace[F] via Trace.localSpanInstance[F, F]. This also adds supporting instances of Lift[F, F] and Unlift[F, F].

This was inspired by natchez's Trace.ioTrace method. I was planning to add equivalents for constructing a Trace[IO] from an IOLocal[Span[IO]], but cats-effect isn't currently available in the inject module. I think there are two approaches I could take to get it working, would you be okay with either?

  1. Add cats-effect as a dependency to inject
  2. Define a new cats-effect module that depends on inject and contains these helper methods

@catostrophe
Copy link
Copy Markdown
Member

@mrdziuban Matt, thanks for the try! Unfortunately, this implementation just does not work in a concurrent environment.

Using a global mutable variable (which is what a Ref is) being rewritten within local scopes is unsafe. This sample would show it:

span("parent") {
  printSpanId >>
  List(
    span("child1")(printSpanParentId),
    span("child2")(printSpanParentId),
    span("child3")(printSpanParentId),
    span("child4")(printSpanParentId),
    span("child5")(printSpanParentId)
  ).parSequence_
}

If you run it against IO + Ref, it will spawn some child spans NOT from the parent span, which is incorrect.

If you run it against Kleisli + IO, it will work correctly.

@mrdziuban
Copy link
Copy Markdown
Contributor Author

Ahh okay, thanks for the feedback @catostrophe! Do you think an IOLocal solution like natchez's would work? And if so, would adding a new cats-effect submodule for it work?

@catostrophe
Copy link
Copy Markdown
Member

As for implementation via IOLocal, this would definitely work. Although you need to take into account something before you start implementing it.

We did some work to allow one to use t4c with a compound context (let's name it Ctx), where a Span[F] is just a part of it. natchez impl doesn't care about it. So please don't copy it as is.

I would also like to have it as a separate module since it's CE3-runtime-specific, and we depend only on ce3-kernel.

@catostrophe
Copy link
Copy Markdown
Member

Closing this one. You are welcome to open a PR with implementation via IOLocal.

As I mentioned before, this should be a separate module. I thought again regarding Ctx and I think it does not relate to IOLocal implementation. You can copy and adapt natchez's solution.

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.

2 participants