1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "fcp/tracing/test_tracing_recorder_impl.h"
16
17 #include <memory>
18 #include <utility>
19
20 #include "fcp/tracing/test_tracing_span_impl.h"
21
22 namespace fcp {
23 namespace tracing_internal {
24
25 using flatbuffers::DetachedBuffer;
26 using flatbuffers::FlatBufferBuilder;
27
EmptyFlatBuffer()28 DetachedBuffer EmptyFlatBuffer() {
29 FlatBufferBuilder fbb;
30 fbb.Finish(fbb.CreateString("ROOT"), "ROOT");
31 return fbb.Release();
32 }
33
TestTracingRecorderImpl(TraceListener * trace_listener)34 TestTracingRecorderImpl::TestTracingRecorderImpl(TraceListener* trace_listener)
35 : trace_listener_(trace_listener),
36 root_span_(
37 std::make_unique<TestTracingSpanImpl>(this, TracingSpanId(0))) {
38 trace_listener_->OnRoot(TracingSpanId(0), EmptyFlatBuffer());
39 }
40
TraceImpl(TracingSpanId id,DetachedBuffer && buf,const TracingTraitsBase & traits)41 void TestTracingRecorderImpl::TraceImpl(TracingSpanId id, DetachedBuffer&& buf,
42 const TracingTraitsBase& traits) {
43 TracingSpanId new_id = TracingSpanId::NextUniqueId();
44 trace_listener_->OnTrace(id, new_id, std::move(buf));
45 }
46
GetRootSpan()47 TracingSpanImpl* TestTracingRecorderImpl::GetRootSpan() {
48 return root_span_.get();
49 }
50
CreateChildSpan(TracingSpanId parent_span_id,flatbuffers::DetachedBuffer && buf,const TracingTraitsBase & traits)51 std::unique_ptr<TracingSpanImpl> TestTracingRecorderImpl::CreateChildSpan(
52 TracingSpanId parent_span_id, flatbuffers::DetachedBuffer&& buf,
53 const TracingTraitsBase& traits) {
54 TracingSpanId new_id = TracingSpanId::NextUniqueId();
55 trace_listener_->OnTrace(parent_span_id, new_id, std::move(buf));
56 // NOTE: shared_from_this() is defined in a base class, so it returns
57 // std::shared_ptr<TracingRecorderImpl> and we have to (safely) cast it here:
58 auto shared_this =
59 std::static_pointer_cast<TestTracingRecorderImpl>(shared_from_this());
60 return std::make_unique<TestTracingSpanImpl>(shared_this, new_id);
61 }
62
63 TestTracingRecorderImpl::~TestTracingRecorderImpl() = default;
64
65 } // namespace tracing_internal
66 } // namespace fcp
67