Skip to content

Commit

Permalink
Request logger (#17)
Browse files Browse the repository at this point in the history
* Sketch Logger

untested sketch for logger hook. ideally we should provide options to pipe into a file and/or insert into a table, etc.

* DEBUG or SYSLOG testing

* syslog

* No syslog for win32 builds

* fix debug output

* Update MainDistributionPipeline.yml

* Update README.md
  • Loading branch information
lmangani authored Dec 4, 2024
1 parent 6008ce1 commit be1356b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/MainDistributionPipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
name: Main Extension Distribution Pipeline
on:
push:
paths-ignore:
- '**.md'
- '**..yml'
pull_request:
workflow_dispatch:

Expand Down
7 changes: 4 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ LOAD httpserver;
Start the HTTP server providing the `host`, `port` and `auth` parameters.<br>
> * If you want no authentication, just pass an empty string as parameter.<br>
> * If you want the API run in foreground set `DUCKDB_HTTPSERVER_FOREGROUND=1`
> * If you want logs set `DUCKDB_HTTPSERVER_DEBUG` or `DUCKDB_HTTPSERVER_SYSLOG`
#### Basic Auth
```sql
Expand Down Expand Up @@ -90,7 +91,7 @@ D SELECT * FROM duck_flock('SELECT version()', ['http://localhost:9999']);
"version"() │
varchar
├─────────────┤
v1.1.1
v1.1.3
└─────────────┘
```

Expand Down Expand Up @@ -122,7 +123,7 @@ curl -X POST -d "SELECT 'hello', version()" "http://localhost:9999/?default_form
"data": [
[
"hello",
"v1.1.1"
"v1.1.3"
]
],
"rows": 1,
Expand Down Expand Up @@ -153,7 +154,7 @@ D SELECT * FROM read_json_auto('http://localhost:9999/?q=SELECT version()');
"version"() │
│ varchar │
├─────────────┤
│ v1.1.1
│ v1.1.3
└─────────────┘
```
Expand Down
2 changes: 1 addition & 1 deletion duckdb
Submodule duckdb updated 480 files
48 changes: 43 additions & 5 deletions src/httpserver_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
#include "duckdb/common/exception/http_exception.hpp"
#include "duckdb/common/allocator.hpp"
#include <chrono>
#include <thread>
#include <memory>
#include <cstdlib>

#ifndef _WIN32
#include <syslog.h>
#endif

#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "httplib.hpp"

// Include yyjson for JSON handling
#include "yyjson.hpp"

#include <thread>
#include <memory>
#include <cstdlib>
#include "play.h"

using namespace duckdb_yyjson; // NOLINT
Expand Down Expand Up @@ -384,6 +386,42 @@ void HttpServerStart(DatabaseInstance& db, string_t host, int32_t port, string_t

string host_str = host.GetString();


#ifndef _WIN32
const char* debug_env = std::getenv("DUCKDB_HTTPSERVER_DEBUG");
const char* use_syslog = std::getenv("DUCKDB_HTTPSERVER_SYSLOG");

if (debug_env != nullptr && std::string(debug_env) == "1") {
global_state.server->set_logger([](const duckdb_httplib_openssl::Request& req, const duckdb_httplib_openssl::Response& res) {
time_t now_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
char timestr[32];
strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&now_time));
// Use \r\n for consistent line endings
fprintf(stdout, "[%s] %s %s - %d - from %s:%d\r\n",
timestr,
req.method.c_str(),
req.path.c_str(),
res.status,
req.remote_addr.c_str(),
req.remote_port);
fflush(stdout);
});
} else if (use_syslog != nullptr && std::string(use_syslog) == "1") {
openlog("duckdb-httpserver", LOG_PID | LOG_NDELAY, LOG_LOCAL0);
global_state.server->set_logger([](const duckdb_httplib_openssl::Request& req, const duckdb_httplib_openssl::Response& res) {
syslog(LOG_INFO, "%s %s - %d - from %s:%d",
req.method.c_str(),
req.path.c_str(),
res.status,
req.remote_addr.c_str(),
req.remote_port);
});
std::atexit([]() {
closelog();
});
}
#endif

const char* run_in_same_thread_env = std::getenv("DUCKDB_HTTPSERVER_FOREGROUND");
bool run_in_same_thread = (run_in_same_thread_env != nullptr && std::string(run_in_same_thread_env) == "1");

Expand Down

0 comments on commit be1356b

Please sign in to comment.