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

Commit 59a0081

Browse files
terencekwtg-easy
authored andcommitted
Make SetSampled() setter methood public in TraceOptions (#279)
1 parent f782922 commit 59a0081

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

opencensus/trace/internal/trace_options.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,11 @@ void TraceOptions::SetSampled(bool is_sampled) {
4646
rep_[0] = (rep_[0] & ~kIsSampled) | (is_sampled ? kIsSampled : 0);
4747
}
4848

49+
TraceOptions TraceOptions::WithSampling(bool is_sampled) const {
50+
TraceOptions child = *this;
51+
child.SetSampled(is_sampled);
52+
return child;
53+
}
54+
4955
} // namespace trace
5056
} // namespace opencensus

opencensus/trace/internal/trace_options_test.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ TEST(TraceOptionsTest, Comparison) {
4848
EXPECT_FALSE(a == b);
4949
}
5050

51+
TEST(TraceOptionsTest, CopyAndSetSampled) {
52+
TraceOptions a;
53+
SpanTestPeer::SetSampled(&a, true);
54+
TraceOptions b = a.WithSampling(false);
55+
EXPECT_TRUE(a.IsSampled());
56+
EXPECT_FALSE(b.IsSampled());
57+
}
58+
5159
} // namespace
5260
} // namespace trace
5361
} // namespace opencensus

opencensus/trace/trace_options.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class TraceOptions final {
5050
// Copies the TraceOptions data to a buffer, which must hold kSize bytes.
5151
void CopyTo(uint8_t* buf) const;
5252

53+
// Returns a copy of these TraceOptions with the sampled bit set to
54+
// is_sampled.
55+
TraceOptions WithSampling(bool is_sampled) const;
56+
5357
private:
5458
friend class SpanGenerator;
5559
friend class SpanTestPeer;

0 commit comments

Comments
 (0)