Skip to content

Commit

Permalink
Add automatically semantic conventions from the spec (open-telemetry#873
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff authored and yxue committed Dec 5, 2022
1 parent 39d04e6 commit 567fb5b
Show file tree
Hide file tree
Showing 13 changed files with 2,204 additions and 443 deletions.
1,000 changes: 557 additions & 443 deletions CHANGELOG.md

Large diffs are not rendered by default.

1,008 changes: 1,008 additions & 0 deletions api/include/opentelemetry/trace/semantic_conventions.h

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions buildscripts/semantic-convention/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOT_DIR="${SCRIPT_DIR}/../../"

# freeze the spec & generator tools versions to make SemanticAttributes generation reproducible
<<<<<<< HEAD
SEMCONV_VERSION=1.15.0
SPEC_VERSION=v$SEMCONV_VERSION
SCHEMA_URL=https://opentelemetry.io/schemas/$SEMCONV_VERSION
GENERATOR_VERSION=0.14.0
=======
SEMCONV_VERSION=1.12.0
SPEC_VERSION=v$SEMCONV_VERSION
SCHEMA_URL=https://opentelemetry.io/schemas/$SEMCONV_VERSION
GENERATOR_VERSION=0.7.0
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))

cd ${SCRIPT_DIR}

Expand All @@ -28,11 +35,18 @@ git reset --hard FETCH_HEAD
cd ${SCRIPT_DIR}

docker run --rm \
<<<<<<< HEAD
-v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions:/source \
-v ${SCRIPT_DIR}/templates:/templates \
-v ${ROOT_DIR}/api/include/opentelemetry/trace/:/output \
otel/semconvgen:$GENERATOR_VERSION \
--only span \
=======
-v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions/trace:/source \
-v ${SCRIPT_DIR}/templates:/templates \
-v ${ROOT_DIR}/api/include/opentelemetry/trace/:/output \
otel/semconvgen:$GENERATOR_VERSION \
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
-f /source code \
--template /templates/SemanticAttributes.h.j2 \
--output /output/semantic_conventions.h \
Expand All @@ -43,11 +57,18 @@ docker run --rm \
-Dnamespace_close="}"

docker run --rm \
<<<<<<< HEAD
-v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions:/source \
-v ${SCRIPT_DIR}/templates:/templates \
-v ${ROOT_DIR}/sdk/include/opentelemetry/sdk/resource/:/output \
otel/semconvgen:$GENERATOR_VERSION \
--only resource \
=======
-v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions/resource:/source \
-v ${SCRIPT_DIR}/templates:/templates \
-v ${ROOT_DIR}/sdk/include/opentelemetry/sdk/resource/:/output \
otel/semconvgen:$GENERATOR_VERSION \
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
-f /source code \
--template /templates/SemanticAttributes.h.j2 \
--output /output/semantic_conventions.h \
Expand Down
30 changes: 30 additions & 0 deletions buildscripts/semantic-convention/templates/SemanticAttributes.h.j2
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@

#include "opentelemetry/version.h"

<<<<<<< HEAD
=======
{%- if semconv == "trace" %}
/*
* Special considerations for FaasDocumentOperationValues::DELETE.
*
* The winnt.h header file is known to define a DELETE symbol,
* which collides with FaasDocumentOperationValues::DELETE.
*/
#ifdef _WIN32
# ifdef DELETE
# pragma message(__FILE__ ": removing define on DELETE")
# undef DELETE
# endif
#endif
{% endif %}

>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
OPENTELEMETRY_BEGIN_NAMESPACE
{{namespace_open}}

Expand All @@ -49,7 +67,11 @@ namespace {{class}}
/**
* The URL of the OpenTelemetry schema for these keys and values.
*/
<<<<<<< HEAD
static constexpr const char *kSchemaUrl = "{{schemaUrl}}";
=======
static constexpr const char *SCHEMA_URL = "{{schemaUrl}}";
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
{%- for attribute in attributes if attribute.is_local and not attribute.ref %}

