[llvm] [llvm]Add a simple Telemetry framework (PR #102323)

Vy Nguyen via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 22 06:17:19 PDT 2024


================
@@ -0,0 +1,107 @@
+//===- llvm/Telemetry/Telemetry.h - Telemetry -------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TELEMETRY_TELEMETRY_H
+#define LLVM_TELEMETRY_TELEMETRY_H
+
+#include <chrono>
+#include <ctime>
+#include <memory>
+#include <optional>
+#include <string>
+
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/JSON.h"
+
+namespace llvm {
+namespace telemetry {
+
+using SteadyTimePoint = std::chrono::time_point<std::chrono::steady_clock>;
+
+struct TelemetryConfig {
+  // If true, telemetry will be enabled.
+  bool enable_telemetry;
+
+  // Additional destinations to send the logged entries.
+  // Could be stdout, stderr, or some local paths.
+  // Note: these are destinations are __in addition to__ whatever the default
+  // destination(s) are, as implemented by vendors.
+  std::vector<std::string> additional_destinations;
----------------
oontvoo wrote:

Hmm, I see your point, but here's the dilemma. This struct is supposed to be the configuration - ie., it specifies a bunch of random properties to tell the `Telemeter` how to set up data `Destination`s.  In other words, it's just descriptions, which is why I thought strings could be OK. 

About your point w.r.t "different kind of destinations", I've mentioned in my other comment, but how (and where) the data is transmitted or serialized is transparent to everyone else, except the `Destination` class itself.

Say you have your downstream Telemetry library that you want to emit data to JSON file(s). You would define a `JsonDesintation` to do that. Eg.:

```
class MyJsonDestination : public TelemetryDestination {
public:
 // ... other stuff ....
  Error EmitEntry(const TelemetryInfo *entry) override {
          // Take the `entry` and serialize it to JSON here
         // Return Error or Success.
  }
  virtual std::string name() const override { return "JsonDestination";}
};

```

Does this make sense?

I'll update the patch shortly with tests, which, hopefully, will make it a bit clearer how all the API interracts.

https://github.com/llvm/llvm-project/pull/102323


More information about the llvm-commits mailing list