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_TRACING_SPAN_IMPL_H_ 16 #define FCP_TRACING_TRACING_SPAN_IMPL_H_ 17 18 #include "fcp/base/monitoring.h" 19 #include "fcp/tracing/tracing_span_ref.h" 20 #include "fcp/tracing/tracing_traits.h" 21 #include "flatbuffers/flatbuffers.h" 22 23 namespace fcp { 24 namespace tracing_internal { 25 26 class TracingSpanImpl { 27 public: 28 // TracingSpanImpl is neither copyable nor movable: 29 TracingSpanImpl(const TracingSpanImpl&) = delete; 30 TracingSpanImpl& operator=(const TracingSpanImpl&) = delete; 31 // Destructor closes the span 32 virtual ~TracingSpanImpl() = default; 33 // Internal logging implementation, to be used by tracing recorder only. 34 virtual void TraceImpl(flatbuffers::DetachedBuffer&& buf, 35 const TracingTraitsBase& traits) = 0; 36 37 // Pushes current tracing span to be the top one on the current thread/fiber: 38 void Push(); 39 // Pops current tracing span 40 void Pop(); 41 // Returns top tracing span for the current thread/fiber 42 static TracingSpanImpl* Top(); 43 44 // Returns reference to this tracing span 45 virtual TracingSpanRef Ref() = 0; 46 47 protected: 48 // TracingSpanImpl can't be directly constructed, use CreateChild(): 49 TracingSpanImpl() = default; 50 51 private: 52 // Optional pointer to the previous tracing span which was a Top() one before 53 // this span was pushed. 54 // This is used so we can restore the top one with Pop(). 55 // NOTE: while this is frequently points to the parent span, it doesn't have 56 // to be the parent span, since a span might be constructed with arbitrary 57 // parent, which doesn't have to be the current Top() one. Example: when a new 58 // fiber is started the parent is on a different stack. 59 TracingSpanImpl* prev_ = nullptr; 60 61 // TODO(team): this assumes 1:1 fiber-thread relationship, use FCB: 62 thread_local static tracing_internal::TracingSpanImpl* top_tracing_span_; 63 }; 64 65 } // namespace tracing_internal 66 } // namespace fcp 67 68 #endif // FCP_TRACING_TRACING_SPAN_IMPL_H_ 69