forked from Query-farm/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_farm_telemetry.cpp
More file actions
106 lines (86 loc) · 3.03 KB
/
Copy pathquery_farm_telemetry.cpp
File metadata and controls
106 lines (86 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "query_farm_telemetry.hpp"
#include <thread>
#include "duckdb.hpp"
#include "duckdb/common/http_util.hpp"
#include "yyjson.hpp"
#include "duckdb/main/extension_helper.hpp"
#include "duckdb/main/config.hpp"
#include <cstdlib>
#include <future>
using namespace duckdb_yyjson; // NOLINT
namespace duckdb
{
namespace
{
// Function to send the actual HTTP request
void sendHTTPRequest(shared_ptr<DatabaseInstance> db, char *json_body, size_t json_body_size)
{
const string TARGET_URL("https://duckdb-in.query-farm.services/");
HTTPHeaders headers;
headers.Insert("Content-Type", "application/json");
auto &http_util = HTTPUtil::Get(*db);
unique_ptr<HTTPParams> params = http_util.InitializeParameters(*db, TARGET_URL);
PostRequestInfo post_request(TARGET_URL, headers, *params, reinterpret_cast<const_data_ptr_t>(json_body),
json_body_size);
try
{
auto response = http_util.Request(post_request);
}
catch (const std::exception &e)
{
// ignore all errors.
}
free(json_body);
return;
}
} // namespace
INTERNAL_FUNC void QueryFarmSendTelemetry(ExtensionLoader &loader, const string &extension_name,
const string &extension_version)
{
const char *opt_out = std::getenv("QUERY_FARM_TELEMETRY_OPT_OUT");
if (opt_out != nullptr)
{
return;
}
try
{
ExtensionHelper::TryAutoLoadExtension(loader.GetDatabaseInstance(), "httpfs");
}
catch (...)
{
return;
}
if (!loader.GetDatabaseInstance().ExtensionIsLoaded("httpfs"))
{
return;
}
// Initialize the telemetry sender
auto doc = yyjson_mut_doc_new(nullptr);
auto result_obj = yyjson_mut_obj(doc);
yyjson_mut_doc_set_root(doc, result_obj);
auto platform = DuckDB::Platform();
yyjson_mut_obj_add_str(doc, result_obj, "extension_name", extension_name.c_str());
yyjson_mut_obj_add_str(doc, result_obj, "extension_version", extension_version.c_str());
yyjson_mut_obj_add_str(doc, result_obj, "user_agent", "query-farm/20260201");
yyjson_mut_obj_add_str(doc, result_obj, "duckdb_platform", platform.c_str());
yyjson_mut_obj_add_str(doc, result_obj, "duckdb_library_version", DuckDB::LibraryVersion());
yyjson_mut_obj_add_str(doc, result_obj, "duckdb_release_codename", DuckDB::ReleaseCodename());
yyjson_mut_obj_add_str(doc, result_obj, "duckdb_source_id", DuckDB::SourceID());
size_t telemetry_len;
auto telemetry_data =
yyjson_mut_val_write_opts(result_obj, YYJSON_WRITE_ALLOW_INF_AND_NAN, NULL, &telemetry_len, nullptr);
if (telemetry_data == nullptr)
{
throw SerializationException("Failed to serialize telemetry data.");
}
yyjson_mut_doc_free(doc);
#ifndef __EMSCRIPTEN__
[[maybe_unused]] auto _ = std::async(
std::launch::async, [db_ptr = loader.GetDatabaseInstance().shared_from_this(), json = telemetry_data,
len = telemetry_len]() mutable
{ sendHTTPRequest(std::move(db_ptr), json, len); });
#else
sendHTTPRequest(loader.GetDatabaseInstance().shared_from_this(), telemetry_data, telemetry_len);
#endif
}
} // namespace duckdb