zipkin/sampling_flags.rs
1// Copyright 2017 Palantir Technologies, Inc.
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//! Sampling flags.
16
17/// Flags used to control sampling.
18#[derive(Debug, Copy, Clone, PartialEq, Eq)]
19pub struct SamplingFlags {
20 sampled: Option<bool>,
21 debug: bool,
22}
23
24impl Default for SamplingFlags {
25 #[inline]
26 fn default() -> SamplingFlags {
27 SamplingFlags::builder().build()
28 }
29}
30
31impl SamplingFlags {
32 /// Returns a builder used to construct `SamplingFlags`.
33 #[inline]
34 pub fn builder() -> Builder {
35 Builder {
36 sampled: None,
37 debug: false,
38 }
39 }
40
41 /// Determines if sampling has been requested for this context.
42 ///
43 /// A value of `None` indicates that the service working in the context is
44 /// responsible for determining if it should be sampled.
45 #[inline]
46 pub fn sampled(self) -> Option<bool> {
47 self.sampled
48 }
49
50 /// Determines if this context is in debug mode.
51 ///
52 /// Debug contexts should always be sampled, regardless of the value of
53 /// `sampled()`.
54 #[inline]
55 pub fn debug(self) -> bool {
56 self.debug
57 }
58}
59
60/// A builder type for `SamplingFlags`.
61pub struct Builder {
62 sampled: Option<bool>,
63 debug: bool,
64}
65
66impl From<SamplingFlags> for Builder {
67 #[inline]
68 fn from(flags: SamplingFlags) -> Builder {
69 Builder {
70 sampled: flags.sampled,
71 debug: flags.debug,
72 }
73 }
74}
75
76impl Builder {
77 /// Sets the sampling request for this context.
78 ///
79 /// Defaults to `None`.
80 #[inline]
81 pub fn sampled(&mut self, sampled: bool) -> &mut Builder {
82 self.sampled = Some(sampled);
83 self
84 }
85
86 /// Sets the debug flag for this request.
87 ///
88 /// Defaults to `false`.
89 #[inline]
90 pub fn debug(&mut self, debug: bool) -> &mut Builder {
91 self.debug = debug;
92 self
93 }
94
95 /// Constructs `SamplingFlags`.
96 #[inline]
97 pub fn build(&self) -> SamplingFlags {
98 SamplingFlags {
99 sampled: if self.debug { Some(true) } else { self.sampled },
100 debug: self.debug,
101 }
102 }
103}