| // Copyright 2026 Google LLC | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // https://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| // | |
| #ifndef THIRD_PARTY_CREDENTIO_TRACING_SPAN_H_ | |
| #define THIRD_PARTY_CREDENTIO_TRACING_SPAN_H_ | |
| #include <cstdint> | |
| #include <string> | |
| #include <variant> | |
| #include "absl/status/status.h" | |
| #include "absl/strings/string_view.h" | |
| namespace credentio_tracing { | |
| // Represents supported primitive attribute values that can be attached to a | |
| // tracing span. | |
| using AttributeValue = | |
| std::variant<std::string, uint64_t, int64_t, double, bool>; | |
| // Abstract interface for a tracing span representing an operation duration. | |
| // | |
| // Implementations of Span record information about execution status, timed | |
| // events, and key-value attributes. | |
| // | |
| // Thread safety: a span may be started on one thread and ended on another -- | |
| // the asynchronous encoding pipeline routinely does this, for example a span | |
| // that ends inside an RPC callback while its siblings end on the calling | |
| // thread. Implementations must therefore synchronize any state they share | |
| // between spans, such as a destination shared by all spans of an operation. | |
| // A single span instance must not be used concurrently from multiple threads; | |
| // callers hand a span off to at most one thread at a time, which establishes | |
| // the happens-before edge that makes its own state safe to access unguarded. | |
| class Span { | |
| public: | |
| virtual ~Span() = default; | |
| // Sets a key-value attribute on the span. | |
| virtual void SetAttribute(absl::string_view key, AttributeValue value) = 0; | |
| // Records a point-in-time event with the specified name within the span. | |
| virtual void AddEvent(absl::string_view name) = 0; | |
| // Sets the execution status for the operation tracked by the span. | |
| virtual void SetStatus(const absl::Status& status) = 0; | |
| // Completes execution of the span and records its end time. | |
| virtual void End() = 0; | |
| // Sets the execution status and completes execution of the span. | |
| void EndWithStatus(const absl::Status& status) { | |
| SetStatus(status); | |
| End(); | |
| } | |
| }; | |
| } // namespace credentio_tracing | |
| #endif // THIRD_PARTY_CREDENTIO_TRACING_SPAN_H_ |