diff --git a/src/memgraph.cpp b/src/memgraph.cpp index afcabf99b..bbffd54e9 100644 --- a/src/memgraph.cpp +++ b/src/memgraph.cpp @@ -458,6 +458,8 @@ struct SessionData { memgraph::utils::Synchronized *auth; #endif + // NOTE: run_id should be const but that complicates code a lot. + std::optional run_id; }; inline constexpr std::string_view default_user_role_regex = "[a-zA-Z0-9_.+-@]+"; @@ -860,7 +862,8 @@ class BoltSession final : public memgraph::communication::bolt::Sessionaudit_log), #endif - endpoint_(endpoint) { + endpoint_(endpoint), + run_id_(data->run_id) { } using memgraph::communication::bolt::Session run_id_; }; using ServerT = memgraph::communication::v2::Server; @@ -1276,6 +1289,7 @@ int main(int argc, char **argv) { if (FLAGS_telemetry_enabled) { telemetry.emplace("https://telemetry.memgraph.com/88b5e7e8-746a-11e8-9f85-538a9e9690cc/", data_directory / "telemetry", std::chrono::minutes(10)); + session_data.run_id = telemetry->GetRunId(); telemetry->AddCollector("storage", [&db]() -> nlohmann::json { auto info = db.GetInfo(); return {{"vertices", info.vertex_count}, {"edges", info.edge_count}}; diff --git a/src/telemetry/telemetry.cpp b/src/telemetry/telemetry.cpp index 028cd9384..464e16d9f 100644 --- a/src/telemetry/telemetry.cpp +++ b/src/telemetry/telemetry.cpp @@ -59,6 +59,8 @@ void Telemetry::AddCollector(const std::string &name, const std::function &func); + /// Required to expose run_id to Bolt server. + std::string GetRunId() const; + ~Telemetry(); Telemetry(const Telemetry &) = delete; diff --git a/tests/e2e/telemetry/README.md b/tests/e2e/telemetry/README.md new file mode 100644 index 000000000..4d11c1d7b --- /dev/null +++ b/tests/e2e/telemetry/README.md @@ -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: +``` diff --git a/tests/e2e/telemetry/get-run_id.js b/tests/e2e/telemetry/get-run_id.js new file mode 100644 index 000000000..47b341703 --- /dev/null +++ b/tests/e2e/telemetry/get-run_id.js @@ -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)); diff --git a/tests/e2e/telemetry/package.json b/tests/e2e/telemetry/package.json new file mode 100644 index 000000000..0956d2486 --- /dev/null +++ b/tests/e2e/telemetry/package.json @@ -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" + } +}