Skip to content

Commit

Permalink
Merge branch 'main' into PopulateInstLibToExporter
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Apr 28, 2021
2 parents 1096bcd + 888c59f commit bee87b3
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 33 deletions.
2 changes: 1 addition & 1 deletion api/include/opentelemetry/nostd/function_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class function_ref<R(Args...)>
typename std::enable_if<!std::is_same<function_ref, typename std::decay<F>::type>::value,
int>::type = 0,
typename std::enable_if<
#if (__cplusplus >= 201703L)
#if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG > 201402))
// std::result_of deprecated in C++17, removed in C++20
std::is_convertible<typename std::invoke_result<F, Args...>::type, R>::value,
#else
Expand Down
6 changes: 5 additions & 1 deletion api/include/opentelemetry/nostd/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@

#pragma once

// Try to use either `std::span` or `gsl::span`
#ifdef HAVE_CPP_STDLIB
# include "opentelemetry/std/span.h"
#else
#endif

// Fallback to `nostd::span` if necessary
#if !defined(HAVE_SPAN)
# include <array>
# include <cassert>
# include <cstddef>
Expand Down
24 changes: 12 additions & 12 deletions api/include/opentelemetry/std/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
# define HAVE_SPAN
# endif
# endif
# if __has_include(<span>) && !defined(HAVE_SPAN) // Check for span
# define HAVE_SPAN
# endif
# if !__has_include(<string_view>) // Check for string_view
# error \
"STL library does not support std::span. Possible solution:" \
" - #undef HAVE_CPP_STDLIB // to use OpenTelemetry nostd::string_view"
# if !defined(HAVE_SPAN)
# // Check for Visual Studio span
# if defined(_MSVC_LANG) && _HAS_CXX20
# define HAVE_SPAN
# endif
# // Check for other compiler span implementation
# if !defined(_MSVC_LANG) && __has_include(<span>)
// This works as long as compiler standard is set to C++20
# define HAVE_SPAN
# endif
# endif
#endif

Expand All @@ -48,12 +51,9 @@ template <class ElementType, std::size_t Extent = gsl::dynamic_extent>
using span = gsl::span<ElementType, Extent>;
}
OPENTELEMETRY_END_NAMESPACE
# define HAVE_SPAN
# else
// No span implementation provided.
# error \
"STL library does not support std::span. Possible solutions:" \
" - #undef HAVE_CPP_STDLIB // to use OpenTelemetry nostd::span .. or " \
" - #define HAVE_GSL // to use gsl::span "
// No `gsl::span`, no `std::span`, fallback to `nostd::span`
# endif

#else // HAVE_SPAN
Expand Down
7 changes: 4 additions & 3 deletions api/include/opentelemetry/trace/propagation/b3_propagator.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ class B3Propagator : public B3PropagatorExtractor

char trace_identity[kTraceIdHexStrLength + kSpanIdHexStrLength + 3];
static_assert(sizeof(trace_identity) == 51, "b3 trace identity buffer size mismatch");
span_context.trace_id().ToLowerBase16({&trace_identity[0], kTraceIdHexStrLength});
span_context.trace_id().ToLowerBase16(
nostd::span<char, 2 * TraceId::kSize>{&trace_identity[0], kTraceIdHexStrLength});
trace_identity[kTraceIdHexStrLength] = '-';
span_context.span_id().ToLowerBase16(
{&trace_identity[kTraceIdHexStrLength + 1], kSpanIdHexStrLength});
span_context.span_id().ToLowerBase16(nostd::span<char, 2 * SpanId::kSize>{
&trace_identity[kTraceIdHexStrLength + 1], kSpanIdHexStrLength});
trace_identity[kTraceIdHexStrLength + kSpanIdHexStrLength + 1] = '-';
trace_identity[kTraceIdHexStrLength + kSpanIdHexStrLength + 2] =
span_context.trace_flags().IsSampled() ? '1' : '0';
Expand Down
11 changes: 8 additions & 3 deletions api/include/opentelemetry/trace/propagation/http_trace_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <array>
#include "detail/context.h"
#include "detail/hex.h"
#include "detail/string.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/trace/default_span.h"
#include "opentelemetry/trace/propagation/text_map_propagator.h"

