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 #ifndef FCP_TRACING_TEXT_TRACING_SPAN_IMPL_H_ 16 #define FCP_TRACING_TEXT_TRACING_SPAN_IMPL_H_ 17 18 #include <atomic> 19 #include <optional> 20 #include <string> 21 #include <utility> 22 23 #include "fcp/tracing/text_tracing_recorder_impl.h" 24 #include "fcp/tracing/tracing_span_impl.h" 25 #include "flatbuffers/flatbuffers.h" 26 27 namespace fcp { 28 namespace tracing_internal { 29 30 // Basic tracing span implementation that owns the flatbuf representing the 31 // current span. 32 class TextTracingSpanImpl : public TracingSpanImpl { 33 public: 34 // Constructs a TextTracingSpanImpl for a child span from a serialized flatbuf 35 // and TracingTraitsBase which provides more context about the flatbuf table. TextTracingSpanImpl(std::shared_ptr<TextTracingRecorderImpl> recorder,const flatbuffers::DetachedBuffer & buf,const TracingTraitsBase & traits,TracingSpanId id,TracingSpanId parent_id)36 TextTracingSpanImpl(std::shared_ptr<TextTracingRecorderImpl> recorder, 37 const flatbuffers::DetachedBuffer& buf, 38 const TracingTraitsBase& traits, TracingSpanId id, 39 TracingSpanId parent_id) 40 : id_(id), 41 recorder_shared_ptr_(std::move(recorder)), 42 buf_text_{traits.Name(), traits.TextFormat(buf)} { 43 recorder_ = recorder_shared_ptr_.get(); 44 recorder_->BeginSpan(id, parent_id, buf_text_.name, buf_text_.text_format); 45 } 46 47 // Constructs a TextTracingSpanImpl for root span. TextTracingSpanImpl(TextTracingRecorderImpl * recorder)48 explicit TextTracingSpanImpl(TextTracingRecorderImpl* recorder) 49 : id_(0), recorder_(recorder), buf_text_{} { 50 recorder_->BeginSpan(TracingSpanId(0), TracingSpanId(0), buf_text_.name, 51 buf_text_.text_format); 52 } 53 ~TextTracingSpanImpl() override; 54 55 // Logs an event in the current tracing span. 56 void TraceImpl(flatbuffers::DetachedBuffer&& buf, 57 const TracingTraitsBase& traits) override; 58 59 TracingSpanRef Ref() override; 60 61 private: 62 struct SpanText { 63 std::string name; 64 std::string text_format; 65 }; 66 TracingSpanId id_; 67 TextTracingRecorderImpl* recorder_; 68 69 // For non-root span the following keeps recorder alive, if set, 70 // this holds the same value as recorder_. Since root span is owned directly 71 // by the recorder we can't store shared_ptr for it here to avoid a loop. 72 std::shared_ptr<TextTracingRecorderImpl> recorder_shared_ptr_; 73 74 // Human readable text describing the flatbuffer representing the current 75 // span; empty if this is the root span. 76 SpanText buf_text_; 77 }; 78 79 } // namespace tracing_internal 80 } // namespace fcp 81 82 #endif // FCP_TRACING_TEXT_TRACING_SPAN_IMPL_H_ 83