Skip to content
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

SpanCaptureReporter.collect returns spans from preceding tests #22

Open
garthk opened this issue Jul 16, 2019 · 2 comments
Open

SpanCaptureReporter.collect returns spans from preceding tests #22

garthk opened this issue Jul 16, 2019 · 2 comments

Comments

@garthk
Copy link

garthk commented Jul 16, 2019

I'm finding SpanCaptureReporter too brittle, with tests flaking out when :oc_reporter waits longer than expected and calls report/2 with spans from preceding tests after the current test calls attach/0.

Workaround:

defmodule MyApp.Module do
  use ExUnit.Case, async: false

  test "sends traces" do
    SpanCaptureReporter.attach()
    on_exit(make_ref(), &SpanCaptureReporter.detach/0)
    began_monotonic = :erlang.monotonic_time()
    
    # ... do the work ...

    spans =
    SpanCaptureReporter.collect()
    |> Enum.filter(fn span ->
        {native_time, _} = span.start_time
        native_time >= began_monotonic
    end)

    # ... check the spans ...
@garthk
Copy link
Author

garthk commented Jul 29, 2019

I'm having trouble solving the flip side of this problem, now: despite :application.get_env(:opencensus, :send_interval_ms, 500) returning 1 and a :timer.sleep(100) in collect/0, and running only one test, I still can't guarantee a call to report/2. What else do I need to do to ensure the reporter runs?

Update: I found I was running two reporters because I forgot about the way config merges arrays. And, I'm still having trouble ensuring report/2 gets called. Even asking for delivery and waiting in collect/0 doesn't help:

  @doc "Collect spans from your process inbox."
  @spec collect() :: list(%Span{})
  def collect do
    send(:oc_reporter, :report_spans)
    :timer.sleep(10)
    collect_span_records([]) |> Enum.map(&Span.from/1)
  end

@garthk
Copy link
Author

garthk commented Jul 31, 2019

Setting send_interval_ms: 100 then forcing delivery with send(:oc_reporter, :report_spans); :timer.sleep(10) works. I don’t understand why that works when the same with send_interval_ms: 1 doesn’t. That said, I'll take it working.

garthk pushed a commit that referenced this issue Jul 31, 2019
* Discard spans started before `attach/0` was called

* Trigger `report/2` and wait 1ms before `collect/0`

* Set and advise `send_interval_ms: 100` to reduce unpredictability
  of `report/2` time, which with `send_interval_ms: 1` would sometimes
  wait until after `detach/0`
garthk pushed a commit that referenced this issue Aug 3, 2019
* Discard spans started before `attach/0` was called

* Trigger `report/2` and wait 1ms before `collect/0`

* Set and advise `send_interval_ms: 100` to reduce unpredictability
  of `report/2` time, which with `send_interval_ms: 1` would sometimes
  wait until after `detach/0`
tsloughter added a commit that referenced this issue Aug 9, 2019
…-#22

Fix SpanCaptureReporter test spillage (#22)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant