Skip to content

Commit

Permalink
Move SessionXXX 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 moves SessionXXX  from eden to edencommon.

Reviewed By: kmancini

Differential Revision: D54556886

fbshipit-source-id: 572099c482b7d86bfd84134ba98a168051b52694
  • Loading branch information
jdelliot authored and facebook-github-bot committed Mar 8, 2024
1 parent 0d176a9 commit 788762a
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 0 deletions.
28 changes: 28 additions & 0 deletions eden/common/telemetry/SessionId.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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/SessionId.h"
#include <random>

namespace {

uint32_t generateSessionId() {
std::random_device rd;
std::uniform_int_distribution<uint32_t> u;
return u(rd);
}

} // namespace

namespace facebook::eden {

uint32_t getSessionId() {
static auto sessionId = generateSessionId();
return sessionId;
}

} // namespace facebook::eden
20 changes: 20 additions & 0 deletions eden/common/telemetry/SessionId.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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 <cstdint>

namespace facebook::eden {

/**
* Returns a random, process-stable positive integer in the range of [0,
* UINT32_MAX]
*/
uint32_t getSessionId();

} // namespace facebook::eden
111 changes: 111 additions & 0 deletions eden/common/telemetry/SessionInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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/SessionInfo.h"

#include <folly/Conv.h>
#include <folly/Exception.h>

#if defined(__linux__) || defined(__APPLE__)
#include <folly/portability/Unistd.h>
#include <sys/utsname.h>
#endif

#if defined(_WIN32)
#include <winsock.h> // @manual
#endif

#include <cstdlib>

#include "eden/common/utils/SysctlUtil.h"

namespace {
/**
* Windows limits hostnames to 256 bytes. Linux provides HOST_NAME_MAX
* and MAXHOSTNAMELEN constants, defined as 64. Both Linux and macOS
* define _POSIX_HOST_NAME_MAX as 256. Both Linux and macOS allow
* reading the host name limit at runtime with
* sysconf(_SC_HOST_NAME_MAX).
*
* RFC 1034 limits complete domain names to 255:
* https://tools.ietf.org/html/rfc1034#section-3.1
* > To simplify implementations, the total number of octets that represent a
* > domain name (i.e., the sum of all label octets and label lengths) is
* > limited to 255.
*
* Rather than querying dynamically or selecting a constant based on platform,
* assume 256 is sufficient everywhere.
*/
constexpr size_t kHostNameMax = 256;
} // namespace

namespace facebook::eden {

std::string getOperatingSystemName() {
#if defined(_WIN32)
return "Windows";
#elif defined(__linux__)
return "Linux";
#elif defined(__APPLE__)
// Presuming EdenFS doesn't run on iOS, watchOS, or tvOS. :)
return "macOS";
#else
return "unknown";
#endif
}

std::string getOperatingSystemVersion() {
#if defined(_WIN32)
// TODO: Implement build version lookup, e.g. 1903
// reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v releaseid
return "10";
#elif defined(__linux__) || defined(__APPLE__)
struct utsname uts;
if (uname(&uts)) {
return "error";
}
return uts.release;
#else
return "unknown";
#endif
}

#if defined(__APPLE__)
std::string getOperatingSystemArchitecture() {
// the c_str strips the trailing null bytes.
return getSysCtlByName("machdep.cpu.brand_string", 64).c_str();
}
#endif

std::string getHostname() {
char hostname[kHostNameMax + 1];
folly::checkUnixError(
gethostname(hostname, sizeof(hostname)),
"gethostname() failed, errno: ",
errno);

// POSIX does not require implementations of gethostname to
// null-terminate. Ensure null-termination after the call.
hostname[kHostNameMax] = 0;

return hostname;
}

std::optional<uint64_t> getSandcastleInstanceId() {
auto str = std::getenv("SANDCASTLE_INSTANCE_ID");
if (!str) {
return std::nullopt;
}
try {
uint64_t id = folly::to<uint64_t>(str);
return std::make_optional(id);
} catch (const folly::ConversionError&) {
return std::nullopt;
}
}

} // namespace facebook::eden
50 changes: 50 additions & 0 deletions eden/common/telemetry/SessionInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 <cstdint>
#include <optional>
#include <string>

namespace facebook::eden {

struct SessionInfo {
std::string username;
std::string hostname;
// TODO(nga): sandcastle is Facebook-specific, should not be used in
// opensource version.
std::optional<uint64_t> sandcastleInstanceId;
std::string os;
std::string osVersion;
std::string edenVersion;
#ifdef __APPLE__
std::string systemArchitecture;
#endif
};

std::string getOperatingSystemName();
std::string getOperatingSystemVersion();
#if defined(__APPLE__)
std::string getOperatingSystemArchitecture();
#endif

/**
* Returns the result of calling gethostname() in a std::string. Throws an
* exception on failure.
*/
std::string getHostname();

/**
* Return the best guess of sandcastle instance id from the environment,
* or return empty if sandcastle instance id is unknown.
*/
// TODO(nga): sandcastle is Facebook-specific, should not be used in
// opensource version.
std::optional<uint64_t> getSandcastleInstanceId();

} // namespace facebook::eden

0 comments on commit 788762a

Please sign in to comment.