Skip to content

Commit 8798fb4

Browse files
committed
feat tracing: native span kinds
commit_hash:1a8669b5e7aa7bf6c92532ff7bbd065313fa50e1
1 parent 63d03fa commit 8798fb4

File tree

8 files changed

+28
-2
lines changed

8 files changed

+28
-2
lines changed

core/include/userver/tracing/tags.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ namespace tracing {
1616
// inferred from a Zipkin span by the presence of ss/sr annotations.
1717

1818
extern const std::string kType;
19+
extern const std::string kSpanKind;
20+
extern const std::string kSpanKindServer;
21+
extern const std::string kSpanKindClient;
22+
extern const std::string kSpanKindInternal;
1923
extern const std::string kHttpUrl;
2024
extern const std::string kHttpMetaType;
2125
extern const std::string kHttpMethod;

core/src/clients/http/plugins/yandex_tracing/plugin.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <userver/clients/http/response.hpp>
44
#include <userver/logging/log.hpp>
55
#include <userver/tracing/span.hpp>
6+
#include <userver/tracing/tags.hpp>
67
#include <userver/utils/algo.hpp>
78

89
#include <userver/http/common_headers.hpp>
@@ -27,7 +28,10 @@ Plugin::Plugin() : http::Plugin(kName) {}
2728

2829
void Plugin::HookPerformRequest(PluginRequest&) {}
2930

30-
void Plugin::HookCreateSpan(PluginRequest&, tracing::Span& span) { span.AddNonInheritableTag(kTypeTag, kTypeRequest); }
31+
void Plugin::HookCreateSpan(PluginRequest&, tracing::Span& span) {
32+
span.AddNonInheritableTag(kTypeTag, kTypeRequest);
33+
span.AddNonInheritableTag(tracing::kSpanKind, tracing::kSpanKindClient);
34+
}
3135

3236
void Plugin::HookOnCompleted(PluginRequest&, Response& response) {
3337
const auto& headers = response.headers();

core/src/server/middlewares/tracing.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ tracing::Span Tracing::MakeSpan(const http::HttpRequest& http_request, std::stri
9595

9696
span.AddNonInheritableTag(tracing::kHttpMetaType, std::string{meta_type});
9797
span.AddNonInheritableTag(tracing::kType, kTracingTypeResponse);
98+
span.AddNonInheritableTag(tracing::kSpanKind, tracing::kSpanKindServer);
9899
span.AddNonInheritableTag(tracing::kHttpMethod, http_request.GetMethodStr());
99100

100101
return span;

core/src/tracing/span.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <userver/logging/impl/logger_base.hpp>
1212
#include <userver/logging/impl/tag_writer.hpp>
1313
#include <userver/tracing/span.hpp>
14+
#include <userver/tracing/tags.hpp>
1415
#include <userver/tracing/tracer.hpp>
1516
#include <userver/utils/assert.hpp>
1617
#include <userver/utils/encoding/hex.hpp>
@@ -143,6 +144,9 @@ void Span::Impl::PutIntoLogger(logging::impl::TagWriter writer) && {
143144
// and log_extra_local_. Merge to deduplicate such tags.
144145
log_extra_inheritable_.Extend(std::move(*log_extra_local_));
145146
}
147+
if (log_extra_inheritable_.GetValue(tracing::kSpanKind) == logging::LogExtra::Value{}) {
148+
log_extra_inheritable_.Extend(tracing::kSpanKind, tracing::kSpanKindInternal);
149+
}
146150
writer.PutLogExtra(log_extra_inheritable_);
147151

148152
LogOpenTracing();

core/src/tracing/span_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ UTEST_F(Span, LogFormat) {
9898
R"(start_timestamp=\d+(\.\d+)?\t)"
9999
R"(my_timer_time=\d+(\.\d+)?\t)"
100100
R"(link=[0-9a-f]+\t)"
101-
R"(my_tag_key=my_tag_value\n)";
101+
R"(my_tag_key=my_tag_value\t)"
102+
R"(span_kind=internal\n)";
102103
{
103104
tracing::Span span("span_name");
104105
span.AddTag("my_tag_key", "my_tag_value");

core/src/tracing/tags.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ namespace tracing {
66

77
const std::string kType = "_type";
88

9+
const std::string kSpanKind = "span_kind";
10+
11+
const std::string kSpanKindServer = "server";
12+
13+
const std::string kSpanKindClient = "client";
14+
15+
const std::string kSpanKindInternal = "internal";
16+
917
const std::string kHttpUrl = "http.url";
1018

1119
const std::string kHttpMetaType = "meta_type";

grpc/src/ugrpc/client/middlewares/log/middleware.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "middleware.hpp"
22

33
#include <userver/logging/log_extra.hpp>
4+
#include <userver/tracing/tags.hpp>
45

56
#include <ugrpc/impl/logging.hpp>
67

@@ -45,6 +46,7 @@ void Middleware::PreStartCall(MiddlewareCallContext& context) const {
4546

4647
span.AddTag("meta_type", std::string{context.GetCallName()});
4748
span.AddTag("type", "request");
49+
span.AddTag(tracing::kSpanKind, tracing::kSpanKindClient);
4850

4951
if (!IsSingleRequest(context.GetCallKind())) {
5052
SpanLogger{context.GetSpan(), settings_.log_level}.Log(

grpc/src/ugrpc/server/middlewares/log/middleware.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <userver/logging/level_serialization.hpp>
44
#include <userver/logging/log_extra.hpp>
55
#include <userver/tracing/span.hpp>
6+
#include <userver/tracing/tags.hpp>
67
#include <userver/yaml_config/yaml_config.hpp>
78

89
#include <ugrpc/impl/logging.hpp>
@@ -73,6 +74,7 @@ void Middleware::Handle(MiddlewareCallContext& context) const {
7374

7475
span.AddTag("meta_type", std::string{context.GetCall().GetCallName()});
7576
span.AddNonInheritableTag("type", "response");
77+
span.AddNonInheritableTag(tracing::kSpanKind, tracing::kSpanKindServer);
7678
if (IsResponseStream(call_kind)) {
7779
// Just like in HTTP, there must be a single trailing Span log
7880
// with type=response and some `body`. We don't have a real single response

0 commit comments

Comments
 (0)