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