xref: /aosp_15_r20/external/grpc-grpc/src/python/grpcio_observability/grpc_observability/sampler.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2023 gRPC 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 #ifndef SAMPLER_MAIN_H
16 #define SAMPLER_MAIN_H
17 
18 #include <cstdint>
19 #include <string>
20 
21 namespace grpc_observability {
22 
23 // Returns true or false for sampling based on the given probability. Objects of
24 // this class should be cached between uses because there is a cost to
25 // constructing them.
26 class ProbabilitySampler final {
27  public:
28   static ProbabilitySampler& Get();
29 
30   bool ShouldSample(const std::string& trace_id);
31 
32   void SetThreshold(double probability);
33 
34  private:
35   ProbabilitySampler() = default;
36 
37   // Probability is converted to a value between [0, UINT64_MAX].
38   uint64_t threshold_;
39 };
40 
41 }  // namespace grpc_observability
42 
43 #endif  // SAMPLER_MAIN_H
44