VRS
A file format for sensor data.
Loading...
Searching...
No Matches
TelemetryLogger.h
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
17#pragma once
18
19#include <atomic>
20#include <memory>
21#include <string>
22
23#include <vrs/VrsExport.h>
24
25namespace vrs {
26
27using std::string;
28using std::unique_ptr;
29
31struct VRS_API OperationContext {
32 string operation;
33 string sourceLocation;
34
35 OperationContext() = default;
36 OperationContext(string op, string sourceLoc)
37 : operation{std::move(op)}, sourceLocation{std::move(sourceLoc)} {}
38 OperationContext(const OperationContext& rhs) = default;
39 OperationContext(OperationContext&& rhs) noexcept = default;
40 ~OperationContext() = default;
41
42 OperationContext& operator=(const OperationContext& rhs) = default;
43 OperationContext& operator=(OperationContext&& rhs) noexcept = default;
44
45 bool operator<(const OperationContext& rhs) const {
46 auto tie = [](const OperationContext& oc) { return std::tie(oc.operation, oc.sourceLocation); };
47 return tie(*this) < tie(rhs);
48 }
49};
50
52struct VRS_API LogEvent {
53 LogEvent() = default;
54 LogEvent(string type, OperationContext opContext, string message, string serverReply)
55 : type{std::move(type)},
56 operationContext{std::move(opContext)},
57 message{std::move(message)},
58 serverReply{std::move(serverReply)} {}
59 LogEvent(const LogEvent& rhs) = default;
60 LogEvent(LogEvent&& rhs) noexcept = default;
61 ~LogEvent() = default;
62
63 LogEvent& operator=(const LogEvent& rhs) = default;
64 LogEvent& operator=(LogEvent&& rhs) noexcept = default;
65
66 string type;
67 OperationContext operationContext;
68 string message;
69 string serverReply;
70};
71
78struct VRS_API TrafficEvent {
79 bool isSuccess = false;
80 bool uploadNotDownload = false;
81 int64_t transferStartTime = 0; // start time
82 int64_t totalDurationMs = -1; // overall request duration, including retries
83 int64_t transferDurationMs = -1; // last network transfer duration (last attempt)
84 size_t transferOffset = 0; // offset to read from
85 size_t transferRequestSize = 0; // bytes
86 size_t transferSize = 0; // bytes
87 size_t retryCount = 0;
88 size_t errorCount = 0;
89 size_t error429Count = 0;
90 long httpStatus = -1;
91 string serverName;
92
93 TrafficEvent& setIsSuccess(bool success) {
94 isSuccess = success;
95 return *this;
96 }
97 TrafficEvent& setIsUpload() {
98 uploadNotDownload = true;
99 return *this;
100 }
101 TrafficEvent& setIsDownload() {
102 uploadNotDownload = false;
103 return *this;
104 }
105 TrafficEvent& setAttemptStartTime();
106 TrafficEvent& setTotalDurationMs(int64_t durationMs) {
107 totalDurationMs = durationMs;
108 return *this;
109 }
110 TrafficEvent& setTransferDurationMs(int64_t aTransferDurationMs) {
111 transferDurationMs = aTransferDurationMs;
112 return *this;
113 }
114 TrafficEvent& setTransferOffset(size_t offset) {
115 transferOffset = offset;
116 return *this;
117 }
118 TrafficEvent& setTransferRequestSize(size_t aTransferRequestSize) {
119 transferRequestSize = aTransferRequestSize;
120 return *this;
121 }
122 TrafficEvent& setTransferSize(size_t aTransferSize) {
123 transferSize = aTransferSize;
124 return *this;
125 }
126 TrafficEvent& setRetryCount(size_t aRetryCount) {
127 retryCount = aRetryCount;
128 return *this;
129 }
130 TrafficEvent& setError429Count(size_t anError429Count) {
131 error429Count = anError429Count;
132 return *this;
133 }
134 TrafficEvent& setErrorCount(size_t anErrorCount) {
135 errorCount = anErrorCount;
136 return *this;
137 }
138 TrafficEvent& setHttpStatus(long status) {
139 httpStatus = status;
140 return *this;
141 }
142 TrafficEvent& setUrl(const string& aServerName);
143};
144
150class VRS_API TelemetryLogger {
151 public:
152 virtual ~TelemetryLogger();
153
154 static constexpr const char* kErrorType = "error";
155 static constexpr const char* kWarningType = "warning";
156 static constexpr const char* kInfoType = "info";
157
161 static void setLogger(unique_ptr<TelemetryLogger> telemetryLogger = nullptr);
162
164 static inline void error(
165 const OperationContext& operationContext,
166 const string& message,
167 const string& serverMessage = {}) {
168 getInstance()->logEvent(LogEvent(kErrorType, operationContext, message, serverMessage));
169 }
170 static inline void warning(
171 const OperationContext& operationContext,
172 const string& message,
173 const string& serverMessage = {}) {
174 getInstance()->logEvent(LogEvent(kWarningType, operationContext, message, serverMessage));
175 }
176 static inline void info(
177 const OperationContext& operationContext,
178 const string& message,
179 const string& serverMessage = {}) {
180 getInstance()->logEvent(LogEvent(kInfoType, operationContext, message, serverMessage));
181 }
182 static inline void event(
183 const string& eventType,
184 const OperationContext& operationContext,
185 const string& message,
186 const string& serverMessage = {}) {
187 getInstance()->logEvent(LogEvent(eventType, operationContext, message, serverMessage));
188 }
189 static inline void traffic(const OperationContext& operationContext, const TrafficEvent& event) {
190 getInstance()->logTraffic(operationContext, event);
191 }
192 static inline void flush() {
193 getInstance()->flushEvents();
194 }
195
197 virtual void logEvent(LogEvent&& event);
198 virtual void logTraffic(const OperationContext& operationContext, const TrafficEvent& event);
199 virtual void flushEvents() {}
200
202 virtual void start() {}
205 virtual void stop() {}
206
207 private:
208 static TelemetryLogger* getDefaultLogger();
209 static std::atomic<TelemetryLogger*>& getCurrentLogger();
210
211 static inline TelemetryLogger* getInstance() {
212 return getCurrentLogger().load(std::memory_order_relaxed);
213 }
214};
215
216} // namespace vrs
TelemetryLogger to report important events.
Definition TelemetryLogger.h:150
virtual void start()
Start telemetry: background threads should be started, as needed.
Definition TelemetryLogger.h:202
virtual void stop()
Definition TelemetryLogger.h:205
static void error(const OperationContext &operationContext, const string &message, const string &serverMessage={})
methods for clients to use without having to get an instance, etc
Definition TelemetryLogger.h:164
Definition Compressor.cpp:113
General purpose telemetry event.
Definition TelemetryLogger.h:52
Context description for telemetry events.
Definition TelemetryLogger.h:31
Telemetry event specialized to report cloud traffic.
Definition TelemetryLogger.h:78