Skip to content

Commit 25636f9

Browse files
committed
fix: telemetry update
1 parent 9eccde1 commit 25636f9

File tree

1 file changed

+95
-77
lines changed

1 file changed

+95
-77
lines changed

src/query_farm_telemetry.cpp

Lines changed: 95 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,85 +9,103 @@
99
#include <future>
1010
using namespace duckdb_yyjson; // NOLINT
1111

12-
namespace duckdb {
12+
namespace duckdb
13+
{
14+
15+
namespace
16+
{
17+
18+
// Function to send the actual HTTP request
19+
void sendHTTPRequest(shared_ptr<DatabaseInstance> db, char *json_body, size_t json_body_size)
20+
{
21+
const string TARGET_URL("https://duckdb-in.query-farm.services/");
22+
23+
HTTPHeaders headers;
24+
headers.Insert("Content-Type", "application/json");
25+
26+
auto &http_util = HTTPUtil::Get(*db);
27+
unique_ptr<HTTPParams> params = http_util.InitializeParameters(*db, TARGET_URL);
28+
29+
PostRequestInfo post_request(TARGET_URL, headers, *params, reinterpret_cast<const_data_ptr_t>(json_body),
30+
json_body_size);
31+
try
32+
{
33+
auto response = http_util.Request(post_request);
34+
}
35+
catch (const std::exception &e)
36+
{
37+
// ignore all errors.
38+
}
39+
40+
free(json_body);
41+
return;
42+
}
43+
44+
} // namespace
45+
46+
INTERNAL_FUNC void QueryFarmSendTelemetry(ExtensionLoader &loader, const string &extension_name,
47+
const string &extension_version)
48+
{
49+
const char *opt_out = std::getenv("QUERY_FARM_TELEMETRY_OPT_OUT");
50+
if (opt_out != nullptr)
51+
{
52+
return;
53+
}
54+
55+
auto &dbconfig = DBConfig::GetConfig(loader.GetDatabaseInstance());
56+
auto old_value = dbconfig.options.autoinstall_known_extensions;
57+
dbconfig.options.autoinstall_known_extensions = false;
58+
try
59+
{
60+
ExtensionHelper::AutoLoadExtension(loader.GetDatabaseInstance(), "httpfs");
61+
}
62+
catch (...)
63+
{
64+
dbconfig.options.autoinstall_known_extensions = old_value;
65+
return;
66+
}
1367

14-
namespace {
15-
16-
// Function to send the actual HTTP request
17-
void sendHTTPRequest(shared_ptr<DatabaseInstance> db, char *json_body, size_t json_body_size) {
18-
const string TARGET_URL("https://duckdb-in.query-farm.services/");
19-
20-
HTTPHeaders headers;
21-
headers.Insert("Content-Type", "application/json");
22-
23-
auto &http_util = HTTPUtil::Get(*db);
24-
unique_ptr<HTTPParams> params = http_util.InitializeParameters(*db, TARGET_URL);
25-
26-
PostRequestInfo post_request(TARGET_URL, headers, *params, reinterpret_cast<const_data_ptr_t>(json_body),
27-
json_body_size);
28-
try {
29-
auto response = http_util.Request(post_request);
30-
} catch (const std::exception &e) {
31-
// ignore all errors.
32-
}
33-
34-
free(json_body);
35-
return;
36-
}
37-
38-
} // namespace
39-
40-
INTERNAL_FUNC void QueryFarmSendTelemetry(ExtensionLoader &loader, const string &extension_name,
41-
const string &extension_version) {
42-
const char *opt_out = std::getenv("QUERY_FARM_TELEMETRY_OPT_OUT");
43-
if (opt_out != nullptr) {
44-
return;
45-
}
46-
47-
auto &dbconfig = DBConfig::GetConfig(loader.GetDatabaseInstance());
48-
auto old_value = dbconfig.options.autoinstall_known_extensions;
49-
dbconfig.options.autoinstall_known_extensions = false;
50-
try {
51-
ExtensionHelper::AutoLoadExtension(loader.GetDatabaseInstance(), "httpfs");
52-
} catch (...) {
5368
dbconfig.options.autoinstall_known_extensions = old_value;
54-
return;
69+
if (!loader.GetDatabaseInstance().ExtensionIsLoaded("httpfs"))
70+
{
71+
return;
72+
}
73+
74+
// Initialize the telemetry sender
75+
auto doc = yyjson_mut_doc_new(nullptr);
76+
77+
auto result_obj = yyjson_mut_obj(doc);
78+
yyjson_mut_doc_set_root(doc, result_obj);
79+
80+
auto platform = DuckDB::Platform();
81+
82+
yyjson_mut_obj_add_str(doc, result_obj, "extension_name", extension_name.c_str());
83+
yyjson_mut_obj_add_str(doc, result_obj, "extension_version", extension_version.c_str());
84+
yyjson_mut_obj_add_str(doc, result_obj, "user_agent", "query-farm/20251011");
85+
yyjson_mut_obj_add_str(doc, result_obj, "duckdb_platform", platform.c_str());
86+
yyjson_mut_obj_add_str(doc, result_obj, "duckdb_library_version", DuckDB::LibraryVersion());
87+
yyjson_mut_obj_add_str(doc, result_obj, "duckdb_release_codename", DuckDB::ReleaseCodename());
88+
yyjson_mut_obj_add_str(doc, result_obj, "duckdb_source_id", DuckDB::SourceID());
89+
90+
size_t telemetry_len;
91+
auto telemetry_data =
92+
yyjson_mut_val_write_opts(result_obj, YYJSON_WRITE_ALLOW_INF_AND_NAN, NULL, &telemetry_len, nullptr);
93+
94+
if (telemetry_data == nullptr)
95+
{
96+
throw SerializationException("Failed to serialize telemetry data.");
97+
}
98+
99+
yyjson_mut_doc_free(doc);
100+
101+
#ifndef __EMSCRIPTEN__
102+
[[maybe_unused]] auto _ = std::async(
103+
std::launch::async, [db_ptr = loader.GetDatabaseInstance().shared_from_this(), json = telemetry_data,
104+
len = telemetry_len]() mutable
105+
{ sendHTTPRequest(std::move(db_ptr), json, len); });
106+
#else
107+
sendHTTPRequest(loader.GetDatabaseInstance().shared_from_this(), telemetry_data, telemetry_len);
108+
#endif
55109
}
56110

57-
dbconfig.options.autoinstall_known_extensions = old_value;
58-
if (!loader.GetDatabaseInstance().ExtensionIsLoaded("httpfs")) {
59-
return;
60-
}
61-
62-
// Initialize the telemetry sender
63-
auto doc = yyjson_mut_doc_new(nullptr);
64-
65-
auto result_obj = yyjson_mut_obj(doc);
66-
yyjson_mut_doc_set_root(doc, result_obj);
67-
68-
auto platform = DuckDB::Platform();
69-
70-
yyjson_mut_obj_add_str(doc, result_obj, "extension_name", extension_name.c_str());
71-
yyjson_mut_obj_add_str(doc, result_obj, "extension_version", extension_version.c_str());
72-
yyjson_mut_obj_add_str(doc, result_obj, "user_agent", "query-farm/20251011");
73-
yyjson_mut_obj_add_str(doc, result_obj, "duckdb_platform", platform.c_str());
74-
yyjson_mut_obj_add_str(doc, result_obj, "duckdb_library_version", DuckDB::LibraryVersion());
75-
yyjson_mut_obj_add_str(doc, result_obj, "duckdb_release_codename", DuckDB::ReleaseCodename());
76-
yyjson_mut_obj_add_str(doc, result_obj, "duckdb_source_id", DuckDB::SourceID());
77-
78-
size_t telemetry_len;
79-
auto telemetry_data =
80-
yyjson_mut_val_write_opts(result_obj, YYJSON_WRITE_ALLOW_INF_AND_NAN, NULL, &telemetry_len, nullptr);
81-
82-
if (telemetry_data == nullptr) {
83-
throw SerializationException("Failed to serialize telemetry data.");
84-
}
85-
86-
yyjson_mut_doc_free(doc);
87-
88-
[[maybe_unused]] auto _ = std::async(
89-
std::launch::async, [db_ptr = loader.GetDatabaseInstance().shared_from_this(), json = telemetry_data,
90-
len = telemetry_len]() mutable { sendHTTPRequest(std::move(db_ptr), json, len); });
91-
}
92-
93111
} // namespace duckdb

0 commit comments

Comments
 (0)