Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xray tracer: set subsegment type for child spans #2

Merged
merged 2 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions source/extensions/tracers/xray/daemon.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ message Segment {
}
// Object containing one or more fields that X-Ray indexes for use with filter expressions.
map<string, string> annotations = 8;
// Set type to "subsegment" when sending a child span so Xray treats it as a subsegment.
// https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html#api-segmentdocuments-subsegments
string type = 14;
}

message Header {
Expand Down
5 changes: 4 additions & 1 deletion source/extensions/tracers/xray/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ void Span::finishSpan() {
s.set_error(clientError());
s.set_fault(serverError());
s.set_throttle(isThrottled());

if (type() == Subsegment) {
s.set_type(std::string(Subsegment));
}
auto* aws = s.mutable_aws()->mutable_fields();
for (const auto& field : aws_metadata_) {
aws->insert({field.first, field.second});
Expand Down Expand Up @@ -115,6 +117,7 @@ Tracing::SpanPtr Span::spawnChild(const Tracing::Config& config, const std::stri
child_span->setParentId(id());
child_span->setTraceId(traceId());
child_span->setSampled(sampled());
child_span->setType(Subsegment);
return child_span;
}

Expand Down
13 changes: 13 additions & 0 deletions source/extensions/tracers/xray/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace Tracers {
namespace XRay {

constexpr auto XRayTraceHeader = "x-amzn-trace-id";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change this also to constexpr absl::string_view ?

constexpr absl::string_view Subsegment = "subsegment";

class Span : public Tracing::Span, Logger::Loggable<Logger::Id::config> {
public:
Expand Down Expand Up @@ -101,6 +102,12 @@ class Span : public Tracing::Span, Logger::Loggable<Logger::Id::config> {
parent_segment_id_ = std::string(parent_segment_id);
}

/**
* Sets the type of the Span. In Xray, an independent subsegment has a type of ``subsegment``.
* https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html#api-segmentdocuments-subsegments
*/
void setType(absl::string_view type) { type_ = std::string(type); }

/**
* Sets the aws metadata field of the Span.
*/
Expand Down Expand Up @@ -156,6 +163,11 @@ class Span : public Tracing::Span, Logger::Loggable<Logger::Id::config> {
*/
const std::string& direction() const { return direction_; }

/**
* Gets this Span's type.
*/
const std::string& type() const { return type_; }
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also considered using a boolean isChild to mark a given span, but this works too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you have is more flexible.


/**
* Gets this Span's name.
*/
Expand Down Expand Up @@ -216,6 +228,7 @@ class Span : public Tracing::Span, Logger::Loggable<Logger::Id::config> {
std::string parent_segment_id_;
std::string name_;
std::string origin_;
std::string type_;
absl::flat_hash_map<std::string, ProtobufWkt::Value> aws_metadata_;
absl::flat_hash_map<std::string, ProtobufWkt::Value> http_request_annotations_;
absl::flat_hash_map<std::string, ProtobufWkt::Value> http_response_annotations_;
Expand Down
8 changes: 8 additions & 0 deletions test/extensions/tracers/xray/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ TEST_F(XRayTracerTest, SerializeSpanTest) {
EXPECT_FALSE(s.id().empty());
EXPECT_EQ(2, s.annotations().size());
EXPECT_TRUE(s.parent_id().empty());
EXPECT_TRUE(s.type().empty());
EXPECT_FALSE(s.fault()); /*server error*/
EXPECT_FALSE(s.error()); /*client error*/
EXPECT_FALSE(s.throttle()); /*request throttled*/
Expand Down Expand Up @@ -142,6 +143,7 @@ TEST_F(XRayTracerTest, SerializeSpanTestServerError) {
EXPECT_FALSE(s.trace_id().empty());
EXPECT_FALSE(s.id().empty());
EXPECT_TRUE(s.parent_id().empty());
EXPECT_TRUE(s.type().empty());
EXPECT_TRUE(s.fault()); /*server error*/
EXPECT_FALSE(s.error()); /*client error*/
EXPECT_EQ(expected_status_code,
Expand Down Expand Up @@ -175,6 +177,7 @@ TEST_F(XRayTracerTest, SerializeSpanTestClientError) {
EXPECT_FALSE(s.trace_id().empty());
EXPECT_FALSE(s.id().empty());
EXPECT_TRUE(s.parent_id().empty());
EXPECT_TRUE(s.type().empty());
EXPECT_FALSE(s.fault()); /*server error*/
EXPECT_TRUE(s.error()); /*client error*/
EXPECT_FALSE(s.throttle()); /*request throttled*/
Expand Down Expand Up @@ -208,6 +211,7 @@ TEST_F(XRayTracerTest, SerializeSpanTestClientErrorWithThrottle) {
EXPECT_FALSE(s.trace_id().empty());
EXPECT_FALSE(s.id().empty());
EXPECT_TRUE(s.parent_id().empty());
EXPECT_TRUE(s.type().empty());
EXPECT_FALSE(s.fault()); /*server error*/
EXPECT_TRUE(s.error()); /*client error*/
EXPECT_TRUE(s.throttle()); /*request throttled*/
Expand Down Expand Up @@ -239,6 +243,7 @@ TEST_F(XRayTracerTest, SerializeSpanTestWithEmptyValue) {
EXPECT_FALSE(s.trace_id().empty());
EXPECT_FALSE(s.id().empty());
EXPECT_TRUE(s.parent_id().empty());
EXPECT_TRUE(s.type().empty());
EXPECT_FALSE(s.http().request().fields().contains(Tracing::Tags::get().Status));
};

Expand Down Expand Up @@ -270,6 +275,7 @@ TEST_F(XRayTracerTest, SerializeSpanTestWithStatusCodeNotANumber) {
EXPECT_FALSE(s.trace_id().empty());
EXPECT_FALSE(s.id().empty());
EXPECT_TRUE(s.parent_id().empty());
EXPECT_TRUE(s.type().empty());
EXPECT_FALSE(s.http().request().fields().contains(Tracing::Tags::get().Status));
EXPECT_FALSE(s.http().request().fields().contains("content_length"));
};
Expand Down Expand Up @@ -347,6 +353,8 @@ TEST_F(XRayTracerTest, ChildSpanHasParentInfo) {
// Hex encoded 64 bit identifier
EXPECT_STREQ("00000000000003e7", s.parent_id().c_str());
EXPECT_EQ(expected_->span_name, s.name().c_str());
EXPECT_TRUE(xray_parent_span->type().empty());
EXPECT_EQ(Subsegment, s.type().c_str());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we update the test to check that type() is not set for a parent span?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

EXPECT_STREQ(xray_parent_span->traceId().c_str(), s.trace_id().c_str());
EXPECT_STREQ("0000003d25bebe62", s.id().c_str());
};
Expand Down