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

Commit 6cb57aa

Browse files
authored
Bring back SpanExporter::ExportForTesting. (#137)
- Make StdoutExporter take an ostream. - Make stdout_exporter_test faster (no sleep) and check that the added annotation appears in the output.
1 parent 20b6f06 commit 6cb57aa

7 files changed

Lines changed: 81 additions & 34 deletions

File tree

opencensus/exporters/trace/stdout/internal/stdout_exporter.cc

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,28 @@
2525
namespace opencensus {
2626
namespace exporters {
2727
namespace trace {
28+
namespace {
29+
30+
class Handler : public ::opencensus::trace::exporter::SpanExporter::Handler {
31+
public:
32+
Handler(std::ostream* stream) : stream_(stream) {}
2833

29-
class StdoutExporter::Handler
30-
: public ::opencensus::trace::exporter::SpanExporter::Handler {
3134
void Export(const std::vector<::opencensus::trace::exporter::SpanData>& spans)
32-
override;
35+
override {
36+
for (const auto& span : spans) {
37+
*stream_ << span.DebugString() << "\n";
38+
}
39+
}
40+
41+
private:
42+
std::ostream* stream_;
3343
};
3444

35-
void StdoutExporter::Handler::Export(
36-
const std::vector<::opencensus::trace::exporter::SpanData>& spans) {
37-
for (const auto& span : spans) {
38-
std::cout << span.DebugString() << "\n";
39-
}
40-
}
45+
} // namespace
4146

42-
void StdoutExporter::Register() {
47+
void StdoutExporter::Register(std::ostream* stream) {
4348
::opencensus::trace::exporter::SpanExporter::RegisterHandler(
44-
absl::make_unique<Handler>());
49+
absl::make_unique<Handler>(stream));
4550
}
4651

4752
} // namespace trace

opencensus/exporters/trace/stdout/internal/stdout_exporter_test.cc

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,51 @@
1414

1515
#include "opencensus/exporters/trace/stdout/stdout_exporter.h"
1616

17+
#include <iostream>
18+
#include <sstream>
19+
1720
#include "absl/time/clock.h"
21+
#include "gmock/gmock.h"
1822
#include "gtest/gtest.h"
23+
#include "opencensus/trace/exporter/span_exporter.h"
1924
#include "opencensus/trace/internal/local_span_store.h"
2025
#include "opencensus/trace/span.h"
2126

2227
namespace opencensus {
23-
namespace exporters {
2428
namespace trace {
29+
namespace exporter {
30+
31+
class SpanExporterTestPeer {
32+
public:
33+
static constexpr auto& ExportForTesting = SpanExporter::ExportForTesting;
34+
};
35+
36+
} // namespace exporter
37+
} // namespace trace
38+
} // namespace opencensus
39+
2540
namespace {
2641

27-
TEST(TraceExporterTest, ExportTrace) {
28-
StdoutExporter::Register();
42+
using ::testing::HasSubstr;
43+
44+
TEST(StdoutExporterTest, Export) {
45+
std::stringstream s;
46+
::opencensus::exporters::trace::StdoutExporter::Register(&s);
2947
static ::opencensus::trace::AlwaysSampler sampler;
3048
::opencensus::trace::StartSpanOptions opts = {&sampler};
3149

3250
auto span1 = ::opencensus::trace::Span::StartSpan("Span1", nullptr, opts);
33-
absl::SleepFor(absl::Milliseconds(100));
3451
auto span2 = ::opencensus::trace::Span::StartSpan("Span2", &span1, opts);
35-
absl::SleepFor(absl::Milliseconds(200));
3652
auto span3 = ::opencensus::trace::Span::StartSpan("Span3", &span2, opts);
37-
absl::SleepFor(absl::Milliseconds(300));
53+
span3.AddAnnotation("Needle.");
3854
span3.End();
3955
span2.End();
4056
span1.End();
4157

42-
// Wait for exporter.
43-
absl::SleepFor(absl::Milliseconds(5200));
58+
opencensus::trace::exporter::SpanExporterTestPeer::ExportForTesting();
59+
const std::string str = s.str();
60+
std::cout << str;
61+
EXPECT_THAT(str, HasSubstr("Needle."));
4462
}
4563

4664
} // namespace
47-
} // namespace trace
48-
} // namespace exporters
49-
} // namespace opencensus

opencensus/exporters/trace/stdout/stdout_exporter.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
#ifndef OPENCENSUS_EXPORTERS_TRACE_STDOUT_STDOUT_EXPORTER_H_
1616
#define OPENCENSUS_EXPORTERS_TRACE_STDOUT_STDOUT_EXPORTER_H_
1717

18+
#include <iostream>
19+
1820
namespace opencensus {
1921
namespace exporters {
2022
namespace trace {
2123

2224
class StdoutExporter {
2325
public:
24-
static void Register();
25-
26-
private:
27-
class Handler;
26+
StdoutExporter() = delete;
27+
static void Register(std::ostream* stream = &std::cout);
2828
};
2929

3030
} // namespace trace

opencensus/trace/exporter/span_exporter.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ namespace opencensus {
2424
namespace trace {
2525
namespace exporter {
2626

27-
class SpanExporterImpl;
28-
29-
// SpanExporter tracks Exporters. Thread-safe.
27+
// SpanExporter allows Exporters to register. Thread-safe.
3028
class SpanExporter final {
3129
public:
3230
// Handlers allow different tracing services to export recorded data for
33-
// sampled spans in their own format. The exporter should provide a static
31+
// sampled spans in their own format. Every exporter must provide a static
3432
// Register() method that takes any arguments needed by the exporter (e.g. a
3533
// URL to export to) and calls SpanExporter::RegisterHandler itself.
3634
class Handler {
@@ -41,6 +39,12 @@ class SpanExporter final {
4139

4240
// This should only be called by Handler's Register() method.
4341
static void RegisterHandler(std::unique_ptr<Handler> handler);
42+
43+
private:
44+
friend class SpanExporterTestPeer;
45+
46+
// Forces an export, only for testing purposes.
47+
static void ExportForTesting();
4448
};
4549

4650
} // namespace exporter

opencensus/trace/internal/span_exporter.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ namespace opencensus {
2323
namespace trace {
2424
namespace exporter {
2525

26+
// static
2627
void SpanExporter::RegisterHandler(std::unique_ptr<Handler> handler) {
2728
SpanExporterImpl::Get()->RegisterHandler(std::move(handler));
2829
}
2930

31+
// static
32+
void SpanExporter::ExportForTesting() {
33+
SpanExporterImpl::Get()->ExportForTesting();
34+
}
35+
3036
} // namespace exporter
3137
} // namespace trace
3238
} // namespace opencensus

opencensus/trace/internal/span_exporter_impl.cc

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ bool SpanExporterImpl::IsBufferFull() const {
6464

6565
void SpanExporterImpl::RunWorkerLoop() {
6666
std::vector<opencensus::trace::exporter::SpanData> span_data_;
67-
std::vector<std::shared_ptr<opencensus::trace::SpanImpl>> spans_copy_;
67+
std::vector<std::shared_ptr<opencensus::trace::SpanImpl>> batch_;
6868
// Thread loops forever.
6969
// TODO: Add in shutdown mechanism.
7070
absl::Time next_forced_export_time = absl::Now() + interval_;
@@ -79,13 +79,13 @@ void SpanExporterImpl::RunWorkerLoop() {
7979
if (spans_.empty()) {
8080
continue;
8181
}
82-
std::swap(spans_copy_, spans_);
82+
std::swap(batch_, spans_);
8383
}
84-
for (const auto& span : spans_copy_) {
84+
for (const auto& span : batch_) {
8585
span_data_.emplace_back(span->ToSpanData());
8686
}
87+
batch_.clear();
8788
Export(span_data_);
88-
spans_copy_.clear();
8989
span_data_.clear();
9090
}
9191
}
@@ -98,6 +98,19 @@ void SpanExporterImpl::Export(const std::vector<SpanData>& span_data) {
9898
}
9999
}
100100

101+
void SpanExporterImpl::ExportForTesting() {
102+
std::vector<opencensus::trace::exporter::SpanData> span_data_;
103+
std::vector<std::shared_ptr<opencensus::trace::SpanImpl>> batch_;
104+
{
105+
absl::MutexLock l(&span_mu_);
106+
std::swap(batch_, spans_);
107+
}
108+
for (const auto& span : batch_) {
109+
span_data_.emplace_back(span->ToSpanData());
110+
}
111+
Export(span_data_);
112+
}
113+
101114
} // namespace exporter
102115
} // namespace trace
103116
} // namespace opencensus

opencensus/trace/internal/span_exporter_impl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class SpanExporterImpl {
4949
// SpanData will take place at a later time via the background thread. This
5050
// is intended to be called at the Span::End().
5151
void AddSpan(const std::shared_ptr<opencensus::trace::SpanImpl>& span_impl);
52+
5253
// Registers a handler with the exporter. This is intended to be done at
5354
// initialization.
5455
void RegisterHandler(std::unique_ptr<SpanExporter::Handler> handler);
@@ -63,13 +64,16 @@ class SpanExporterImpl {
6364
SpanExporterImpl& operator=(const SpanExporterImpl&) = delete;
6465
SpanExporterImpl& operator=(SpanExporterImpl&&) = delete;
6566
friend class Span;
67+
friend class SpanExporter; // For ExportForTesting() only.
6668

6769
void StartExportThread() EXCLUSIVE_LOCKS_REQUIRED(handler_mu_);
6870
void RunWorkerLoop();
6971

7072
// Calls all registered handlers and exports the spans contained in span_data.
7173
void Export(const std::vector<SpanData>& span_data);
7274

75+
// Only for testing purposes: runs the export on the current thread and
76+
// returns when complete.
7377
void ExportForTesting();
7478

7579
// Returns true if the spans_ buffer has filled up.

0 commit comments

Comments
 (0)