You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by "jxnu-liguobin (via GitHub)" <gi...@apache.org> on 2024/01/04 02:35:17 UTC
[PR] add scala zio cn blog [skywalking-website]
jxnu-liguobin opened a new pull request, #671:
URL: https://github.com/apache/skywalking-website/pull/671
(no comment)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] add scala zio cn blog [skywalking-website]
Posted by "jxnu-liguobin (via GitHub)" <gi...@apache.org>.
jxnu-liguobin commented on code in PR #671:
URL: https://github.com/apache/skywalking-website/pull/671#discussion_r1441253414
##########
content/zh/2024-01-04-skywalking-for-scala-effect-runtime/index.md:
##########
@@ -0,0 +1,137 @@
+---
+title: "SkyWalking 如何支持 ZIO 等 Scala Effect Runtime"
+author: "jxnu-liguobin"
+date: 2024-01-04
+description: "本篇文章演示了如何使用 SkyWalking 收集 ZIO 应用的 Trace,并通过源码分析,讲述怎样为其开发相关插件。"
+tags:
+- Nginx
+---
+
+## 背景介绍
+
+在 Scala 中,纯函数式中主要使用 Fiber,而不是线程,诸如 [Cats-Effect](https://github.com/typelevel/cats-effect)、[ZIO](https://github.com/zio/zio) 等 Effect 框架。
+您可以将 Fiber 视为轻量级线程,它是一种并发模型,由框架本身掌控控制权,从而消除了上下文切换的开销。
+基于这些 Effect 框架开发的 HTTP、gRCP、GraphQL 库而开发的应用,我们一般称为 **纯函数式应用程序**。
+
+我们以 ZIO 为切入点, 演示 SkyWalking Scala 如何支持 Effect 生态。
+
+## ZIO
+
+首先,我们想要实现 Fiber 上下文传递,而不是监控 Fiber 本身。对于一个大型应用来说,可能存在成千上万个 Fiber,监控 Fiber 本身的意义不大。
+
+## 使用
+
+在 Release Notes 下载[插件](https://github.com/bitlap/skywalking-scala/releases/tag/v0.2.0-beta1),放到您的 `skywalking-agent/plugins` 目录即可。
+虽然 Fiber 的 Span 是在活跃时才会创建,但难免会有目前遗漏的场景,所以提供了一个配置 `plugin.ziov2.ignore_fiber_regexes`。
+它将使用正则去匹配 Fiber location,匹配上的 Fiber 将不会创建 Span。
+
+Fiber Span的信息如下:
+
+
+
+下面是我们使用本 ZIO 插件,和一些官方插件(hikaricp、jdbc、pulsar)完成的 Trace:
+
+
+
+## 分析
+在 ZIO 中,Fiber可以有两种方式被调度,它们都是 `zio.Executor` 的子类。当然您也可以使用自己的线程池,这样也需被 ZIO 包装,其实就类似下面的 `blockingExecutor`。
+
+```scala
+abstract class Executor extends ExecutorPlatformSpecific {
+ self =>
+ def submit(runnable: Runnable)(implicit unsafe: Unsafe): Boolean
+}
+```
+
+一种是系统默认线程池 `defaultExecutor`:
+```scala
+private[zio] trait RuntimePlatformSpecific {
+ final val defaultExecutor: Executor =
+ Executor.makeDefault()
+}
+```
+
+另一种是专用于阻塞 IO 的线程池 `blockingExecutor`:
+```scala
+private[zio] trait RuntimePlatformSpecific {
+ final val defaultBlockingExecutor: Executor =
+ Blocking.blockingExecutor
+}
+```
+
+## 默认线程池 `defaultExecutor`
+
+对于 `defaultExecutor`,其本身是很复杂的,但它就是一个 ZIO 的 Fiber 调度(执行)器:
+```scala
+/**
+ * A `ZScheduler` is an `Executor` that is optimized for running ZIO
+ * applications. Inspired by "Making the Tokio Scheduler 10X Faster" by Carl
+ * Lerche. [[https://tokio.rs/blog/2019-10-scheduler]]
+ */
+private final class ZScheduler extends Executor
+```
+
+由于它们都是 `zio.Executor` 的子类,我们只需要对其及其子类进行增强:
+```scala
+final val ENHANCE_CLASS = LogicalMatchOperation.or(
+ HierarchyMatch.byHierarchyMatch("zio.Executor"),
+ MultiClassNameMatch.byMultiClassMatch("zio.Executor")
+)
+```
+
+它们都是线程池,我们只需要在 `zio.Executor` 的 `submit` 方法上进行类似 `ThreadPoolExecutor` 上下文捕获的操作,可以参考 [jdk-threadpool-plugin](https://github.com/apache/skywalking-java/blob/main/apm-sniffer/bootstrap-plugins/jdk-threadpool-plugin/src/main/java/org/apache/skywalking/apm/plugin/ThreadPoolSubmitMethodInterceptor.java)
+
+这里需要注意,因为 Fiber 也是一种 `Runnable`:
+```scala
+private[zio] trait FiberRunnable extends Runnable {
+ def location: Trace
+ def run(depth: Int): Unit
+}
+```
+
+[zio-v2x-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/zio-v2x-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/zio/v2x/define/ZioExecutorInstrumentation.scala)
+
+## 阻塞线程池 `blockingExecutor`
+
+对于 `blockingExecutor`,其实它只是对 Java 线程池进行了一个包装:
+```scala
+object Blocking {
+
+ val blockingExecutor: zio.Executor =
+ zio.Executor.fromThreadPoolExecutor {
+ val corePoolSize = 0
+ val maxPoolSize = Int.MaxValue
+ val keepAliveTime = 60000L
+ val timeUnit = TimeUnit.MILLISECONDS
+ val workQueue = new SynchronousQueue[Runnable]()
+ val threadFactory = new NamedThreadFactory("zio-default-blocking", true)
+
+ val threadPool = new ThreadPoolExecutor(
+ corePoolSize,
+ maxPoolSize,
+ keepAliveTime,
+ timeUnit,
+ workQueue,
+ threadFactory
+ )
+
+ threadPool
+ }
+}
+```
+
+由于其本身是对 `ThreadPoolExecutor` 的封装,所以,当我们已经实现了 `zio.Executor `的增强后,只需要使用官方 `jdk-threadpool-plugin` 插件即可。
+这里我们还想要对代码进行定制修改和复用,所以重新使用 Scala 实现了一个 [executors-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/executors-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/executor/define/ThreadPoolExecutorInstrumentation.scala) 插件。
+
+## 串连 Fiber 上下文
+
+最后,上面谈到过,Fiber 也是一种 `Runnable`,因此还需要对 `zio.internal.FiberRunnable` 进行增强。大致分为两点,其实与 `jdk-threading-plugin` 是一样的。
+1. 每次创建 `zio.internal.FiberRunnable` 实例时,都需要保存 **现场**,即构造函数增强。
+2. 每次运行时创建一个过渡的 Span,将当前线程上下文与之前保存在构造函数中的上下文进行关联。Fiber 可能被不同线程执行,所以这是必须的。
+
+[zio-v2x-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/zio-v2x-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/zio/v2x/define/ZioFiberRuntimeInstrumentation.scala)
+
+## 说明
Review Comment:
Yes, it's here `## 使用`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] add scala zio cn blog [skywalking-website]
Posted by "jxnu-liguobin (via GitHub)" <gi...@apache.org>.
jxnu-liguobin commented on PR #671:
URL: https://github.com/apache/skywalking-website/pull/671#issuecomment-1876844552
> CN x-platform post is done, https://juejin.cn/post/7320048976119513138
thank you
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] add scala zio cn blog [skywalking-website]
Posted by "jxnu-liguobin (via GitHub)" <gi...@apache.org>.
jxnu-liguobin commented on code in PR #671:
URL: https://github.com/apache/skywalking-website/pull/671#discussion_r1441253414
##########
content/zh/2024-01-04-skywalking-for-scala-effect-runtime/index.md:
##########
@@ -0,0 +1,137 @@
+---
+title: "SkyWalking 如何支持 ZIO 等 Scala Effect Runtime"
+author: "jxnu-liguobin"
+date: 2024-01-04
+description: "本篇文章演示了如何使用 SkyWalking 收集 ZIO 应用的 Trace,并通过源码分析,讲述怎样为其开发相关插件。"
+tags:
+- Nginx
+---
+
+## 背景介绍
+
+在 Scala 中,纯函数式中主要使用 Fiber,而不是线程,诸如 [Cats-Effect](https://github.com/typelevel/cats-effect)、[ZIO](https://github.com/zio/zio) 等 Effect 框架。
+您可以将 Fiber 视为轻量级线程,它是一种并发模型,由框架本身掌控控制权,从而消除了上下文切换的开销。
+基于这些 Effect 框架开发的 HTTP、gRCP、GraphQL 库而开发的应用,我们一般称为 **纯函数式应用程序**。
+
+我们以 ZIO 为切入点, 演示 SkyWalking Scala 如何支持 Effect 生态。
+
+## ZIO
+
+首先,我们想要实现 Fiber 上下文传递,而不是监控 Fiber 本身。对于一个大型应用来说,可能存在成千上万个 Fiber,监控 Fiber 本身的意义不大。
+
+## 使用
+
+在 Release Notes 下载[插件](https://github.com/bitlap/skywalking-scala/releases/tag/v0.2.0-beta1),放到您的 `skywalking-agent/plugins` 目录即可。
+虽然 Fiber 的 Span 是在活跃时才会创建,但难免会有目前遗漏的场景,所以提供了一个配置 `plugin.ziov2.ignore_fiber_regexes`。
+它将使用正则去匹配 Fiber location,匹配上的 Fiber 将不会创建 Span。
+
+Fiber Span的信息如下:
+
+
+
+下面是我们使用本 ZIO 插件,和一些官方插件(hikaricp、jdbc、pulsar)完成的 Trace:
+
+
+
+## 分析
+在 ZIO 中,Fiber可以有两种方式被调度,它们都是 `zio.Executor` 的子类。当然您也可以使用自己的线程池,这样也需被 ZIO 包装,其实就类似下面的 `blockingExecutor`。
+
+```scala
+abstract class Executor extends ExecutorPlatformSpecific {
+ self =>
+ def submit(runnable: Runnable)(implicit unsafe: Unsafe): Boolean
+}
+```
+
+一种是系统默认线程池 `defaultExecutor`:
+```scala
+private[zio] trait RuntimePlatformSpecific {
+ final val defaultExecutor: Executor =
+ Executor.makeDefault()
+}
+```
+
+另一种是专用于阻塞 IO 的线程池 `blockingExecutor`:
+```scala
+private[zio] trait RuntimePlatformSpecific {
+ final val defaultBlockingExecutor: Executor =
+ Blocking.blockingExecutor
+}
+```
+
+## 默认线程池 `defaultExecutor`
+
+对于 `defaultExecutor`,其本身是很复杂的,但它就是一个 ZIO 的 Fiber 调度(执行)器:
+```scala
+/**
+ * A `ZScheduler` is an `Executor` that is optimized for running ZIO
+ * applications. Inspired by "Making the Tokio Scheduler 10X Faster" by Carl
+ * Lerche. [[https://tokio.rs/blog/2019-10-scheduler]]
+ */
+private final class ZScheduler extends Executor
+```
+
+由于它们都是 `zio.Executor` 的子类,我们只需要对其及其子类进行增强:
+```scala
+final val ENHANCE_CLASS = LogicalMatchOperation.or(
+ HierarchyMatch.byHierarchyMatch("zio.Executor"),
+ MultiClassNameMatch.byMultiClassMatch("zio.Executor")
+)
+```
+
+它们都是线程池,我们只需要在 `zio.Executor` 的 `submit` 方法上进行类似 `ThreadPoolExecutor` 上下文捕获的操作,可以参考 [jdk-threadpool-plugin](https://github.com/apache/skywalking-java/blob/main/apm-sniffer/bootstrap-plugins/jdk-threadpool-plugin/src/main/java/org/apache/skywalking/apm/plugin/ThreadPoolSubmitMethodInterceptor.java)
+
+这里需要注意,因为 Fiber 也是一种 `Runnable`:
+```scala
+private[zio] trait FiberRunnable extends Runnable {
+ def location: Trace
+ def run(depth: Int): Unit
+}
+```
+
+[zio-v2x-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/zio-v2x-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/zio/v2x/define/ZioExecutorInstrumentation.scala)
+
+## 阻塞线程池 `blockingExecutor`
+
+对于 `blockingExecutor`,其实它只是对 Java 线程池进行了一个包装:
+```scala
+object Blocking {
+
+ val blockingExecutor: zio.Executor =
+ zio.Executor.fromThreadPoolExecutor {
+ val corePoolSize = 0
+ val maxPoolSize = Int.MaxValue
+ val keepAliveTime = 60000L
+ val timeUnit = TimeUnit.MILLISECONDS
+ val workQueue = new SynchronousQueue[Runnable]()
+ val threadFactory = new NamedThreadFactory("zio-default-blocking", true)
+
+ val threadPool = new ThreadPoolExecutor(
+ corePoolSize,
+ maxPoolSize,
+ keepAliveTime,
+ timeUnit,
+ workQueue,
+ threadFactory
+ )
+
+ threadPool
+ }
+}
+```
+
+由于其本身是对 `ThreadPoolExecutor` 的封装,所以,当我们已经实现了 `zio.Executor `的增强后,只需要使用官方 `jdk-threadpool-plugin` 插件即可。
+这里我们还想要对代码进行定制修改和复用,所以重新使用 Scala 实现了一个 [executors-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/executors-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/executor/define/ThreadPoolExecutorInstrumentation.scala) 插件。
+
+## 串连 Fiber 上下文
+
+最后,上面谈到过,Fiber 也是一种 `Runnable`,因此还需要对 `zio.internal.FiberRunnable` 进行增强。大致分为两点,其实与 `jdk-threading-plugin` 是一样的。
+1. 每次创建 `zio.internal.FiberRunnable` 实例时,都需要保存 **现场**,即构造函数增强。
+2. 每次运行时创建一个过渡的 Span,将当前线程上下文与之前保存在构造函数中的上下文进行关联。Fiber 可能被不同线程执行,所以这是必须的。
+
+[zio-v2x-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/zio-v2x-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/zio/v2x/define/ZioFiberRuntimeInstrumentation.scala)
+
+## 说明
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] add scala zio cn blog [skywalking-website]
Posted by "jxnu-liguobin (via GitHub)" <gi...@apache.org>.
jxnu-liguobin commented on PR #671:
URL: https://github.com/apache/skywalking-website/pull/671#issuecomment-1876265159
> Do you plan to submit EN blog as well?
will write it later
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] add scala zio cn blog [skywalking-website]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #671:
URL: https://github.com/apache/skywalking-website/pull/671#issuecomment-1876530009
CN x-platform post is done, https://juejin.cn/post/7320048976119513138
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] add scala zio cn blog [skywalking-website]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on code in PR #671:
URL: https://github.com/apache/skywalking-website/pull/671#discussion_r1441229389
##########
content/zh/2024-01-04-skywalking-for-scala-effect-runtime/index.md:
##########
@@ -0,0 +1,137 @@
+---
+title: "SkyWalking 如何支持 ZIO 等 Scala Effect Runtime"
+author: "jxnu-liguobin"
+date: 2024-01-04
+description: "本篇文章演示了如何使用 SkyWalking 收集 ZIO 应用的 Trace,并通过源码分析,讲述怎样为其开发相关插件。"
+tags:
+- Nginx
+---
+
+## 背景介绍
+
+在 Scala 中,纯函数式中主要使用 Fiber,而不是线程,诸如 [Cats-Effect](https://github.com/typelevel/cats-effect)、[ZIO](https://github.com/zio/zio) 等 Effect 框架。
+您可以将 Fiber 视为轻量级线程,它是一种并发模型,由框架本身掌控控制权,从而消除了上下文切换的开销。
+基于这些 Effect 框架开发的 HTTP、gRCP、GraphQL 库而开发的应用,我们一般称为 **纯函数式应用程序**。
+
+我们以 ZIO 为切入点, 演示 SkyWalking Scala 如何支持 Effect 生态。
+
+## ZIO
+
+首先,我们想要实现 Fiber 上下文传递,而不是监控 Fiber 本身。对于一个大型应用来说,可能存在成千上万个 Fiber,监控 Fiber 本身的意义不大。
+
+## 使用
+
+在 Release Notes 下载[插件](https://github.com/bitlap/skywalking-scala/releases/tag/v0.2.0-beta1),放到您的 `skywalking-agent/plugins` 目录即可。
+虽然 Fiber 的 Span 是在活跃时才会创建,但难免会有目前遗漏的场景,所以提供了一个配置 `plugin.ziov2.ignore_fiber_regexes`。
+它将使用正则去匹配 Fiber location,匹配上的 Fiber 将不会创建 Span。
+
+Fiber Span的信息如下:
+
+
+
+下面是我们使用本 ZIO 插件,和一些官方插件(hikaricp、jdbc、pulsar)完成的 Trace:
+
+
+
+## 分析
+在 ZIO 中,Fiber可以有两种方式被调度,它们都是 `zio.Executor` 的子类。当然您也可以使用自己的线程池,这样也需被 ZIO 包装,其实就类似下面的 `blockingExecutor`。
+
+```scala
+abstract class Executor extends ExecutorPlatformSpecific {
+ self =>
+ def submit(runnable: Runnable)(implicit unsafe: Unsafe): Boolean
+}
+```
+
+一种是系统默认线程池 `defaultExecutor`:
+```scala
+private[zio] trait RuntimePlatformSpecific {
+ final val defaultExecutor: Executor =
+ Executor.makeDefault()
+}
+```
+
+另一种是专用于阻塞 IO 的线程池 `blockingExecutor`:
+```scala
+private[zio] trait RuntimePlatformSpecific {
+ final val defaultBlockingExecutor: Executor =
+ Blocking.blockingExecutor
+}
+```
+
+## 默认线程池 `defaultExecutor`
+
+对于 `defaultExecutor`,其本身是很复杂的,但它就是一个 ZIO 的 Fiber 调度(执行)器:
+```scala
+/**
+ * A `ZScheduler` is an `Executor` that is optimized for running ZIO
+ * applications. Inspired by "Making the Tokio Scheduler 10X Faster" by Carl
+ * Lerche. [[https://tokio.rs/blog/2019-10-scheduler]]
+ */
+private final class ZScheduler extends Executor
+```
+
+由于它们都是 `zio.Executor` 的子类,我们只需要对其及其子类进行增强:
+```scala
+final val ENHANCE_CLASS = LogicalMatchOperation.or(
+ HierarchyMatch.byHierarchyMatch("zio.Executor"),
+ MultiClassNameMatch.byMultiClassMatch("zio.Executor")
+)
+```
+
+它们都是线程池,我们只需要在 `zio.Executor` 的 `submit` 方法上进行类似 `ThreadPoolExecutor` 上下文捕获的操作,可以参考 [jdk-threadpool-plugin](https://github.com/apache/skywalking-java/blob/main/apm-sniffer/bootstrap-plugins/jdk-threadpool-plugin/src/main/java/org/apache/skywalking/apm/plugin/ThreadPoolSubmitMethodInterceptor.java)
+
+这里需要注意,因为 Fiber 也是一种 `Runnable`:
+```scala
+private[zio] trait FiberRunnable extends Runnable {
+ def location: Trace
+ def run(depth: Int): Unit
+}
+```
+
+[zio-v2x-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/zio-v2x-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/zio/v2x/define/ZioExecutorInstrumentation.scala)
+
+## 阻塞线程池 `blockingExecutor`
+
+对于 `blockingExecutor`,其实它只是对 Java 线程池进行了一个包装:
+```scala
+object Blocking {
+
+ val blockingExecutor: zio.Executor =
+ zio.Executor.fromThreadPoolExecutor {
+ val corePoolSize = 0
+ val maxPoolSize = Int.MaxValue
+ val keepAliveTime = 60000L
+ val timeUnit = TimeUnit.MILLISECONDS
+ val workQueue = new SynchronousQueue[Runnable]()
+ val threadFactory = new NamedThreadFactory("zio-default-blocking", true)
+
+ val threadPool = new ThreadPoolExecutor(
+ corePoolSize,
+ maxPoolSize,
+ keepAliveTime,
+ timeUnit,
+ workQueue,
+ threadFactory
+ )
+
+ threadPool
+ }
+}
+```
+
+由于其本身是对 `ThreadPoolExecutor` 的封装,所以,当我们已经实现了 `zio.Executor `的增强后,只需要使用官方 `jdk-threadpool-plugin` 插件即可。
+这里我们还想要对代码进行定制修改和复用,所以重新使用 Scala 实现了一个 [executors-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/executors-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/executor/define/ThreadPoolExecutorInstrumentation.scala) 插件。
+
+## 串连 Fiber 上下文
+
+最后,上面谈到过,Fiber 也是一种 `Runnable`,因此还需要对 `zio.internal.FiberRunnable` 进行增强。大致分为两点,其实与 `jdk-threading-plugin` 是一样的。
+1. 每次创建 `zio.internal.FiberRunnable` 实例时,都需要保存 **现场**,即构造函数增强。
+2. 每次运行时创建一个过渡的 Span,将当前线程上下文与之前保存在构造函数中的上下文进行关联。Fiber 可能被不同线程执行,所以这是必须的。
+
+[zio-v2x-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/zio-v2x-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/zio/v2x/define/ZioFiberRuntimeInstrumentation.scala)
+
+## 说明
Review Comment:
Before the end of the blog, it should be better to put how to download and activate your plugins in the blog as well.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] add scala zio cn blog [skywalking-website]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng commented on PR #671:
URL: https://github.com/apache/skywalking-website/pull/671#issuecomment-1876242841
Do you plan to submit EN blog as well?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] add scala zio cn blog [skywalking-website]
Posted by "wu-sheng (via GitHub)" <gi...@apache.org>.
wu-sheng merged PR #671:
URL: https://github.com/apache/skywalking-website/pull/671
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] add scala zio cn blog [skywalking-website]
Posted by "netlify[bot] (via GitHub)" <gi...@apache.org>.
netlify[bot] commented on PR #671:
URL: https://github.com/apache/skywalking-website/pull/671#issuecomment-1876228706
### <span aria-hidden="true">👷</span> Deploy Preview for *skywalking-website-preview* processing.
| Name | Link |
|:-:|------------------------|
|<span aria-hidden="true">🔨</span> Latest commit | 65b1133515fc2437e242716c00e7159218572354 |
|<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/sites/skywalking-website-preview/deploys/659619653e83110008bc5c80 |
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] add scala zio cn blog [skywalking-website]
Posted by "jxnu-liguobin (via GitHub)" <gi...@apache.org>.
jxnu-liguobin commented on code in PR #671:
URL: https://github.com/apache/skywalking-website/pull/671#discussion_r1441252273
##########
content/zh/2024-01-04-skywalking-for-scala-effect-runtime/index.md:
##########
@@ -0,0 +1,137 @@
+---
+title: "SkyWalking 如何支持 ZIO 等 Scala Effect Runtime"
+author: "jxnu-liguobin"
+date: 2024-01-04
+description: "本篇文章演示了如何使用 SkyWalking 收集 ZIO 应用的 Trace,并通过源码分析,讲述怎样为其开发相关插件。"
+tags:
+- Nginx
+---
+
+## 背景介绍
+
+在 Scala 中,纯函数式中主要使用 Fiber,而不是线程,诸如 [Cats-Effect](https://github.com/typelevel/cats-effect)、[ZIO](https://github.com/zio/zio) 等 Effect 框架。
+您可以将 Fiber 视为轻量级线程,它是一种并发模型,由框架本身掌控控制权,从而消除了上下文切换的开销。
+基于这些 Effect 框架开发的 HTTP、gRCP、GraphQL 库而开发的应用,我们一般称为 **纯函数式应用程序**。
+
+我们以 ZIO 为切入点, 演示 SkyWalking Scala 如何支持 Effect 生态。
+
+## ZIO
+
+首先,我们想要实现 Fiber 上下文传递,而不是监控 Fiber 本身。对于一个大型应用来说,可能存在成千上万个 Fiber,监控 Fiber 本身的意义不大。
+
+## 使用
+
+在 Release Notes 下载[插件](https://github.com/bitlap/skywalking-scala/releases/tag/v0.2.0-beta1),放到您的 `skywalking-agent/plugins` 目录即可。
+虽然 Fiber 的 Span 是在活跃时才会创建,但难免会有目前遗漏的场景,所以提供了一个配置 `plugin.ziov2.ignore_fiber_regexes`。
+它将使用正则去匹配 Fiber location,匹配上的 Fiber 将不会创建 Span。
+
+Fiber Span的信息如下:
+
+
+
+下面是我们使用本 ZIO 插件,和一些官方插件(hikaricp、jdbc、pulsar)完成的 Trace:
+
+
+
+## 分析
+在 ZIO 中,Fiber可以有两种方式被调度,它们都是 `zio.Executor` 的子类。当然您也可以使用自己的线程池,这样也需被 ZIO 包装,其实就类似下面的 `blockingExecutor`。
+
+```scala
+abstract class Executor extends ExecutorPlatformSpecific {
+ self =>
+ def submit(runnable: Runnable)(implicit unsafe: Unsafe): Boolean
+}
+```
+
+一种是系统默认线程池 `defaultExecutor`:
+```scala
+private[zio] trait RuntimePlatformSpecific {
+ final val defaultExecutor: Executor =
+ Executor.makeDefault()
+}
+```
+
+另一种是专用于阻塞 IO 的线程池 `blockingExecutor`:
+```scala
+private[zio] trait RuntimePlatformSpecific {
+ final val defaultBlockingExecutor: Executor =
+ Blocking.blockingExecutor
+}
+```
+
+## 默认线程池 `defaultExecutor`
+
+对于 `defaultExecutor`,其本身是很复杂的,但它就是一个 ZIO 的 Fiber 调度(执行)器:
+```scala
+/**
+ * A `ZScheduler` is an `Executor` that is optimized for running ZIO
+ * applications. Inspired by "Making the Tokio Scheduler 10X Faster" by Carl
+ * Lerche. [[https://tokio.rs/blog/2019-10-scheduler]]
+ */
+private final class ZScheduler extends Executor
+```
+
+由于它们都是 `zio.Executor` 的子类,我们只需要对其及其子类进行增强:
+```scala
+final val ENHANCE_CLASS = LogicalMatchOperation.or(
+ HierarchyMatch.byHierarchyMatch("zio.Executor"),
+ MultiClassNameMatch.byMultiClassMatch("zio.Executor")
+)
+```
+
+它们都是线程池,我们只需要在 `zio.Executor` 的 `submit` 方法上进行类似 `ThreadPoolExecutor` 上下文捕获的操作,可以参考 [jdk-threadpool-plugin](https://github.com/apache/skywalking-java/blob/main/apm-sniffer/bootstrap-plugins/jdk-threadpool-plugin/src/main/java/org/apache/skywalking/apm/plugin/ThreadPoolSubmitMethodInterceptor.java)
+
+这里需要注意,因为 Fiber 也是一种 `Runnable`:
+```scala
+private[zio] trait FiberRunnable extends Runnable {
+ def location: Trace
+ def run(depth: Int): Unit
+}
+```
+
+[zio-v2x-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/zio-v2x-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/zio/v2x/define/ZioExecutorInstrumentation.scala)
+
+## 阻塞线程池 `blockingExecutor`
+
+对于 `blockingExecutor`,其实它只是对 Java 线程池进行了一个包装:
+```scala
+object Blocking {
+
+ val blockingExecutor: zio.Executor =
+ zio.Executor.fromThreadPoolExecutor {
+ val corePoolSize = 0
+ val maxPoolSize = Int.MaxValue
+ val keepAliveTime = 60000L
+ val timeUnit = TimeUnit.MILLISECONDS
+ val workQueue = new SynchronousQueue[Runnable]()
+ val threadFactory = new NamedThreadFactory("zio-default-blocking", true)
+
+ val threadPool = new ThreadPoolExecutor(
+ corePoolSize,
+ maxPoolSize,
+ keepAliveTime,
+ timeUnit,
+ workQueue,
+ threadFactory
+ )
+
+ threadPool
+ }
+}
+```
+
+由于其本身是对 `ThreadPoolExecutor` 的封装,所以,当我们已经实现了 `zio.Executor `的增强后,只需要使用官方 `jdk-threadpool-plugin` 插件即可。
+这里我们还想要对代码进行定制修改和复用,所以重新使用 Scala 实现了一个 [executors-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/executors-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/executor/define/ThreadPoolExecutorInstrumentation.scala) 插件。
+
+## 串连 Fiber 上下文
+
+最后,上面谈到过,Fiber 也是一种 `Runnable`,因此还需要对 `zio.internal.FiberRunnable` 进行增强。大致分为两点,其实与 `jdk-threading-plugin` 是一样的。
+1. 每次创建 `zio.internal.FiberRunnable` 实例时,都需要保存 **现场**,即构造函数增强。
+2. 每次运行时创建一个过渡的 Span,将当前线程上下文与之前保存在构造函数中的上下文进行关联。Fiber 可能被不同线程执行,所以这是必须的。
+
+[zio-v2x-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/zio-v2x-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/zio/v2x/define/ZioFiberRuntimeInstrumentation.scala)
+
+## 说明
Review Comment:
will write it later
##########
content/zh/2024-01-04-skywalking-for-scala-effect-runtime/index.md:
##########
@@ -0,0 +1,137 @@
+---
+title: "SkyWalking 如何支持 ZIO 等 Scala Effect Runtime"
+author: "jxnu-liguobin"
+date: 2024-01-04
+description: "本篇文章演示了如何使用 SkyWalking 收集 ZIO 应用的 Trace,并通过源码分析,讲述怎样为其开发相关插件。"
+tags:
+- Nginx
+---
+
+## 背景介绍
+
+在 Scala 中,纯函数式中主要使用 Fiber,而不是线程,诸如 [Cats-Effect](https://github.com/typelevel/cats-effect)、[ZIO](https://github.com/zio/zio) 等 Effect 框架。
+您可以将 Fiber 视为轻量级线程,它是一种并发模型,由框架本身掌控控制权,从而消除了上下文切换的开销。
+基于这些 Effect 框架开发的 HTTP、gRCP、GraphQL 库而开发的应用,我们一般称为 **纯函数式应用程序**。
+
+我们以 ZIO 为切入点, 演示 SkyWalking Scala 如何支持 Effect 生态。
+
+## ZIO
+
+首先,我们想要实现 Fiber 上下文传递,而不是监控 Fiber 本身。对于一个大型应用来说,可能存在成千上万个 Fiber,监控 Fiber 本身的意义不大。
+
+## 使用
+
+在 Release Notes 下载[插件](https://github.com/bitlap/skywalking-scala/releases/tag/v0.2.0-beta1),放到您的 `skywalking-agent/plugins` 目录即可。
+虽然 Fiber 的 Span 是在活跃时才会创建,但难免会有目前遗漏的场景,所以提供了一个配置 `plugin.ziov2.ignore_fiber_regexes`。
+它将使用正则去匹配 Fiber location,匹配上的 Fiber 将不会创建 Span。
+
+Fiber Span的信息如下:
+
+
+
+下面是我们使用本 ZIO 插件,和一些官方插件(hikaricp、jdbc、pulsar)完成的 Trace:
+
+
+
+## 分析
+在 ZIO 中,Fiber可以有两种方式被调度,它们都是 `zio.Executor` 的子类。当然您也可以使用自己的线程池,这样也需被 ZIO 包装,其实就类似下面的 `blockingExecutor`。
+
+```scala
+abstract class Executor extends ExecutorPlatformSpecific {
+ self =>
+ def submit(runnable: Runnable)(implicit unsafe: Unsafe): Boolean
+}
+```
+
+一种是系统默认线程池 `defaultExecutor`:
+```scala
+private[zio] trait RuntimePlatformSpecific {
+ final val defaultExecutor: Executor =
+ Executor.makeDefault()
+}
+```
+
+另一种是专用于阻塞 IO 的线程池 `blockingExecutor`:
+```scala
+private[zio] trait RuntimePlatformSpecific {
+ final val defaultBlockingExecutor: Executor =
+ Blocking.blockingExecutor
+}
+```
+
+## 默认线程池 `defaultExecutor`
+
+对于 `defaultExecutor`,其本身是很复杂的,但它就是一个 ZIO 的 Fiber 调度(执行)器:
+```scala
+/**
+ * A `ZScheduler` is an `Executor` that is optimized for running ZIO
+ * applications. Inspired by "Making the Tokio Scheduler 10X Faster" by Carl
+ * Lerche. [[https://tokio.rs/blog/2019-10-scheduler]]
+ */
+private final class ZScheduler extends Executor
+```
+
+由于它们都是 `zio.Executor` 的子类,我们只需要对其及其子类进行增强:
+```scala
+final val ENHANCE_CLASS = LogicalMatchOperation.or(
+ HierarchyMatch.byHierarchyMatch("zio.Executor"),
+ MultiClassNameMatch.byMultiClassMatch("zio.Executor")
+)
+```
+
+它们都是线程池,我们只需要在 `zio.Executor` 的 `submit` 方法上进行类似 `ThreadPoolExecutor` 上下文捕获的操作,可以参考 [jdk-threadpool-plugin](https://github.com/apache/skywalking-java/blob/main/apm-sniffer/bootstrap-plugins/jdk-threadpool-plugin/src/main/java/org/apache/skywalking/apm/plugin/ThreadPoolSubmitMethodInterceptor.java)
+
+这里需要注意,因为 Fiber 也是一种 `Runnable`:
+```scala
+private[zio] trait FiberRunnable extends Runnable {
+ def location: Trace
+ def run(depth: Int): Unit
+}
+```
+
+[zio-v2x-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/zio-v2x-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/zio/v2x/define/ZioExecutorInstrumentation.scala)
+
+## 阻塞线程池 `blockingExecutor`
+
+对于 `blockingExecutor`,其实它只是对 Java 线程池进行了一个包装:
+```scala
+object Blocking {
+
+ val blockingExecutor: zio.Executor =
+ zio.Executor.fromThreadPoolExecutor {
+ val corePoolSize = 0
+ val maxPoolSize = Int.MaxValue
+ val keepAliveTime = 60000L
+ val timeUnit = TimeUnit.MILLISECONDS
+ val workQueue = new SynchronousQueue[Runnable]()
+ val threadFactory = new NamedThreadFactory("zio-default-blocking", true)
+
+ val threadPool = new ThreadPoolExecutor(
+ corePoolSize,
+ maxPoolSize,
+ keepAliveTime,
+ timeUnit,
+ workQueue,
+ threadFactory
+ )
+
+ threadPool
+ }
+}
+```
+
+由于其本身是对 `ThreadPoolExecutor` 的封装,所以,当我们已经实现了 `zio.Executor `的增强后,只需要使用官方 `jdk-threadpool-plugin` 插件即可。
+这里我们还想要对代码进行定制修改和复用,所以重新使用 Scala 实现了一个 [executors-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/executors-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/executor/define/ThreadPoolExecutorInstrumentation.scala) 插件。
+
+## 串连 Fiber 上下文
+
+最后,上面谈到过,Fiber 也是一种 `Runnable`,因此还需要对 `zio.internal.FiberRunnable` 进行增强。大致分为两点,其实与 `jdk-threading-plugin` 是一样的。
+1. 每次创建 `zio.internal.FiberRunnable` 实例时,都需要保存 **现场**,即构造函数增强。
+2. 每次运行时创建一个过渡的 Span,将当前线程上下文与之前保存在构造函数中的上下文进行关联。Fiber 可能被不同线程执行,所以这是必须的。
+
+[zio-v2x-plugin](https://github.com/bitlap/skywalking-scala/blob/master/plugins/zio-v2x-plugin/src/main/scala/org/bitlap/skywalking/apm/plugin/zio/v2x/define/ZioFiberRuntimeInstrumentation.scala)
+
+## 说明
Review Comment:
will write it later
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@skywalking.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org