Merge master (#554)

This commit is contained in:
Boris Taševski 2022-09-15 07:25:36 +02:00 committed by GitHub
parent 0876a8848d
commit 43e0520bc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 106 additions and 1 deletions

View File

@ -462,6 +462,8 @@ struct SessionData {
memgraph::utils::Synchronized<memgraph::auth::Auth, memgraph::utils::WritePrioritizedRWLock> *auth; memgraph::utils::Synchronized<memgraph::auth::Auth, memgraph::utils::WritePrioritizedRWLock> *auth;
#endif #endif
// NOTE: run_id should be const but that complicates code a lot.
std::optional<std::string> run_id;
}; };
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
@ -482,7 +484,8 @@ class BoltSession final : public memgraph::communication::bolt::Session<memgraph
#if MG_ENTERPRISE #if MG_ENTERPRISE
audit_log_(data->audit_log), audit_log_(data->audit_log),
#endif #endif
endpoint_(endpoint) { endpoint_(endpoint),
run_id_(data->run_id) {
} }
using memgraph::communication::bolt::Session<memgraph::communication::v2::InputStream, using memgraph::communication::bolt::Session<memgraph::communication::v2::InputStream,
@ -576,6 +579,14 @@ class BoltSession final : public memgraph::communication::bolt::Session<memgraph
} }
decoded_summary.emplace(kv.first, std::move(*maybe_value)); decoded_summary.emplace(kv.first, std::move(*maybe_value));
} }
// Add this memgraph instance run_id, received from telemetry
// This is sent with every query, instead of only on bolt init inside
// communication/bolt/v1/states/init.hpp because neo4jdriver does not
// read the init message.
if (auto run_id = run_id_; run_id) {
decoded_summary.emplace("run_id", *run_id);
}
return decoded_summary; return decoded_summary;
} catch (const memgraph::query::QueryException &e) { } catch (const memgraph::query::QueryException &e) {
// Wrap QueryException into ClientError, because we want to allow the // Wrap QueryException into ClientError, because we want to allow the
@ -627,6 +638,8 @@ class BoltSession final : public memgraph::communication::bolt::Session<memgraph
memgraph::audit::Log *audit_log_; memgraph::audit::Log *audit_log_;
#endif #endif
memgraph::communication::v2::ServerEndpoint endpoint_; memgraph::communication::v2::ServerEndpoint endpoint_;
// NOTE: run_id should be const but that complicates code a lot.
std::optional<std::string> run_id_;
}; };
using ServerT = memgraph::communication::v2::Server<BoltSession, SessionData>; using ServerT = memgraph::communication::v2::Server<BoltSession, SessionData>;
@ -898,6 +911,7 @@ int main(int argc, char **argv) {
if (FLAGS_telemetry_enabled) { if (FLAGS_telemetry_enabled) {
telemetry.emplace("https://telemetry.memgraph.com/88b5e7e8-746a-11e8-9f85-538a9e9690cc/", telemetry.emplace("https://telemetry.memgraph.com/88b5e7e8-746a-11e8-9f85-538a9e9690cc/",
data_directory / "telemetry", std::chrono::minutes(10)); data_directory / "telemetry", std::chrono::minutes(10));
session_data.run_id = telemetry->GetRunId();
telemetry->AddCollector("storage", [&db]() -> nlohmann::json { telemetry->AddCollector("storage", [&db]() -> nlohmann::json {
auto info = db.GetInfo(); auto info = db.GetInfo();
return {{"vertices", info.vertex_count}, {"edges", info.edge_count}}; return {{"vertices", info.vertex_count}, {"edges", info.edge_count}};

View File

@ -59,6 +59,8 @@ void Telemetry::AddCollector(const std::string &name, const std::function<const
collectors_.emplace_back(name, func); collectors_.emplace_back(name, func);
} }
std::string Telemetry::GetRunId() const { return uuid_; }
Telemetry::~Telemetry() { Telemetry::~Telemetry() {
scheduler_.Stop(); scheduler_.Stop();
CollectData("shutdown"); CollectData("shutdown");

View File

@ -39,6 +39,9 @@ class Telemetry final {
void AddCollector(const std::string &name, const std::function<const nlohmann::json(void)> &func); void AddCollector(const std::string &name, const std::function<const nlohmann::json(void)> &func);
/// Required to expose run_id to Bolt server.
std::string GetRunId() const;
~Telemetry(); ~Telemetry();
Telemetry(const Telemetry &) = delete; Telemetry(const Telemetry &) = delete;

View File

@ -0,0 +1,29 @@
## Local test to check whether run_id is inside query metadata
In order to run this test, you should enable telemetry while running Memgraph,
because telemetry generated the run_id of a Memgraph instance.
Before doing that, we need to restrict the communication with memgraph's
telemetry server, so tests wouldn't give us garbage telemetry. To do that you
should add:
```
127.0.0.1 telemetry.memgraph.com
```
to the beginning of `/etc/hosts`.
Now that you blocked telemetry from being sent, you should run memgraph with
telemetry enabled:
```
memgraph --telemetry-enabled=True
```
Finally, to run the test:
```
npm install
node get-run_id.js
```
Output should be:
```
Query: MATCH (n) RETURN n LIMIT 1;
run_id: <your-memgraph-run_id>
```

View File

@ -0,0 +1,42 @@
const neo4j = require('neo4j-driver');
const runQuery = async (driver, query) => {
return new Promise((resolve, reject) => {
const session = driver.session();
const activeSession = session.run(query);
activeSession._createSummary = async (metadata) => {
return metadata;
};
activeSession.subscribe({
onCompleted: async (summary) => {
await session.close();
const result = summary;
resolve(result);
},
onError: async (error) => {
await session.close();
reject(error);
},
});
})
};
const main = async () => {
const driver = neo4j.driver('bolt://localhost:7687');
query = "MATCH (n) RETURN n LIMIT 1;"
console.log('Query:', query);
const results = await runQuery(driver, query);
if (results.hasOwnProperty("run_id")) {
console.log("run_id:", results["run_id"]);
} else {
process.exit(1);
}
await driver.close();
};
main(process.argv[2])
.catch((error) => console.error(error));

View File

@ -0,0 +1,15 @@
{
"name": "testing-neo4j-driver",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.21",
"neo4j-driver": "^4.2.3"
}
}