diff --git a/CHANGELOG.md b/CHANGELOG.md index ac7d3af1ee..50d1d9ba34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,7 @@ Increment the: ### SDK -* Cleanup GetEnvironmentVariable and remove unused variable under NO_GETENV ([#976](https://github.com/open-telemetry/opentelemetry-cpp/pull/976)) +* Clean up `GetEnvironmentVariable` and remove unused variable under `NO_GETENV` ([#976](https://github.com/open-telemetry/opentelemetry-cpp/pull/976)) * :collision: Resources: Add note on experimental semantic convention implementation, prefix semantics headers with experimental tag ([#970](https://github.com/open-telemetry/opentelemetry-cpp/pull/970)) ### OTLP Exporter @@ -43,7 +43,7 @@ Increment the: ### DOCS -* :book: Add Getting started documentation for SDK: ([#942](https://github.com/open-telemetry/opentelemetry-cpp/pull/942)) +* :book: Add getting-started documentation for SDK: ([#942](https://github.com/open-telemetry/opentelemetry-cpp/pull/942)) * :book: Remove unnecessary spaces and spelling of gRPC in README ([#965](https://github.com/open-telemetry/opentelemetry-cpp/pull/965)) ### BUILD diff --git a/CMakeLists.txt b/CMakeLists.txt index 16899ad521..272e10927b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,12 @@ cmake_minimum_required(VERSION 3.1) # versions of gtest cmake_policy(SET CMP0057 NEW) +# See https://cmake.org/cmake/help/v3.12/policy/CMP0074.html required by certain +# version of zlib which is dependeded by cURL. +if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12") + cmake_policy(SET CMP0074 NEW) +endif() + project(opentelemetry-cpp) # Mark variables as used so cmake doesn't complain about them @@ -81,14 +87,25 @@ else() ) endif() -if(NOT DEFINED CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 11) -endif() - option(WITH_STL "Whether to use Standard Library for C++latest features" OFF) option(WITH_ABSEIL "Whether to use Abseil for C++latest features" OFF) +if(NOT DEFINED CMAKE_CXX_STANDARD) + if(WITH_STL) + # Require at least C++17. C++20 is needed to avoid gsl::span + if(CMAKE_VERSION VERSION_GREATER 3.11.999) + # Ask for 20, may get anything below + set(CMAKE_CXX_STANDARD 20) + else() + # Ask for 17, may get anything below + set(CMAKE_CXX_STANDARD 17) + endif() + else() + set(CMAKE_CXX_STANDARD 11) + endif() +endif() + if(WITH_ABSEIL) find_package(absl CONFIG REQUIRED) @@ -106,14 +123,6 @@ if(WITH_STL) # the global project build definitions. add_definitions(-DHAVE_CPP_STDLIB) add_definitions(-DHAVE_GSL) - # Require at least C++17. C++20 is needed to avoid gsl::span - if(CMAKE_VERSION VERSION_GREATER 3.11.999) - # Ask for 20, may get anything below - set(CMAKE_CXX_STANDARD 20) - else() - # Ask for 17, may get anything below - set(CMAKE_CXX_STANDARD 17) - endif() # Guidelines Support Library path. Used if we are not on not get C++20. # diff --git a/Versioning.md b/Versioning.md index b95fc77650..b965ffd413 100644 --- a/Versioning.md +++ b/Versioning.md @@ -65,6 +65,17 @@ Refer to the [ABI Policy](./docs/abi-policy.md) for more details. To summarise: allowed to break existing stable interfaces. Feature flags will be removed once we have a stable implementation for the signal. +* As an exception, small experimental features in otherwise stable signals/components + mayn't necessarily be released under feature flag. These would be flagged as experimental + by adding a `NOTE` in it's header file - either at the beginning of file, or as the comment for + the experimental API methods. Also, if the complete header is experimental, it would be prefixed + as `experimental_`. As an example, the semantic conventions for + trace signal is experimental at the time of the writing and is within `experimental_semantic_conventions.h` + +* Code under the "*::detail" namespace implements internal details, + and NOT part of public interface. Also, any API not documented in the [public + documentation](https://opentelemetry-cpp.readthedocs.io/en/latest/) is NOT part of public interface. + * GitHub releases will be made for all released versions. ## Example Versioning Lifecycle diff --git a/api/include/opentelemetry/common/key_value_iterable_view.h b/api/include/opentelemetry/common/key_value_iterable_view.h index 1712cfcda7..2a0cbbc445 100644 --- a/api/include/opentelemetry/common/key_value_iterable_view.h +++ b/api/include/opentelemetry/common/key_value_iterable_view.h @@ -14,6 +14,8 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace common { +// NOTE - code within `detail` namespace implements internal details, and not part +// of the public interface. namespace detail { inline void take_key_value(nostd::string_view, common::AttributeValue) {} diff --git a/api/include/opentelemetry/detail/preprocessor.h b/api/include/opentelemetry/detail/preprocessor.h index 5585c69db4..dc8eb57824 100644 --- a/api/include/opentelemetry/detail/preprocessor.h +++ b/api/include/opentelemetry/detail/preprocessor.h @@ -1,6 +1,9 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +// NOTE - code within detail namespace implements internal details, and not part +// of the public interface. + #pragma once #define OPENTELEMETRY_STRINGIFY(S) OPENTELEMETRY_STRINGIFY_(S) diff --git a/api/include/opentelemetry/trace/context.h b/api/include/opentelemetry/trace/context.h new file mode 100644 index 0000000000..165f1540b9 --- /dev/null +++ b/api/include/opentelemetry/trace/context.h @@ -0,0 +1,33 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/context/context.h" +#include "opentelemetry/trace/default_span.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace trace +{ + +// Get Span from explicit context +inline nostd::shared_ptr GetSpan(const opentelemetry::context::Context &context) +{ + context::ContextValue span = context.GetValue(kSpanKey); + if (nostd::holds_alternative>(span)) + { + return nostd::get>(span); + } + return nostd::shared_ptr(new DefaultSpan(SpanContext::GetInvalid())); +} + +// Set Span into explicit context +inline context::Context SetSpan(opentelemetry::context::Context &context, + nostd::shared_ptr span) +{ + return context.SetValue(kSpanKey, span); +} + +} // namespace trace +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/api/include/opentelemetry/trace/semantic_conventions.h b/api/include/opentelemetry/trace/experimental_semantic_conventions.h similarity index 97% rename from api/include/opentelemetry/trace/semantic_conventions.h rename to api/include/opentelemetry/trace/experimental_semantic_conventions.h index 13c5905783..3f38b9010e 100644 --- a/api/include/opentelemetry/trace/semantic_conventions.h +++ b/api/include/opentelemetry/trace/experimental_semantic_conventions.h @@ -1,6 +1,12 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +// NOTE: +// This implementation is based on the experimental specs for trace semantic convention as defined +// here: +// https://github.com/open-telemetry/opentelemetry-specification/tree/v1.0.0/specification/trace/semantic_conventions +// and MAY will change in future. + #pragma once #include "opentelemetry/common/string_util.h" diff --git a/api/include/opentelemetry/trace/propagation/b3_propagator.h b/api/include/opentelemetry/trace/propagation/b3_propagator.h index f9485bd619..d5661cb2a9 100644 --- a/api/include/opentelemetry/trace/propagation/b3_propagator.h +++ b/api/include/opentelemetry/trace/propagation/b3_propagator.h @@ -3,10 +3,10 @@ #pragma once -#include "detail/context.h" #include "detail/hex.h" #include "detail/string.h" #include "opentelemetry/context/propagation/text_map_propagator.h" +#include "opentelemetry/trace/context.h" #include "opentelemetry/trace/default_span.h" #include @@ -50,7 +50,7 @@ class B3PropagatorExtractor : public opentelemetry::context::propagation::TextMa { SpanContext span_context = ExtractImpl(carrier); nostd::shared_ptr sp{new DefaultSpan(span_context)}; - return SetSpan(context, sp); + return trace::SetSpan(context, sp); } static TraceId TraceIdFromHex(nostd::string_view trace_id) @@ -131,7 +131,7 @@ class B3Propagator : public B3PropagatorExtractor void Inject(opentelemetry::context::propagation::TextMapCarrier &carrier, const context::Context &context) noexcept override { - SpanContext span_context = GetSpan(context)->GetContext(); + SpanContext span_context = trace::GetSpan(context)->GetContext(); if (!span_context.IsValid()) { return; diff --git a/api/include/opentelemetry/trace/propagation/detail/context.h b/api/include/opentelemetry/trace/propagation/detail/context.h deleted file mode 100644 index 728a198911..0000000000 --- a/api/include/opentelemetry/trace/propagation/detail/context.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -#pragma once -#include "opentelemetry/context/context.h" -#include "opentelemetry/trace/default_span.h" - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace trace -{ -namespace propagation -{ - -inline nostd::shared_ptr GetSpan(const context::Context &context) -{ - context::ContextValue span = context.GetValue(trace::kSpanKey); - if (nostd::holds_alternative>(span)) - { - return nostd::get>(span); - } - return nostd::shared_ptr(new DefaultSpan(SpanContext::GetInvalid())); -} - -inline context::Context SetSpan(context::Context &context, nostd::shared_ptr span) -{ - - return context.SetValue(kSpanKey, span); -} - -} // namespace propagation -} // namespace trace -OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/trace/propagation/detail/hex.h b/api/include/opentelemetry/trace/propagation/detail/hex.h index 1a9f597070..10a17ff51b 100644 --- a/api/include/opentelemetry/trace/propagation/detail/hex.h +++ b/api/include/opentelemetry/trace/propagation/detail/hex.h @@ -13,6 +13,8 @@ namespace trace { namespace propagation { +// NOTE - code within `detail` namespace implements internal details, and not part +// of the public interface. namespace detail { diff --git a/api/include/opentelemetry/trace/propagation/detail/string.h b/api/include/opentelemetry/trace/propagation/detail/string.h index c583934251..67e934b408 100644 --- a/api/include/opentelemetry/trace/propagation/detail/string.h +++ b/api/include/opentelemetry/trace/propagation/detail/string.h @@ -10,6 +10,8 @@ namespace trace { namespace propagation { +// NOTE - code within `detail` namespace implements internal details, and not part +// of the public interface. namespace detail { diff --git a/api/include/opentelemetry/trace/propagation/http_trace_context.h b/api/include/opentelemetry/trace/propagation/http_trace_context.h index 41c6b1e61d..eca0d68f28 100644 --- a/api/include/opentelemetry/trace/propagation/http_trace_context.h +++ b/api/include/opentelemetry/trace/propagation/http_trace_context.h @@ -2,11 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 #include -#include "detail/context.h" #include "detail/hex.h" #include "detail/string.h" #include "opentelemetry/context/propagation/text_map_propagator.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/trace/context.h" #include "opentelemetry/trace/default_span.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -34,7 +34,7 @@ class HttpTraceContext : public opentelemetry::context::propagation::TextMapProp void Inject(opentelemetry::context::propagation::TextMapCarrier &carrier, const context::Context &context) noexcept override { - SpanContext span_context = GetSpan(context)->GetContext(); + SpanContext span_context = trace::GetSpan(context)->GetContext(); if (!span_context.IsValid()) { return; @@ -47,7 +47,7 @@ class HttpTraceContext : public opentelemetry::context::propagation::TextMapProp { SpanContext span_context = ExtractImpl(carrier); nostd::shared_ptr sp{new DefaultSpan(span_context)}; - return SetSpan(context, sp); + return trace::SetSpan(context, sp); } static TraceId TraceIdFromHex(nostd::string_view trace_id) diff --git a/api/include/opentelemetry/trace/propagation/jaeger.h b/api/include/opentelemetry/trace/propagation/jaeger.h index 6203752b91..78fc9e9c07 100644 --- a/api/include/opentelemetry/trace/propagation/jaeger.h +++ b/api/include/opentelemetry/trace/propagation/jaeger.h @@ -3,10 +3,10 @@ #pragma once -#include "detail/context.h" #include "detail/hex.h" #include "detail/string.h" #include "opentelemetry/context/propagation/text_map_propagator.h" +#include "opentelemetry/trace/context.h" #include "opentelemetry/trace/default_span.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -23,7 +23,7 @@ class JaegerPropagator : public context::propagation::TextMapPropagator void Inject(context::propagation::TextMapCarrier &carrier, const context::Context &context) noexcept override { - SpanContext span_context = GetSpan(context)->GetContext(); + SpanContext span_context = trace::GetSpan(context)->GetContext(); if (!span_context.IsValid()) { return; @@ -53,7 +53,7 @@ class JaegerPropagator : public context::propagation::TextMapPropagator { SpanContext span_context = ExtractImpl(carrier); nostd::shared_ptr sp{new DefaultSpan(span_context)}; - return SetSpan(context, sp); + return trace::SetSpan(context, sp); } bool Fields(nostd::function_ref callback) const noexcept override diff --git a/api/include/opentelemetry/trace/span.h b/api/include/opentelemetry/trace/span.h index 87b64747cf..77de4050d4 100644 --- a/api/include/opentelemetry/trace/span.h +++ b/api/include/opentelemetry/trace/span.h @@ -7,7 +7,6 @@ #include "opentelemetry/common/attribute_value.h" #include "opentelemetry/common/key_value_iterable_view.h" -#include "opentelemetry/common/timestamp.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/string_view.h" @@ -15,72 +14,14 @@ #include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/trace/canonical_code.h" #include "opentelemetry/trace/span_context.h" +#include "opentelemetry/trace/span_metadata.h" + #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace trace { -// The key identifies the active span in the current context. -constexpr char kSpanKey[] = "active_span"; - -enum class SpanKind -{ - kInternal, - kServer, - kClient, - kProducer, - kConsumer, -}; - -// StatusCode - Represents the canonical set of status codes of a finished Span. - -enum class StatusCode -{ - kUnset, // default status - kOk, // Operation has completed successfully. - kError // The operation contains an error -}; - -/** - * StartSpanOptions provides options to set properties of a Span at the time of - * its creation - */ -struct StartSpanOptions -{ - // Optionally sets the start time of a Span. - // - // If the start time of a Span is set, timestamps from both the system clock - // and steady clock must be provided. - // - // Timestamps from the steady clock can be used to most accurately measure a - // Span's duration, while timestamps from the system clock can be used to most - // accurately place a Span's - // time point relative to other Spans collected across a distributed system. - common::SystemTimestamp start_system_time; - common::SteadyTimestamp start_steady_time; - - // Explicitly set the parent of a Span. - // - // This defaults to an invalid span context. In this case, the Span is - // automatically parented to the currently active span. - SpanContext parent = SpanContext::GetInvalid(); - - // TODO: - // SpanContext remote_parent; - // Links - SpanKind kind = SpanKind::kInternal; -}; -/** - * StartEndOptions provides options to set properties of a Span when it is - * ended. - */ -struct EndSpanOptions -{ - // Optionally sets the end time of a Span. - common::SteadyTimestamp end_steady_time; -}; - class Tracer; /** @@ -176,7 +117,7 @@ class Span * @param options can be used to manually define span properties like the end * timestamp */ - virtual void End(const EndSpanOptions &options = {}) noexcept = 0; + virtual void End(const trace::EndSpanOptions &options = {}) noexcept = 0; virtual trace::SpanContext GetContext() const noexcept = 0; diff --git a/api/include/opentelemetry/trace/span_context_kv_iterable_view.h b/api/include/opentelemetry/trace/span_context_kv_iterable_view.h index c6b74bc00d..252b4cf175 100644 --- a/api/include/opentelemetry/trace/span_context_kv_iterable_view.h +++ b/api/include/opentelemetry/trace/span_context_kv_iterable_view.h @@ -15,6 +15,8 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace trace { +// NOTE - code within `detail` namespace implements internal details, and not part +// of the public interface. namespace detail { template diff --git a/api/include/opentelemetry/trace/span_metadata.h b/api/include/opentelemetry/trace/span_metadata.h new file mode 100644 index 0000000000..977329a151 --- /dev/null +++ b/api/include/opentelemetry/trace/span_metadata.h @@ -0,0 +1,43 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/common/timestamp.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace trace +{ + +enum class SpanKind +{ + kInternal, + kServer, + kClient, + kProducer, + kConsumer, +}; + +// The key identifies the active span in the current context. +constexpr char kSpanKey[] = "active_span"; + +// StatusCode - Represents the canonical set of status codes of a finished Span. +enum class StatusCode +{ + kUnset, // default status + kOk, // Operation has completed successfully. + kError // The operation contains an error +}; + +/** + * EndSpanOptions provides options to set properties of a Span when it is + * ended. + */ +struct EndSpanOptions +{ + // Optionally sets the end time of a Span. + common::SteadyTimestamp end_steady_time; +}; + +} // namespace trace +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/api/include/opentelemetry/trace/span_startoptions.h b/api/include/opentelemetry/trace/span_startoptions.h new file mode 100644 index 0000000000..688b768bd0 --- /dev/null +++ b/api/include/opentelemetry/trace/span_startoptions.h @@ -0,0 +1,45 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/context/context.h" +#include "opentelemetry/trace/span_context.h" +#include "opentelemetry/trace/span_metadata.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace trace +{ + +/** + * StartSpanOptions provides options to set properties of a Span at the time of + * its creation + */ +struct StartSpanOptions +{ + // Optionally sets the start time of a Span. + // + // If the start time of a Span is set, timestamps from both the system clock + // and steady clock must be provided. + // + // Timestamps from the steady clock can be used to most accurately measure a + // Span's duration, while timestamps from the system clock can be used to most + // accurately place a Span's + // time point relative to other Spans collected across a distributed system. + common::SystemTimestamp start_system_time; + common::SteadyTimestamp start_steady_time; + + // Explicitly set the parent of a Span. + // + // This defaults to an invalid span context. In this case, the Span is + // automatically parented to the currently active span. + nostd::variant parent = SpanContext::GetInvalid(); + + // TODO: + // SpanContext remote_parent; + // Links + SpanKind kind = SpanKind::kInternal; +}; + +} // namespace trace +OPENTELEMETRY_END_NAMESPACE \ No newline at end of file diff --git a/api/include/opentelemetry/trace/tracer.h b/api/include/opentelemetry/trace/tracer.h index 425e085116..b60336a48d 100644 --- a/api/include/opentelemetry/trace/tracer.h +++ b/api/include/opentelemetry/trace/tracer.h @@ -3,6 +3,7 @@ #pragma once +#include "opentelemetry/context/context.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/unique_ptr.h" @@ -10,6 +11,7 @@ #include "opentelemetry/trace/scope.h" #include "opentelemetry/trace/span.h" #include "opentelemetry/trace/span_context_kv_iterable_view.h" +#include "opentelemetry/trace/span_startoptions.h" #include "opentelemetry/version.h" #include @@ -17,7 +19,6 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace trace { - /** * Handles span creation and in-process context propagation. * diff --git a/api/test/trace/propagation/http_text_format_test.cc b/api/test/trace/propagation/http_text_format_test.cc index 497d716bb9..a815920a83 100644 --- a/api/test/trace/propagation/http_text_format_test.cc +++ b/api/test/trace/propagation/http_text_format_test.cc @@ -3,6 +3,7 @@ #include "opentelemetry/context/propagation/global_propagator.h" #include "opentelemetry/context/runtime_context.h" +#include "opentelemetry/trace/context.h" #include "opentelemetry/trace/propagation/http_trace_context.h" #include "opentelemetry/trace/scope.h" #include "util.h" @@ -150,7 +151,7 @@ TEST(TextMapPropagatorTest, InvalidIdentitiesAreNotExtracted) context::Context ctx1 = context::Context{}; context::Context ctx2 = format.Extract(carrier, ctx1); - auto span = trace::propagation::GetSpan(ctx2)->GetContext(); + auto span = trace::GetSpan(ctx2)->GetContext(); EXPECT_FALSE(span.IsValid()); } } diff --git a/api/test/trace/propagation/jaeger_propagation_test.cc b/api/test/trace/propagation/jaeger_propagation_test.cc index a0e0ade270..c337617383 100644 --- a/api/test/trace/propagation/jaeger_propagation_test.cc +++ b/api/test/trace/propagation/jaeger_propagation_test.cc @@ -98,7 +98,7 @@ TEST(JaegerPropagatorTest, ExtractValidSpans) context::Context ctx1 = context::Context{}; context::Context ctx2 = format.Extract(carrier, ctx1); - auto span = trace::propagation::GetSpan(ctx2)->GetContext(); + auto span = trace::GetSpan(ctx2)->GetContext(); EXPECT_TRUE(span.IsValid()); EXPECT_EQ(Hex(span.trace_id()), test_trace.expected_trace_id); @@ -129,7 +129,7 @@ TEST(JaegerPropagatorTest, ExctractInvalidSpans) context::Context ctx1 = context::Context{}; context::Context ctx2 = format.Extract(carrier, ctx1); - auto span = trace::propagation::GetSpan(ctx2)->GetContext(); + auto span = trace::GetSpan(ctx2)->GetContext(); EXPECT_FALSE(span.IsValid()); } } diff --git a/api/test/trace/span_benchmark.cc b/api/test/trace/span_benchmark.cc index a25d8f4a2d..238e8ca8a0 100644 --- a/api/test/trace/span_benchmark.cc +++ b/api/test/trace/span_benchmark.cc @@ -2,8 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/trace/context.h" #include "opentelemetry/trace/noop.h" -#include "opentelemetry/trace/propagation/detail/context.h" #include "opentelemetry/trace/span_id.h" #include "opentelemetry/trace/trace_id.h" @@ -112,7 +112,7 @@ void BM_SpanCreationWitContextPropagation(benchmark::State &state) auto outer_span_context = SpanContext(trace_id, span_id, trace_api::TraceFlags(), false); auto outer_span = nostd::shared_ptr(new trace_api::DefaultSpan(outer_span_context)); - trace_api::propagation::SetSpan(current_ctx, outer_span); + trace_api::SetSpan(current_ctx, outer_span); auto inner_child = tracer->StartSpan("inner"); auto scope = tracer->WithActiveSpan(inner_child); { diff --git a/docs/public/Doxyfile b/docs/public/Doxyfile index fb34196b71..b5fcb202a6 100644 --- a/docs/public/Doxyfile +++ b/docs/public/Doxyfile @@ -848,6 +848,7 @@ EXCLUDE = ../../api/include/opentelemetry/common/spin_lock_mutex. ../../api/include/opentelemetry/common/kv_properties.h \ ../../api/include/opentelemetry/common/string_util.h \ ../../api/include/opentelemetry/trace/span_context_kv_iterable_view.h \ + ./../api/include/opentelemetry/trace/propagation/detail \ ../../api/include/opentelemetry/nostd # The EXCLUDE_SYMLINKS tag can be used to select whether or not files or diff --git a/examples/grpc/BUILD b/examples/grpc/BUILD index 2ac65c226c..7de7049f36 100644 --- a/examples/grpc/BUILD +++ b/examples/grpc/BUILD @@ -32,7 +32,7 @@ cc_library( cc_binary( name = "client_grpc", srcs = [ - "client.cpp", + "client.cc", ], defines = ["BAZEL_BUILD"], deps = [ @@ -47,7 +47,7 @@ cc_binary( cc_binary( name = "server_grpc", srcs = [ - "server.cpp", + "server.cc", ], defines = ["BAZEL_BUILD"], deps = [ diff --git a/examples/grpc/CMakeLists.txt b/examples/grpc/CMakeLists.txt index 94d8a11129..3a90f57f71 100644 --- a/examples/grpc/CMakeLists.txt +++ b/examples/grpc/CMakeLists.txt @@ -37,7 +37,7 @@ else() endif() foreach(_target client server) - add_executable(${_target} "${_target}.cpp") + add_executable(${_target} "${_target}.cc") target_link_libraries( ${_target} example_grpc_proto protobuf::libprotobuf gRPC::grpc++ opentelemetry_trace opentelemetry_exporter_ostream_span) diff --git a/examples/grpc/client.cpp b/examples/grpc/client.cc similarity index 80% rename from examples/grpc/client.cpp rename to examples/grpc/client.cc index fc170a7c5b..21149c1958 100644 --- a/examples/grpc/client.cpp +++ b/examples/grpc/client.cc @@ -3,16 +3,16 @@ // modern compilers are unaffected. #include #ifdef BAZEL_BUILD -#include "examples/grpc/protos/messages.grpc.pb.h" +# include "examples/grpc/protos/messages.grpc.pb.h" #else -#include "messages.grpc.pb.h" +# include "messages.grpc.pb.h" #endif -#include "opentelemetry/trace/semantic_conventions.h" -#include "tracer_common.h" #include #include #include +#include "opentelemetry/trace/experimental_semantic_conventions.h" +#include "tracer_common.h" using grpc::Channel; using grpc::ClientContext; @@ -23,7 +23,6 @@ using grpc_example::Greeter; using grpc_example::GreetRequest; using grpc_example::GreetResponse; - namespace { @@ -45,13 +44,14 @@ class GreeterClient options.kind = opentelemetry::trace::SpanKind::kClient; 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}}, - options); + 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}}, + options); auto scope = get_tracer("grpc-client")->WithActiveSpan(span); diff --git a/examples/grpc/server.cpp b/examples/grpc/server.cc similarity index 77% rename from examples/grpc/server.cpp rename to examples/grpc/server.cc index b22ad2a6ca..c5ee379baf 100644 --- a/examples/grpc/server.cpp +++ b/examples/grpc/server.cc @@ -1,11 +1,13 @@ #ifdef BAZEL_BUILD -#include "examples/grpc/protos/messages.grpc.pb.h" +# include "examples/grpc/protos/messages.grpc.pb.h" #else -#include "messages.grpc.pb.h" +# include "messages.grpc.pb.h" #endif -#include "tracer_common.h" + +#include "opentelemetry/trace/context.h" +#include "opentelemetry/trace/experimental_semantic_conventions.h" #include "opentelemetry/trace/span_context_kv_iterable_view.h" -#include "opentelemetry/trace/semantic_conventions.h" +#include "tracer_common.h" #include #include @@ -14,10 +16,10 @@ #include #include +#include #include #include #include -#include using grpc::Server; using grpc::ServerBuilder; @@ -29,7 +31,7 @@ using grpc_example::Greeter; using grpc_example::GreetRequest; using grpc_example::GreetResponse; -using Span = opentelemetry::trace::Span; +using Span = opentelemetry::trace::Span; using SpanContext = opentelemetry::trace::SpanContext; using namespace opentelemetry::trace; @@ -42,7 +44,8 @@ class GreeterServer final : public Greeter::Service const GreetRequest *request, GreetResponse *response) override { - for( auto elem: context->client_metadata()) { + for (auto elem : context->client_metadata()) + { std::cout << "ELEM: " << elem.first << " " << elem.second << "\n"; } @@ -56,16 +59,16 @@ class GreeterServer final : public Greeter::Service auto prop = opentelemetry::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator(); auto current_ctx = opentelemetry::context::RuntimeContext::GetCurrent(); auto new_context = prop->Extract(carrier, current_ctx); - options.parent = opentelemetry::trace::propagation::GetSpan(new_context)->GetContext(); + options.parent = opentelemetry::trace::GetSpan(new_context)->GetContext(); 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}}, - options); + 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}}, + options); auto scope = get_tracer("grpc")->WithActiveSpan(span); // Fetch and parse whatever HTTP headers we can from the gRPC request. diff --git a/examples/http/client.cc b/examples/http/client.cc index 77694815a1..99874fa55c 100644 --- a/examples/http/client.cc +++ b/examples/http/client.cc @@ -3,7 +3,7 @@ #include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/ext/http/common/url_parser.h" -#include "opentelemetry/trace/semantic_conventions.h" +#include "opentelemetry/trace/experimental_semantic_conventions.h" #include "tracer_common.h" namespace diff --git a/examples/http/server.cc b/examples/http/server.cc index 6c1c9ce713..fd509c8b67 100644 --- a/examples/http/server.cc +++ b/examples/http/server.cc @@ -2,7 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 #include "server.h" -#include "opentelemetry/trace/semantic_conventions.h" +#include "opentelemetry/trace/context.h" +#include "opentelemetry/trace/experimental_semantic_conventions.h" #include "tracer_common.h" #include @@ -12,7 +13,7 @@ namespace { using namespace opentelemetry::trace; -; + uint16_t server_port = 8800; constexpr const char *server_name = "localhost"; @@ -32,7 +33,7 @@ class RequestHandler : public HTTP_SERVER_NS::HttpRequestCallback auto prop = opentelemetry::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator(); auto current_ctx = opentelemetry::context::RuntimeContext::GetCurrent(); auto new_context = prop->Extract(carrier, current_ctx); - options.parent = opentelemetry::trace::propagation::GetSpan(new_context)->GetContext(); + options.parent = GetSpan(new_context)->GetContext(); // start span with parent context extracted from http header auto span = get_tracer("http-server") diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h index 2806bfa593..47e6fa79c4 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_tracer.h @@ -569,8 +569,15 @@ class Tracer : public trace::Tracer // Parent Context: // - either use current span // - or attach to parent SpanContext specified in options - const auto parentContext = - (options.parent.IsValid()) ? options.parent : GetCurrentSpan()->GetContext(); + trace::SpanContext parentContext = GetCurrentSpan()->GetContext(); + if (nostd::holds_alternative(options.parent)) + { + auto span_context = nostd::get(options.parent); + if (span_context.IsValid()) + { + parentContext = span_context; + } + } // Populate Etw.RelatedActivityId at envelope level if enabled GUID RelatedActivityId; @@ -1089,8 +1096,9 @@ class TracerProvider : public trace::TracerProvider // identifier, see EventActivityIdControl. GetOption(options, "enableActivityId", config_.enableActivityId, false); - // Map parent `SpanId` to RelatedActivityId - Activity identifier from the previous component. - // Use this parameter to link your component's events to the previous component's events. + // Map parent `SpanId` to RelatedActivityId - Activity identifier from the previous + // component. Use this parameter to link your component's events to the previous component's + // events. GetOption(options, "enableRelatedActivityId", config_.enableRelatedActivityId, false); // When a new Span is started, the current span automatically becomes its parent. diff --git a/exporters/jaeger/src/recordable.cc b/exporters/jaeger/src/recordable.cc index f0e455923d..0e78ee3d71 100644 --- a/exporters/jaeger/src/recordable.cc +++ b/exporters/jaeger/src/recordable.cc @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/exporters/jaeger/recordable.h" -#include "opentelemetry/sdk/resource/semantic_conventions.h" +#include "opentelemetry/sdk/resource/experimental_semantic_conventions.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter diff --git a/exporters/otlp/README.md b/exporters/otlp/README.md index 8c6efe90d3..7bb5fd951b 100644 --- a/exporters/otlp/README.md +++ b/exporters/otlp/README.md @@ -42,11 +42,28 @@ options.url = "localhost:12345"; auto exporter = std::unique_ptr(new otlp::OtlpHttpExporter(options)); ``` -### Configuration options +### Configuration options ( OTLP GRPC Exporter ) -| Option | Default | -| ------------ |----------------- | -| `endpoint` | `localhost:4317` | +| Option | Env Variable |Default | Description | +| ------------ |---------------|------------ |----------------| +| `endpoint` | `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4317`| The OTLP GRPC endpoint to connect to | +| | `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` | | | +| `use_ssl_credentials` | `OTEL_EXPORTER_OTLP_SSL_ENABLE`| `false` | Whether the endpoint is SSL enabled | +| | `OTEL_EXPORTER_OTLP_TRACES_SSL_ENABLE` | | | +| `ssl_credentials_cacert_path` | `OTEL_EXPORTER_OTLP_CERTIFICATE` | `""` | SSL Certificate file path | +| | `OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE` | | | +| `ssl_credentials_cacert_as_string` | `OTEL_EXPORTER_OTLP_CERTIFICATE_STRING` | `""` | SSL Certifcate as in-memory string | +| | `OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE_STRING` | | | | + +### Configuration options ( OTLP HTTP Exporter ) + +| Option | Env Variable |Default | Description | +| ------------ |-----|------------ |------| +| `url` | n/a | `http://localhost:4317/v1/traces` | The OTLP HTTP endpoint to connect to | +| `content_type` | n/a | `application/json` | Data format used - JSON or Binary | +| `json_bytes_mapping` | n/a | `JsonBytesMappingKind::kHexId` | Encoding used for trace_id and span_id | +| `use_json_name` | n/a | `false` | Whether to use json name of protobuf field to set the key of json | +| `timeout` | n/a | `30000 ms` | http timeout | ## Example diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h index 31ad058c6d..af3c97b4ab 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h @@ -19,19 +19,30 @@ namespace exporter namespace otlp { -inline const std::string GetOtlpGrpcDefaultEndpoint() +inline const std::string GetOtlpDefaultEndpoint() { - constexpr char kOtlpGrpcEndpointEnv[] = "OTEL_EXPORTER_OTLP_GRPC_ENDPOINT"; - constexpr char kOtlpGrpcEndpointDefault[] = "localhost:4317"; + constexpr char kOtlpTracesEndpointEnv[] = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"; + constexpr char kOtlpEndpointEnv[] = "OTEL_EXPORTER_OTLP_ENDPOINT"; + constexpr char kOtlpEndpointDefault[] = "http://localhost:4317"; - auto endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpGrpcEndpointEnv); - return endpoint.size() ? endpoint : kOtlpGrpcEndpointDefault; + auto endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesEndpointEnv); + if (endpoint.size() == 0) + { + endpoint = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpEndpointEnv); + } + return endpoint.size() ? endpoint : kOtlpEndpointDefault; } -inline const bool GetOtlpGrpcDefaultIsSslEnable() +inline const bool GetOtlpDefaultIsSslEnable() { - constexpr char kOtlpGrpcIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_GRPC_SSL_ENABLE"; - auto ssl_enable = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpGrpcIsSslEnableEnv); + constexpr char kOtlpTracesIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_TRACES_SSL_ENABLE"; + constexpr char kOtlpIsSslEnableEnv[] = "OTEL_EXPORTER_OTLP_SSL_ENABLE"; + + auto ssl_enable = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesIsSslEnableEnv); + if (ssl_enable.size() == 0) + { + ssl_enable = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpIsSslEnableEnv); + } if (ssl_enable == "True" || ssl_enable == "TRUE" || ssl_enable == "true" || ssl_enable == "1") { return true; @@ -39,10 +50,29 @@ inline const bool GetOtlpGrpcDefaultIsSslEnable() return false; } -inline const std::string GetOtlpGrpcDefaultSslCertificate() +inline const std::string GetOtlpDefaultSslCertificatePath() +{ + constexpr char kOtlpTracesSslCertificate[] = "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE"; + constexpr char kOtlpSslCertificate[] = "OTEL_EXPORTER_OTLP_CERTIFICATE "; + auto ssl_cert_path = + opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesSslCertificate); + if (ssl_cert_path.size() == 0) + { + ssl_cert_path = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpSslCertificate); + } + return ssl_cert_path.size() ? ssl_cert_path : ""; +} + +inline const std::string GetOtlpDefaultSslCertificateString() { - constexpr char kOtlpGrpcSslCertificate[] = "OTEL_EXPORTER_OTLP_GRPC_SSL_CERTIFICATE"; - auto ssl_cert = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpGrpcSslCertificate); + constexpr char kOtlpTracesSslCertificateString[] = "OTEL_EXPORTER_OTLP_CERTIFICATE_STRING"; + constexpr char kOtlpSslCertificateString[] = "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE_STRING "; + auto ssl_cert = + opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpTracesSslCertificateString); + if (ssl_cert.size() == 0) + { + ssl_cert = opentelemetry::sdk::common::GetEnvironmentVariable(kOtlpSslCertificateString); + } return ssl_cert.size() ? ssl_cert : ""; } @@ -52,15 +82,15 @@ inline const std::string GetOtlpGrpcDefaultSslCertificate() struct OtlpGrpcExporterOptions { // The endpoint to export to. By default the OpenTelemetry Collector's default endpoint. - std::string endpoint = GetOtlpGrpcDefaultEndpoint(); + std::string endpoint = GetOtlpDefaultEndpoint(); // By default when false, uses grpc::InsecureChannelCredentials(); If true, // uses ssl_credentials_cacert_path if non-empty, else uses ssl_credentials_cacert_as_string - bool use_ssl_credentials = GetOtlpGrpcDefaultIsSslEnable(); + bool use_ssl_credentials = GetOtlpDefaultIsSslEnable(); // ssl_credentials_cacert_path specifies path to .pem file to be used for SSL encryption. - std::string ssl_credentials_cacert_path = ""; + std::string ssl_credentials_cacert_path = GetOtlpDefaultSslCertificatePath(); // ssl_credentials_cacert_as_string in-memory string representation of .pem file to be used for // SSL encryption. - std::string ssl_credentials_cacert_as_string = GetOtlpGrpcDefaultSslCertificate(); + std::string ssl_credentials_cacert_as_string = GetOtlpDefaultSslCertificateString(); }; /** diff --git a/exporters/otlp/src/otlp_grpc_exporter.cc b/exporters/otlp/src/otlp_grpc_exporter.cc index 2d522ca3cd..dec65fadda 100644 --- a/exporters/otlp/src/otlp_grpc_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_exporter.cc @@ -59,6 +59,7 @@ std::unique_ptr MakeServiceStub const OtlpGrpcExporterOptions &options) { std::shared_ptr channel; + if (options.use_ssl_credentials) { grpc::SslCredentialsOptions ssl_opts; diff --git a/exporters/otlp/test/otlp_grpc_exporter_test.cc b/exporters/otlp/test/otlp_grpc_exporter_test.cc index d2b999382a..23c27242be 100644 --- a/exporters/otlp/test/otlp_grpc_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_exporter_test.cc @@ -139,12 +139,12 @@ TEST_F(OtlpGrpcExporterTestPeer, ConfigSslCredentialsTest) TEST_F(OtlpGrpcExporterTestPeer, ConfigFromEnv) { const std::string cacert_str = "--begin and end fake cert--"; - const std::string cacert_env = "OTEL_EXPORTER_OTLP_GRPC_SSL_CERTIFICATE=" + cacert_str; + const std::string cacert_env = "OTEL_EXPORTER_OTLP_CERTIFICATE_STRING=" + cacert_str; putenv(const_cast(cacert_env.data())); - char ssl_enable_env[] = "OTEL_EXPORTER_OTLP_GRPC_SSL_ENABLE=True"; + char ssl_enable_env[] = "OTEL_EXPORTER_OTLP_SSL_ENABLE=True"; putenv(ssl_enable_env); const std::string endpoint = "http://localhost:9999"; - const std::string endpoint_env = "OTEL_EXPORTER_OTLP_GRPC_ENDPOINT=" + endpoint; + const std::string endpoint_env = "OTEL_EXPORTER_OTLP_ENDPOINT=" + endpoint; putenv(const_cast(endpoint_env.data())); std::unique_ptr exporter(new OtlpGrpcExporter()); @@ -152,14 +152,14 @@ TEST_F(OtlpGrpcExporterTestPeer, ConfigFromEnv) EXPECT_EQ(GetOptions(exporter).use_ssl_credentials, true); EXPECT_EQ(GetOptions(exporter).endpoint, endpoint); # if defined(_MSC_VER) - putenv("OTEL_EXPORTER_OTLP_GRPC_ENDPOINT="); - putenv("OTEL_EXPORTER_OTLP_GRPC_SSL_CERTIFICATE="); - putenv("OTEL_EXPORTER_OTLP_GRPC_SSL_ENABLE="); + putenv("OTEL_EXPORTER_OTLP_ENDPOINT="); + putenv("OTEL_EXPORTER_OTLP_CERTIFICATE_STRING="); + putenv("OTEL_EXPORTER_OTLP_SSL_ENABLE="); # else - unsetenv("OTEL_EXPORTER_OTLP_GRPC_ENDPOINT"); - unsetenv("OTEL_EXPORTER_OTLP_GRPC_SSL_CERTIFICATE"); - unsetenv("OTEL_EXPORTER_OTLP_GRPC_SSL_ENABLE"); + unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); + unsetenv("OTEL_EXPORTER_OTLP_CERTIFICATE_STRING"); + unsetenv("OTEL_EXPORTER_OTLP_SSL_ENABLE"); # endif } diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 2dfd32ce05..daab046439 100644 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -222,7 +222,9 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) child_span->End(); parent_span->End(); - child_span_opts.parent.trace_id().ToLowerBase16(MakeSpan(trace_id_hex)); + nostd::get(child_span_opts.parent) + .trace_id() + .ToLowerBase16(MakeSpan(trace_id_hex)); report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); } @@ -282,7 +284,9 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) child_span->End(); parent_span->End(); - child_span_opts.parent.trace_id().CopyBytesTo(MakeSpan(trace_id_binary)); + nostd::get(child_span_opts.parent) + .trace_id() + .CopyBytesTo(MakeSpan(trace_id_binary)); report_trace_id.assign(reinterpret_cast(trace_id_binary), sizeof(trace_id_binary)); } diff --git a/exporters/zipkin/src/recordable.cc b/exporters/zipkin/src/recordable.cc index 327fc317ca..e9da830348 100644 --- a/exporters/zipkin/src/recordable.cc +++ b/exporters/zipkin/src/recordable.cc @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/exporters/zipkin/recordable.h" -#include "opentelemetry/sdk/resource/semantic_conventions.h" +#include "opentelemetry/sdk/resource/experimental_semantic_conventions.h" #include #include diff --git a/sdk/include/opentelemetry/sdk/common/env_variables.h b/sdk/include/opentelemetry/sdk/common/env_variables.h index 63d16d12b5..c989b8921d 100644 --- a/sdk/include/opentelemetry/sdk/common/env_variables.h +++ b/sdk/include/opentelemetry/sdk/common/env_variables.h @@ -15,9 +15,8 @@ namespace common // Returns the env variable set. inline const std::string GetEnvironmentVariable(const char *env_var_name) { +#if !defined(NO_GETENV) const char *endpoint_from_env = nullptr; - -#ifndef NO_GETENV # if defined(_MSC_VER) // avoid calling std::getenv which is deprecated in MSVC. size_t required_size = 0; @@ -31,10 +30,12 @@ inline const std::string GetEnvironmentVariable(const char *env_var_name) } # else endpoint_from_env = std::getenv(env_var_name); -# endif -#endif - return endpoint_from_env == nullptr ? std::string() : endpoint_from_env; +# endif // defined(_MSC_VER) + return endpoint_from_env == nullptr ? std::string{} : std::string{endpoint_from_env}; +#else + return std::string{}; +#endif // !defined(NO_GETENV) } } // namespace common } // namespace sdk -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/resource/semantic_conventions.h b/sdk/include/opentelemetry/sdk/resource/experimental_semantic_conventions.h similarity index 94% rename from sdk/include/opentelemetry/sdk/resource/semantic_conventions.h rename to sdk/include/opentelemetry/sdk/resource/experimental_semantic_conventions.h index 693b3e108a..bb7941ff1b 100644 --- a/sdk/include/opentelemetry/sdk/resource/semantic_conventions.h +++ b/sdk/include/opentelemetry/sdk/resource/experimental_semantic_conventions.h @@ -1,3 +1,12 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +// NOTE: +// This implementation is based on the experimental specs for resource semantic convention as +// defined here: +// https://github.com/open-telemetry/opentelemetry-specification/tree/v1.0.0/specification/resource/semantic_conventions +// and MAY will change in future. + #pragma once #include diff --git a/sdk/src/resource/resource.cc b/sdk/src/resource/resource.cc index bdbbc91b13..930e8e6640 100644 --- a/sdk/src/resource/resource.cc +++ b/sdk/src/resource/resource.cc @@ -3,8 +3,8 @@ #include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/nostd/span.h" +#include "opentelemetry/sdk/resource/experimental_semantic_conventions.h" #include "opentelemetry/sdk/resource/resource_detector.h" -#include "opentelemetry/sdk/resource/semantic_conventions.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index 977722d0ca..be5e5f7d96 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -5,6 +5,7 @@ #include "opentelemetry/context/runtime_context.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/sdk/common/atomic_shared_ptr.h" +#include "opentelemetry/trace/context.h" #include "opentelemetry/version.h" #include "src/trace/span.h" @@ -27,8 +28,25 @@ nostd::shared_ptr Tracer::StartSpan( const trace_api::SpanContextKeyValueIterable &links, const trace_api::StartSpanOptions &options) noexcept { - trace_api::SpanContext parent_context = - options.parent.IsValid() ? options.parent : GetCurrentSpan()->GetContext(); + trace_api::SpanContext parent_context = GetCurrentSpan()->GetContext(); + if (nostd::holds_alternative(options.parent)) + { + auto span_context = nostd::get(options.parent); + if (span_context.IsValid()) + { + parent_context = span_context; + } + } + else if (nostd::holds_alternative(options.parent)) + { + auto context = nostd::get(options.parent); + // fetch span context from parent span stored in the context + auto span_context = opentelemetry::trace::GetSpan(context)->GetContext(); + if (span_context.IsValid()) + { + parent_context = span_context; + } + } trace_api::TraceId trace_id; trace_api::SpanId span_id = GetIdGenerator().GenerateSpanId(); diff --git a/sdk/test/resource/resource_test.cc b/sdk/test/resource/resource_test.cc index f426c23fe3..7f00ddaf48 100644 --- a/sdk/test/resource/resource_test.cc +++ b/sdk/test/resource/resource_test.cc @@ -5,8 +5,8 @@ #include "opentelemetry/common/key_value_iterable_view.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/attribute_utils.h" +#include "opentelemetry/sdk/resource/experimental_semantic_conventions.h" #include "opentelemetry/sdk/resource/resource_detector.h" -#include "opentelemetry/sdk/resource/semantic_conventions.h" #include #include diff --git a/sdk/test/trace/tracer_test.cc b/sdk/test/trace/tracer_test.cc index 92659a8a5a..94e57a4cd7 100644 --- a/sdk/test/trace/tracer_test.cc +++ b/sdk/test/trace/tracer_test.cc @@ -9,6 +9,7 @@ #include "opentelemetry/sdk/trace/samplers/parent.h" #include "opentelemetry/sdk/trace/simple_processor.h" #include "opentelemetry/sdk/trace/span_data.h" +#include "opentelemetry/trace/context.h" #include @@ -621,6 +622,44 @@ TEST(Tracer, ExpectParent) EXPECT_EQ(spandata_second->GetSpanId(), spandata_third->GetParentSpanId()); } +TEST(Tracer, ExpectParentAsContext) +{ + std::unique_ptr exporter(new InMemorySpanExporter()); + std::shared_ptr span_data = exporter->GetData(); + auto tracer = initTracer(std::move(exporter)); + auto spans = span_data.get()->GetSpans(); + + ASSERT_EQ(0, spans.size()); + + auto span_first = tracer->StartSpan("span 1"); + + opentelemetry::context::Context c1; + auto c2 = trace_api::SetSpan(c1, span_first); + trace_api::StartSpanOptions options; + options.parent = c2; + auto span_second = tracer->StartSpan("span 2", options); + + auto c3 = trace_api::SetSpan(c2, span_second); + options.parent = c3; + auto span_third = tracer->StartSpan("span 3", options); + + span_third->End(); + span_second->End(); + span_first->End(); + + spans = span_data->GetSpans(); + ASSERT_EQ(3, spans.size()); + auto spandata_first = std::move(spans.at(2)); + auto spandata_second = std::move(spans.at(1)); + auto spandata_third = std::move(spans.at(0)); + EXPECT_EQ("span 1", spandata_first->GetName()); + EXPECT_EQ("span 2", spandata_second->GetName()); + EXPECT_EQ("span 3", spandata_third->GetName()); + + EXPECT_EQ(spandata_first->GetSpanId(), spandata_second->GetParentSpanId()); + EXPECT_EQ(spandata_second->GetSpanId(), spandata_third->GetParentSpanId()); +} + TEST(Tracer, ValidTraceIdToSampler) { std::unique_ptr exporter(new InMemorySpanExporter());