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

tracing: use X-Ray cluster_name if segment_name is not specified #10149

Merged
merged 2 commits into from
Feb 26, 2020
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
10 changes: 6 additions & 4 deletions source/extensions/tracers/xray/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,18 @@ Tracing::SpanPtr Span::spawnChild(const Tracing::Config&, const std::string& ope
return child_span;
}

Tracing::SpanPtr Tracer::startSpan(const std::string& span_name, const std::string& operation_name,
Envoy::SystemTime start_time,
Tracing::SpanPtr Tracer::startSpan(const std::string& operation_name, Envoy::SystemTime start_time,
const absl::optional<XRayHeader>& xray_header) {

const auto ticks = time_source_.monotonicTime().time_since_epoch().count();
auto span_ptr = std::make_unique<XRay::Span>(time_source_, *daemon_broker_);
span_ptr->setId(ticks);
span_ptr->setName(span_name);
span_ptr->setName(segment_name_);
span_ptr->setOperation(operation_name);
// Even though we have a TimeSource member in the tracer, we assume the start_time argument has a
// more precise value than calling the systemTime() at this point in time.
span_ptr->setStartTime(start_time);

if (xray_header) { // there's a previous span that this span should be based-on
span_ptr->setParentId(xray_header->parent_id_);
span_ptr->setTraceId(xray_header->trace_id_);
Expand All @@ -155,7 +157,7 @@ XRay::SpanPtr Tracer::createNonSampledSpan() const {
auto span_ptr = std::make_unique<XRay::Span>(time_source_, *daemon_broker_);
const auto ticks = time_source_.monotonicTime().time_since_epoch().count();
span_ptr->setId(ticks);
span_ptr->setName("NotSampled");
span_ptr->setName(segment_name_);
span_ptr->setTraceId(generateTraceId(time_source_.systemTime()));
span_ptr->setSampled(false);
return span_ptr;
Expand Down
3 changes: 1 addition & 2 deletions source/extensions/tracers/xray/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ class Tracer {
/**
* Starts a tracing span for X-Ray
*/
Tracing::SpanPtr startSpan(const std::string& span_name, const std::string& operation_name,
Envoy::SystemTime start_time,
Tracing::SpanPtr startSpan(const std::string& operation_name, Envoy::SystemTime start_time,
const absl::optional<XRayHeader>& xray_header);
/**
* Creates a Span that is marked as not-sampled.
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/tracers/xray/xray_tracer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Tracing::SpanPtr Driver::startSpan(const Tracing::Config& config, Http::HeaderMa

auto* tracer = tls_slot_ptr_->getTyped<Driver::TlsTracer>().tracer_.get();
if (should_trace.value()) {
return tracer->startSpan(xray_config_.segment_name_, operation_name, start_time,
return tracer->startSpan(operation_name, start_time,
header ? absl::optional<XRayHeader>(xray_header) : absl::nullopt);
}

Expand Down
13 changes: 6 additions & 7 deletions test/extensions/tracers/xray/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ TEST_F(XRayTracerTest, SerializeSpanTest) {

EXPECT_CALL(*broker_, send(_)).WillOnce(Invoke(on_send));
Tracer tracer{expected_span_name, std::move(broker_), server_.timeSource()};
auto span = tracer.startSpan(expected_span_name, expected_operation_name,
server_.timeSource().systemTime(), absl::nullopt /*headers*/);
auto span = tracer.startSpan(expected_operation_name, server_.timeSource().systemTime(),
absl::nullopt /*headers*/);
span->setTag("http.method", expected_http_method);
span->setTag("http.url", expected_http_url);
span->setTag("user_agent", expected_user_agent);
Expand All @@ -88,8 +88,8 @@ TEST_F(XRayTracerTest, ChildSpanHasParentInfo) {
constexpr auto expected_operation_name = "Create";
const auto& broker = *broker_;
Tracer tracer{expected_span_name, std::move(broker_), server_.timeSource()};
auto parent_span = tracer.startSpan(expected_span_name, expected_operation_name,
server_.timeSource().systemTime(), absl::nullopt /*headers*/);
auto parent_span = tracer.startSpan(expected_operation_name, server_.timeSource().systemTime(),
absl::nullopt /*headers*/);

const XRay::Span* xray_parent_span = static_cast<XRay::Span*>(parent_span.get());
const std::string expected_parent_id = xray_parent_span->Id();
Expand Down Expand Up @@ -118,8 +118,7 @@ TEST_F(XRayTracerTest, UseExistingHeaderInformation) {
constexpr auto operation_name = "my operation";

Tracer tracer{span_name, std::move(broker_), server_.timeSource()};
auto span =
tracer.startSpan(span_name, operation_name, server_.timeSource().systemTime(), xray_header);
auto span = tracer.startSpan(operation_name, server_.timeSource().systemTime(), xray_header);

const XRay::Span* xray_span = static_cast<XRay::Span*>(span.get());
ASSERT_STREQ(xray_header.trace_id_.c_str(), xray_span->traceId().c_str());
Expand All @@ -131,7 +130,7 @@ TEST_F(XRayTracerTest, SpanInjectContextHasXRayHeader) {
constexpr auto operation_name = "my operation";

Tracer tracer{span_name, std::move(broker_), server_.timeSource()};
auto span = tracer.startSpan(span_name, operation_name, server_.timeSource().systemTime(),
auto span = tracer.startSpan(operation_name, server_.timeSource().systemTime(),
absl::nullopt /*headers*/);
Http::HeaderMapImpl request_headers;
span->injectContext(request_headers);
Expand Down
16 changes: 16 additions & 0 deletions test/extensions/tracers/xray/xray_tracer_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using ::testing::ReturnRef;

namespace Envoy {
namespace Extensions {
namespace Tracers {
Expand Down Expand Up @@ -90,6 +92,20 @@ TEST_F(XRayDriverTest, NoXRayTracerHeader) {
ASSERT_NE(span, nullptr);
}

TEST_F(XRayDriverTest, EmptySegmentNameDefaultToClusterName) {
const std::string cluster_name = "FooBar";
EXPECT_CALL(server_.local_info_, clusterName()).WillRepeatedly(ReturnRef(cluster_name));
XRayConfiguration config{"" /*daemon_endpoint*/, "", "" /*sampling_rules*/};
Driver driver(config, server_);

Tracing::Decision tracing_decision{Tracing::Reason::Sampling, true /*sampled*/};
Envoy::SystemTime start_time;
auto span = driver.startSpan(tracing_config_, request_headers_, operation_name_, start_time,
tracing_decision);
auto* xray_span = static_cast<XRay::Span*>(span.get());
ASSERT_STREQ(xray_span->name().c_str(), cluster_name.c_str());
}

} // namespace
} // namespace XRay
} // namespace Tracers
Expand Down