Skip to content

Commit 838aa58

Browse files
authored
always use latest exit span to inject propagation header (#89)
1 parent 34053a0 commit 838aa58

File tree

6 files changed

+13
-31
lines changed

6 files changed

+13
-31
lines changed

cpp2sky/tracing_context.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,9 @@ class TracingContext {
234234

235235
/**
236236
* Generate sw8 value to send SegmentRef.
237-
* @param parent Parent span that belongs to current segment.
238237
* @param target_address Target address to send request. For more detail:
239238
* https://github.com/apache/skywalking-data-collect-protocol/blob/master/language-agent/Tracing.proto#L97-L101
240239
*/
241-
virtual std::optional<std::string> createSW8HeaderValue(
242-
TracingSpanPtr parent, const std::string_view target_address) = 0;
243-
// If you don't specify parent span, stored to current segment, it will be
244-
// selected newest span as parent span.
245240
virtual std::optional<std::string> createSW8HeaderValue(
246241
const std::string_view target_address) = 0;
247242

example/sample_client.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int main() {
5656
* httplib::Client cli("remote", 8082);
5757
* httplib::Headers headers = {
5858
* {kPropagationHeader.data(),
59-
* tracing_context->createSW8HeaderValue(current_span, "remote:8082")}};
59+
* tracing_context->createSW8HeaderValue("remote:8082")}};
6060
*
6161
* auto res = cli.Get("/ping", headers);
6262
*
@@ -74,8 +74,8 @@ int main() {
7474

7575
httplib::Client cli("127.0.0.1", 8081);
7676
httplib::Headers headers = {
77-
{kPropagationHeader.data(), *tracing_context->createSW8HeaderValue(
78-
exit_span.get(), target_address)}};
77+
{kPropagationHeader.data(),
78+
*tracing_context->createSW8HeaderValue(target_address)}};
7979

8080
auto res = cli.Get("/ping", headers);
8181
}

source/tracing_context_impl.cc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,8 @@ TracingSpanPtr TracingContextImpl::createEntrySpan() {
192192
}
193193

194194
std::optional<std::string> TracingContextImpl::createSW8HeaderValue(
195-
TracingSpanPtr parent_span, const std::string_view target_address) {
196-
TracingSpanPtr target_span = parent_span;
197-
if (target_span == nullptr) {
198-
if (spans_.empty()) {
199-
throw TracerException(
200-
"Can't create propagation header because current segment has no "
201-
"valid span.");
202-
}
203-
target_span = spans_.back();
204-
}
195+
const std::string_view target_address) {
196+
auto target_span = spans_.back();
205197
if (target_span->spanType() != skywalking::v3::SpanType::Exit) {
206198
return std::nullopt;
207199
}

source/tracing_context_impl.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,6 @@ class TracingContextImpl : public TracingContext {
145145

146146
TracingSpanPtr createEntrySpan() override;
147147
std::optional<std::string> createSW8HeaderValue(
148-
const std::string_view target_address) override {
149-
return createSW8HeaderValue(nullptr, target_address);
150-
}
151-
std::optional<std::string> createSW8HeaderValue(
152-
TracingSpanPtr parent_span,
153148
const std::string_view target_address) override;
154149
skywalking::v3::SegmentObject createSegmentObject() override;
155150
void setSkipAnalysis() override { should_skip_analysis_ = true; }

test/e2e/provider.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ int main() {
5151

5252
httplib::Client cli("consumer", 8080);
5353
httplib::Headers headers = {
54-
{kPropagationHeader.data(), *tracing_context->createSW8HeaderValue(
55-
exit_span.get(), target_address)}};
54+
{kPropagationHeader.data(),
55+
*tracing_context->createSW8HeaderValue(target_address)}};
5656
auto res = cli.Get("/pong", headers);
5757
}
5858
}
@@ -75,8 +75,8 @@ int main() {
7575

7676
httplib::Client cli("interm", 8082);
7777
httplib::Headers headers = {
78-
{kPropagationHeader.data(), *tracing_context->createSW8HeaderValue(
79-
exit_span.get(), target_address)}};
78+
{kPropagationHeader.data(),
79+
*tracing_context->createSW8HeaderValue(target_address)}};
8080
auto res = cli.Get("/users", headers);
8181
}
8282
}

test/tracing_context_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ TEST_F(TracingContextTest, SW8CreateTest) {
329329
std::string target_address("10.0.0.1:443");
330330

331331
// Entry span should be rejected as propagation context
332-
EXPECT_FALSE(sc.createSW8HeaderValue(span, target_address).has_value());
332+
EXPECT_FALSE(sc.createSW8HeaderValue(target_address).has_value());
333333

334334
auto span2 = sc.createExitSpan(span);
335335

@@ -341,7 +341,7 @@ TEST_F(TracingContextTest, SW8CreateTest) {
341341
std::string expect_sw8(
342342
"1-MQ==-dXVpZA==-1-bWVzaA==-c2VydmljZV8w-c2FtcGxlMQ==-MTAuMC4wLjE6NDQz");
343343

344-
EXPECT_EQ(expect_sw8, *sc.createSW8HeaderValue(span2, target_address));
344+
EXPECT_EQ(expect_sw8, *sc.createSW8HeaderValue(target_address));
345345

346346
std::vector<char> target_address_based_vector;
347347
target_address_based_vector.reserve(target_address.size() * 2);
@@ -354,14 +354,14 @@ TEST_F(TracingContextTest, SW8CreateTest) {
354354

355355
EXPECT_EQ(target_address_based_vector.size(), target_address.size());
356356
EXPECT_EQ(expect_sw8,
357-
*sc.createSW8HeaderValue(span2, target_address_based_vector_view));
357+
*sc.createSW8HeaderValue(target_address_based_vector_view));
358358

359359
// Make sure that the end of target_address_based_vector_view is not '\0'. We
360360
// reserve enough memory for target_address_based_vector, so push back will
361361
// not cause content to be re-allocated.
362362
target_address_based_vector.push_back('x');
363363
EXPECT_EQ(expect_sw8,
364-
*sc.createSW8HeaderValue(span2, target_address_based_vector_view));
364+
*sc.createSW8HeaderValue(target_address_based_vector_view));
365365
}
366366

367367
TEST_F(TracingContextTest, ReadyToSendTest) {

0 commit comments

Comments
 (0)