Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 70b402d

Browse files
authored
Don't collect spans for export until an exporter has been registered. (#277)
Otherwise sampled spans will pile up in memory.
1 parent 8ed07d7 commit 70b402d

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

opencensus/trace/internal/span_exporter_impl.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ SpanExporterImpl* SpanExporterImpl::Get() {
3232
return global_span_exporter_impl;
3333
}
3434

35-
// Create detached worker thread
3635
SpanExporterImpl::SpanExporterImpl(uint32_t buffer_size,
3736
absl::Duration interval)
3837
: buffer_size_(buffer_size), interval_(interval) {}
@@ -49,12 +48,15 @@ void SpanExporterImpl::RegisterHandler(
4948
void SpanExporterImpl::AddSpan(
5049
const std::shared_ptr<opencensus::trace::SpanImpl>& span_impl) {
5150
absl::MutexLock l(&span_mu_);
51+
if (!collect_spans_) return;
5252
spans_.emplace_back(span_impl);
5353
}
5454

5555
void SpanExporterImpl::StartExportThread() {
5656
t_ = std::thread(&SpanExporterImpl::RunWorkerLoop, this);
5757
thread_started_ = true;
58+
absl::MutexLock l(&span_mu_);
59+
collect_spans_ = true;
5860
}
5961

6062
bool SpanExporterImpl::IsBufferFull() const {

opencensus/trace/internal/span_exporter_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class SpanExporterImpl {
8888
std::vector<std::unique_ptr<SpanExporter::Handler>> handlers_
8989
GUARDED_BY(handler_mu_);
9090
bool thread_started_ GUARDED_BY(handler_mu_) = false;
91+
// Don't collect spans until an exporter has been registered.
92+
bool collect_spans_ GUARDED_BY(span_mu_) = false;
9193
std::thread t_ GUARDED_BY(handler_mu_);
9294
};
9395

0 commit comments

Comments
 (0)