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
23namespace vrs {
24
25using std::string;
26
29 string operation;
30 string sourceLocation;
31
32 OperationContext() = default;
33 OperationContext(string op, string sourceLoc)
34 : operation{std::move(op)}, sourceLocation{std::move(sourceLoc)} {}
35 OperationContext(const OperationContext& rhs) = default;
37 : operation{std::move(rhs.operation)}, sourceLocation{std::move(rhs.sourceLocation)} {}
38
39 bool operator<(const OperationContext& rhs) const {
40 auto tie = [](const OperationContext& oc) { return std::tie(oc.operation, oc.sourceLocation); };
41 return tie(*this) < tie(rhs);
42 }
43 OperationContext& operator=(OperationContext&& rhs) noexcept {
44 operation = std::move(rhs.operation);
45 sourceLocation = std::move(rhs.sourceLocation);
46 return *this;
47 }
48};
49
51struct LogEvent {
52 LogEvent() = default;
53 LogEvent(std::string type, OperationContext opContext, std::string message, string serverReply)
54 : type{std::move(type)},
55 operationContext{std::move(opContext)},
56 message{std::move(message)},
57 serverReply{std::move(serverReply)} {}
58 LogEvent(LogEvent&& rhs) noexcept
59 : type{std::move(rhs.type)},
60 operationContext{std::move(rhs.operationContext)},
61 message{std::move(rhs.message)},
62 serverReply{std::move(rhs.serverReply)} {}
63
64 LogEvent& operator=(LogEvent&& rhs) noexcept {
65 type = std::move(rhs.type);
66 operationContext = std::move(rhs.operationContext);
67 message = std::move(rhs.message);
68 serverReply = std::move(rhs.serverReply);
69 return *this;
70 }
71
72 std::string type;
73 OperationContext operationContext;
74 std::string message;
75 std::string serverReply;
76};
77
85 bool isSuccess = false;
86 bool uploadNotDownload = false;
87 int64_t transferStartTime = 0; // start time
88 int64_t totalDurationMs = -1; // overall request duration, including retries
89 int64_t transferDurationMs = -1; // last network transfer duration (last attempt)
90 size_t transferOffset = 0; // offset to read from
91 size_t transferRequestSize = 0; // bytes
92 size_t transferSize = 0; // bytes
93 size_t retryCount = 0;
94 size_t errorCount = 0;
95 size_t error429Count = 0;
96 long httpStatus = -1;
97 string serverName;
98
99 TrafficEvent& setIsSuccess(bool success) {
100 isSuccess = success;
101 return *this;
102 }
103 TrafficEvent& setIsUpload() {
104 uploadNotDownload = true;
105 return *this;
106 }
107 TrafficEvent& setIsDownload() {
108 uploadNotDownload = false;
109 return *this;
110 }
111 TrafficEvent& setAttemptStartTime();
112 TrafficEvent& setTotalDurationMs(int64_t durationMs) {
113 totalDurationMs = durationMs;
114 return *this;
115 }
116 TrafficEvent& setTransferDurationMs(int64_t aTransferDurationMs) {
117 transferDurationMs = aTransferDurationMs;
118 return *this;
119 }
120 TrafficEvent& setTransferOffset(size_t offset) {
121 transferOffset = offset;
122 return *this;
123 }
124 TrafficEvent& setTransferRequestSize(size_t aTransferRequestSize) {
125 transferRequestSize = aTransferRequestSize;
126 return *this;
127 }
128 TrafficEvent& setTransferSize(size_t aTransferSize) {
129 transferSize = aTransferSize;
130 return *this;
131 }
132 TrafficEvent& setRetryCount(size_t aRetryCount) {
133 retryCount = aRetryCount;
134 return *this;
135 }
136 TrafficEvent& setError429Count(size_t anError429Count) {
137 error429Count = anError429Count;
138 return *this;
139 }
140 TrafficEvent& setErrorCount(size_t anErrorCount) {
141 errorCount = anErrorCount;
142 return *this;
143 }
144 TrafficEvent& setHttpStatus(long status) {
145 httpStatus = status;
146 return *this;
147 }
148 TrafficEvent& setUrl(const string& aServerName);
149};
150
157 public:
158 virtual ~TelemetryLogger();
159
160 static constexpr const char* kErrorType = "error";
161 static constexpr const char* kWarningType = "warning";
162 static constexpr const char* kInfoType = "info";
163
167 static void setLogger(std::unique_ptr<TelemetryLogger> telemetryLogger = nullptr);
168
170 static inline void error(
171 const OperationContext& operationContext,
172 const string& message,
173 const string& serverMessage = {}) {
174 getInstance()->logEvent(LogEvent(kErrorType, operationContext, message, serverMessage));
175 }
176 static inline void warning(
177 const OperationContext& operationContext,
178 const string& message,
179 const string& serverMessage = {}) {
180 getInstance()->logEvent(LogEvent(kWarningType, operationContext, message, serverMessage));
181 }
182 static inline void info(
183 const OperationContext& operationContext,
184 const string& message,
185 const string& serverMessage = {}) {
186 getInstance()->logEvent(LogEvent(kInfoType, operationContext, message, serverMessage));
187 }
188 static inline void event(
189 const std::string& eventType,
190 const OperationContext& operationContext,
191 const string& message,
192 const string& serverMessage = {}) {
193 getInstance()->logEvent(LogEvent(eventType, operationContext, message, serverMessage));
194 }
195 static inline void traffic(const OperationContext& operationContext, const TrafficEvent& event) {
196 getInstance()->logTraffic(operationContext, event);
197 }
198 static inline void flush() {
199 getInstance()->flushEvents();
200 }
201
203 virtual void logEvent(LogEvent&& event);
204 virtual void logTraffic(const OperationContext& operationContext, const TrafficEvent& event);
205 virtual void flushEvents() {}
206
208 virtual void start() {}
211 virtual void stop() {}
212
213 private:
214 static TelemetryLogger* getDefaultLogger();
215 static std::atomic<TelemetryLogger*>& getCurrentLogger();
216
217 static inline TelemetryLogger* getInstance() {
218 return getCurrentLogger().load(std::memory_order_relaxed);
219 }
220};
221
222} // namespace vrs
TelemetryLogger to report important events.
Definition TelemetryLogger.h:156
virtual void start()
Start telemetry: background threads should be started, as needed.
Definition TelemetryLogger.h:208
virtual void stop()
Definition TelemetryLogger.h:211
static void setLogger(std::unique_ptr< TelemetryLogger > telemetryLogger=nullptr)
Definition TelemetryLogger.cpp:42
virtual void logEvent(LogEvent &&event)
Actual methods that implement the behaviors.
Definition TelemetryLogger.cpp:59
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:170
Definition AsyncDiskFileChunk.hpp:49
General purpose telemetry event.
Definition TelemetryLogger.h:51
Context description for telemetry events.
Definition TelemetryLogger.h:28
Telemetry event specialized to report cloud traffic.
Definition TelemetryLogger.h:84