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

Commit 1a184af

Browse files
authored
Add GetCurrentSpan() and GetSpanFromContext(). (#211)
1 parent 3567aed commit 1a184af

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

opencensus/context/context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace tags {
2626
class WithTagMap;
2727
} // namespace tags
2828
namespace trace {
29+
class ContextPeer;
2930
class WithSpan;
3031
} // namespace trace
3132
namespace context {
@@ -66,6 +67,7 @@ class Context {
6667
friend class ContextTestPeer;
6768
friend class WithContext;
6869
friend class ::opencensus::tags::WithTagMap;
70+
friend class ::opencensus::trace::ContextPeer;
6971
friend class ::opencensus::trace::WithSpan;
7072

7173
opencensus::tags::TagMap tags_;

opencensus/trace/BUILD

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ cc_library(
9292
],
9393
)
9494

95+
cc_library(
96+
name = "context_util",
97+
srcs = ["internal/context_util.cc"],
98+
hdrs = ["context_util.h"],
99+
copts = DEFAULT_COPTS,
100+
visibility = ["//visibility:public"],
101+
deps = [
102+
":trace",
103+
"//opencensus/context",
104+
],
105+
)
106+
95107
cc_library(
96108
name = "with_span",
97109
srcs = ["internal/with_span.cc"],
@@ -140,6 +152,19 @@ cc_test(
140152
],
141153
)
142154

155+
cc_test(
156+
name = "context_util_test",
157+
srcs = ["internal/context_util_test.cc"],
158+
copts = TEST_COPTS,
159+
deps = [
160+
":context_util",
161+
":trace",
162+
":with_span",
163+
"//opencensus/context",
164+
"@com_google_googletest//:gtest_main",
165+
],
166+
)
167+
143168
cc_test(
144169
name = "link_test",
145170
srcs = ["internal/link_test.cc"],

opencensus/trace/context_util.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef OPENCENSUS_TRACE_CONTEXT_UTIL_H_
16+
#define OPENCENSUS_TRACE_CONTEXT_UTIL_H_
17+
18+
#include "opencensus/context/context.h"
19+
#include "opencensus/trace/span.h"
20+
21+
namespace opencensus {
22+
namespace trace {
23+
24+
// Returns the Span from the current Context.
25+
const Span& GetCurrentSpan();
26+
27+
// Returns the Span from the given Context.
28+
const Span& GetSpanFromContext(const opencensus::context::Context& ctx);
29+
30+
} // namespace trace
31+
} // namespace opencensus
32+
33+
#endif // OPENCENSUS_TRACE_CONTEXT_UTIL_H_
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "opencensus/trace/context_util.h"
16+
17+
#include "opencensus/context/context.h"
18+
#include "opencensus/trace/span.h"
19+
20+
using ::opencensus::context::Context;
21+
22+
namespace opencensus {
23+
namespace trace {
24+
25+
class ContextPeer {
26+
public:
27+
static const Span& GetSpanFromContext(const Context& ctx) {
28+
return ctx.span_;
29+
}
30+
};
31+
32+
const Span& GetCurrentSpan() { return GetSpanFromContext(Context::Current()); }
33+
34+
const Span& GetSpanFromContext(const Context& ctx) {
35+
return ContextPeer::GetSpanFromContext(ctx);
36+
}
37+
38+
} // namespace trace
39+
} // namespace opencensus
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "opencensus/trace/context_util.h"
16+
17+
#include "gtest/gtest.h"
18+
#include "opencensus/context/context.h"
19+
#include "opencensus/trace/span.h"
20+
#include "opencensus/trace/span_context.h"
21+
#include "opencensus/trace/with_span.h"
22+
23+
// Not in namespace ::opencensus::context in order to better reflect what user
24+
// code should look like.
25+
26+
namespace {
27+
28+
TEST(FromContextTest, GetCurrentSpan) {
29+
opencensus::trace::SpanContext zeroed_span_context;
30+
auto span = opencensus::trace::Span::StartSpan("MySpan");
31+
EXPECT_EQ(zeroed_span_context, opencensus::trace::GetCurrentSpan().context());
32+
{
33+
opencensus::trace::WithSpan ws(span);
34+
EXPECT_EQ(span.context(), opencensus::trace::GetCurrentSpan().context());
35+
}
36+
span.End();
37+
}
38+
39+
TEST(FromContextTest, GetSpanFromContext) {
40+
opencensus::trace::SpanContext zeroed_span_context;
41+
auto span = opencensus::trace::Span::StartSpan("MySpan");
42+
opencensus::context::Context ctx = opencensus::context::Context::Current();
43+
EXPECT_EQ(zeroed_span_context,
44+
opencensus::trace::GetSpanFromContext(ctx).context());
45+
{
46+
opencensus::trace::WithSpan ws(span);
47+
ctx = opencensus::context::Context::Current();
48+
}
49+
EXPECT_EQ(zeroed_span_context, opencensus::trace::GetCurrentSpan().context());
50+
EXPECT_EQ(span.context(),
51+
opencensus::trace::GetSpanFromContext(ctx).context());
52+
span.End();
53+
}
54+
55+
} // namespace

0 commit comments

Comments
 (0)