Skip to content

Commit

Permalink
Partial revert #61 (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shikugawa authored Mar 6, 2021
1 parent 7234e1c commit 6b068ba
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 39 deletions.
15 changes: 15 additions & 0 deletions cpp2sky/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,18 @@ cc_library(
],
visibility = ["//visibility:public"],
)

cc_library(
name = "cpp2sky_data_interface",
hdrs = [
"tracing_context.h",
"propagation.h",
"well_known_names.h",
"exception.h",
"time.h",
],
deps = [
":config_cc_proto",
],
visibility = ["//visibility:public"],
)
9 changes: 3 additions & 6 deletions source/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,19 @@ cc_library(

cc_library(
name = "cpp2sky_data_lib",
hdrs = [
hdrs =[
"propagation_impl.h",
"tracing_context_impl.h",
"dynamic_config.h",
],
srcs = [
"propagation_impl.cc",
"tracing_context_impl.cc",
],
deps = [
"@skywalking_data_collect_protocol//language-agent:configuration_discovery_service_cc_proto",
"@skywalking_data_collect_protocol//language-agent:tracing_protocol_cc_proto",
"@com_google_absl//absl/strings:strings",
"//cpp2sky:config_cc_proto",
"//cpp2sky:cpp2sky_interface",
"//cpp2sky:cpp2sky_data_interface",
"//source/utils:util_lib",
],
visibility = ["//visibility:public"],
)
)
4 changes: 2 additions & 2 deletions source/tracer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TracerImpl::TracerImpl(TracerConfig& config,
std::shared_ptr<grpc::ChannelCredentials> cred)
: config_(config),
grpc_callback_thread_([this] { this->run(); }),
segment_factory_(config_) {
segment_factory_(config) {
init(config, cred);
}

Expand All @@ -39,7 +39,7 @@ TracerImpl::TracerImpl(
: config_(config),
reporter_client_(std::move(reporter_client)),
grpc_callback_thread_([this] { this->run(); }),
segment_factory_(config_) {
segment_factory_(config) {
init(config, nullptr);
}

Expand Down
41 changes: 25 additions & 16 deletions source/tracing_context_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,28 +143,34 @@ void TracingSpanImpl::setComponentId(int32_t component_id) {
component_id_ = component_id;
}

TracingContextImpl::TracingContextImpl(const DynamicConfig& config,
TracingContextImpl::TracingContextImpl(const std::string& service_name,
const std::string& instance_name,
RandomGenerator& random)
: trace_id_(random.uuid()),
trace_segment_id_(random.uuid()),
config_(config) {}
service_(service_name),
service_instance_(instance_name) {}

TracingContextImpl::TracingContextImpl(
const DynamicConfig& config, SpanContextPtr parent_span_context,
const std::string& service_name, const std::string& instance_name,
SpanContextPtr parent_span_context,
SpanContextExtensionPtr parent_ext_span_context, RandomGenerator& random)
: parent_span_context_(std::move(parent_span_context)),
parent_ext_span_context_(std::move(parent_ext_span_context)),
trace_id_(parent_span_context_->traceId()),
trace_segment_id_(random.uuid()),
config_(config) {}
service_(service_name),
service_instance_(instance_name) {}

TracingContextImpl::TracingContextImpl(const DynamicConfig& config,
TracingContextImpl::TracingContextImpl(const std::string& service_name,
const std::string& instance_name,
SpanContextPtr parent_span_context,
RandomGenerator& random)
: parent_span_context_(std::move(parent_span_context)),
trace_id_(parent_span_context_->traceId()),
trace_segment_id_(random.uuid()),
config_(config) {}
service_(service_name),
service_instance_(instance_name) {}

TracingSpanPtr TracingContextImpl::createExitSpan(TracingSpanPtr parent_span) {
auto current_span = createSpan();
Expand Down Expand Up @@ -214,8 +220,8 @@ std::string TracingContextImpl::encodeSpan(
header_value += Base64::encode(trace_id_) + "-";
header_value += Base64::encode(trace_segment_id_) + "-";
header_value += parent_spanid + "-";
header_value += Base64::encode(config_.tracerConfig().service_name()) + "-";
header_value += Base64::encode(config_.tracerConfig().instance_name()) + "-";
header_value += Base64::encode(service_) + "-";
header_value += Base64::encode(service_instance_) + "-";
header_value += Base64::encode(endpoint) + "-";
header_value += Base64::encode(target_address.data());

Expand All @@ -240,8 +246,8 @@ skywalking::v3::SegmentObject TracingContextImpl::createSegmentObject() {

obj.set_traceid(trace_id_);
obj.set_tracesegmentid(trace_segment_id_);
obj.set_service(config_.tracerConfig().service_name());
obj.set_serviceinstance(config_.tracerConfig().instance_name());
obj.set_service(service_);
obj.set_serviceinstance(service_instance_);

for (auto& span : spans_) {
auto* entry = obj.mutable_spans()->Add();
Expand All @@ -260,22 +266,25 @@ bool TracingContextImpl::readyToSend() {
return true;
}

TracingContextFactory::TracingContextFactory(const DynamicConfig& config)
: config_(config) {}
TracingContextFactory::TracingContextFactory(const TracerConfig& config)
: service_name_(config.service_name()),
instance_name_(config.instance_name()) {}

TracingContextPtr TracingContextFactory::create() {
return std::make_unique<TracingContextImpl>(config_, random_generator_);
return std::make_unique<TracingContextImpl>(service_name_, instance_name_,
random_generator_);
}

TracingContextPtr TracingContextFactory::create(SpanContextPtr span_context) {
return std::make_unique<TracingContextImpl>(config_, span_context,
random_generator_);
return std::make_unique<TracingContextImpl>(service_name_, instance_name_,
span_context, random_generator_);
}

TracingContextPtr TracingContextFactory::create(
SpanContextPtr span_context, SpanContextExtensionPtr ext_span_context) {
auto context = std::make_unique<TracingContextImpl>(
config_, span_context, ext_span_context, random_generator_);
service_name_, instance_name_, span_context, ext_span_context,
random_generator_);
if (ext_span_context->tracingMode() == TracingMode::Skip) {
context->setSkipAnalysis();
}
Expand Down
24 changes: 13 additions & 11 deletions source/tracing_context_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "cpp2sky/config.pb.h"
#include "cpp2sky/propagation.h"
#include "cpp2sky/tracing_context.h"
#include "source/dynamic_config.h"
#include "source/utils/random_generator.h"

namespace cpp2sky {
Expand Down Expand Up @@ -116,11 +115,14 @@ class TracingSpanImpl : public TracingSpan {
class TracingContextImpl : public TracingContext {
public:
// This constructor is called when there is no parent SpanContext.
TracingContextImpl(const DynamicConfig& config, RandomGenerator& random);
TracingContextImpl(const DynamicConfig& config,
TracingContextImpl(const std::string& service_name,
const std::string& instance_name, RandomGenerator& random);
TracingContextImpl(const std::string& service_name,
const std::string& instance_name,
SpanContextPtr parent_span_context,
RandomGenerator& random);
TracingContextImpl(const DynamicConfig& config,
TracingContextImpl(const std::string& service_name,
const std::string& instance_name,
SpanContextPtr parent_span_context,
SpanContextExtensionPtr parent_ext_span_context,
RandomGenerator& random);
Expand All @@ -130,11 +132,9 @@ class TracingContextImpl : public TracingContext {
const std::string& traceSegmentId() const override {
return trace_segment_id_;
}
const std::string& service() const override {
return config_.tracerConfig().service_name();
}
const std::string& service() const override { return service_; }
const std::string& serviceInstance() const override {
return config_.tracerConfig().instance_name();
return service_instance_;
}
const std::list<TracingSpanPtr>& spans() const override { return spans_; }
SpanContextPtr parentSpanContext() const override {
Expand Down Expand Up @@ -174,22 +174,24 @@ class TracingContextImpl : public TracingContext {
// https://github.com/apache/skywalking-data-collect-protocol/blob/master/language-agent/Tracing.proto
std::string trace_id_;
std::string trace_segment_id_;
const DynamicConfig& config_;
std::string service_;
std::string service_instance_;

bool should_skip_analysis_ = false;
};

class TracingContextFactory {
public:
TracingContextFactory(const DynamicConfig& config);
TracingContextFactory(const TracerConfig& config);

TracingContextPtr create();
TracingContextPtr create(SpanContextPtr span_context);
TracingContextPtr create(SpanContextPtr span_context,
SpanContextExtensionPtr ext_span_context);

private:
const DynamicConfig& config_;
std::string service_name_;
std::string instance_name_;
RandomGeneratorImpl random_generator_;
};

Expand Down
7 changes: 3 additions & 4 deletions test/tracing_context_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ class TracingContextTest : public testing::Test {
span_ctx_ = std::make_shared<SpanContextImpl>(sample_ctx);
span_ext_ctx_ = std::make_shared<SpanContextExtensionImpl>("1");

dy_config_ = std::make_unique<DynamicConfig>(config_);
factory_ = std::make_unique<TracingContextFactory>(*dy_config_);
factory_ = std::make_unique<TracingContextFactory>(config_);
}

protected:
std::unique_ptr<DynamicConfig> dy_config_;
NiceMock<MockRandomGenerator> random_;
std::string service_name_ = "mesh";
std::string instance_name_ = "service_0";
Expand Down Expand Up @@ -289,7 +287,8 @@ TEST_F(TracingContextTest, SkipAnalysisSegment) {
}

TEST_F(TracingContextTest, SW8CreateTest) {
TracingContextImpl sc(*dy_config_, span_ctx_, span_ext_ctx_, random_);
TracingContextImpl sc(config_.service_name(), config_.instance_name(),
span_ctx_, span_ext_ctx_, random_);
EXPECT_EQ(sc.service(), "mesh");
EXPECT_EQ(sc.serviceInstance(), "service_0");

Expand Down

0 comments on commit 6b068ba

Please sign in to comment.