Expand Down Expand Up @@ -95,11 +97,14 @@ class HttpTraceContext : public TextMapPropagator
trace_parent[0] = '0';
trace_parent[1] = '0';
trace_parent[2] = '-';
span_context.trace_id().ToLowerBase16({&trace_parent[3], kTraceIdSize});
span_context.trace_id().ToLowerBase16(
nostd::span<char, 2 * TraceId::kSize>{&trace_parent[3], kTraceIdSize});
trace_parent[kTraceIdSize + 3] = '-';
span_context.span_id().ToLowerBase16({&trace_parent[kTraceIdSize + 4], kSpanIdSize});
span_context.span_id().ToLowerBase16(
nostd::span<char, 2 * SpanId::kSize>{&trace_parent[kTraceIdSize + 4], kSpanIdSize});
trace_parent[kTraceIdSize + kSpanIdSize + 4] = '-';
span_context.trace_flags().ToLowerBase16({&trace_parent[kTraceIdSize + kSpanIdSize + 5], 2});
span_context.trace_flags().ToLowerBase16(
nostd::span<char, 2>{&trace_parent[kTraceIdSize + kSpanIdSize + 5], 2});

carrier.Set(kTraceParent, nostd::string_view(trace_parent, sizeof(trace_parent)));
carrier.Set(kTraceState, span_context.trace_state()->ToHeader());
Expand Down
6 changes: 4 additions & 2 deletions api/include/opentelemetry/trace/propagation/jaeger.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ class JaegerPropagator : public TextMapPropagator

// trace-id(32):span-id(16):0:debug(2)
char trace_identity[trace_id_length + span_id_length + 6];
span_context.trace_id().ToLowerBase16({&trace_identity[0], trace_id_length});
span_context.trace_id().ToLowerBase16(
nostd::span<char, 2 * TraceId::kSize>{&trace_identity[0], trace_id_length});
trace_identity[trace_id_length] = ':';
span_context.span_id().ToLowerBase16({&trace_identity[trace_id_length + 1], span_id_length});
span_context.span_id().ToLowerBase16(
nostd::span<char, 2 * SpanId::kSize>{&trace_identity[trace_id_length + 1], span_id_length});
trace_identity[trace_id_length + span_id_length + 1] = ':';
trace_identity[trace_id_length + span_id_length + 2] = '0';
trace_identity[trace_id_length + span_id_length + 3] = ':';
Expand Down
2 changes: 2 additions & 0 deletions exporters/ostream/test/ostream_log_test.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <array>
#include "opentelemetry/exporters/ostream/log_exporter.h"
#include "opentelemetry/logs/provider.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/sdk/logs/logger_provider.h"
#include "opentelemetry/sdk/logs/simple_log_processor.h"

Expand Down
33 changes: 25 additions & 8 deletions exporters/otlp/test/otlp_exporter_test.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
#include "opentelemetry/exporters/otlp/otlp_exporter.h"
#ifndef HAVE_CPP_STDLIB
// Unfortunately as of 04/27/2021 the fix is NOT in the vcpkg snapshot of Google Test.
// Remove above `#ifdef` once the GMock fix for C++20 is in the mainline.
//
// Please refer to this GitHub issue for additional details:
// https://github.com/google/googletest/issues/2914
// https://github.com/google/googletest/commit/61f010d703b32de9bfb20ab90ece38ab2f25977f
//
// If we compile using Visual Studio 2019 with `c++latest` (C++20) without the GMock fix,
// then the compilation here fails in `gmock-actions.h` from:
// .\tools\vcpkg\installed\x64-windows\include\gmock\gmock-actions.h(819):
// error C2653: 'result_of': is not a class or namespace name
//
// That is because `std::result_of` has been removed in C++20.

