xref: /aosp_15_r20/external/federated-compute/fcp/tracing/tracing_span_id.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1*14675a02SAndroid Build Coastguard Worker /*
2*14675a02SAndroid Build Coastguard Worker  * Copyright 2021 Google LLC
3*14675a02SAndroid Build Coastguard Worker  *
4*14675a02SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*14675a02SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*14675a02SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*14675a02SAndroid Build Coastguard Worker  *
8*14675a02SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*14675a02SAndroid Build Coastguard Worker  *
10*14675a02SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*14675a02SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*14675a02SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*14675a02SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*14675a02SAndroid Build Coastguard Worker  * limitations under the License.
15*14675a02SAndroid Build Coastguard Worker  */
16*14675a02SAndroid Build Coastguard Worker 
17*14675a02SAndroid Build Coastguard Worker #ifndef FCP_TRACING_TRACING_SPAN_ID_H_
18*14675a02SAndroid Build Coastguard Worker #define FCP_TRACING_TRACING_SPAN_ID_H_
19*14675a02SAndroid Build Coastguard Worker 
20*14675a02SAndroid Build Coastguard Worker #include <atomic>
21*14675a02SAndroid Build Coastguard Worker #include <cstdint>
22*14675a02SAndroid Build Coastguard Worker #include <ostream>
23*14675a02SAndroid Build Coastguard Worker 
24*14675a02SAndroid Build Coastguard Worker namespace fcp {
25*14675a02SAndroid Build Coastguard Worker 
26*14675a02SAndroid Build Coastguard Worker // Uniquely identifies tracing span within a process.
27*14675a02SAndroid Build Coastguard Worker struct TracingSpanId {
28*14675a02SAndroid Build Coastguard Worker   std::int64_t value;
29*14675a02SAndroid Build Coastguard Worker 
30*14675a02SAndroid Build Coastguard Worker   // Generates next unique id
31*14675a02SAndroid Build Coastguard Worker   static TracingSpanId NextUniqueId();
TracingSpanIdTracingSpanId32*14675a02SAndroid Build Coastguard Worker   explicit constexpr TracingSpanId(int id) : value(id) {}
33*14675a02SAndroid Build Coastguard Worker 
34*14675a02SAndroid Build Coastguard Worker  private:
35*14675a02SAndroid Build Coastguard Worker   static std::atomic<std::int64_t> id_source;
36*14675a02SAndroid Build Coastguard Worker };
37*14675a02SAndroid Build Coastguard Worker 
38*14675a02SAndroid Build Coastguard Worker inline bool operator==(const TracingSpanId& a, const TracingSpanId& b) {
39*14675a02SAndroid Build Coastguard Worker   return a.value == b.value;
40*14675a02SAndroid Build Coastguard Worker }
41*14675a02SAndroid Build Coastguard Worker 
42*14675a02SAndroid Build Coastguard Worker inline bool operator!=(const TracingSpanId& a, const TracingSpanId& b) {
43*14675a02SAndroid Build Coastguard Worker   return a.value != b.value;
44*14675a02SAndroid Build Coastguard Worker }
45*14675a02SAndroid Build Coastguard Worker 
46*14675a02SAndroid Build Coastguard Worker // Overload comparison operators to make it possible to sort by order of ID
47*14675a02SAndroid Build Coastguard Worker // generation.
48*14675a02SAndroid Build Coastguard Worker inline bool operator<(const TracingSpanId& a, const TracingSpanId& b) {
49*14675a02SAndroid Build Coastguard Worker   return a.value < b.value;
50*14675a02SAndroid Build Coastguard Worker }
51*14675a02SAndroid Build Coastguard Worker 
52*14675a02SAndroid Build Coastguard Worker inline std::ostream& operator<<(std::ostream& s, const TracingSpanId& id) {
53*14675a02SAndroid Build Coastguard Worker   return s << id.value;
54*14675a02SAndroid Build Coastguard Worker }
55*14675a02SAndroid Build Coastguard Worker 
56*14675a02SAndroid Build Coastguard Worker template <typename H>
AbslHashValue(H h,const TracingSpanId & id)57*14675a02SAndroid Build Coastguard Worker H AbslHashValue(H h, const TracingSpanId& id) {
58*14675a02SAndroid Build Coastguard Worker   return H::combine(std::move(h), id.value);
59*14675a02SAndroid Build Coastguard Worker }
60*14675a02SAndroid Build Coastguard Worker 
61*14675a02SAndroid Build Coastguard Worker }  // namespace fcp
62*14675a02SAndroid Build Coastguard Worker 
63*14675a02SAndroid Build Coastguard Worker #endif  // FCP_TRACING_TRACING_SPAN_ID_H_
64