Skip to content

Commit

Permalink
Refactor and move StructuredLoggerFactory 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 refactors StructuredLoggerFactory to be a template specializing on a stats pointer (e.g. EdenStatsPtr) and then moves it from eden to edencommon.

Reviewed By: kmancini

Differential Revision: D56069823

fbshipit-source-id: 3507d521e809f91746a951c4273c5c323f965fa9
  • Loading branch information
jdelliot authored and facebook-github-bot committed Apr 12, 2024
1 parent c5d6f1e commit c2c22c3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
18 changes: 18 additions & 0 deletions eden/common/telemetry/Stats.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 "eden/common/telemetry/StatsGroup.h"

namespace facebook::eden {

struct TelemetryStats : StatsGroup<TelemetryStats> {
Counter subprocessLoggerFailure{"telemetry.subprocess_logger_failure"};
};

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

#include "eden/common/telemetry/NullStructuredLogger.h"
#include "eden/common/telemetry/ScubaStructuredLogger.h"
#include "eden/common/telemetry/Stats.h"
#include "eden/common/telemetry/SubprocessScribeLogger.h"
#include "eden/common/utils/RefPtr.h"

namespace facebook::eden {

class StructuredLogger;
struct SessionInfo;

/**
* Returns a StructuredLogger appropriate for this platform and
* configuration.
*/
template <typename StatsPtr>
std::shared_ptr<StructuredLogger> makeDefaultStructuredLogger(
const std::string& binary,
const std::string& category,
SessionInfo sessionInfo,
StatsPtr stats) {
if (binary.empty()) {
return std::make_shared<NullStructuredLogger>();
}

if (category.empty()) {
XLOGF(
WARN,
"Scribe binary '{}' specified, but no category specified. Structured logging is disabled.",
binary);
return std::make_shared<NullStructuredLogger>();
}

try {
auto logger =
std::make_unique<SubprocessScribeLogger>(binary.c_str(), category);
return std::make_shared<ScubaStructuredLogger>(
std::move(logger), std::move(sessionInfo));
} catch (const std::exception& ex) {
stats->increment(&TelemetryStats::subprocessLoggerFailure, 1);
XLOGF(
ERR,
"Failed to create SubprocessScribeLogger: {}. Structured logging is disabled.",
folly::exceptionStr(ex));
return std::make_shared<NullStructuredLogger>();
}
}

} // namespace facebook::eden

0 comments on commit c2c22c3

Please sign in to comment.