8000 Propagate Tracing Information In Scopes by adamgfraser · Pull Request #6584 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Propagate Tracing Information In Scopes #6584

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

Merged
merged 1 commit into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions core/shared/src/main/scala/zio/Scope.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package zio

import zio.stacktracer.TracingImplicits.disableAutoTrace

import scala.collection.immutable.LongMap

/**
Expand All @@ -30,19 +32,19 @@ trait Scope extends Serializable { self =>
* Adds a finalizer to this scope. The finalizer is guaranteed to be run when
* the scope is closed.
*/
def addFinalizerExit(finalizer: Exit[Any, Any] => UIO[Any]): UIO[Unit]
def addFinalizerExit(finalizer: Exit[Any, Any] => UIO[Any])(implicit trace: ZTraceElement): UIO[Unit]

/**
* Forks a new scope that is a child of this scope. The child scope will
* automatically be closed when this scope is closed.
*/
def fork: UIO[Scope.Closeable]
def fork(implicit trace: ZTraceElement): UIO[Scope.Closeable]

/**
* A simplified version of `addFinalizerWith` when the `finalizer` does not
* depend on the `Exit` value that the scope is closed with.
*/
final def addFinalizer(finalizer: => UIO[Any]): UIO[Unit] =
final def addFinalizer(finalizer: => UIO[Any])(implicit trace: ZTraceElement): UIO[Unit] =
addFinalizerExit(_ => finalizer)

/**
Expand All @@ -63,7 +65,7 @@ object Scope {
* Closes a scope with the specified exit value, running all finalizers that
* have been added to the scope.
*/
def close(exit: => Exit[Any, Any]): UIO[Unit]
def close(exit: => Exit[Any, Any])(implicit trace: ZTraceElement): UIO[Unit]

/**
* Uses the scope by providing it to a `ZIO` workflow that needs a scope,
Expand All @@ -78,13 +80,15 @@ object Scope {
/**
* Accesses a scope in the environment and adds a finalizer to it.
*/
def addFinalizer(finalizer: => UIO[Any]): ZIO[Scope, Nothing, Unit] =
def addFinalizer(finalizer: => UIO[Any])(implicit trace: ZTraceElement): ZIO[Scope, Nothing, Unit] =
ZIO.serviceWithZIO(_.addFinalizer(finalizer))

/**
* Accesses a scope in the environment and adds a finalizer to it.
*/
def addFinalizerExit(finalizer: Exit[Any, Any] => UIO[Any]): ZIO[Scope, Nothing, Unit] =
def addFinalizerExit(finalizer: Exit[Any, Any] => UIO[Any])(implicit
trace: ZTraceElement
): ZIO[Scope, Nothing, Unit] =
ZIO.serviceWithZIO(_.addFinalizerExit(finalizer))

/**
Expand All @@ -102,11 +106,11 @@ object Scope {
*/
val global: Scope.Closeable =
new Scope.Closeable {
def addFinalizerExit(finalizer: Exit[Any, Any] => UIO[Any]): UIO[Unit] =
def addFinalizerExit(finalizer: Exit[Any, Any] => UIO[Any])(implicit trace: ZTraceElement): UIO[Unit] =
ZIO.unit
def close(exit: => Exit[Any, Any]): UIO[Unit] =
def close(exit: => Exit[Any, Any])(implicit trace: ZTraceElement): UIO[Unit] =
ZIO.unit
def fork: UIO[Scope.Closeable] =
def fork(implicit trace: ZTraceElement): UIO[Scope.Closeable] =
make
}

Expand All @@ -115,21 +119,21 @@ object Scope {
* the reverse of the order in which they were added when this scope is
* closed.
*/
def make: UIO[Scope.Closeable] =
def make(implicit trace: ZTraceElement): UIO[Scope.Closeable] =
makeWith(ExecutionStrategy.Sequential)

/**
* Makes a scope. Finalizers added to this scope will be run according to the
* specified `ExecutionStrategy`.
*/
def makeWith(executionStrategy: => ExecutionStrategy): UIO[Scope.Closeable] =
def makeWith(executionStrategy: => ExecutionStrategy)(implicit trace: ZTraceElement): UIO[Scope.Closeable] =
ReleaseMap.make.map { releaseMap =>
new Scope.Closeable { self =>
def addFinalizerExit(finalizer: Exit[Any, Any] => UIO[Any]): UIO[Unit] =
def addFinalizerExit(finalizer: Exit[Any, Any] => UIO[Any])(implicit trace: ZTraceElement): UIO[Unit] =
releaseMap.add(finalizer).unit
def close(exit: => Exit[Any, Any]): UIO[Unit] =
def close(exit: => Exit[Any, Any])(implicit trace: ZTraceElement): UIO[Unit] =
ZIO.suspendSucceed(releaseMap.releaseAll(exit, executionStrategy).unit)
def fork: UIO[Scope.Closeable] =
def fork(implicit trace: ZTraceElement): UIO[Scope.Closeable] =
ZIO.uninterruptible {
for {
scope <- Scope.make
Expand All @@ -144,16 +148,16 @@ object Scope {
* Makes a scope. Finalizers added to this scope will be run in parallel when
* this scope is closed.
*/
def parallel: UIO[Scope.Closeable] =
def parallel(implicit trace: ZTraceElement): UIO[Scope.Closeable] =
makeWith(ExecutionStrategy.Parallel)

final class ExtendPartiallyApplied[R](private val scope: Scope) extends AnyVal {
def apply[E, A](zio: => ZIO[Scope with R, E, A]): ZIO[R, E, A] =
def apply[E, A](zio: => ZIO[Scope with R, E, A])(implicit trace: ZTraceElement): ZIO[R, E, A] =
zio.provideSomeEnvironment[R](_.union[Scope](ZEnvironment(scope)))
}

final class UsePartiallyApplied[R](private val scope: Scope.Closeable) extends AnyVal {
def apply[E, A](zio: => ZIO[Scope with R, E, A]): ZIO[R, E, A] =
def apply[E, A](zio: => ZIO[Scope with R, E, A])(implicit trace: ZTraceElement): ZIO[R, E, A] =
scope.extend[R](zio).onExit(scope.close(_))
}

Expand Down
4 changes: 3 additions & 1 deletion core/shared/src/main/scala/zio/ZLayer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,9 @@ object ZLayer extends ZLayerCompanionVersionSpecific {
val scope: ZLayer[Any, Nothing, Scope.Closeable] =
ZLayer.scopedEnvironment(
ZIO
.acquireReleaseExit(Scope.make)((scope, exit) => scope.close(exit))(ZTraceElement.empty)
.acquireReleaseExit(Scope.make(ZTraceElement.empty))((scope, exit) => scope.close(exit)(ZTraceElement.empty))(
ZTraceElement.empty
)
.map(ZEnvironment(_))(ZTraceElement.empty)
)(ZTraceElement.empty)

Expand Down
0