Skip to content

Add option to disable span activation #1187

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

Closed
wants to merge 6 commits into from

Conversation

mwisnicki
Copy link
Contributor

For now implemented only for Datadog.

Stacked on top of:

Activation binds span to current thread via ThreadLocal so it will become incorrect
when IO/Fiber switches threads but at least it's an improvement and consistent with
root spans.

Ideally there would be a Span option to disable activation entirely.
For now implemented only for Datadog.
) extends Options {
override def withParentKernel(kernel: Kernel): Options =
OptionsImpl(Some(kernel), spanCreationPolicy, spanKind, links)
OptionsImpl(Some(kernel), spanCreationPolicy, spanKind, links, activateSpan)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

why not copy?

Copy link
Member

Choose a reason for hiding this comment

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

oversight? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed to use copy

@@ -175,13 +175,18 @@ object Span {
def spanKind: Span.SpanKind
def links: Chain[Kernel]

/** Whether to activate span in underlying tracing framework. This typically binds span to a ThreadLocal. */
def activateSpan: Boolean
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: shouldActivateSpan, perhaps? The verb form gives me a little ick.

Or spanActivationPolicy, that'd be more consistent with the existing spanCreationPolicy param

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed to shouldActivateSpan, adding enum for binary option seemed overkill

}

val Defaults: Options =
OptionsImpl(None, SpanCreationPolicy.Default, SpanKind.Internal, Chain.empty)
OptionsImpl(None, SpanCreationPolicy.Default, SpanKind.Internal, Chain.empty, true)
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: I suggest using named params

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Resource.make(Sync[F].delay(tracer.activateSpan(span)))(s => Sync[F].delay(s.close()))
Resource
.make(Sync[F].delay(tracer.activateSpan(span)))(s => Sync[F].delay(s.close()))
.whenA(options.activateSpan)
Copy link
Member

Choose a reason for hiding this comment

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

question: any other entrypoints/spans that should have this?

Copy link
Contributor Author

@mwisnicki mwisnicki Jun 25, 2025

Choose a reason for hiding this comment

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

Presumably other backends could use it but I'm not familiar with them and don't know if anyone uses them.

ThreadLocal based span activation on start is error prone so I'm not even sure if it's worth adding this option except it's the default behavior of datadog backend and I want ability to selectively disable it.

With opentelemetry a better solution would be similar to typelevel/otel4s#214

In general there really ought to be some kind of Java-interop method like in otel4s.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Found a better solution to java-interop: #1189

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although disabling automatic span activation is still the right thing to do.
It seems other backends don't activate by default so maybe a better option is to do the opposite of #1186 and just disable it completely.

@@ -1,6 +1,6 @@
import com.typesafe.tools.mima.core._

ThisBuild / tlBaseVersion := "0.3"
ThisBuild / tlBaseVersion := "0.4"
Copy link
Member

Choose a reason for hiding this comment

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

I haven't had a chance to fully look at this PR yet, but is there any way to achieve this without an early-semver-major version bump?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess I would have to provide default noop implementations in the trait?
I.e. def withActivateSpan(b: Boolean) = this. Not sure if this makes sense.

I wish I knew how to test it without waiting for PR checks.

I've tried sbt ++2.13.16 mimaReportBinaryIssues as per https://typelevel.org/cats/contributing.html but it's not working:

[info] natchez-core: mimaPreviousArtifacts is empty, not analyzing binary compatibility.

@mwisnicki
Copy link
Contributor Author

mwisnicki commented Jun 30, 2025

I think it's safer not to do this at all (#1191) and require explicit activation with #1189.

@mwisnicki mwisnicki closed this Jun 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants