proxygen
proxygen::TraceEvent Class Reference

#include <TraceEvent.h>

Classes

class  Iterator
 
struct  MetaData
 

Public Types

using MetaDataMap = std::map< TraceFieldType, MetaData >
 

Public Member Functions

FB_EXPORT TraceEvent (TraceEventType type, uint32_t parentID=0)
 
void start (const TimeUtil &tm)
 
void start (TimePoint startTime)
 
void end (const TimeUtil &tm)
 
void end (TimePoint endTime)
 
bool hasStarted () const
 
bool hasEnded () const
 
TimePoint getStartTime () const
 
TimePoint getEndTime () const
 
TraceEventType getType () const
 
uint32_t getID () const
 
void setParentID (uint32_t parent)
 
uint32_t getParentID () const
 
bool hasTraceField (TraceFieldType field) const
 
template<typename T >
T getTraceFieldDataAs (TraceFieldType field) const
 
void setMetaData (MetaDataMap &&input)
 
const MetaDataMapgetMetaData () const
 
Iterator getMetaDataItr () const
 
template<typename T >
bool addMeta (TraceFieldType key, T &&value)
 
template<typename T >
bool addMeta (TraceFieldType key, const T &value)
 
template<typename T >
bool readIntMeta (TraceFieldType key, T &dest) const
 
bool readBoolMeta (TraceFieldType key, bool &dest) const
 
bool readStrMeta (TraceFieldType key, std::string &dest) const
 
std::string toString () const
 

Private Types

enum  State { NOT_STARTED = 0, STARTED = 1, ENDED = 2 }
 

Private Member Functions

template<typename T >
bool readMeta (TraceFieldType key, T &dest) const
 
FB_EXPORT bool addMetaInternal (TraceFieldType key, MetaData &&val)
 

Private Attributes

uint8_t stateFlags_ {0}
 
TraceEventType type_
 
uint32_t id_
 
uint32_t parentID_
 
TimePoint start_
 
TimePoint end_
 
MetaDataMap metaData_
 

Friends

class Iterator
 
std::ostream & operator<< (std::ostream &out, const TraceEvent &event)
 

Detailed Description

Simple structure to track timing of event in request flow then we can report back to the application.

Definition at line 37 of file TraceEvent.h.

Member Typedef Documentation

Member Enumeration Documentation

Enumerator
NOT_STARTED 
STARTED 
ENDED 

Definition at line 264 of file TraceEvent.h.

Constructor & Destructor Documentation

proxygen::TraceEvent::TraceEvent ( TraceEventType  type,
uint32_t  parentID = 0 
)
explicit

Definition at line 30 of file TraceEvent.cpp.

References counter, and id_.

30  :
31  type_(type),
32  parentID_(parentID) {
33  static std::atomic<uint32_t> counter(0);
34  id_ = counter++;
35 }
TraceEventType type_
Definition: TraceEvent.h:271
PskType type
std::atomic< int > counter

Member Function Documentation

template<typename T >
bool proxygen::TraceEvent::addMeta ( TraceFieldType  key,
T &&  value 
)
inline

Definition at line 218 of file TraceEvent.h.

References addMetaInternal(), folly::gen::move, val, and value.

Referenced by TEST().

