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

Commit

Permalink
Add Span::SetName() (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyessenov authored and g-easy committed Jun 26, 2019
1 parent a506cf8 commit 858195c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
6 changes: 6 additions & 0 deletions opencensus/trace/internal/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ void Span::SetStatus(StatusCode canonical_code,
}
}

void Span::SetName(absl::string_view name) const {
if (IsRecording()) {
span_impl_->SetName(name);
}
}

void Span::End() const {
if (IsRecording()) {
if (!span_impl_->End()) {
Expand Down
7 changes: 7 additions & 0 deletions opencensus/trace/internal/span_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ void SpanImpl::SetStatus(exporter::Status&& status) {
}
}

void SpanImpl::SetName(absl::string_view name) {
absl::MutexLock l(&mu_);
if (!has_ended_) {
name_ = std::string(name);
}
}

bool SpanImpl::End() {
absl::MutexLock l(&mu_);
if (has_ended_) {
Expand Down
4 changes: 3 additions & 1 deletion opencensus/trace/internal/span_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class SpanImpl final {

void SetStatus(exporter::Status&& status) LOCKS_EXCLUDED(mu_);

void SetName(absl::string_view name) LOCKS_EXCLUDED(mu_);

// Returns true on success (if this is the first time the Span has ended) and
// also marks the end of the Span and sets its end_time_.
bool End() LOCKS_EXCLUDED(mu_);
Expand Down Expand Up @@ -119,7 +121,7 @@ class SpanImpl final {
// The status of the span. Only set if start_options_.record_events is true.
exporter::Status status_ GUARDED_BY(mu_);
// The displayed name of the span.
const std::string name_;
std::string name_ GUARDED_BY(mu_);
// The parent SpanId of this span. Parent SpanId will be not valid if this is
// a root span.
const SpanId parent_span_id_;
Expand Down
5 changes: 4 additions & 1 deletion opencensus/trace/internal/span_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ TEST(SpanTest, FullSpanTest) {

span.SetStatus(StatusCode::DEADLINE_EXCEEDED, "desc");

// Change span name
span.SetName("NewSpanName");

EXPECT_TRUE(span.context().IsValid());
span.End();
// Add a few extra things and make sure they are NOT included since the span
Expand All @@ -213,7 +216,7 @@ TEST(SpanTest, FullSpanTest) {

const exporter::SpanData data = SpanTestPeer::ToSpanData(&span);

EXPECT_EQ("MyRootSpan", data.name());
EXPECT_EQ("NewSpanName", data.name());
EXPECT_EQ(parent.context().span_id(), data.parent_span_id());
EXPECT_EQ(0, data.annotations().dropped_events_count());
EXPECT_EQ(0, data.message_events().dropped_events_count());
Expand Down
3 changes: 3 additions & 0 deletions opencensus/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ class Span final {
void SetStatus(StatusCode canonical_code,
absl::string_view message = "") const;

// Set the span name.
void SetName(absl::string_view name) const;

// Marks the end of a Span. No further changes can be made to the Span after
// End is called.
void End() const;
Expand Down
8 changes: 5 additions & 3 deletions tools/travis/build_bazel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ if [[ "$TRAVIS_COMPILER" = "clang" ]]; then
export BAZEL_OPTIONS="$BAZEL_OPTIONS --copt=-Werror=thread-safety"
fi

wget https://github.com/bazelbuild/bazel/releases/download/0.20.0/bazel-0.20.0-installer-${BAZEL_OS}-x86_64.sh
chmod +x bazel-0.20.0-installer-${BAZEL_OS}-x86_64.sh
./bazel-0.20.0-installer-${BAZEL_OS}-x86_64.sh --user
export BAZEL_VERSION="0.24.1"

wget https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-${BAZEL_OS}-x86_64.sh
chmod +x bazel-${BAZEL_VERSION}-installer-${BAZEL_OS}-x86_64.sh
./bazel-${BAZEL_VERSION}-installer-${BAZEL_OS}-x86_64.sh --user
echo "build --disk_cache=$HOME/bazel-cache" > ~/.bazelrc
echo "build --experimental_strict_action_env" >> ~/.bazelrc
du -sk $HOME/bazel-cache || true
Expand Down

0 comments on commit 858195c

Please sign in to comment.