Skip to content

Commit

Permalink
tracing: expose stream info to the extended tracing driver (envoyprox…
Browse files Browse the repository at this point in the history
…y#27062)

* tracing: expose stream info to the extended tracing driver

Signed-off-by: wbpcode <[email protected]>
  • Loading branch information
code authored May 2, 2023
1 parent 6e706d9 commit 37cdc12
Show file tree
Hide file tree
Showing 38 changed files with 267 additions and 231 deletions.
1 change: 1 addition & 0 deletions envoy/tracing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ envoy_cc_library(
hdrs = ["trace_driver.h"],
deps = [
":trace_config_interface",
"//envoy/stream_info:stream_info_interface",
],
)

Expand Down
6 changes: 4 additions & 2 deletions envoy/tracing/trace_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>

#include "envoy/common/pure.h"
#include "envoy/stream_info/stream_info.h"
#include "envoy/tracing/trace_config.h"

namespace Envoy {
Expand Down Expand Up @@ -107,8 +108,9 @@ class Driver {
/**
* Start driver specific span.
*/
virtual SpanPtr startSpan(const Config& config, TraceContext& trace_conext,
const std::string& operation_name, SystemTime start_time,
virtual SpanPtr startSpan(const Config& config, TraceContext& trace_context,
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
const Tracing::Decision tracing_decision) PURE;
};

Expand Down
7 changes: 1 addition & 6 deletions envoy/tracing/tracer.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
#pragma once

#include <memory>
#include <string>
#include <vector>

#include "envoy/access_log/access_log.h"
#include "envoy/common/pure.h"
#include "envoy/tracing/trace_context.h"
#include "envoy/tracing/trace_driver.h"
#include "envoy/tracing/trace_reason.h"

#include "trace_context.h"
#include "tracer.h"

namespace Envoy {
namespace Tracing {

Expand Down
4 changes: 2 additions & 2 deletions source/common/tracing/tracer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ SpanPtr TracerImpl::startSpan(const Config& config, TraceContext& trace_context,
span_name.append(std::string(trace_context.host()));
}

SpanPtr active_span = driver_->startSpan(config, trace_context, span_name,
stream_info.startTime(), tracing_decision);
SpanPtr active_span =
driver_->startSpan(config, trace_context, stream_info, span_name, tracing_decision);

// Set tags related to the local environment
if (active_span) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ OpenTracingDriver::OpenTracingDriver(Stats::Scope& scope)

Tracing::SpanPtr OpenTracingDriver::startSpan(const Tracing::Config& config,
Tracing::TraceContext& trace_context,
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
SystemTime start_time,
const Tracing::Decision tracing_decision) {
const PropagationMode propagation_mode = this->propagationMode();
const opentracing::Tracer& tracer = this->tracer();
Expand Down Expand Up @@ -196,7 +196,7 @@ Tracing::SpanPtr OpenTracingDriver::startSpan(const Tracing::Config& config,
opentracing::StartSpanOptions options;
options.references.emplace_back(opentracing::SpanReferenceType::ChildOfRef,
parent_span_ctx.get());
options.start_system_timestamp = start_time;
options.start_system_timestamp = stream_info.startTime();
if (!tracing_decision.traced) {
options.tags.emplace_back(opentracing::ext::sampling_priority, 0);
}
Expand Down
5 changes: 3 additions & 2 deletions source/extensions/tracers/common/ot/opentracing_driver_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ class OpenTracingDriver : public Tracing::Driver, protected Logger::Loggable<Log
public:
explicit OpenTracingDriver(Stats::Scope& scope);

// Tracer::TracingDriver
// Tracing::Driver
Tracing::SpanPtr startSpan(const Tracing::Config& config, Tracing::TraceContext& trace_context,
const std::string& operation_name, SystemTime start_time,
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
const Tracing::Decision tracing_decision) override;

virtual opentracing::Tracer& tracer() PURE;
Expand Down
5 changes: 3 additions & 2 deletions source/extensions/tracers/datadog/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ Tracer::Tracer(const std::string& collector_cluster, const std::string& collecto
// Tracer::TracingDriver

Tracing::SpanPtr Tracer::startSpan(const Tracing::Config&, Tracing::TraceContext& trace_context,
const std::string& operation_name, SystemTime start_time,
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
const Tracing::Decision tracing_decision) {
ThreadLocalTracer& thread_local_tracer = **thread_local_slot_;
if (!thread_local_tracer.tracer) {
Expand All @@ -86,7 +87,7 @@ Tracing::SpanPtr Tracer::startSpan(const Tracing::Config&, Tracing::TraceContext
// so we will as well.
datadog::tracing::SpanConfig span_config;
span_config.name = operation_name;
span_config.start = estimateTime(start_time);
span_config.start = estimateTime(stream_info.startTime());

datadog::tracing::Tracer& tracer = *thread_local_tracer.tracer;
TraceContextReader reader{trace_context};
Expand Down
9 changes: 5 additions & 4 deletions source/extensions/tracers/datadog/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,28 @@ class Tracer : public Tracing::Driver, private Logger::Loggable<Logger::Id::trac
datadog::tracing::Optional<datadog::tracing::Tracer> tracer;
};

// Tracer::TracingDriver
// Tracing::Driver

/**
* Create a Datadog span from the specified \p trace_context, and having the
* specified \p operation_name, \p start_time and \p tracing_decision.
* specified \p operation_name, \p stream_info and \p tracing_decision.
* If this tracer encountered an error during initialization, then return a
* \c Tracing::NullSpan instead.
* @param config this parameter is ignored
* @param trace_context possibly contains information about an existing trace
* that the returned span will be a part of; otherwise, the returned span is
* the root of a new trace
* @param stream_info contains information about the stream.
* @param operation_name the name of the operation representation by this
* span, e.g. "handle.request"
* @param start_time when the span begins
* @param tracing_decision the sampling decision made in advance by Envoy for
* this trace. If the decision is to drop the trace, then this tracer will
* honor that decision. If the decision is to keep the trace, then this tracer
* will apply its own sampling logic, which might keep or drop the trace.
*/
Tracing::SpanPtr startSpan(const Tracing::Config& config, Tracing::TraceContext& trace_context,
const std::string& operation_name, SystemTime start_time,
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
const Tracing::Decision tracing_decision) override;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,11 @@ void Driver::applyTraceConfig(const opencensus::proto::trace::v1::TraceConfig& c

Tracing::SpanPtr Driver::startSpan(const Tracing::Config& config,
Tracing::TraceContext& trace_context,
const std::string& operation_name, SystemTime start_time,
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
const Tracing::Decision tracing_decision) {
return std::make_unique<Span>(config, oc_config_, trace_context, operation_name, start_time,
tracing_decision);
return std::make_unique<Span>(config, oc_config_, trace_context, operation_name,
stream_info.startTime(), tracing_decision);
}

} // namespace OpenCensus
Expand Down
7 changes: 3 additions & 4 deletions source/extensions/tracers/opencensus/opencensus_tracer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ class Driver : public Tracing::Driver, Logger::Loggable<Logger::Id::tracing> {
Driver(const envoy::config::trace::v3::OpenCensusConfig& oc_config,
const LocalInfo::LocalInfo& localinfo, Api::Api& api);

/**
* Implements the abstract Driver's startSpan operation.
*/
// Tracing::Driver
Tracing::SpanPtr startSpan(const Tracing::Config& config, Tracing::TraceContext& trace_context,
const std::string& operation_name, SystemTime start_time,
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
const Tracing::Decision tracing_decision) override;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,24 @@ Driver::Driver(const envoy::config::trace::v3::OpenTelemetryConfig& opentelemetr

Tracing::SpanPtr Driver::startSpan(const Tracing::Config& config,
Tracing::TraceContext& trace_context,
const std::string& operation_name, SystemTime start_time,
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
const Tracing::Decision tracing_decision) {
// Get tracer from TLS and start span.
auto& tracer = tls_slot_ptr_->getTyped<Driver::TlsTracer>().tracer();
SpanContextExtractor extractor(trace_context);
if (!extractor.propagationHeaderPresent()) {
// No propagation header, so we can create a fresh span with the given decision.
Tracing::SpanPtr new_open_telemetry_span =
tracer.startSpan(config, operation_name, start_time, tracing_decision);
tracer.startSpan(config, operation_name, stream_info.startTime(), tracing_decision);
new_open_telemetry_span->setSampled(tracing_decision.traced);
return new_open_telemetry_span;
} else {
// Try to extract the span context. If we can't, just return a null span.
absl::StatusOr<SpanContext> span_context = extractor.extractSpanContext();
if (span_context.ok()) {
return tracer.startSpan(config, operation_name, start_time, span_context.value());
return tracer.startSpan(config, operation_name, stream_info.startTime(),
span_context.value());
} else {
ENVOY_LOG(trace, "Unable to extract span context: ", span_context.status());
return std::make_unique<Tracing::NullSpan>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ class Driver : Logger::Loggable<Logger::Id::tracing>, public Tracing::Driver {
Driver(const envoy::config::trace::v3::OpenTelemetryConfig& opentelemetry_config,
Server::Configuration::TracerFactoryContext& context);

/**
* Implements the abstract Driver's startSpan operation.
*/
// Tracing::Driver
Tracing::SpanPtr startSpan(const Tracing::Config& config, Tracing::TraceContext& trace_context,
const std::string& operation_name, SystemTime start_time,
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
const Tracing::Decision tracing_decision) override;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Driver::Driver(const envoy::config::trace::v3::SkyWalkingConfig& proto_config,
}

Tracing::SpanPtr Driver::startSpan(const Tracing::Config&, Tracing::TraceContext& trace_context,
const std::string&, Envoy::SystemTime,
const StreamInfo::StreamInfo&, const std::string&,
const Tracing::Decision decision) {
auto& tracer = tls_slot_ptr_->getTyped<Driver::TlsTracer>().tracer();
TracingContextPtr tracing_context;
Expand Down
6 changes: 4 additions & 2 deletions source/extensions/tracers/skywalking/skywalking_tracer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ class Driver : public Tracing::Driver, public Logger::Loggable<Logger::Id::traci
explicit Driver(const envoy::config::trace::v3::SkyWalkingConfig& config,
Server::Configuration::TracerFactoryContext& context);

// Tracing::Driver
Tracing::SpanPtr startSpan(const Tracing::Config& config, Tracing::TraceContext& trace_context,
const std::string& operation, Envoy::SystemTime start_time,
const Tracing::Decision decision) override;
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
const Tracing::Decision tracing_decision) override;

private:
void loadConfig(const envoy::config::trace::v3::ClientConfig& client_config,
Expand Down
5 changes: 3 additions & 2 deletions source/extensions/tracers/xray/xray_tracer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ Driver::Driver(const XRayConfiguration& config,

Tracing::SpanPtr Driver::startSpan(const Tracing::Config& config,
Tracing::TraceContext& trace_context,
const std::string& operation_name, Envoy::SystemTime start_time,
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
const Tracing::Decision tracing_decision) {
// First thing is to determine whether this request will be sampled or not.
// if there's a X-Ray header and it has a sampling decision already determined (i.e. Sample=1)
Expand Down Expand Up @@ -105,7 +106,7 @@ Tracing::SpanPtr Driver::startSpan(const Tracing::Config& config,

auto* tracer = tls_slot_ptr_->getTyped<Driver::TlsTracer>().tracer_.get();
if (should_trace.value()) {
return tracer->startSpan(config, operation_name, start_time,
return tracer->startSpan(config, operation_name, stream_info.startTime(),
header.has_value() ? absl::optional<XRayHeader>(xray_header)
: absl::nullopt,
trace_context.getByKey(XForwardedForHeader));
Expand Down
4 changes: 3 additions & 1 deletion source/extensions/tracers/xray/xray_tracer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ class Driver : public Tracing::Driver, public Logger::Loggable<Logger::Id::traci
Driver(const XRay::XRayConfiguration& config,
Server::Configuration::TracerFactoryContext& context);

// Tracing::Driver
Tracing::SpanPtr startSpan(const Tracing::Config& config, Tracing::TraceContext& trace_context,
const std::string& operation_name, Envoy::SystemTime start_time,
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
const Tracing::Decision tracing_decision) override;

private:
Expand Down
12 changes: 6 additions & 6 deletions source/extensions/tracers/zipkin/zipkin_tracer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ Driver::Driver(const envoy::config::trace::v3::ZipkinConfig& zipkin_config,
}

Tracing::SpanPtr Driver::startSpan(const Tracing::Config& config,
Tracing::TraceContext& trace_context, const std::string&,
SystemTime start_time,
Tracing::TraceContext& trace_context,
const StreamInfo::StreamInfo& stream_info, const std::string&,
const Tracing::Decision tracing_decision) {
Tracer& tracer = *tls_->getTyped<TlsTracer>().tracer_;
SpanPtr new_zipkin_span;
Expand All @@ -122,13 +122,13 @@ Tracing::SpanPtr Driver::startSpan(const Tracing::Config& config,
auto ret_span_context = extractor.extractSpanContext(sampled);
if (!ret_span_context.second) {
// Create a root Zipkin span. No context was found in the headers.
new_zipkin_span = tracer.startSpan(config, std::string(trace_context.host()), start_time);
new_zipkin_span =
tracer.startSpan(config, std::string(trace_context.host()), stream_info.startTime());
new_zipkin_span->setSampled(sampled);
} else {
new_zipkin_span = tracer.startSpan(config, std::string(trace_context.host()), start_time,
ret_span_context.first);
new_zipkin_span = tracer.startSpan(config, std::string(trace_context.host()),
stream_info.startTime(), ret_span_context.first);
}

} catch (const ExtractorException& e) {
return std::make_unique<Tracing::NullSpan>();
}
Expand Down
5 changes: 3 additions & 2 deletions source/extensions/tracers/zipkin/zipkin_tracer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ class Driver : public Tracing::Driver {
* Thus, this implementation of the virtual function startSpan() ignores the operation name
* ("ingress" or "egress") passed by the caller.
*/
Tracing::SpanPtr startSpan(const Tracing::Config&, Tracing::TraceContext& trace_context,
const std::string&, SystemTime start_time,
Tracing::SpanPtr startSpan(const Tracing::Config& config, Tracing::TraceContext& trace_context,
const StreamInfo::StreamInfo& stream_info,
const std::string& operation_name,
const Tracing::Decision tracing_decision) override;

// Getters to return the ZipkinDriver's key members.
Expand Down
12 changes: 3 additions & 9 deletions test/common/tracing/tracer_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,37 +301,31 @@ class TracerImplTest : public testing::Test {

TEST_F(TracerImplTest, BasicFunctionalityNullSpan) {
EXPECT_CALL(config_, operationName()).Times(2);
EXPECT_CALL(stream_info_, startTime());
const std::string operation_name = "ingress";
EXPECT_CALL(*driver_, startSpan_(_, _, operation_name, stream_info_.start_time_, _))
.WillOnce(Return(nullptr));
EXPECT_CALL(*driver_, startSpan_(_, _, _, operation_name, _)).WillOnce(Return(nullptr));
tracer_->startSpan(config_, request_headers_, stream_info_, {Reason::Sampling, true});
}

TEST_F(TracerImplTest, BasicFunctionalityNodeSet) {
EXPECT_CALL(stream_info_, startTime());
EXPECT_CALL(local_info_, nodeName());
EXPECT_CALL(config_, operationName()).Times(2).WillRepeatedly(Return(OperationName::Egress));

NiceMock<MockSpan>* span = new NiceMock<MockSpan>();
const std::string operation_name = "egress test";
EXPECT_CALL(*driver_, startSpan_(_, _, operation_name, stream_info_.start_time_, _))
.WillOnce(Return(span));
EXPECT_CALL(*driver_, startSpan_(_, _, _, operation_name, _)).WillOnce(Return(span));
EXPECT_CALL(*span, setTag(_, _)).Times(testing::AnyNumber());
EXPECT_CALL(*span, setTag(Eq(Tracing::Tags::get().NodeId), Eq("node_name")));

tracer_->startSpan(config_, request_headers_, stream_info_, {Reason::Sampling, true});
}

TEST_F(TracerImplTest, ChildGrpcUpstreamSpanTest) {
EXPECT_CALL(stream_info_, startTime());
EXPECT_CALL(local_info_, nodeName());
EXPECT_CALL(config_, operationName()).Times(2).WillRepeatedly(Return(OperationName::Egress));

NiceMock<MockSpan>* span = new NiceMock<MockSpan>();
const std::string operation_name = "egress test";
EXPECT_CALL(*driver_, startSpan_(_, _, operation_name, stream_info_.start_time_, _))
.WillOnce(Return(span));
EXPECT_CALL(*driver_, startSpan_(_, _, _, operation_name, _)).WillOnce(Return(span));
EXPECT_CALL(*span, setTag(_, _)).Times(testing::AnyNumber());
EXPECT_CALL(*span, setTag(Eq(Tracing::Tags::get().NodeId), Eq("node_name")));

Expand Down
4 changes: 2 additions & 2 deletions test/common/tracing/tracer_manager_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace {

class SampleDriver : public Driver {
public:
SpanPtr startSpan(const Config&, Tracing::TraceContext&, const std::string&, SystemTime,
const Tracing::Decision) override {
SpanPtr startSpan(const Config&, Tracing::TraceContext&, const StreamInfo::StreamInfo&,
const std::string&, const Tracing::Decision) override {
return nullptr;
}
};
Expand Down
1 change: 1 addition & 0 deletions test/extensions/tracers/common/ot/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ envoy_extension_cc_test(
"//source/extensions/tracers/dynamic_ot:dynamic_opentracing_driver_lib",
"//test/mocks/http:http_mocks",
"//test/mocks/stats:stats_mocks",
"//test/mocks/stream_info:stream_info_mocks",
"//test/mocks/tracing:tracing_mocks",
"@io_opentracing_cpp//mocktracer",
],
Expand Down
Loading

0 comments on commit 37cdc12

Please sign in to comment.