218  {
219  MetaData val(std::forward<T>(value));
220  return addMetaInternal(key, std::move(val));
221  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
double val
Definition: String.cpp:273
static const char *const value
Definition: Conv.cpp:50
FB_EXPORT bool addMetaInternal(TraceFieldType key, MetaData &&val)
Definition: TraceEvent.cpp:72
template<typename T >
bool proxygen::TraceEvent::addMeta ( TraceFieldType  key,
const T value 
)
inline

Definition at line 224 of file TraceEvent.h.

References addMetaInternal(), folly::gen::move, and val.

224  {
225  MetaData val(value);
226  return addMetaInternal(key, std::move(val));
227  }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
double val
Definition: String.cpp:273
static const char *const value
Definition: Conv.cpp:50
FB_EXPORT bool addMetaInternal(TraceFieldType key, MetaData &&val)
Definition: TraceEvent.cpp:72
bool proxygen::TraceEvent::addMetaInternal ( TraceFieldType  key,
MetaData &&  val 
)
private

Definition at line 72 of file TraceEvent.cpp.

References metaData_, and value.

Referenced by addMeta(), and readMeta().

72  {
73  auto rc = metaData_.emplace(key, value);
74 
75  // replace if key already exist
76  if (!rc.second) {
77  rc.first->second = value;
78  }
79 
80  return rc.second;
81 }
MetaDataMap metaData_
Definition: TraceEvent.h:276
static const char *const value
Definition: Conv.cpp:50
void proxygen::TraceEvent::end ( const TimeUtil tm)

Sets the end time to the current time according to the TimeUtil.

Definition at line 47 of file TraceEvent.cpp.

References end_, proxygen::TimeUtilGeneric< ClockType >::now(), and stateFlags_.

Referenced by TEST().

47  {
48  stateFlags_ |= State::ENDED;
49  end_ = tm.now();
50 }
void proxygen::TraceEvent::end ( TimePoint  endTime)

Sets the end time to the given TimePoint.

Definition at line 52 of file TraceEvent.cpp.

References end_, and stateFlags_.

52  {
53  stateFlags_ |= State::ENDED;
54  end_ = endTime;
55 }
TimePoint proxygen::TraceEvent::getEndTime ( ) const
inline

Definition at line 174 of file TraceEvent.h.

References end_.

174  {
175  return end_;
176  }
uint32_t proxygen::TraceEvent::getID ( ) const
inline

Definition at line 182 of file TraceEvent.h.

References id_.

Referenced by TEST().

182  {
183  return id_;
184  }
const MetaDataMap& proxygen::TraceEvent::getMetaData ( ) const
inline

Definition at line 209 of file TraceEvent.h.

References metaData_.

209  {
210  return metaData_;
211  }
MetaDataMap metaData_
Definition: TraceEvent.h:276
Iterator proxygen::TraceEvent::getMetaDataItr ( ) const
inline

Definition at line 213 of file TraceEvent.h.

References Iterator.

Referenced by TEST(), and toString().

213  {
214  return Iterator(*this);
215  }
friend class Iterator
Definition: TraceEvent.h:245
uint32_t proxygen::TraceEvent::getParentID ( ) const
inline

Definition at line 190 of file TraceEvent.h.

References parentID_.

190  {
191  return parentID_;
192  }
TimePoint proxygen::TraceEvent::getStartTime ( ) const
inline

Definition at line 170 of file TraceEvent.h.

References start_.

170  {
171  return start_;
172  }
template<typename T >
T proxygen::TraceEvent::getTraceFieldDataAs ( TraceFieldType  field) const
inline

Definition at line 199 of file TraceEvent.h.

References metaData_, and T.

Referenced by TEST().

199  {
200  const auto itr = metaData_.find(field);
201  CHECK(itr != metaData_.end());
202  return itr->second.getValueAs<T>();
203  }
#define T(v)
Definition: http_parser.c:233
MetaDataMap metaData_
Definition: TraceEvent.h:276
Map field(FieldType Class::*field)
Definition: Base.h:641
TraceEventType proxygen::TraceEvent::getType ( ) const
inline

Definition at line 178 of file TraceEvent.h.

References type_.

178  {
179  return type_;
180  }
TraceEventType type_
Definition: TraceEvent.h:271
bool proxygen::TraceEvent::hasEnded ( ) const

true iff end() has been called on this TraceEvent.

Definition at line 61 of file TraceEvent.cpp.

References stateFlags_.

61  {
62  return stateFlags_ & State::ENDED;
63 }
bool proxygen::TraceEvent::hasStarted ( ) const

true iff start() has been called on this TraceEvent.

Definition at line 57 of file TraceEvent.cpp.

References stateFlags_.

57  {
58  return stateFlags_ & State::STARTED;
59 }
bool proxygen::TraceEvent::hasTraceField ( TraceFieldType  field) const
inline

Definition at line 194 of file TraceEvent.h.

References metaData_.

194  {
195  return metaData_.count(field);
196  }
MetaDataMap metaData_
Definition: TraceEvent.h:276
Map field(FieldType Class::*field)
Definition: Base.h:641
bool proxygen::TraceEvent::readBoolMeta ( TraceFieldType  key,
bool &  dest 
) const

Definition at line 65 of file TraceEvent.cpp.

References readMeta().

Referenced by readIntMeta().

65  {
66  return readMeta(key, dest);
67 }
dest
Definition: upload.py:394
bool readMeta(TraceFieldType key, T &dest) const
Definition: TraceEvent.h:249
template<typename T >
bool proxygen::TraceEvent::readIntMeta ( TraceFieldType  key,
T dest 
) const
inline

Definition at line 230 of file TraceEvent.h.

References upload::dest, operator<<, readBoolMeta(), readMeta(), readStrMeta(), string, toString(), and value.

230  {
232  "readIntMeta should take an intergral type of paremeter");
233  return readMeta(key, dest);
234  }
dest
Definition: upload.py:394
bool readMeta(TraceFieldType key, T &dest) const
Definition: TraceEvent.h:249
static const char *const value
Definition: Conv.cpp:50
template<typename T >
bool proxygen::TraceEvent::readMeta ( TraceFieldType  key,
T dest 
) const
inlineprivate

Definition at line 249 of file TraceEvent.h.

References addMetaInternal(), FB_EXPORT, metaData_, T, and val.

Referenced by readBoolMeta(), readIntMeta(), and readStrMeta().

