1 | /** |
2 | * Copyright (c) Glow Contributors. See CONTRIBUTORS file. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | #ifndef GLOW_RUNTIME_TRACEEXPORTER_H |
17 | #define GLOW_RUNTIME_TRACEEXPORTER_H |
18 | |
19 | #include "glow/ExecutionContext/TraceEvents.h" |
20 | |
21 | #include <mutex> |
22 | #include <vector> |
23 | |
24 | namespace glow { |
25 | |
26 | /// Interface for exporting trace events. |
27 | // TraceExporter provides two functionalities |
28 | // 1) shouldTrace() : a method to determine if the runtimeshould trace a |
29 | // particular request. This can be used to windowed tracing on-demand on |
30 | // a production system |
31 | // 2) exportTrace(..) : that passes collected trace events to be exported |
32 | // in the target format and destination. |
33 | // |
34 | // The base implementation delegates to any subclass registered |
35 | // via `registerTraceExporter`. |
36 | |
37 | class TraceExporter { |
38 | public: |
39 | /// Dtor. |
40 | virtual ~TraceExporter() = default; |
41 | |
42 | /// Determine if this request should be traced. |
43 | virtual bool shouldTrace() = 0; |
44 | |
45 | /// Export events from the given TraceContext |
46 | virtual void exportTrace(TraceContext *context) = 0; |
47 | }; |
48 | |
49 | /// Registry of TraceExporters. |
50 | class TraceExporterRegistry final { |
51 | public: |
52 | /// Determine if this request should be traced. |
53 | bool shouldTrace(); |
54 | |
55 | /// Export events from the given TraceContext |
56 | void exportTrace(TraceContext *tcontext); |
57 | |
58 | /// Register a TraceExporter. |
59 | void registerTraceExporter(TraceExporter *exporter); |
60 | |
61 | /// Revoke a TraceExporter. |
62 | void revokeTraceExporter(TraceExporter *exporter); |
63 | |
64 | /// Static singleton TraceExporter. |
65 | static std::shared_ptr<TraceExporterRegistry> getInstance(); |
66 | |
67 | private: |
68 | /// Registered TraceExporters. |
69 | std::vector<TraceExporter *> exporters_; |
70 | std::mutex mutex_; |
71 | }; |
72 | |
73 | } // namespace glow |
74 | |
75 | #endif // GLOW_RUNTIME_TRACEEXPORTER_H |
76 | |