diff --git a/cpp2sky/tracing_context.h b/cpp2sky/tracing_context.h index bd87cc1..7353b72 100644 --- a/cpp2sky/tracing_context.h +++ b/cpp2sky/tracing_context.h @@ -88,8 +88,8 @@ class TracingSpan { /** * Get tags. */ - virtual const std::vector>& tags() - const = 0; + virtual const std::vector>& + tags() const = 0; /** * Get logs. @@ -109,10 +109,10 @@ class TracingSpan { /** * Set start time to calculate execution time. */ - virtual void startSpan(std::string operation_name) = 0; - virtual void startSpan(std::string operation_name, + virtual void startSpan(std::string_view operation_name) = 0; + virtual void startSpan(std::string_view operation_name, TimePoint current_time) = 0; - virtual void startSpan(std::string operation_name, + virtual void startSpan(std::string_view operation_name, TimePoint current_time) = 0; /** @@ -158,15 +158,15 @@ class TracingSpan { /** * Set tag to current span. */ - virtual void addTag(std::string key, std::string value) = 0; + virtual void addTag(std::string_view key, std::string_view value) = 0; /** * Add log related with current span. */ - virtual void addLog(std::string key, std::string value) = 0; - virtual void addLog(std::string key, std::string value, + virtual void addLog(std::string_view key, std::string_view value) = 0; + virtual void addLog(std::string_view key, std::string_view value, TimePoint current_time) = 0; - virtual void addLog(std::string key, std::string value, + virtual void addLog(std::string_view key, std::string_view value, TimePoint current_time) = 0; /** diff --git a/source/tracing_context_impl.cc b/source/tracing_context_impl.cc index ea31b48..0ddb303 100644 --- a/source/tracing_context_impl.cc +++ b/source/tracing_context_impl.cc @@ -15,6 +15,7 @@ #include "source/tracing_context_impl.h" #include +#include #include "cpp2sky/exception.h" #include "cpp2sky/time.h" @@ -61,8 +62,8 @@ skywalking::v3::SpanObject TracingSpanImpl::createSpanObject() { for (auto& tag : tags_) { auto* entry = obj.mutable_tags()->Add(); - entry->set_key(tag.first); - entry->set_value(tag.second); + entry->set_key(std::string(tag.first)); + entry->set_value(std::string(tag.second)); } for (auto& log : logs_) { @@ -73,46 +74,46 @@ skywalking::v3::SpanObject TracingSpanImpl::createSpanObject() { return obj; } -void TracingSpanImpl::addLog(std::string key, std::string value) { +void TracingSpanImpl::addLog(std::string_view key, std::string_view value) { assert(!finished_); auto now = TimePoint(); addLog(key, value, now); } -void TracingSpanImpl::addLog(std::string key, std::string value, +void TracingSpanImpl::addLog(std::string_view key, std::string_view value, TimePoint current_time) { assert(!finished_); skywalking::v3::Log l; l.set_time(current_time.fetch()); auto* entry = l.add_data(); - entry->set_key(key); - entry->set_value(value); + entry->set_key(std::string(key)); + entry->set_value(std::string(value)); logs_.emplace_back(l); } -void TracingSpanImpl::addLog(std::string key, std::string value, +void TracingSpanImpl::addLog(std::string_view key, std::string_view value, TimePoint current_time) { assert(!finished_); skywalking::v3::Log l; l.set_time(current_time.fetch()); auto* entry = l.add_data(); - entry->set_key(key); - entry->set_value(value); + entry->set_key(std::string(key)); + entry->set_value(std::string(value)); logs_.emplace_back(l); } -void TracingSpanImpl::startSpan(std::string operation_name) { +void TracingSpanImpl::startSpan(std::string_view operation_name) { auto now = TimePoint(); startSpan(operation_name, now); } -void TracingSpanImpl::startSpan(std::string operation_name, +void TracingSpanImpl::startSpan(std::string_view operation_name, TimePoint current_time) { operation_name_ = operation_name; start_time_ = current_time.fetch(); } -void TracingSpanImpl::startSpan(std::string operation_name, +void TracingSpanImpl::startSpan(std::string_view operation_name, TimePoint current_time) { operation_name_ = operation_name; start_time_ = current_time.fetch(); diff --git a/source/tracing_context_impl.h b/source/tracing_context_impl.h index 8fb375a..0db091e 100644 --- a/source/tracing_context_impl.h +++ b/source/tracing_context_impl.h @@ -14,6 +14,8 @@ #pragma once +#include + #include "cpp2sky/config.pb.h" #include "cpp2sky/propagation.h" #include "cpp2sky/tracing_context.h" @@ -37,7 +39,7 @@ class TracingSpanImpl : public TracingSpan { bool errorStatus() const override { return is_error_; } bool skipAnalysis() const override { return skip_analysis_; } int32_t componentId() const override { return component_id_; } - const std::vector>& tags() + const std::vector>& tags() const override { return tags_; } @@ -51,10 +53,10 @@ class TracingSpanImpl : public TracingSpan { assert(!finished_); parent_span_id_ = span_id; } - void startSpan(std::string operation_name) override; - void startSpan(std::string operation_name, + void startSpan(std::string_view operation_name) override; + void startSpan(std::string_view operation_name, TimePoint current_time) override; - void startSpan(std::string operation_name, + void startSpan(std::string_view operation_name, TimePoint current_time) override; void endSpan() override; void endSpan(TimePoint current_time) override; @@ -73,14 +75,14 @@ class TracingSpanImpl : public TracingSpan { } void setErrorStatus() override { is_error_ = true; } void setSkipAnalysis() override { skip_analysis_ = true; } - void addTag(std::string key, std::string value) override { + void addTag(std::string_view key, std::string_view value) override { assert(!finished_); tags_.emplace_back(key, value); } - void addLog(std::string key, std::string value) override; - void addLog(std::string key, std::string value, + void addLog(std::string_view key, std::string_view value) override; + void addLog(std::string_view key, std::string_view value, TimePoint current_time) override; - void addLog(std::string key, std::string value, + void addLog(std::string_view key, std::string_view value, TimePoint current_time) override; void setComponentId(int32_t component_id) override; @@ -100,7 +102,7 @@ class TracingSpanImpl : public TracingSpan { // https://github.com/apache/skywalking/blob/master/docs/en/guides/Component-library-settings.md int32_t component_id_ = 9000; bool is_error_ = false; - std::vector> tags_; + std::vector> tags_; std::vector logs_; bool skip_analysis_ = false; bool finished_ = false; diff --git a/test/tracing_context_test.cc b/test/tracing_context_test.cc index 03acd0b..c72742d 100644 --- a/test/tracing_context_test.cc +++ b/test/tracing_context_test.cc @@ -18,6 +18,7 @@ #include #include +#include #include "external/skywalking_data_collect_protocol/language-agent/Tracing.pb.h" #include "mocks.h" @@ -189,13 +190,25 @@ TEST_F(TracingContextTest, ChildSegmentContext) { span_child->setPeer("localhost:9000"); span_child->addTag("category", "database"); + std::string_view key = "method"; + std::string_view value = "GETxxxx"; + value.remove_suffix(4); + span_child->addTag(key, value); + std::string log_key = "service_0"; std::string log_value = "error"; - auto t3 = TimePoint( SystemTime(std::chrono::duration(300))); span_child->addLog(log_key, log_value, t3); + std::string_view log_key2 = "service_1"; + std::string_view log_value2 = "succeeded\x01\x03"; + log_value2.remove_suffix(2); + + auto t4 = TimePoint( + SystemTime(std::chrono::duration(300))); + span_child->addLog(log_key2, log_value2, t4); + span_child->endSpan(t2); std::string json2 = R"EOF( @@ -219,17 +232,32 @@ TEST_F(TracingContextTest, ChildSegmentContext) { "spanLayer": "Http", "componentId": "9000", "skipAnalysis": "false", - "tags": { - "key": "category", - "value": "database" - }, - "logs": { - "time": "300", - "data": { - "key": "service_0", - "value": "error" + "tags": [ + { + "key": "category", + "value": "database" + }, + { + "key": "method", + "value": "GET" } - }, + ], + "logs": [ + { + "time": "300", + "data": { + "key": "service_0", + "value": "error" + } + }, + { + "time": "300", + "data": { + "key": "service_1", + "value": "succeeded" + } + } + ], "operationName": "sample1", } )EOF";