]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #pragma once | |
5 | ||
6 | #include <cstdint> | |
7 | ||
8 | #include "opentelemetry/common/attribute_value.h" | |
9 | #include "opentelemetry/common/key_value_iterable_view.h" | |
10 | #include "opentelemetry/nostd/shared_ptr.h" | |
11 | #include "opentelemetry/nostd/span.h" | |
12 | #include "opentelemetry/nostd/string_view.h" | |
13 | #include "opentelemetry/nostd/type_traits.h" | |
14 | #include "opentelemetry/nostd/unique_ptr.h" | |
15 | #include "opentelemetry/trace/canonical_code.h" | |
16 | #include "opentelemetry/trace/span_context.h" | |
17 | #include "opentelemetry/trace/span_metadata.h" | |
18 | ||
19 | #include "opentelemetry/version.h" | |
20 | ||
21 | OPENTELEMETRY_BEGIN_NAMESPACE | |
22 | namespace trace | |
23 | { | |
24 | ||
25 | class Tracer; | |
26 | ||
27 | /** | |
28 | * A Span represents a single operation within a Trace. | |
29 | */ | |
30 | class Span | |
31 | { | |
32 | public: | |
33 | // Note that Spans should be created using the Tracer class. Please refer to | |
34 | // tracer.h for documentation. | |
35 | Span() = default; | |
36 | ||
37 | // The Span destructor End()s the Span, if it hasn't been ended already. | |
38 | virtual ~Span() = default; | |
39 | ||
40 | // Not copiable or movable. | |
41 | Span(const Span &) = delete; | |
42 | Span(Span &&) = delete; | |
43 | Span &operator=(const Span &) = delete; | |
44 | Span &operator=(Span &&) = delete; | |
45 | ||
46 | // Sets an attribute on the Span. If the Span previously contained a mapping | |
47 | // for | |
48 | // the key, the old value is replaced. | |
49 | virtual void SetAttribute(nostd::string_view key, | |
50 | const common::AttributeValue &value) noexcept = 0; | |
51 | ||
52 | // Adds an event to the Span. | |
53 | virtual void AddEvent(nostd::string_view name) noexcept = 0; | |
54 | ||
55 | // Adds an event to the Span, with a custom timestamp. | |
56 | virtual void AddEvent(nostd::string_view name, common::SystemTimestamp timestamp) noexcept = 0; | |
57 | ||
58 | // Adds an event to the Span, with a custom timestamp, and attributes. | |
59 | virtual void AddEvent(nostd::string_view name, | |
60 | common::SystemTimestamp timestamp, | |
61 | const common::KeyValueIterable &attributes) noexcept = 0; | |
62 | ||
63 | virtual void AddEvent(nostd::string_view name, | |
64 | const common::KeyValueIterable &attributes) noexcept | |
65 | { | |
66 | this->AddEvent(name, std::chrono::system_clock::now(), attributes); | |
67 | } | |
68 | ||
69 | template <class T, | |
70 | nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr> | |
71 | void AddEvent(nostd::string_view name, | |
72 | common::SystemTimestamp timestamp, | |
73 | const T &attributes) noexcept | |
74 | { | |
75 | this->AddEvent(name, timestamp, common::KeyValueIterableView<T>{attributes}); | |
76 | } | |
77 | ||
78 | template <class T, | |
79 | nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr> | |
80 | void AddEvent(nostd::string_view name, const T &attributes) noexcept | |
81 | { | |
82 | this->AddEvent(name, common::KeyValueIterableView<T>{attributes}); | |
83 | } | |
84 | ||
85 | void AddEvent(nostd::string_view name, | |
86 | common::SystemTimestamp timestamp, | |
87 | std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> | |
88 | attributes) noexcept | |
89 | { | |
90 | this->AddEvent(name, timestamp, | |
91 | nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{ | |
92 | attributes.begin(), attributes.end()}); | |
93 | } | |
94 | ||
95 | void AddEvent(nostd::string_view name, | |
96 | std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> | |
97 | attributes) noexcept | |
98 | { | |
99 | this->AddEvent(name, std::chrono::system_clock::now(), | |
100 | nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{ | |
101 | attributes.begin(), attributes.end()}); | |
102 | } | |
103 | ||
104 | // Sets the status of the span. The default status is Unset. Only the value of | |
105 | // the last call will be | |
106 | // recorded, and implementations are free to ignore previous calls. | |
107 | virtual void SetStatus(StatusCode code, nostd::string_view description = "") noexcept = 0; | |
108 | ||
109 | // Updates the name of the Span. If used, this will override the name provided | |
110 | // during creation. | |
111 | virtual void UpdateName(nostd::string_view name) noexcept = 0; | |
112 | ||
113 | /** | |
114 | * Mark the end of the Span. | |
115 | * Only the timing of the first End call for a given Span will be recorded, | |
116 | * and implementations are free to ignore all further calls. | |
117 | * @param options can be used to manually define span properties like the end | |
118 | * timestamp | |
119 | */ | |
120 | virtual void End(const trace::EndSpanOptions &options = {}) noexcept = 0; | |
121 | ||
122 | virtual trace::SpanContext GetContext() const noexcept = 0; | |
123 | ||
124 | // Returns true if this Span is recording tracing events (e.g. SetAttribute, | |
125 | // AddEvent). | |
126 | virtual bool IsRecording() const noexcept = 0; | |
127 | }; | |
128 | ||
129 | } // namespace trace | |
130 | OPENTELEMETRY_END_NAMESPACE |