用于在 Markdown 中显示分布式跟踪的美人鱼甘特图 (2023)
Mermaid Gantt diagrams for displaying distributed traces in Markdown (2023)

原始链接: https://brycemecum.com/2023/03/31/til-mermaid-tracing/

Mermaid 是一个 JavaScript 库,用于使用 Markdown 表示法生成图表。 支持流程图、时序图、甘特图等多种图表类型。 最近,Mermaid 被用来可视化从 JaegerUI 获得的分布式痕迹,为利用 JaegerUI 图像的传统方法提供了一种有效的替代方案。 在 Mermaid 的语法中,基本的甘特图可以定义如下: ````CSS 甘特图 标题 甘特图 日期格式 YYYY-MM-DD A 部分 任务A1, 2014-01-01, 30d 另一个任务:a1之后,20d 秒内的另一个任务 2014年1月12日 12日 另一个任务:24d ````` 渲染代码时,它将创建一个具有视觉吸引力的交互式甘特图,类似于下面显示的图: ````CSS 甘特图 标题 甘特图 日期格式 YYYY-MM-DD A 部分 任务A1, 2014-01-01, 30d 另一个任务:a1之后,20d 秒内的另一个任务 2014年1月12日 12日 另一个任务:24d ````` ![](https://i.imgur.com/zNXGVpP.png) 这种特定的语法可以更轻松地在 Markdown 文档或其他在线平台中表示 OpenTelemetry 跟踪。 通过在“dateFormat”中指定“x”格式,时间线将基于微秒而不是日历日期显示。 此外,将每个服务分成单独的部分有助于区分它们,同时使用“:crit”或“:done”等标签添加样式并区分跨度。 此外,mtsuhiko 还提供了涉及多个服务的真实分布式跟踪示例,有效演示了美人鱼甘特图如何有效地表示复杂系统。

Graphviz 允许用户在同一直线上使用节点和边创建图形,如以下链接所示:[graphviz 示例](https://viz-js.com/?dot=ZGlncmFwaCB7CiAgYSAtPiBiIC0-IGMgLT4g...)。 然而,对于复杂的图表,单独创建它们可能是有益的。 与 Graphviz 不同,Mermaid 特别专注于创建软件开发图,例如流程图、序列图和类图。 Graphviz 中的图表创建需要更多自定义,包括对箭头和节点形状、线条样式和其他低级格式化元素的详细调整。 相比之下,Mermaid 的设计使用了更专业的术语,例如“基数”和“可见性”,这反映了其专注于为软件工程师提供一个用户友好的环境来可视化其设计。 因此,这些工具的选择很大程度上取决于每个项目的具体要求。 如果您的目标是生成用于文档目的的专业软件工程图,那么 Mermaid 可能是理想的选择,因为它易于使用且具有定制功能。 另一方面,如果您需要更大的灵活性或希望快速可视化非软件工程概念,那么这两种工具都可能有用。
相关文章

原文

TIL: Mermaid Gantt diagrams are great for displaying distributed traces in Markdown

2023/ 03/31

Tagged til jekyll mermaidjs opentelemetry tracing

Today I noticed via a tweet by @mitsuhiko that Mermaid Gantt diagrams are great for displaying distributed trace information like what you’d get from JaegerUI. I’ve been working with OpenTelemetry a fair bit recently and, in recent projects, I’ve been including screenshots of JaegerUI whenever I need to show a distributed trace in my documentation. This generally works fine but I’m happy to have an alternative that’s more at home in Markdown and on the web.

If you’re not familiar with Mermaid, they have great docs.

Gantt Diagrams

Gantt diagrams are typically used for scheduling multiple tasks along a shared timeline. In hindsight, it makes total sense to reach for a Gantt diagrams for diagraming a distributed trace.

The Mermaid syntax for a pretty typical Gantt looks like:

gantt
    title A Gantt Diagram
    dateFormat  YYYY-MM-DD
    section Section
    A task             : a1, 2014-01-01, 30d
    Another task       : after a1  , 20d
    section Another
    Task in sec        : 2014-01-12  , 12d
    another task       : 24d

and, when rendered, looks like:

gantt title A Gantt Diagram dateFormat YYYY-MM-DD section Section A task : a1, 2014-01-01, 30d Another task : after a1 , 20d section Another Task in sec : 2014-01-12 , 12d another task : 24d

A Basic Trace Diagram

The tweet I mentioned previously shows code for a Gantt diagrams of a simple trace:

gantt title Trace Showing Attached and Detached Spans dateFormat x axisFormat %S.%L section Frontend /checkout :crit, 0, 500ms App :300, 180ms POST /api/analytics :done, 450, 70ms GET /assistant/poll :done, 450, 120ms POST /api/analytics :done, 580, 70ms

The code for which is:

gantt
    title Trace Showing Attached and Detached Spans
    dateFormat x
    axisFormat %S.%L

    section Frontend
    /checkout               :crit, 0, 500ms
    App                     :300, 180ms
    POST /api/analytics     :done, 450, 70ms
    GET /assistant/poll     :done, 450, 120ms
    POST /api/analytics     :done, 580, 70ms

To do this, the code above uses just a few features of Mermaid’s Gantt syntax to make the diagram look less like a typical Gantt diagrams and more like an OpenTelemetry trace:

  1. To show everything on a time scale instead of a calender date scale:
    • Specify a dateFormat of x (milliseconds) instead of the usual YYYY-MM-DD
    • Specify an axisFormat of %S.%L which makes the chart use seconds with milliseconds instead of dates
  2. Separate each service into its own section
  3. Visually distinguish spans using tags like :crit, :done which apply styling by default

A More Realistic Example

@mitsuhiko also linked to a Sentry RFC that’s in the works with a more representative example:

gantt title Example Starfish Trace dateFormat x axisFormat %S.%L section Frontend /checkout :crit, 0, 1500ms GET /api/session :150, 170ms POST /api/analytics :190, 70ms GET /api/checkout/state :200, 500ms GET /api/checkout/cart :1100, 140ms App :1300, 180ms POST /api/analytics :done, 1450, 70ms GET /assistant/poll :done, 1450, 120ms POST /api/analytics :done, 1580, 70ms section API Service /api/checkout/state :crit, 240, 440ms cache.get session#58;[redacted] :360, 10ms db.query select from session :370, 20ms db.query select from user :390, 20ms db.query select from checkout :410, 20ms http.request GET http#58;//payments/poll :450, 210ms thread.spawn refresh-checkout-cache :done, 670, 220ms section Payment Service /poll :crit, 470, 180ms db.query select from payment :490, 30ms db.query update payment :530, 60ms

which has the following code:

gantt
    title Example Starfish Trace
    dateFormat x
    axisFormat %S.%L

    section Frontend
    /checkout                                        :crit, 0, 1500ms
    GET /api/session                                 :150, 170ms
    POST /api/analytics                              :190, 70ms
    GET /api/checkout/state                          :200, 500ms
    GET /api/checkout/cart                           :1100, 140ms
    App                                              :1300, 180ms
    POST /api/analytics                              :done, 1450, 70ms
    GET /assistant/poll                              :done, 1450, 120ms
    POST /api/analytics                              :done, 1580, 70ms

    section API Service
    /api/checkout/state                              :crit, 240, 440ms
    cache.get session#58;[redacted]                  :360, 10ms
    db.query select from session                     :370, 20ms
    db.query select from user                        :390, 20ms
    db.query select from checkout                    :410, 20ms
    http.request GET http#58;//payments/poll  :450, 210ms
    thread.spawn refresh-checkout-cache              :done, 670, 220ms

    section Payment Service
    /poll                                            :crit, 470, 180ms
    db.query select from payment                     :490, 30ms
    db.query update payment                          :530, 60ms
联系我们 contact @ memedata.com