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

Commit c64764c

Browse files
authored
Add test for stats stdout exporter. (#158)
1 parent a0e7708 commit c64764c

File tree

5 files changed

+106
-24
lines changed

5 files changed

+106
-24
lines changed

opencensus/exporters/stats/stdout/BUILD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,16 @@ cc_library(
3131
"@com_google_absl//absl/time",
3232
],
3333
)
34+
35+
cc_test(
36+
name = "stdout_exporter_test",
37+
srcs = ["internal/stdout_exporter_test.cc"],
38+
copts = TEST_COPTS,
39+
deps = [
40+
":stdout_exporter",
41+
"//opencensus/stats",
42+
"//opencensus/stats:test_utils",
43+
"@com_google_absl//absl/time",
44+
"@com_google_googletest//:gtest_main",
45+
],
46+
)

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ std::string DataToString(const opencensus::stats::Distribution& data) {
4545
return output;
4646
}
4747

48-
} // namespace
49-
50-
class StdoutExporter::Handler
51-
: public opencensus::stats::StatsExporter::Handler {
48+
class Handler : public opencensus::stats::StatsExporter::Handler {
5249
public:
50+
Handler(std::ostream* stream) : stream_(stream) {}
51+
5352
void ExportViewData(
5453
const std::vector<std::pair<opencensus::stats::ViewDescriptor,
5554
opencensus::stats::ViewData>>& data) override;
@@ -61,15 +60,11 @@ class StdoutExporter::Handler
6160
const opencensus::stats::ViewDescriptor& descriptor,
6261
absl::Time start_time, absl::Time end_time,
6362
const opencensus::stats::ViewData::DataMap<DataValueT>& data);
64-
};
6563

66-
// static
67-
void StdoutExporter::Register() {
68-
opencensus::stats::StatsExporter::RegisterPushHandler(
69-
absl::make_unique<StdoutExporter::Handler>());
70-
}
64+
std::ostream* stream_;
65+
};
7166

72-
void StdoutExporter::Handler::ExportViewData(
67+
void Handler::ExportViewData(
7368
const std::vector<std::pair<opencensus::stats::ViewDescriptor,
7469
opencensus::stats::ViewData>>& data) {
7570
for (const auto& datum : data) {
@@ -92,18 +87,17 @@ void StdoutExporter::Handler::ExportViewData(
9287
}
9388

9489
template <typename DataValueT>
95-
void StdoutExporter::Handler::ExportViewDataImpl(
90+
void Handler::ExportViewDataImpl(
9691
const opencensus::stats::ViewDescriptor& descriptor, absl::Time start_time,
9792
absl::Time end_time,
9893
const opencensus::stats::ViewData::DataMap<DataValueT>& data) {
9994
if (data.size() == 0) {
100-
std::cout << absl::StrCat("No data for view \"", descriptor.name(),
101-
"\" from ", absl::FormatTime(start_time),
102-
".\n\n");
95+
*stream_ << absl::StrCat("No data for view \"", descriptor.name(),
96+
"\" from ", absl::FormatTime(start_time), ".\n\n");
10397
return;
10498
}
105-
// Build a string so we can write it to cout in one shot to minimize crosstalk
106-
// if multiple threads write to cout simultaneously.
99+
// Build a string so we can write it in one shot to minimize crosstalk if
100+
// multiple threads write to stream_ simultaneously.
107101
std::string output = absl::StrCat("Data for view \"", descriptor.name(),
108102
"\" from ", absl::FormatTime(start_time),
109103
" to ", absl::FormatTime(end_time), ":\n");
@@ -116,7 +110,15 @@ void StdoutExporter::Handler::ExportViewDataImpl(
116110
absl::StrAppend(&output, DataToString(row.second));
117111
}
118112
absl::StrAppend(&output, "\n");
119-
std::cout << output;
113+
*stream_ << output;
114+
}
115+
116+
} // namespace
117+
118+
// static
119+
void StdoutExporter::Register(std::ostream* stream) {
120+
opencensus::stats::StatsExporter::RegisterPushHandler(
121+
absl::make_unique<Handler>(stream));
120122
}
121123

122124
} // namespace stats
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "opencensus/exporters/stats/stdout/stdout_exporter.h"
16+
17+
#include <iostream>
18+
#include <sstream>
19+
20+
#include "absl/time/clock.h"
21+
#include "gmock/gmock.h"
22+
#include "gtest/gtest.h"
23+
#include "opencensus/stats/stats.h"
24+
#include "opencensus/stats/stats_exporter.h"
25+
#include "opencensus/stats/testing/test_utils.h"
26+
27+
namespace opencensus {
28+
namespace stats {
29+
30+
class StatsExporterTest {
31+
public:
32+
static constexpr auto& ExportForTesting = StatsExporter::ExportForTesting;
33+
};
34+
35+
} // namespace stats
36+
} // namespace opencensus
37+
38+
namespace {
39+
40+
using ::testing::HasSubstr;
41+
42+
TEST(StdoutExporterTest, Export) {
43+
std::stringstream s;
44+
::opencensus::exporters::stats::StdoutExporter::Register(&s);
45+
46+
auto key = ::opencensus::stats::TagKey::Register("test_key");
47+
auto measure = ::opencensus::stats::MeasureDouble::Register(
48+
"example.com/Test/Measure", "Test description.", "1");
49+
auto descriptor = ::opencensus::stats::ViewDescriptor()
50+
.set_name("example.com/Test/View")
51+
.set_measure("example.com/Test/Measure")
52+
.set_aggregation(opencensus::stats::Aggregation::Sum())
53+
.add_column(key);
54+
descriptor.RegisterForExport();
55+
::opencensus::stats::Record({{measure, 123456.}}, {{key, "value1"}});
56+
57+
::opencensus::stats::testing::TestUtils::Flush();
58+
::opencensus::stats::StatsExporterTest::ExportForTesting();
59+
60+
const std::string str = s.str();
61+
std::cout << str;
62+
EXPECT_THAT(str, HasSubstr("test_key"));
63+
EXPECT_THAT(str, HasSubstr("value1"));
64+
EXPECT_THAT(str, HasSubstr("123456"));
65+
}
66+
67+
} // namespace

opencensus/exporters/stats/stdout/stdout_exporter.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@
1515
#ifndef OPENCENSUS_EXPORTERS_STATS_STDOUT_STDOUT_EXPORTER_H_
1616
#define OPENCENSUS_EXPORTERS_STATS_STDOUT_STDOUT_EXPORTER_H_
1717

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

22-
// A stats exporter that exports views registered with StatsExporter to stdout.
23-
// StdoutExporter is immutable.
24+
// A stats exporter that writes to stdout.
2425
class StdoutExporter {
2526
public:
26-
static void Register();
27-
28-
private:
29-
class Handler;
27+
StdoutExporter() = delete;
28+
static void Register(std::ostream* stream = &std::cout);
3029
};
3130

3231
} // namespace stats

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Handler : public ::opencensus::trace::exporter::SpanExporter::Handler {
4444

4545
} // namespace
4646

47+
// static
4748
void StdoutExporter::Register(std::ostream* stream) {
4849
::opencensus::trace::exporter::SpanExporter::RegisterHandler(
4950
absl::make_unique<Handler>(stream));

0 commit comments

Comments
 (0)