From 6b068ba4b17dd6a78675f6b4af7f1d5e4f423d28 Mon Sep 17 00:00:00 2001 From: Rei Shimizu Date: Sat, 6 Mar 2021 09:52:47 +0900 Subject: [PATCH] Partial revert #61 (#69) --- cpp2sky/BUILD | 15 +++++++++++++ source/BUILD | 9 +++----- source/tracer_impl.cc | 4 ++-- source/tracing_context_impl.cc | 41 +++++++++++++++++++++------------- source/tracing_context_impl.h | 24 +++++++++++--------- test/tracing_context_test.cc | 7 +++--- 6 files changed, 61 insertions(+), 39 deletions(-) diff --git a/cpp2sky/BUILD b/cpp2sky/BUILD index 970f462..420abbe 100644 --- a/cpp2sky/BUILD +++ b/cpp2sky/BUILD @@ -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"], +) diff --git a/source/BUILD b/source/BUILD index ec6a1ed..7fc231d 100644 --- a/source/BUILD +++ b/source/BUILD @@ -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"], -) \ No newline at end of file +) diff --git a/source/tracer_impl.cc b/source/tracer_impl.cc index c2743a2..c37ecf3 100644 --- a/source/tracer_impl.cc +++ b/source/tracer_impl.cc @@ -29,7 +29,7 @@ TracerImpl::TracerImpl(TracerConfig& config, std::shared_ptr cred) : config_(config), grpc_callback_thread_([this] { this->run(); }), - segment_factory_(config_) { + segment_factory_(config) { init(config, cred); } @@ -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); } diff --git a/source/tracing_context_impl.cc b/source/tracing_context_impl.cc index 93957ba..ea31b48 100644 --- a/source/tracing_context_impl.cc +++ b/source/tracing_context_impl.cc @@ -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(); @@ -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()); @@ -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(); @@ -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(config_, random_generator_); + return std::make_unique(service_name_, instance_name_, + random_generator_); } TracingContextPtr TracingContextFactory::create(SpanContextPtr span_context) { - return std::make_unique(config_, span_context, - random_generator_); + return std::make_unique(service_name_, instance_name_, + span_context, random_generator_); } TracingContextPtr TracingContextFactory::create( SpanContextPtr span_context, SpanContextExtensionPtr ext_span_context) { auto context = std::make_unique( - 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(); } diff --git a/source/tracing_context_impl.h b/source/tracing_context_impl.h index 87c862b..1eb6f07 100644 --- a/source/tracing_context_impl.h +++ b/source/tracing_context_impl.h @@ -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 { @@ -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); @@ -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& spans() const override { return spans_; } SpanContextPtr parentSpanContext() const override { @@ -174,14 +174,15 @@ 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); @@ -189,7 +190,8 @@ class TracingContextFactory { SpanContextExtensionPtr ext_span_context); private: - const DynamicConfig& config_; + std::string service_name_; + std::string instance_name_; RandomGeneratorImpl random_generator_; }; diff --git a/test/tracing_context_test.cc b/test/tracing_context_test.cc index 2e0958c..03acd0b 100644 --- a/test/tracing_context_test.cc +++ b/test/tracing_context_test.cc @@ -43,12 +43,10 @@ class TracingContextTest : public testing::Test { span_ctx_ = std::make_shared(sample_ctx); span_ext_ctx_ = std::make_shared("1"); - dy_config_ = std::make_unique(config_); - factory_ = std::make_unique(*dy_config_); + factory_ = std::make_unique(config_); } protected: - std::unique_ptr dy_config_; NiceMock random_; std::string service_name_ = "mesh"; std::string instance_name_ = "service_0"; @@ -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");