Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename OTEL_CPP_GET_ATTR macro, and define it using fully qualified attr function. #1140

Merged
merged 3 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions examples/grpc/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ class GreeterClient
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);
Expand All @@ -66,7 +66,7 @@ class GreeterClient
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();
Expand All @@ -75,7 +75,7 @@ class GreeterClient
{
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";
Expand Down
8 changes: 4 additions & 4 deletions examples/grpc/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class GreeterServer final : public Greeter::Service
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);

Expand Down
8 changes: 4 additions & 4 deletions examples/http/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions examples/http/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ class RequestHandler : public HTTP_SERVER_NS::HttpRequestCallback
// 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);
Expand Down
4 changes: 2 additions & 2 deletions exporters/zipkin/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
13 changes: 7 additions & 6 deletions sdk/src/resource/resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
24 changes: 12 additions & 12 deletions sdk/test/resource/resource_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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}};
Expand Down Expand Up @@ -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}};
Expand Down Expand Up @@ -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);
Expand Down