|
| 1 | +//===-- Telemetry.cpp -----------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +#include "lldb/Core/Telemetry.h" |
| 9 | +#include "lldb/Core/Debugger.h" |
| 10 | +#include "lldb/Utility/LLDBLog.h" |
| 11 | +#include "lldb/Utility/UUID.h" |
| 12 | +#include "lldb/lldb-enumerations.h" |
| 13 | +#include "lldb/lldb-forward.h" |
| 14 | +#include "llvm/ADT/StringRef.h" |
| 15 | +#include "llvm/Support/Error.h" |
| 16 | +#include "llvm/Support/RandomNumberGenerator.h" |
| 17 | +#include "llvm/Telemetry/Telemetry.h" |
| 18 | +#include <chrono> |
| 19 | +#include <cstdlib> |
| 20 | +#include <memory> |
| 21 | +#include <string> |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +namespace lldb_private { |
| 25 | +namespace telemetry { |
| 26 | + |
| 27 | +using ::llvm::Error; |
| 28 | +using ::llvm::telemetry::Destination; |
| 29 | +using ::llvm::telemetry::Serializer; |
| 30 | +using ::llvm::telemetry::TelemetryInfo; |
| 31 | + |
| 32 | +static uint64_t ToNanosec(const SteadyTimePoint Point) { |
| 33 | + return std::chrono::nanoseconds(Point.time_since_epoch()).count(); |
| 34 | +} |
| 35 | + |
| 36 | +void LLDBBaseTelemetryInfo::serialize(Serializer &serializer) const { |
| 37 | + serializer.write("entry_kind", getKind()); |
| 38 | + serializer.write("session_id", SessionId); |
| 39 | + serializer.write("start_time", ToNanosec(start_time)); |
| 40 | + if (end_time.has_value()) |
| 41 | + serializer.write("end_time", ToNanosec(end_time.value())); |
| 42 | +} |
| 43 | + |
| 44 | +static std::string MakeUUID(lldb_private::Debugger *debugger) { |
| 45 | + uint8_t random_bytes[16]; |
| 46 | + if (auto ec = llvm::getRandomBytes(random_bytes, 16)) { |
| 47 | + LLDB_LOG(GetLog(LLDBLog::Object), |
| 48 | + "Failed to generate random bytes for UUID: {0}", ec.message()); |
| 49 | + // fallback to using timestamp + debugger ID. |
| 50 | + return llvm::formatv( |
| 51 | + "{0}_{1}", std::chrono::steady_clock::now().time_since_epoch().count(), |
| 52 | + debugger->GetID()); |
| 53 | + } |
| 54 | + return lldb_private::UUID(random_bytes).GetAsString(); |
| 55 | +} |
| 56 | + |
| 57 | +TelemetryManager::TelemetryManager( |
| 58 | + std::unique_ptr<llvm::telemetry::Config> config) |
| 59 | + : m_config(std::move(config)) {} |
| 60 | + |
| 61 | +llvm::Error TelemetryManager::preDispatch(TelemetryInfo *entry) { |
| 62 | + // Do nothing for now. |
| 63 | + // In up-coming patch, this would be where the manager |
| 64 | + // attach the session_uuid to the entry. |
| 65 | + return Error::success(); |
| 66 | +} |
| 67 | + |
| 68 | +} // namespace telemetry |
| 69 | +} // namespace lldb_private |
0 commit comments