Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ client never generates one, never reads or writes the file, and sends nothing.
| **Returns immediately** | The HTTP call runs on one thread the client owns. A game loop can report from its main thread and no frame waits on the network. |
| **Never throws** | Nothing in the client throws. A server that is down, slow, or rejecting the key — or a machine with no `curl` — is a dropped report, not an exception or a crash in your program. Drops are passed to the optional `Logger` (a `std::function<void(const std::string&)>`, last constructor argument), otherwise not mentioned at all. |
| **Bounded** | At most 256 reports wait to be sent; past that, new ones are dropped. A trace server that is unreachable for a week costs a few kilobytes, not your memory. |
| **`close()` drains** | Reports already queued get up to the client timeout (5 s total) to be sent before the thread stops, so a CLI that reports and exits at once does not lose its event. Still bounded: an unreachable server delays exit by at most the timeout — a request still in flight then is abandoned and its `curl` process killed. `close(seconds)` takes a shorter bound. Once `close()` returns, the `Logger` is never called again. |
| **`close()` drains** | Reports already queued get up to the client timeout (5 s total) to be sent before the thread stops, so a CLI that reports and exits at once does not lose its event. Still bounded: an unreachable server delays exit by at most the timeout — a request still in flight then is abandoned and its `curl` process killed. `close(seconds)` takes another bound: a NaN or negative one waits for nothing, and `close(INFINITY)` waits until the queue is sent, each request still bounded by the timeout. Once `close()` returns, the `Logger` is never called again. |
| **Fits the server's limits** | Names and tag keys/values are cut to 255 bytes (never mid-character), at most 32 tags are sent, blank tag keys are dropped, invalid UTF-8 becomes U+FFFD, and NaN or infinite values are left out — so a report is never rejected as a whole for one bad field. A report whose name is empty or blank is not an event at all: it is ignored, without a mention to the `Logger`. |

## Turning it off
Expand Down
33 changes: 33 additions & 0 deletions test/test_trace_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,37 @@ static void closeIsPromptAndIdempotent() {
client.report("after-close"); // a no-op, not a crash
}

static void closeWithABoundTooLongForTheClockSendsEverythingQueued() {
// Infinity, or a bound whose deadline the clock cannot hold, means "as
// long as it takes" -- not a deadline that wrapped round into the past.
const double bounds[] = {HUGE_VAL, std::numeric_limits<double>::max(), 1e300, 1e12};
StubServer server;
std::size_t expected = 0;
for (std::size_t i = 0; i < sizeof bounds / sizeof bounds[0]; ++i) {
trace_client::TraceClient client(server.baseUrl(), "MyCli", "1.2.3", "k");
for (int n = 0; n < 5; ++n) client.report("startup", {{"run", std::to_string(n)}});
client.close(bounds[i]);
expected += 5;
CHECK_EQ(expected, server.received().size());
}
}

static void closeWithANegativeOrNanBoundWaitsForNothing() {
const double bounds[] = {std::nan(""), -1.0, -HUGE_VAL};
for (std::size_t i = 0; i < sizeof bounds / sizeof bounds[0]; ++i) {
StubServer server;
server.hold(); // never answers while held
trace_client::TraceClient client(server.baseUrl(), "MyCli", "1.2.3", "k");
client.report("startup");
client.report("second");
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
client.close(bounds[i]);
CHECK(secondsSince(start) < 1.0);
CHECK(!client.isEnabled());
server.release();
}
}

// ---------------------------------------------------------------- installation ID

static std::string tempBase() {
Expand Down Expand Up @@ -1376,6 +1407,8 @@ int main() {
{"everyAllocationTheConstructorMakesMayFailWithoutAborting", everyAllocationTheConstructorMakesMayFailWithoutAborting},
{"closeReturnsWithinTheTimeoutWhenTheServerHangs", closeReturnsWithinTheTimeoutWhenTheServerHangs},
{"closeIsPromptAndIdempotent", closeIsPromptAndIdempotent},
{"closeWithABoundTooLongForTheClockSendsEverythingQueued", closeWithABoundTooLongForTheClockSendsEverythingQueued},
{"closeWithANegativeOrNanBoundWaitsForNothing", closeWithANegativeOrNanBoundWaitsForNothing},
{"reportTagsEveryEventWithTheProgramVersionTrimmed", reportTagsEveryEventWithTheProgramVersionTrimmed},
{"anEventsOwnVersionTagWinsOverTheProgramVersion", anEventsOwnVersionTagWinsOverTheProgramVersion},
{"theCallersTagsAreNeverModified", theCallersTagsAreNeverModified},
Expand Down
15 changes: 12 additions & 3 deletions trace_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,9 @@ class TraceClient {
/**
* Stops the sending thread, giving reports already queued up to
* timeoutSeconds in total to be sent first; whatever is still in flight
* then is abandoned. Safe to call more than once, and on a disabled client.
* then is abandoned. A NaN or negative timeoutSeconds waits for nothing;
* an infinite one waits until the queue is sent. Safe to call more than
* once, and on a disabled client.
*/
void close(double timeoutSeconds = TIMEOUT_SECONDS) {
std::shared_ptr<detail::Shared> shared;
Expand All @@ -1047,10 +1049,17 @@ class TraceClient {
std::unique_lock<std::mutex> lock(shared->mutex);
shared->closing = true;
shared->changed.notify_all();
// NaN and negative bounds wait for nothing. Past about three years
// (infinity included) the deadline would overflow the clock, so
// such a bound waits until the queue is sent; each request is
// still bounded by curl's own timeout.
double seconds = timeoutSeconds > 0 ? timeoutSeconds : 0;
bool unbounded = seconds > 1e8;
std::chrono::steady_clock::time_point deadline = std::chrono::steady_clock::now()
+ std::chrono::milliseconds(static_cast<long long>(timeoutSeconds * 1000));
+ std::chrono::milliseconds(unbounded ? 0 : static_cast<long long>(seconds * 1000));
while (!shared->done) {
if (shared->changed.wait_until(lock, deadline) == std::cv_status::timeout) break;
if (unbounded) shared->changed.wait(lock);
else if (shared->changed.wait_until(lock, deadline) == std::cv_status::timeout) break;
}
if (shared->done) {
lock.unlock();
Expand Down
Loading