@@ -66,7 +66,12 @@ struct StartSpanOptions {
6666 const std::vector<Span*> parent_links;
6767};
6868
69- // Span represents a trace span. It has a SpanContext. Span is thread-safe.
69+ // Span represents a sub-operation in a larger Trace.
70+ //
71+ // A Span is uniquely identified by a SpanContext. The Span object is just a
72+ // handle to add Annotations and Attributes in an implementation-defined data
73+ // structure, hence all these operations are marked const. The Span must be
74+ // explicitly End()ed when the suboperation is complete. Span is thread-safe.
7075class Span final {
7176 public:
7277 // Constructs a no-op Span with an invalid context. Attempts to add
@@ -80,7 +85,7 @@ class Span final {
8085 // auto root_span = ::opencensus::trace::Span::StartSpan("MyOperation");
8186 //
8287 // Example for child span:
83- // // Constructing a ProbabilitySampler can be expensive.
88+ // // Constructing a ProbabilitySampler can be expensive, use static .
8489 // static ::opencensus::trace::ProbabilitySampler sampler(0.1);
8590 // auto child_span = ::opencensus::trace::Span::StartSpan(
8691 // "SubOperation", &root_span, {&sampler});
@@ -92,42 +97,52 @@ class Span final {
9297 absl::string_view name, const SpanContext& parent_ctx,
9398 const StartSpanOptions& options = StartSpanOptions());
9499
100+ // Spans can be copied, in order to e.g. hand the Span off to a callback.
101+ Span (const Span&) = default ;
102+ Span& operator =(const Span&) = default ;
103+
104+ // Spans can be move-constructed, but this is mostly for convenient syntax,
105+ // not performance reasons.
106+ Span (Span&&) = default ;
107+ Span& operator =(Span&&) = delete ;
108+
95109 // Attempts to insert an attribute into the Span, unless it already exists in
96110 // which case it will update the value of that attribute. If the max number of
97111 // attributes is exceeded, one of the previous attributes will be evicted.
98112 // AddAttributes is faster due to batching.
99- void AddAttribute (absl::string_view key, AttributeValueRef attribute);
100- void AddAttributes (AttributesRef attributes);
113+ void AddAttribute (absl::string_view key, AttributeValueRef attribute) const ;
114+ void AddAttributes (AttributesRef attributes) const ;
101115
102116 // Adds an Annotation to the Span. If the max number of Annotations is
103117 // exceeded, an Annotation will be evicted in a FIFO manner.
104118 // In future, there will be a limit of 4 attributes per annotation.
105119 void AddAnnotation (absl::string_view description,
106- AttributesRef attributes = {});
120+ AttributesRef attributes = {}) const ;
107121
108122 // Adds a MessageEvent to the Span. If the max number of MessageEvents is
109123 // exceeded, a MessageEvent will be evicted in a FIFO manner.
110124 void AddSentMessageEvent (uint32_t message_id,
111125 uint32_t compressed_message_size,
112- uint32_t uncompressed_message_size);
126+ uint32_t uncompressed_message_size) const ;
113127 void AddReceivedMessageEvent (uint32_t message_id,
114128 uint32_t compressed_message_size,
115- uint32_t uncompressed_message_size);
129+ uint32_t uncompressed_message_size) const ;
116130
117131 // Adds a Link to the Span. If the max number of Links is exceeded, a Link
118132 // will be evicted in a FIFO manner. In future, there will be a limit of 32
119133 // attributes per link.
120134 void AddParentLink (const SpanContext& parent_ctx,
121- AttributesRef attributes = {});
135+ AttributesRef attributes = {}) const ;
122136 void AddChildLink (const SpanContext& child_ctx,
123- AttributesRef attributes = {});
137+ AttributesRef attributes = {}) const ;
124138
125139 // Sets the status of the Span. See status_code.h for canonical codes.
126- void SetStatus (StatusCode canonical_code, absl::string_view message = " " );
140+ void SetStatus (StatusCode canonical_code,
141+ absl::string_view message = " " ) const ;
127142
128143 // Marks the end of a Span. No further changes can be made to the Span after
129144 // End is called.
130- void End ();
145+ void End () const ;
131146
132147 // Returns the SpanContext associated with this Span.
133148 const SpanContext& context () const ;
@@ -142,7 +157,7 @@ class Span final {
142157 bool IsRecording () const ;
143158
144159 private:
145- Span () {}
160+ Span () = delete ;
146161 Span (const SpanContext& context, SpanImpl* impl);
147162
148163 // Returns span_impl_, only used for testing.
@@ -153,8 +168,9 @@ class Span final {
153168 const SpanContext context_;
154169
155170 // Shared pointer to the underlying Span representation. This is nullptr for
156- // Spans which are not recording events.
157- std::shared_ptr<SpanImpl> span_impl_;
171+ // Spans which are not recording events. This is an implementation detail, not
172+ // part of the public API.
173+ const std::shared_ptr<SpanImpl> span_impl_;
158174
159175 friend class ::opencensus::trace::exporter::RunningSpanStoreImpl;
160176 friend class ::opencensus::trace::exporter::LocalSpanStoreImpl;
0 commit comments