#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"
# include "opentelemetry/exporters/otlp/otlp_exporter.h"

#include "opentelemetry/proto/collector/trace/v1/trace_service_mock.grpc.pb.h"
# include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"

#include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"
// Problematic code that pulls in Gmock and breaks with vs2019/c++latest :
# include "opentelemetry/proto/collector/trace/v1/trace_service_mock.grpc.pb.h"

#include "opentelemetry/sdk/trace/simple_processor.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/trace/provider.h"
# include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"

#include <gtest/gtest.h>
# include "opentelemetry/sdk/trace/simple_processor.h"
# include "opentelemetry/sdk/trace/tracer_provider.h"
# include "opentelemetry/trace/provider.h"

# include <gtest/gtest.h>

using namespace testing;

Expand Down Expand Up @@ -112,3 +128,4 @@ TEST_F(OtlpExporterTestPeer, ConfigSslCredentialsTest)
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,23 @@ const char *kZipkinEndpointDefault = "http://localhost:9411/api/v2/spans";

static const std::string GetDefaultZipkinEndpoint()
{
auto endpoint_from_env = std::getenv("OTEL_EXPORTER_ZIPKIN_ENDPOINT");
const char *otel_exporter_zipkin_endpoint_env = "OTEL_EXPORTER_ZIPKIN_ENDPOINT";
#if defined(_MSC_VER)
// avoid calling std::getenv which is deprecated in MSVC.
size_t required_size = 0;
getenv_s(&required_size, nullptr, 0, otel_exporter_zipkin_endpoint_env);
const char *endpoint_from_env = nullptr;
std::unique_ptr<char> endpoint_buffer;
if (required_size > 0)
{
endpoint_buffer = std::unique_ptr<char>{new char[required_size]};
getenv_s(&required_size, endpoint_buffer.get(), required_size,
otel_exporter_zipkin_endpoint_env);
endpoint_from_env = endpoint_buffer.get();
}
#else
auto endpoint_from_env = std::getenv(otel_exporter_zipkin_endpoint_env);
#endif
return std::string{endpoint_from_env ? endpoint_from_env : kZipkinEndpointDefault};
}

Expand Down
2 changes: 1 addition & 1 deletion exporters/zipkin/test/zipkin_recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ struct ZipkinIntAttributeTest : public testing::Test
};

using IntTypes = testing::Types<int, int64_t, unsigned int, uint64_t>;
TYPED_TEST_CASE(ZipkinIntAttributeTest, IntTypes);
TYPED_TEST_SUITE(ZipkinIntAttributeTest, IntTypes);

TYPED_TEST(ZipkinIntAttributeTest, SetIntSingleAttribute)
{
Expand Down
12 changes: 11 additions & 1 deletion sdk/src/resource/resource_detector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ const char *OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES";

Resource OTELResourceDetector::Detect() noexcept
{
#if defined(_MSC_VER)
size_t required_size = 0;
getenv_s(&required_size, nullptr, 0, OTEL_RESOURCE_ATTRIBUTES);
if (required_size == 0)
return Resource();
std::unique_ptr<char> attributes_buffer{new char[required_size]};
getenv_s(&required_size, attributes_buffer.get(), required_size, OTEL_RESOURCE_ATTRIBUTES);
char *attributes_str = attributes_buffer.get();
#else
char *attributes_str = std::getenv(OTEL_RESOURCE_ATTRIBUTES);
if (attributes_str == nullptr)
return Resource();
// return Resource::GetEmpty();
#endif

ResourceAttributes attributes;
std::istringstream iss(attributes_str);
std::string token;
Expand Down
2 changes: 2 additions & 0 deletions sdk/test/logs/logger_provider_sdk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* limitations under the License.
*/

#include <array>
#include "opentelemetry/logs/provider.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/logs/log_record.h"
#include "opentelemetry/sdk/logs/logger.h"
#include "opentelemetry/sdk/logs/logger_provider.h"
Expand Down

0 comments on commit bee87b3

Please sign in to comment.