From b0da0cd33d8b51cdd49764d70f92dc41bc221031 Mon Sep 17 00:00:00 2001 From: Serafin Leschke Date: Thu, 6 Oct 2022 09:29:06 +0200 Subject: [PATCH] feat: Add flushing destructor for Tracer Rational: In short lived applications (e.g. Kubernetes Jobs) most of the time all traces get lost becasue they are not synced. Users need to call `Close` manually, but as all spans hold a shared_ptr on the tracer it is hard to have the correct place to call it. Calling it in the destructor assures all spans will be included in the flush. --- src/tracer.cpp | 7 +++++++ src/tracer.h | 3 +++ 2 files changed, 10 insertions(+) diff --git a/src/tracer.cpp b/src/tracer.cpp index dfd81d6e..4dbcf6a2 100644 --- a/src/tracer.cpp +++ b/src/tracer.cpp @@ -241,6 +241,13 @@ Tracer::Tracer(TracerOptions options, std::shared_ptr writer, options.service, traceTagsPropagationMaxLength(options, *logger_)}); } +Tracer::~Tracer() { + try { + Close(); + } catch (...) { + } +} + std::unique_ptr Tracer::StartSpanWithOptions(ot::string_view operation_name, const ot::StartSpanOptions &options) const noexcept try { diff --git a/src/tracer.h b/src/tracer.h index 4439f084..1a8912be 100644 --- a/src/tracer.h +++ b/src/tracer.h @@ -45,6 +45,9 @@ class Tracer : public ot::Tracer, public std::enable_shared_from_this { Tracer() = delete; + // Destructs tracer and flushes internal buffers + virtual ~Tracer(); + // Starts a new span. std::unique_ptr StartSpanWithOptions(ot::string_view operation_name, const ot::StartSpanOptions &options) const