-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move DynamicEvent from eden to edencommon
Summary: To support better telemetry and logging in watchman we want to use Eden's components. Lets migrate and detangle the needed pieces. This change move DynamicEvent from eden to edencommon. Reviewed By: kmancini Differential Revision: D54046154 fbshipit-source-id: de9466c73c0b2a2a16302cc0114e4c2a12426b30
- Loading branch information
1 parent
9c4edd2
commit 36bdeae
Showing
4 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
file(GLOB telemetry_headers CONFIGURE_DEPENDS *.h) | ||
file(GLOB telemetry_sources CONFIGURE_DEPENDS *.cpp) | ||
|
||
add_library( | ||
edencommon_telemetry | ||
${telemetry_headers} | ||
${telemetry_sources}) | ||
|
||
target_link_libraries( | ||
edencommon_telemetry | ||
PUBLIC | ||
edencommon_utils | ||
Folly::folly | ||
) | ||
|
||
|
||
target_include_directories( | ||
edencommon_telemetry | ||
PUBLIC | ||
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}> | ||
$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}> | ||
) | ||
|
||
install( | ||
TARGETS edencommon_telemetry | ||
EXPORT edencommon-exports | ||
LIBRARY DESTINATION "${LIB_INSTALL_DIR}" | ||
ARCHIVE DESTINATION "${LIB_INSTALL_DIR}" | ||
) | ||
|
||
install( | ||
FILES ${telemetry_headers} | ||
DESTINATION ${INCLUDE_INSTALL_DIR}/eden/common/telemetry | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include "eden/common/telemetry/DynamicEvent.h" | ||
|
||
#include <folly/Conv.h> | ||
#include <folly/Unicode.h> | ||
#include <folly/logging/xlog.h> | ||
#include "eden/common/utils/Throw.h" | ||
|
||
namespace { | ||
void validateUtf8(folly::StringPiece sp) { | ||
auto* p = reinterpret_cast<const unsigned char*>(sp.begin()); | ||
auto* const end = reinterpret_cast<const unsigned char*>(sp.end()); | ||
while (p < end) { | ||
(void)folly::utf8ToCodePoint(p, end, false); | ||
} | ||
} | ||
} // namespace | ||
|
||
namespace facebook::eden { | ||
|
||
void DynamicEvent::addInt(std::string name, int64_t value) { | ||
auto [iter, inserted] = ints_.emplace(std::move(name), value); | ||
if (!inserted) { | ||
throw_<std::logic_error>( | ||
"Attempted to insert duplicate int: ", iter->first); | ||
} | ||
} | ||
|
||
void DynamicEvent::addString(std::string name, std::string value) { | ||
validateUtf8(value); | ||
auto [iter, inserted] = strings_.emplace(std::move(name), std::move(value)); | ||
if (!inserted) { | ||
throw_<std::logic_error>( | ||
"Attempted to insert duplicate string: ", iter->first); | ||
} | ||
} | ||
|
||
void DynamicEvent::addDouble(std::string name, double value) { | ||
XCHECK(std::isfinite(value)) | ||
<< "Attempted to insert double-precision value that cannot be represented in JSON: " | ||
<< name; | ||
auto [iter, inserted] = doubles_.emplace(std::move(name), value); | ||
if (!inserted) { | ||
throw_<std::logic_error>( | ||
"Attempted to insert duplicate double: ", iter->first); | ||
} | ||
} | ||
|
||
} // namespace facebook::eden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <folly/portability/SysTypes.h> | ||
#include <cstdint> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
namespace facebook::eden { | ||
|
||
class DynamicEvent { | ||
public: | ||
using IntMap = std::unordered_map<std::string, int64_t>; | ||
using StringMap = std::unordered_map<std::string, std::string>; | ||
using DoubleMap = std::unordered_map<std::string, double>; | ||
|
||
DynamicEvent() = default; | ||
DynamicEvent(const DynamicEvent&) = default; | ||
DynamicEvent(DynamicEvent&&) = default; | ||
DynamicEvent& operator=(const DynamicEvent&) = default; | ||
DynamicEvent& operator=(DynamicEvent&&) = default; | ||
|
||
void addInt(std::string name, int64_t value); | ||
void addString(std::string name, std::string value); | ||
void addDouble(std::string name, double value); | ||
|
||
/** | ||
* Convenience function that adds boolean values as integer 0 or 1. | ||
*/ | ||
void addBool(std::string name, bool value) { | ||
addInt(std::move(name), value); | ||
} | ||
|
||
const IntMap& getIntMap() const { | ||
return ints_; | ||
} | ||
const StringMap& getStringMap() const { | ||
return strings_; | ||
} | ||
const DoubleMap& getDoubleMap() const { | ||
return doubles_; | ||
} | ||
|
||
private: | ||
// Due to limitations in the underlying log database, limit the field types to | ||
// int64_t, double, string, and vector<string> | ||
// TODO: add vector<string> support if needed. | ||
IntMap ints_; | ||
StringMap strings_; | ||
DoubleMap doubles_; | ||
}; | ||
|
||
} // namespace facebook::eden |