derailed / ex_ray

An Elixir OpenTracing library based on Otter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect parent/child tracking

smanolloff opened this issue · comments

The library has a fundamental issue with managing parent/child relationships:
it assumes that an opened span is always a child of the previously opened active span.

Example:

A--------.--------.----------.---------.------A
         |        |          |         |
         |        C----------C         |
         |                             |
         B-----------------------------B

"C" will be treated as child of "B", which is incorrect.

Code to reproduce:

defmodule Nested do
  use ExRay, pre: :before_fun, post: :after_fun
  require Logger
  alias ExRay.Span

  @req_id "req_id"

  @trace kind: :a
  def a do
    Task.async(__MODULE__, :b, [1000])
    Task.async(__MODULE__, :c, [100])
    "a"
  end

  @trace kind: :b
  def b(sleep) do
    Process.sleep(sleep)
    "b"
  end

  @trace kind: :c
  def c(sleep) do
    Process.sleep(sleep)
    "c"
  end

  defp before_fun(ctx) do
    ctx.target
    |> Span.open(@req_id)
    |> :otter.tag(:kind, ctx.meta[:kind])
    |> :otter.log(">>> #{ctx.target} with #{ctx.args |> inspect}")
  end

  defp after_fun(ctx, span, res) do
    span
    |> :otter.log("<<< #{ctx.target} returned #{res}")
    |> Span.close(@req_id)
  end
end

Trace JSON excerpt:

{
  "data": [
    {
      "traceID": "4c1d4c1fda73b674",
      "spans": [
        {
          "traceID": "4c1d4c1fda73b674",
          "spanID": "369a646474b2d6af",
          "operationName": "a",
          "startTime": 1586854644105913,
        },
        {
          "traceID": "4c1d4c1fda73b674",
          "spanID": "2b7b7c557815976a",
          "operationName": "b",
          "references": [
            {
              "refType": "CHILD_OF",
              "traceID": "4c1d4c1fda73b674",
              "spanID": "369a646474b2d6af"   // <= "a"
            }
          ],
          "startTime": 1586854644122536,
        },
        {
          "traceID": "4c1d4c1fda73b674",
          "spanID": "f6caec8f60da4727",
          "operationName": "c",
          "references": [
            {
              "refType": "CHILD_OF",
              "traceID": "4c1d4c1fda73b674",
              "spanID": "2b7b7c557815976a"    // <= "b", but should be "a"
            }
          ],
          "startTime": 1586854644122713,
        }
      ],
    }
  ],
}