summaryrefslogtreecommitdiffstats
path: root/mocktracer/src/json_recorder.cpp
blob: 5d30d9aeabfab06ef3a86a07b98bd2c198dbafee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <opentracing/mocktracer/json.h>
#include <opentracing/mocktracer/json_recorder.h>

namespace opentracing {
BEGIN_OPENTRACING_ABI_NAMESPACE
namespace mocktracer {
JsonRecorder::JsonRecorder(std::unique_ptr<std::ostream>&& out)
    : out_{std::move(out)} {}

void JsonRecorder::RecordSpan(SpanData&& span_data) noexcept try {
  std::lock_guard<std::mutex> lock_guard{mutex_};
  spans_.emplace_back(std::move(span_data));
} catch (const std::exception&) {
  // Drop span.
}

void JsonRecorder::Close() noexcept try {
  if (out_ == nullptr) {
    return;
  }
  std::lock_guard<std::mutex> lock_guard{mutex_};
  ToJson(*out_, spans_);
  out_->flush();
  spans_.clear();
} catch (const std::exception&) {
  // Ignore errors.
}
}  // namespace mocktracer
END_OPENTRACING_ABI_NAMESPACE
}  // namespace opentracing