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

Commit 9f3e03e

Browse files
authored
Get rid of TraceOptions::SetSampled() in favor of WithSampling(). (#285)
1 parent 002dea8 commit 9f3e03e

File tree

6 files changed

+12
-41
lines changed

6 files changed

+12
-41
lines changed

opencensus/trace/internal/running_span_store_test.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@
3838
namespace opencensus {
3939
namespace trace {
4040

41-
class SpanTestPeer {
42-
public:
43-
static void SetSampled(TraceOptions* opts, bool is_sampled) {
44-
opts->SetSampled(is_sampled);
45-
}
46-
};
47-
4841
namespace exporter {
4942
class RunningSpanStoreImplTestPeer {
5043
public:

opencensus/trace/internal/span.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class SpanGenerator {
8989
parent_ctx, has_remote_parent, trace_id, span_id, name,
9090
options.parent_links);
9191
}
92-
trace_options.SetSampled(should_sample);
92+
trace_options = trace_options.WithSampling(should_sample);
9393
}
9494
SpanContext context(trace_id, span_id, trace_options);
9595
SpanImpl* impl = nullptr;

opencensus/trace/internal/span_test.cc

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ class SpanTestPeer {
3535
return span->span_impl_for_test()->parent_span_id();
3636
}
3737

38-
static void SetSampled(TraceOptions* opts, bool is_sampled) {
39-
opts->SetSampled(is_sampled);
40-
}
41-
4238
static exporter::SpanData ToSpanData(Span* span) {
4339
return span->span_impl_for_test()->ToSpanData();
4440
}
@@ -291,10 +287,8 @@ TEST(SpanTest, ChildInheritsTraceOption) {
291287
9, 10, 11, 12, 13, 14, 15, 16};
292288
constexpr uint8_t span_id[] = {1, 0, 0, 0, 0, 0, 0, 11};
293289
// Create a parent context with TraceOptions set to sampled.
294-
TraceOptions trace_options;
295-
SpanTestPeer::SetSampled(&trace_options, true);
296-
SpanContext parent_ctx1{TraceId(trace_id), SpanId(span_id),
297-
TraceOptions(trace_options)};
290+
TraceOptions trace_options = TraceOptions().WithSampling(true);
291+
SpanContext parent_ctx1{TraceId(trace_id), SpanId(span_id), trace_options};
298292

299293
// Create a child Span with default sampling.
300294
auto span1 = Span::StartSpanWithRemoteParent("Span1", parent_ctx1);

opencensus/trace/internal/trace_options.cc

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,11 @@ std::string TraceOptions::ToHex() const {
4242

4343
void TraceOptions::CopyTo(uint8_t* buf) const { memcpy(buf, rep_, kSize); }
4444

45-
void TraceOptions::SetSampled(bool is_sampled) {
46-
rep_[0] = (rep_[0] & ~kIsSampled) | (is_sampled ? kIsSampled : 0);
47-
}
48-
4945
TraceOptions TraceOptions::WithSampling(bool is_sampled) const {
50-
TraceOptions child = *this;
51-
child.SetSampled(is_sampled);
52-
return child;
46+
uint8_t buf[kSize];
47+
CopyTo(buf);
48+
buf[0] = (buf[0] & ~kIsSampled) | (is_sampled ? kIsSampled : 0);
49+
return TraceOptions(buf);
5350
}
5451

5552
} // namespace trace

opencensus/trace/internal/trace_options_test.cc

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,31 @@
1919
namespace opencensus {
2020
namespace trace {
2121

22-
class SpanTestPeer {
23-
public:
24-
static void SetSampled(TraceOptions* opts, bool is_sampled) {
25-
opts->SetSampled(is_sampled);
26-
}
27-
};
28-
2922
namespace {
3023

3124
TEST(TraceOptionsTest, SetSampled) {
3225
TraceOptions opts;
3326

3427
EXPECT_EQ("00", opts.ToHex());
3528
EXPECT_FALSE(opts.IsSampled());
36-
SpanTestPeer::SetSampled(&opts, true);
29+
opts = opts.WithSampling(true);
3730
EXPECT_EQ("01", opts.ToHex());
3831
EXPECT_TRUE(opts.IsSampled());
39-
SpanTestPeer::SetSampled(&opts, false);
32+
opts = opts.WithSampling(false);
33+
EXPECT_EQ("00", opts.ToHex());
4034
EXPECT_FALSE(opts.IsSampled());
4135
}
4236

4337
TEST(TraceOptionsTest, Comparison) {
4438
TraceOptions a;
4539
TraceOptions b;
4640
EXPECT_EQ(a, b);
47-
SpanTestPeer::SetSampled(&a, true);
41+
b = b.WithSampling(true);
4842
EXPECT_FALSE(a == b);
4943
}
5044

5145
TEST(TraceOptionsTest, CopyAndSetSampled) {
52-
TraceOptions a;
53-
SpanTestPeer::SetSampled(&a, true);
46+
TraceOptions a = TraceOptions().WithSampling(true);
5447
TraceOptions b = a.WithSampling(false);
5548
EXPECT_TRUE(a.IsSampled());
5649
EXPECT_FALSE(b.IsSampled());

opencensus/trace/trace_options.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ class TraceOptions final {
5555
TraceOptions WithSampling(bool is_sampled) const;
5656

5757
private:
58-
friend class SpanGenerator;
59-
friend class SpanTestPeer;
60-
61-
// Only StartSpan should be calling this.
62-
void SetSampled(bool is_sampled);
63-
6458
uint8_t rep_[kSize];
6559
};
6660

0 commit comments

Comments
 (0)