/**
Expand All @@ -67,7 +89,11 @@ static constexpr const char *kSchemaUrl = "{{schemaUrl}}";
{%- if attribute.deprecated %}
@Deprecated
{%- endif %}
<<<<<<< HEAD
static constexpr const char *k{{attribute.fqn | to_camelcase(True)}} = "{{attribute.fqn}}";
=======
static constexpr const char *{{attribute.fqn | to_const_name}} = "{{attribute.fqn}}";
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
{%- endfor %}

// Enum definitions
Expand All @@ -79,7 +105,11 @@ namespace {{enum_name}}
{
{%- for member in attribute.attr_type.members %}
/** {% filter escape %}{{member.brief | to_doc_brief}}.{% endfilter %} */
<<<<<<< HEAD
static constexpr const {{ type }} k{{ member.member_id | to_camelcase(True) }} = {{ print_value(type, member.value) }};
=======
static constexpr const {{ type }} {{ member.member_id | to_const_name }} = {{ print_value(type, member.value) }};
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
{%- endfor %}
}
{% endif %}
Expand Down
16 changes: 16 additions & 0 deletions examples/grpc/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,19 @@ class GreeterClient
std::string span_name = "GreeterClient/Greet";
auto span = get_tracer("grpc")->StartSpan(
span_name,
<<<<<<< HEAD
{{SemanticConventions::kRpcSystem, "grpc"},
{SemanticConventions::kRpcService, "grpc-example.GreetService"},
{SemanticConventions::kRpcMethod, "Greet"},
{SemanticConventions::kNetSockPeerAddr, ip},
{SemanticConventions::kNetPeerPort, port}},
=======
{{SemanticConventions::RPC_SYSTEM, "grpc"},
{SemanticConventions::RPC_SERVICE, "grpc-example.GreetService"},
{SemanticConventions::RPC_METHOD, "Greet"},
{SemanticConventions::NET_PEER_IP, ip},
{SemanticConventions::NET_PEER_PORT, port}},
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
options);

