Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 42c65bd

Browse files
authored
Mark all Span methods const. (#191)
Also, improve documentation and be explicit about copies and moves.
1 parent b7e622e commit 42c65bd

2 files changed

Lines changed: 43 additions & 25 deletions

File tree

opencensus/trace/internal/span.cc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class SpanGenerator {
112112
}
113113
};
114114

115-
Span Span::BlankSpan() { return Span(); }
115+
Span Span::BlankSpan() { return Span(SpanContext(), nullptr); }
116116

117117
Span Span::StartSpan(absl::string_view name, const Span* parent,
118118
const StartSpanOptions& options) {
@@ -139,28 +139,29 @@ Span::Span(const SpanContext& context, SpanImpl* impl)
139139
}
140140
}
141141

142-
void Span::AddAttribute(absl::string_view key, AttributeValueRef attribute) {
142+
void Span::AddAttribute(absl::string_view key,
143+
AttributeValueRef attribute) const {
143144
if (IsRecording()) {
144145
span_impl_->AddAttributes({{key, attribute}});
145146
}
146147
}
147148

148-
void Span::AddAttributes(AttributesRef attributes) {
149+
void Span::AddAttributes(AttributesRef attributes) const {
149150
if (IsRecording()) {
150151
span_impl_->AddAttributes(attributes);
151152
}
152153
}
153154

154155
void Span::AddAnnotation(absl::string_view description,
155-
AttributesRef attributes) {
156+
AttributesRef attributes) const {
156157
if (IsRecording()) {
157158
span_impl_->AddAnnotation(description, attributes);
158159
}
159160
}
160161

161162
void Span::AddSentMessageEvent(uint32_t message_id,
162163
uint32_t compressed_message_size,
163-
uint32_t uncompressed_message_size) {
164+
uint32_t uncompressed_message_size) const {
164165
if (IsRecording()) {
165166
span_impl_->AddMessageEvent(exporter::MessageEvent::Type::SENT, message_id,
166167
compressed_message_size,
@@ -170,7 +171,7 @@ void Span::AddSentMessageEvent(uint32_t message_id,
170171

171172
void Span::AddReceivedMessageEvent(uint32_t message_id,
172173
uint32_t compressed_message_size,
173-
uint32_t uncompressed_message_size) {
174+
uint32_t uncompressed_message_size) const {
174175
if (IsRecording()) {
175176
span_impl_->AddMessageEvent(exporter::MessageEvent::Type::RECEIVED,
176177
message_id, compressed_message_size,
@@ -179,31 +180,32 @@ void Span::AddReceivedMessageEvent(uint32_t message_id,
179180
}
180181

181182
void Span::AddParentLink(const SpanContext& parent_ctx,
182-
AttributesRef attributes) {
183+
AttributesRef attributes) const {
183184
if (IsRecording()) {
184185
span_impl_->AddLink(parent_ctx, exporter::Link::Type::kParentLinkedSpan,
185186
attributes);
186187
}
187188
}
188189

189190
void Span::AddChildLink(const SpanContext& child_ctx,
190-
AttributesRef attributes) {
191+
AttributesRef attributes) const {
191192
if (IsRecording()) {
192193
span_impl_->AddLink(child_ctx, exporter::Link::Type::kChildLinkedSpan,
193194
attributes);
194195
}
195196
}
196197

197-
void Span::SetStatus(StatusCode canonical_code, absl::string_view message) {
198+
void Span::SetStatus(StatusCode canonical_code,
199+
absl::string_view message) const {
198200
if (IsRecording()) {
199201
span_impl_->SetStatus(exporter::Status(canonical_code, message));
200202
}
201203
}
202204

203-
void Span::End() {
205+
void Span::End() const {
204206
if (IsRecording()) {
205207
if (!span_impl_->End()) {
206-
// In non-debug builds, ignore the second End().
208+
// The Span already ended, ignore this call.
207209
return;
208210
}
209211
exporter::RunningSpanStoreImpl::Get()->RemoveSpan(span_impl_);

opencensus/trace/span.h

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
7075
class 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

Comments
 (0)