249  {
250  const auto itr = metaData_.find(key);
251  if (itr != metaData_.end()) {
252  try {
253  dest = itr->second.getValueAs<T>();
254  return true;
255  } catch (const std::exception&) {
256  return false;
257  }
258  }
259  return false;
260  }
#define T(v)
Definition: http_parser.c:233
MetaDataMap metaData_
Definition: TraceEvent.h:276
dest
Definition: upload.py:394
bool proxygen::TraceEvent::readStrMeta ( TraceFieldType  key,
std::string dest 
) const

Definition at line 69 of file TraceEvent.cpp.

References readMeta().

Referenced by readIntMeta().

69  {
70  return readMeta(key, dest);
71 }
dest
Definition: upload.py:394
bool readMeta(TraceFieldType key, T &dest) const
Definition: TraceEvent.h:249
void proxygen::TraceEvent::setMetaData ( MetaDataMap &&  input)
inline

Definition at line 205 of file TraceEvent.h.

References metaData_.

205  {
206  metaData_ = input;
207  }
MetaDataMap metaData_
Definition: TraceEvent.h:276
void proxygen::TraceEvent::setParentID ( uint32_t  parent)
inline

Definition at line 186 of file TraceEvent.h.

References parent, and parentID_.

186  {
187  parentID_ = parent;
188  }
folly::Function< void()> parent
Definition: AtFork.cpp:34
void proxygen::TraceEvent::start ( const TimeUtil tm)

Sets the start time to the current time according to the TimeUtil.

Definition at line 37 of file TraceEvent.cpp.

References proxygen::TimeUtilGeneric< ClockType >::now(), start_, and stateFlags_.

Referenced by TEST().

37  {
38  stateFlags_ |= State::STARTED;
39  start_ = tm.now();
40 }
void proxygen::TraceEvent::start ( TimePoint  startTime)

Sets the start time to the given TimePoint.

Definition at line 42 of file TraceEvent.cpp.

References start_, and stateFlags_.

42  {
43  stateFlags_ |= State::STARTED;
44  start_ = startTime;
45 }
std::string proxygen::TraceEvent::toString ( ) const

Definition at line 83 of file TraceEvent.cpp.

References count, end_, getMetaDataItr(), proxygen::getTraceEventTypeString(), proxygen::getTraceFieldTypeString(), id_, parentID_, start_, string, and type_.

Referenced by readIntMeta(), and TEST().

83  {
84  std::ostringstream out;
85  int startSinceEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(
86  start_.time_since_epoch()).count();
87  int endSinceEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(
88  end_.time_since_epoch()).count();
89  out << "TraceEvent(";
90  out << "type='" << getTraceEventTypeString(type_) << "', ";
91  out << "id='" << id_ << "', ";
92  out << "parentID='" << parentID_ << "', ";
93  out << "start='" << startSinceEpoch << "', ";
94  out << "end='" << endSinceEpoch << "', ";
95  out << "metaData='{";
96  auto itr = getMetaDataItr();
97  while (itr.isValid()) {
98  out << getTraceFieldTypeString(itr.getKey()) << ": "
99  << itr.getValueAs<std::string>() << ", ";
100  itr.next();
101  }
102  out << "}')";
103  return out.str();
104 }
const std::string & getTraceFieldTypeString(TraceFieldType type)
TraceEventType type_
Definition: TraceEvent.h:271
Iterator getMetaDataItr() const
Definition: TraceEvent.h:213
int * count
const char * string
Definition: Conv.cpp:212
const std::string & getTraceEventTypeString(TraceEventType type)

Friends And Related Function Documentation

friend class Iterator
friend

Definition at line 245 of file TraceEvent.h.

Referenced by getMetaDataItr().

std::ostream& operator<< ( std::ostream &  out,
const TraceEvent event 
)
friend

Definition at line 116 of file TraceEvent.cpp.

Referenced by readIntMeta().

116  {
117  out << event.toString();
118  return out;
119 }

Member Data Documentation

TimePoint proxygen::TraceEvent::end_
private

Definition at line 275 of file TraceEvent.h.

Referenced by end(), getEndTime(), and toString().

uint32_t proxygen::TraceEvent::id_
private

Definition at line 272 of file TraceEvent.h.

Referenced by getID(), toString(), and TraceEvent().

MetaDataMap proxygen::TraceEvent::metaData_
private
uint32_t proxygen::TraceEvent::parentID_
private

Definition at line 273 of file TraceEvent.h.

Referenced by getParentID(), setParentID(), and toString().

TimePoint proxygen::TraceEvent::start_
private

Definition at line 274 of file TraceEvent.h.

Referenced by getStartTime(), start(), and toString().

uint8_t proxygen::TraceEvent::stateFlags_ {0}
private

Definition at line 270 of file TraceEvent.h.

Referenced by end(), hasEnded(), hasStarted(), and start().

TraceEventType proxygen::TraceEvent::type_
private

Definition at line 271 of file TraceEvent.h.

Referenced by getType(), and toString().


The documentation for this class was generated from the following files: