xref: /aosp_15_r20/external/federated-compute/fcp/tracing/tracing_recorder_impl.h (revision 14675a029014e728ec732f129a32e299b2da0601)
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_RECORDER_IMPL_H_
16 #define FCP_TRACING_TRACING_RECORDER_IMPL_H_
17 
18 #include <memory>
19 
20 #include "fcp/tracing/tracing_span_impl.h"
21 
22 namespace fcp {
23 namespace tracing_internal {
24 
25 class TracingRecorderImpl
26     : public std::enable_shared_from_this<TracingRecorderImpl> {
27  public:
28   TracingRecorderImpl() = default;
29   virtual ~TracingRecorderImpl();
30 
31   // TracingRecorderImpl is neither copyable nor movable.
32   TracingRecorderImpl(const TracingRecorderImpl&) = delete;
33   TracingRecorderImpl& operator=(const TracingRecorderImpl&) = delete;
34 
35   // Trace an event represented by the flatbuffer.
36   virtual void TraceImpl(TracingSpanId span_id,
37                          flatbuffers::DetachedBuffer&& buf,
38                          const TracingTraitsBase& traits) = 0;
39   virtual TracingSpanImpl* GetRootSpan() = 0;
40 
41   // Creates child span.
42   virtual std::unique_ptr<TracingSpanImpl> CreateChildSpan(
43       TracingSpanId parent_span_id, flatbuffers::DetachedBuffer&& buf,
44       const TracingTraitsBase& traits) = 0;
45 
46   // Installs this tracing recorder as global singleton instance.
47   void InstallAsGlobal();
48 
49   // Uninstalls this tracing recorder as global instance. Automatically
50   // called upon destruction.
51   void UninstallAsGlobal();
52 
53   // Installs this tracing recorder as thread local singleton instance.
54   void InstallAsThreadLocal();
55 
56   // Uninstalls this tracing recorder as thread local singleton instance.
57   void UninstallAsThreadLocal();
58 
59   // Gets the current thread local tracing recorder if set; otherwise gets
60   // the current global tracing recorder.
61   static std::shared_ptr<TracingRecorderImpl> GetCurrent();
62   bool is_global_ = false;
63 };
64 
65 }  // namespace tracing_internal
66 }  // namespace fcp
67 
68 #endif  // FCP_TRACING_TRACING_RECORDER_IMPL_H_
69