Skip to content

Commit

Permalink
Move DynamicEvent from eden to edencommon
Browse files Browse the repository at this point in the history
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
jdelliot authored and facebook-github-bot committed Feb 28, 2024
1 parent 9c4edd2 commit 36bdeae
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ include(GoogleTest)
enable_testing()

add_subdirectory(eden/common/os)
add_subdirectory(eden/common/telemetry)
add_subdirectory(eden/common/utils)

# Install our own CMake package files for dependent projects.
Expand Down
39 changes: 39 additions & 0 deletions eden/common/telemetry/CMakeLists.txt
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
)
55 changes: 55 additions & 0 deletions eden/common/telemetry/DynamicEvent.cpp
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
59 changes: 59 additions & 0 deletions eden/common/telemetry/DynamicEvent.h
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

0 comments on commit 36bdeae

Please sign in to comment.