sem ver (#1140)
This commit is contained in:
Родитель
3701a6e4c7
Коммит
4f5505f9cb
|
@ -12,12 +12,12 @@
|
|||
#include "opentelemetry/common/string_util.h"
|
||||
#include "opentelemetry/version.h"
|
||||
|
||||
#define OTEL_GET_TRACE_ATTR(name) opentelemetry::trace::attr(OTEL_CPP_CONST_HASHCODE(name))
|
||||
|
||||
OPENTELEMETRY_BEGIN_NAMESPACE
|
||||
namespace trace
|
||||
{
|
||||
|
||||
#define OTEL_CPP_GET_ATTR(name) attr(OTEL_CPP_CONST_HASHCODE(name))
|
||||
|
||||
/**
|
||||
* Stores the Constants for semantic kAttribute names outlined by the OpenTelemetry specifications.
|
||||
* <see
|
||||
|
|
|
@ -46,11 +46,11 @@ public:
|
|||
std::string span_name = "GreeterClient/Greet";
|
||||
auto span = get_tracer("grpc")->StartSpan(
|
||||
span_name,
|
||||
{{OTEL_CPP_GET_ATTR(AttrRpcSystem), "grpc"},
|
||||
{OTEL_CPP_GET_ATTR(AttrRpcService), "grpc-example.GreetService"},
|
||||
{OTEL_CPP_GET_ATTR(AttrRpcMethod), "Greet"},
|
||||
{OTEL_CPP_GET_ATTR(AttrNetPeerIp), ip},
|
||||
{OTEL_CPP_GET_ATTR(AttrNetPeerPort), port}},
|
||||
{{OTEL_GET_TRACE_ATTR(AttrRpcSystem), "grpc"},
|
||||
{OTEL_GET_TRACE_ATTR(AttrRpcService), "grpc-example.GreetService"},
|
||||
{OTEL_GET_TRACE_ATTR(AttrRpcMethod), "Greet"},
|
||||
{OTEL_GET_TRACE_ATTR(AttrNetPeerIp), ip},
|
||||
{OTEL_GET_TRACE_ATTR(AttrNetPeerPort), port}},
|
||||
options);
|
||||
|
||||
auto scope = get_tracer("grpc-client")->WithActiveSpan(span);
|
||||
|
@ -66,7 +66,7 @@ public:
|
|||
if (status.ok())
|
||||
{
|
||||
span->SetStatus(StatusCode::kOk);
|
||||
span->SetAttribute(OTEL_CPP_GET_ATTR(AttrRpcGrpcStatusCode), status.error_code());
|
||||
span->SetAttribute(OTEL_GET_TRACE_ATTR(AttrRpcGrpcStatusCode), status.error_code());
|
||||
// Make sure to end your spans!
|
||||
span->End();
|
||||
return response.response();
|
||||
|
@ -75,7 +75,7 @@ public:
|
|||
{
|
||||
std::cout << status.error_code() << ": " << status.error_message() << std::endl;
|
||||
span->SetStatus(StatusCode::kError);
|
||||
span->SetAttribute(OTEL_CPP_GET_ATTR(AttrRpcGrpcStatusCode), status.error_code());
|
||||
span->SetAttribute(OTEL_GET_TRACE_ATTR(AttrRpcGrpcStatusCode), status.error_code());
|
||||
// Make sure to end your spans!
|
||||
span->End();
|
||||
return "RPC failed";
|
||||
|
|
|
@ -66,10 +66,10 @@ public:
|
|||
std::string span_name = "GreeterService/Greet";
|
||||
auto span =
|
||||
get_tracer("grpc")->StartSpan(span_name,
|
||||
{{OTEL_CPP_GET_ATTR(AttrRpcSystem), "grpc"},
|
||||
{OTEL_CPP_GET_ATTR(AttrRpcService), "GreeterService"},
|
||||
{OTEL_CPP_GET_ATTR(AttrRpcMethod), "Greet"},
|
||||
{OTEL_CPP_GET_ATTR(AttrRpcGrpcStatusCode), 0}},
|
||||
{{OTEL_GET_TRACE_ATTR(AttrRpcSystem), "grpc"},
|
||||
{OTEL_GET_TRACE_ATTR(AttrRpcService), "GreeterService"},
|
||||
{OTEL_GET_TRACE_ATTR(AttrRpcMethod), "Greet"},
|
||||
{OTEL_GET_TRACE_ATTR(AttrRpcGrpcStatusCode), 0}},
|
||||
options);
|
||||
auto scope = get_tracer("grpc")->WithActiveSpan(span);
|
||||
|
||||
|
|
|
@ -26,9 +26,9 @@ void sendRequest(const std::string &url)
|
|||
std::string span_name = url_parser.path_;
|
||||
auto span = get_tracer("http-client")
|
||||
->StartSpan(span_name,
|
||||
{{OTEL_CPP_GET_ATTR(AttrHttpUrl), url_parser.url_},
|
||||
{OTEL_CPP_GET_ATTR(AttrHttpScheme), url_parser.scheme_},
|
||||
{OTEL_CPP_GET_ATTR(AttrHttpMethod), "GET"}},
|
||||
{{OTEL_GET_TRACE_ATTR(AttrHttpUrl), url_parser.url_},
|
||||
{OTEL_GET_TRACE_ATTR(AttrHttpScheme), url_parser.scheme_},
|
||||
{OTEL_GET_TRACE_ATTR(AttrHttpMethod), "GET"}},
|
||||
options);
|
||||
auto scope = get_tracer("http-client")->WithActiveSpan(span);
|
||||
|
||||
|
@ -44,7 +44,7 @@ void sendRequest(const std::string &url)
|
|||
{
|
||||
// set span attributes
|
||||
auto status_code = result.GetResponse().GetStatusCode();
|
||||
span->SetAttribute(OTEL_CPP_GET_ATTR(AttrHttpStatusCode), status_code);
|
||||
span->SetAttribute(OTEL_GET_TRACE_ATTR(AttrHttpStatusCode), status_code);
|
||||
result.GetResponse().ForEachHeader(
|
||||
[&span](nostd::string_view header_name, nostd::string_view header_value) {
|
||||
span->SetAttribute("http.header." + std::string(header_name.data()), header_value);
|
||||
|
|
|
@ -39,13 +39,13 @@ public:
|
|||
// start span with parent context extracted from http header
|
||||
auto span = get_tracer("http-server")
|
||||
->StartSpan(span_name,
|
||||
{{OTEL_CPP_GET_ATTR(AttrHttpServerName), server_name},
|
||||
{OTEL_CPP_GET_ATTR(AttrNetHostPort), server_port},
|
||||
{OTEL_CPP_GET_ATTR(AttrHttpMethod), request.method},
|
||||
{OTEL_CPP_GET_ATTR(AttrHttpScheme), "http"},
|
||||
{OTEL_CPP_GET_ATTR(AttrHttpRequestContentLength),
|
||||
{{OTEL_GET_TRACE_ATTR(AttrHttpServerName), server_name},
|
||||
{OTEL_GET_TRACE_ATTR(AttrNetHostPort), server_port},
|
||||
{OTEL_GET_TRACE_ATTR(AttrHttpMethod), request.method},
|
||||
{OTEL_GET_TRACE_ATTR(AttrHttpScheme), "http"},
|
||||
{OTEL_GET_TRACE_ATTR(AttrHttpRequestContentLength),
|
||||
static_cast<uint64_t>(request.content.length())},
|
||||
{OTEL_CPP_GET_ATTR(AttrHttpClientIp), request.client}},
|
||||
{OTEL_GET_TRACE_ATTR(AttrHttpClientIp), request.client}},
|
||||
options);
|
||||
|
||||
auto scope = get_tracer("http_server")->WithActiveSpan(span);
|
||||
|
|
|
@ -216,9 +216,9 @@ void Recordable::SetResource(const sdk::resource::Resource &resource) noexcept
|
|||
{
|
||||
// only service.name attribute is supported by specs as of now.
|
||||
auto attributes = resource.GetAttributes();
|
||||
if (attributes.find(OTEL_CPP_GET_ATTR(AttrServiceName)) != attributes.end())
|
||||
if (attributes.find(OTEL_GET_RESOURCE_ATTR(AttrServiceName)) != attributes.end())
|
||||
{
|
||||
service_name_ = nostd::get<std::string>(attributes[OTEL_CPP_GET_ATTR(AttrServiceName)]);
|
||||
service_name_ = nostd::get<std::string>(attributes[OTEL_GET_RESOURCE_ATTR(AttrServiceName)]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,14 +15,15 @@
|
|||
#include "opentelemetry/common/string_util.h"
|
||||
#include "opentelemetry/version.h"
|
||||
|
||||
#define OTEL_GET_RESOURCE_ATTR(name) \
|
||||
opentelemetry::sdk::resource::attr(OTEL_CPP_CONST_HASHCODE(name))
|
||||
|
||||
OPENTELEMETRY_BEGIN_NAMESPACE
|
||||
namespace sdk
|
||||
{
|
||||
namespace resource
|
||||
{
|
||||
|
||||
#define OTEL_CPP_GET_ATTR(name) attr(OTEL_CPP_CONST_HASHCODE(name))
|
||||
|
||||
static const std::unordered_map<uint32_t, const char *> attribute_ids = {
|
||||
{OTEL_CPP_CONST_HASHCODE(AttrServiceName), "service.name"},
|
||||
{OTEL_CPP_CONST_HASHCODE(AttrServiceNamespace), "service.namespace"},
|
||||
|
|
|
@ -36,16 +36,17 @@ Resource Resource::Create(const ResourceAttributes &attributes, const std::strin
|
|||
auto resource =
|
||||
Resource::GetDefault().Merge(otel_resource).Merge(Resource{attributes, schema_url});
|
||||
|
||||
if (resource.attributes_.find(OTEL_CPP_GET_ATTR(AttrServiceName)) == resource.attributes_.end())
|
||||
if (resource.attributes_.find(OTEL_GET_RESOURCE_ATTR(AttrServiceName)) ==
|
||||
resource.attributes_.end())
|
||||
{
|
||||
std::string default_service_name = "unknown_service";
|
||||
auto it_process_executable_name =
|
||||
resource.attributes_.find(OTEL_CPP_GET_ATTR(AttrProcessExecutableName));
|
||||
resource.attributes_.find(OTEL_GET_RESOURCE_ATTR(AttrProcessExecutableName));
|
||||
if (it_process_executable_name != resource.attributes_.end())
|
||||
{
|
||||
default_service_name += ":" + nostd::get<std::string>(it_process_executable_name->second);
|
||||
}
|
||||
resource.attributes_[OTEL_CPP_GET_ATTR(AttrServiceName)] = default_service_name;
|
||||
resource.attributes_[OTEL_GET_RESOURCE_ATTR(AttrServiceName)] = default_service_name;
|
||||
}
|
||||
return resource;
|
||||
}
|
||||
|
@ -59,9 +60,9 @@ Resource &Resource::GetEmpty()
|
|||
Resource &Resource::GetDefault()
|
||||
{
|
||||
static Resource default_resource(
|
||||
{{OTEL_CPP_GET_ATTR(AttrTelemetrySdkLanguage), "cpp"},
|
||||
{OTEL_CPP_GET_ATTR(AttrTelemetrySdkName), "opentelemetry"},
|
||||
{OTEL_CPP_GET_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION}},
|
||||
{{OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkLanguage), "cpp"},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkName), "opentelemetry"},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION}},
|
||||
std::string{});
|
||||
return default_resource;
|
||||
}
|
||||
|
|
|
@ -35,10 +35,10 @@ TEST(ResourceTest, create_without_servicename)
|
|||
{"service", "backend"},
|
||||
{"version", (uint32_t)1},
|
||||
{"cost", 234.23},
|
||||
{OTEL_CPP_GET_ATTR(AttrTelemetrySdkLanguage), "cpp"},
|
||||
{OTEL_CPP_GET_ATTR(AttrTelemetrySdkName), "opentelemetry"},
|
||||
{OTEL_CPP_GET_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION},
|
||||
{OTEL_CPP_GET_ATTR(AttrServiceName), "unknown_service"}};
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkLanguage), "cpp"},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkName), "opentelemetry"},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrServiceName), "unknown_service"}};
|
||||
|
||||
ResourceAttributes attributes = {
|
||||
{"service", "backend"}, {"version", (uint32_t)1}, {"cost", 234.23}};
|
||||
|
@ -68,10 +68,10 @@ TEST(ResourceTest, create_with_servicename)
|
|||
ResourceAttributes expected_attributes = {
|
||||
{"version", (uint32_t)1},
|
||||
{"cost", 234.23},
|
||||
{OTEL_CPP_GET_ATTR(AttrTelemetrySdkLanguage), "cpp"},
|
||||
{OTEL_CPP_GET_ATTR(AttrTelemetrySdkName), "opentelemetry"},
|
||||
{OTEL_CPP_GET_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION},
|
||||
{OTEL_CPP_GET_ATTR(AttrServiceName), "backend"},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkLanguage), "cpp"},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkName), "opentelemetry"},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrServiceName), "backend"},
|
||||
};
|
||||
ResourceAttributes attributes = {
|
||||
{"service.name", "backend"}, {"version", (uint32_t)1}, {"cost", 234.23}};
|
||||
|
@ -99,10 +99,10 @@ TEST(ResourceTest, create_with_servicename)
|
|||
TEST(ResourceTest, create_with_emptyatrributes)
|
||||
{
|
||||
ResourceAttributes expected_attributes = {
|
||||
{OTEL_CPP_GET_ATTR(AttrTelemetrySdkLanguage), "cpp"},
|
||||
{OTEL_CPP_GET_ATTR(AttrTelemetrySdkName), "opentelemetry"},
|
||||
{OTEL_CPP_GET_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION},
|
||||
{OTEL_CPP_GET_ATTR(AttrServiceName), "unknown_service"},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkLanguage), "cpp"},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkName), "opentelemetry"},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrTelemetrySdkVersion), OPENTELEMETRY_SDK_VERSION},
|
||||
{OTEL_GET_RESOURCE_ATTR(AttrServiceName), "unknown_service"},
|
||||
};
|
||||
ResourceAttributes attributes = {};
|
||||
auto resource = Resource::Create(attributes);
|
||||
|
|
Загрузка…
Ссылка в новой задаче