auto scope = get_tracer("grpc-client")->WithActiveSpan(span);
Expand All @@ -67,7 +75,11 @@ class GreeterClient
if (status.ok())
{
span->SetStatus(StatusCode::kOk);
<<<<<<< HEAD
span->SetAttribute(SemanticConventions::kRpcGrpcStatusCode, status.error_code());
=======
span->SetAttribute(SemanticConventions::RPC_GRPC_STATUS_CODE, status.error_code());
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
// Make sure to end your spans!
span->End();
return response.response();
Expand All @@ -76,7 +88,11 @@ class GreeterClient
{
std::cout << status.error_code() << ": " << status.error_message() << std::endl;
span->SetStatus(StatusCode::kError);
<<<<<<< HEAD
span->SetAttribute(SemanticConventions::kRpcGrpcStatusCode, status.error_code());
=======
span->SetAttribute(SemanticConventions::RPC_GRPC_STATUS_CODE, status.error_code());
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
// Make sure to end your spans!
span->End();
return "RPC failed";
Expand Down
7 changes: 7 additions & 0 deletions examples/grpc/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,17 @@ class GreeterServer final : public Greeter::Service

std::string span_name = "GreeterService/Greet";
auto span = get_tracer("grpc")->StartSpan(span_name,
<<<<<<< HEAD
{{SemanticConventions::kRpcSystem, "grpc"},
{SemanticConventions::kRpcService, "GreeterService"},
{SemanticConventions::kRpcMethod, "Greet"},
{SemanticConventions::kRpcGrpcStatusCode, 0}},
=======
{{SemanticConventions::RPC_SYSTEM, "grpc"},
{SemanticConventions::RPC_SERVICE, "GreeterService"},
{SemanticConventions::RPC_METHOD, "Greet"},
{SemanticConventions::RPC_GRPC_STATUS_CODE, 0}},
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
options);
auto scope = get_tracer("grpc")->WithActiveSpan(span);

Expand Down
10 changes: 10 additions & 0 deletions examples/http/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ void sendRequest(const std::string &url)
std::string span_name = url_parser.path_;
auto span = get_tracer("http-client")
->StartSpan(span_name,
<<<<<<< HEAD
{{SemanticConventions::kHttpUrl, url_parser.url_},
{SemanticConventions::kHttpScheme, url_parser.scheme_},
{SemanticConventions::kHttpMethod, "GET"}},
=======
{{SemanticConventions::HTTP_URL, url_parser.url_},
{SemanticConventions::HTTP_SCHEME, url_parser.scheme_},
{SemanticConventions::HTTP_METHOD, "GET"}},
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
options);
auto scope = get_tracer("http-client")->WithActiveSpan(span);

Expand All @@ -44,7 +50,11 @@ void sendRequest(const std::string &url)
{
// set span attributes
auto status_code = result.GetResponse().GetStatusCode();
<<<<<<< HEAD
span->SetAttribute(SemanticConventions::kHttpStatusCode, status_code);
=======
span->SetAttribute(SemanticConventions::HTTP_STATUS_CODE, status_code);
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
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
10 changes: 10 additions & 0 deletions examples/http/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,23 @@ 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,
<<<<<<< HEAD
{{SemanticConventions::kNetHostName, server_name},
{SemanticConventions::kNetHostPort, server_port},
{SemanticConventions::kHttpMethod, request.method},
{SemanticConventions::kHttpScheme, "http"},
{SemanticConventions::kHttpRequestContentLength,
static_cast<uint64_t>(request.content.length())},
{SemanticConventions::kHttpClientIp, request.client}},
=======
{{SemanticConventions::HTTP_SERVER_NAME, server_name},
{SemanticConventions::NET_HOST_PORT, server_port},
{SemanticConventions::HTTP_METHOD, request.method},
{SemanticConventions::HTTP_SCHEME, "http"},
{SemanticConventions::HTTP_REQUEST_CONTENT_LENGTH,
static_cast<uint64_t>(request.content.length())},
{SemanticConventions::HTTP_CLIENT_IP, request.client}},
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
options);

auto scope = get_tracer("http_server")->WithActiveSpan(span);
Expand Down
12 changes: 12 additions & 0 deletions exporters/otlp/test/otlp_log_recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,29 @@ TEST(OtlpLogRecordable, DefaultResource)
for (int i = 0; i < proto_resource.attributes_size(); i++)
{
auto attr = proto_resource.attributes(static_cast<int>(i));
<<<<<<< HEAD
if (attr.key() == resource::SemanticConventions::kTelemetrySdkLanguage)
=======
if (attr.key() == resource::SemanticConventions::TELEMETRY_SDK_LANGUAGE)
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
{
EXPECT_EQ(attr.value().string_value(), "cpp");
++found_resource_count;
}
<<<<<<< HEAD
else if (attr.key() == resource::SemanticConventions::kTelemetrySdkName)
=======
else if (attr.key() == resource::SemanticConventions::TELEMETRY_SDK_NAME)
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
{
EXPECT_EQ(attr.value().string_value(), "opentelemetry");
++found_resource_count;
}
<<<<<<< HEAD
else if (attr.key() == resource::SemanticConventions::kTelemetrySdkVersion)
=======
else if (attr.key() == resource::SemanticConventions::TELEMETRY_SDK_VERSION)
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
{
EXPECT_EQ(attr.value().string_value(), OPENTELEMETRY_SDK_VERSION);
++found_resource_count;
Expand Down
6 changes: 6 additions & 0 deletions exporters/zipkin/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,15 @@ void Recordable::SetResource(const sdk::resource::Resource &resource) noexcept
{
// only service.name attribute is supported by specs as of now.
auto attributes = resource.GetAttributes();
<<<<<<< HEAD
if (attributes.find(SemanticConventions::kServiceName) != attributes.end())
{
service_name_ = nostd::get<std::string>(attributes[SemanticConventions::kServiceName]);
=======
if (attributes.find(SemanticConventions::SERVICE_NAME) != attributes.end())
{
service_name_ = nostd::get<std::string>(attributes[SemanticConventions::SERVICE_NAME]);
>>>>>>> d9ad0afb (Add automatically semantic conventions from the spec (#873) (#1497))
}
}

Expand Down
Loading

0 comments on commit 567fb5b

Please sign in to comment.