Skip to content

Commit

Permalink
[EXPORTER] OTLP file: use fopen, fwrite, fflush and fclose wh…
Browse files Browse the repository at this point in the history
…ich are thread-safe. (#2675)

* Use `fopen`, `fwrite`, `fflush` and `fclose` which are thread-safety.

* Add some macros to help to find problems.

Add `OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND` , `OPENTELEMETRY_SANITIZER_NO_MEMORY` , `OPENTELEMETRY_SANITIZER_NO_THREAD`, `OPENTELEMETRY_SANITIZER_NO_ADDRESS`, `OPENTELEMETRY_HAVE_BUILTIN`, `OPENTELEMETRY_HAVE_FEATURE`, `OPENTELEMETRY_HAVE_ATTRIBUTE`, `OPENTELEMETRY_HAVE_CPP_ATTRIBUTE`

* Append EOL before call Export of backend

---------

Co-authored-by: Marc Alff <[email protected]>
  • Loading branch information
owent and marcalff authored Jun 8, 2024
1 parent 436a981 commit 3efd3ce
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 29 deletions.
171 changes: 171 additions & 0 deletions api/include/opentelemetry/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,73 @@

#pragma once

/*
OPENTELEMETRY_HAVE_BUILTIN&OPENTELEMETRY_HAVE_FEATURE
Checks whether the compiler supports a Clang Feature Checking Macro, and if
so, checks whether it supports the provided builtin function "x" where x
is one of the functions noted in
https://clang.llvm.org/docs/LanguageExtensions.html
Note: Use this macro to avoid an extra level of #ifdef __has_builtin check.
http://releases.llvm.org/3.3/tools/clang/docs/LanguageExtensions.html
*/
#if !defined(OPENTELEMETRY_HAVE_BUILTIN)
# ifdef __has_builtin
# define OPENTELEMETRY_HAVE_BUILTIN(x) __has_builtin(x)
# else
# define OPENTELEMETRY_HAVE_BUILTIN(x) 0
# endif
#endif

#if !defined(OPENTELEMETRY_HAVE_FEATURE)
# ifdef __has_feature
# define OPENTELEMETRY_HAVE_FEATURE(f) __has_feature(f)
# else
# define OPENTELEMETRY_HAVE_FEATURE(f) 0
# endif
#endif

/*
has feature
OPENTELEMETRY_HAVE_ATTRIBUTE
A function-like feature checking macro that is a wrapper around
`__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
nonzero constant integer if the attribute is supported or 0 if not.
It evaluates to zero if `__has_attribute` is not defined by the compiler.
GCC: https://gcc.gnu.org/gcc-5/changes.html
Clang: https://clang.llvm.org/docs/LanguageExtensions.html
*/
#if !defined(OPENTELEMETRY_HAVE_ATTRIBUTE)
# ifdef __has_attribute
# define OPENTELEMETRY_HAVE_ATTRIBUTE(x) __has_attribute(x)
# else
# define OPENTELEMETRY_HAVE_ATTRIBUTE(x) 0
# endif
#endif

/*
OPENTELEMETRY_HAVE_CPP_ATTRIBUTE
A function-like feature checking macro that accepts C++11 style attributes.
It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6
(https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't
find `__has_cpp_attribute`, will evaluate to 0.
*/
#if !defined(OPENTELEMETRY_HAVE_CPP_ATTRIBUTE)
# if defined(__cplusplus) && defined(__has_cpp_attribute)
// NOTE: requiring __cplusplus above should not be necessary, but
// works around https://bugs.llvm.org/show_bug.cgi?id=23435.
# define OPENTELEMETRY_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
# else
# define OPENTELEMETRY_HAVE_CPP_ATTRIBUTE(x) 0
# endif
#endif

/*
Expected usage pattern:
Expand Down Expand Up @@ -319,3 +386,107 @@ point.
# define OPENTELEMETRY_EXPORT

#endif

/*
OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function
parameter or implicit object parameter is retained by the return value of the
annotated function (or, for a parameter of a constructor, in the value of the
constructed object). This attribute causes warnings to be produced if a
temporary object does not live long enough.
When applied to a reference parameter, the referenced object is assumed to be
retained by the return value of the function. When applied to a non-reference
parameter (for example, a pointer or a class type), all temporaries
referenced by the parameter are assumed to be retained by the return value of
the function.
See also the upstream documentation:
https://clang.llvm.org/docs/AttributeReference.html#lifetimebound
*/
#ifndef OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND
# if OPENTELEMETRY_HAVE_CPP_ATTRIBUTE(clang::lifetimebound)
# define OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND [[clang::lifetimebound]]
# elif OPENTELEMETRY_HAVE_ATTRIBUTE(lifetimebound)
# define OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND __attribute__((lifetimebound))
# else
# define OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND
# endif
#endif

// OPENTELEMETRY_HAVE_MEMORY_SANITIZER
//
// MemorySanitizer (MSan) is a detector of uninitialized reads. It consists of
// a compiler instrumentation module and a run-time library.
#ifndef OPENTELEMETRY_HAVE_MEMORY_SANITIZER
# if !defined(__native_client__) && OPENTELEMETRY_HAVE_FEATURE(memory_sanitizer)
# define OPENTELEMETRY_HAVE_MEMORY_SANITIZER 1
# else
# define OPENTELEMETRY_HAVE_MEMORY_SANITIZER 0
# endif
#endif

#if OPENTELEMETRY_HAVE_MEMORY_SANITIZER && OPENTELEMETRY_HAVE_ATTRIBUTE(no_sanitize_memory)
# define OPENTELEMETRY_SANITIZER_NO_MEMORY \
__attribute__((no_sanitize_memory)) // __attribute__((no_sanitize("memory")))
#else
# define OPENTELEMETRY_SANITIZER_NO_MEMORY
#endif

// OPENTELEMETRY_HAVE_THREAD_SANITIZER
//
// ThreadSanitizer (TSan) is a fast data race detector.
#ifndef OPENTELEMETRY_HAVE_THREAD_SANITIZER
# if defined(__SANITIZE_THREAD__)
# define OPENTELEMETRY_HAVE_THREAD_SANITIZER 1
# elif OPENTELEMETRY_HAVE_FEATURE(thread_sanitizer)
# define OPENTELEMETRY_HAVE_THREAD_SANITIZER 1
# else
# define OPENTELEMETRY_HAVE_THREAD_SANITIZER 0
# endif
#endif

#if OPENTELEMETRY_HAVE_THREAD_SANITIZER && OPENTELEMETRY_HAVE_ATTRIBUTE(no_sanitize_thread)
# define OPENTELEMETRY_SANITIZER_NO_THREAD \
__attribute__((no_sanitize_thread)) // __attribute__((no_sanitize("thread")))
#else
# define OPENTELEMETRY_SANITIZER_NO_THREAD
#endif

// OPENTELEMETRY_HAVE_ADDRESS_SANITIZER
//
// AddressSanitizer (ASan) is a fast memory error detector.
#ifndef OPENTELEMETRY_HAVE_ADDRESS_SANITIZER
# if defined(__SANITIZE_ADDRESS__)
# define OPENTELEMETRY_HAVE_ADDRESS_SANITIZER 1
# elif OPENTELEMETRY_HAVE_FEATURE(address_sanitizer)
# define OPENTELEMETRY_HAVE_ADDRESS_SANITIZER 1
# else
# define OPENTELEMETRY_HAVE_ADDRESS_SANITIZER 0
# endif
#endif

// OPENTELEMETRY_HAVE_HWADDRESS_SANITIZER
//
// Hardware-Assisted AddressSanitizer (or HWASAN) is even faster than asan
// memory error detector which can use CPU features like ARM TBI, Intel LAM or
// AMD UAI.
#ifndef OPENTELEMETRY_HAVE_HWADDRESS_SANITIZER
# if defined(__SANITIZE_HWADDRESS__)
# define OPENTELEMETRY_HAVE_HWADDRESS_SANITIZER 1
# elif OPENTELEMETRY_HAVE_FEATURE(hwaddress_sanitizer)
# define OPENTELEMETRY_HAVE_HWADDRESS_SANITIZER 1
# else
# define OPENTELEMETRY_HAVE_HWADDRESS_SANITIZER 0
# endif
#endif

#if OPENTELEMETRY_HAVE_ADDRESS_SANITIZER && OPENTELEMETRY_HAVE_ATTRIBUTE(no_sanitize_address)
# define OPENTELEMETRY_SANITIZER_NO_ADDRESS \
__attribute__((no_sanitize_address)) // __attribute__((no_sanitize("address")))
#elif OPENTELEMETRY_HAVE_ADDRESS_SANITIZER && defined(_MSC_VER) && _MSC_VER >= 1928
# define OPENTELEMETRY_SANITIZER_NO_ADDRESS __declspec(no_sanitize_address)
#elif OPENTELEMETRY_HAVE_HWADDRESS_SANITIZER && OPENTELEMETRY_HAVE_ATTRIBUTE(no_sanitize)
# define OPENTELEMETRY_SANITIZER_NO_ADDRESS __attribute__((no_sanitize("hwaddress")))
#else
# define OPENTELEMETRY_SANITIZER_NO_ADDRESS
#endif
3 changes: 2 additions & 1 deletion api/include/opentelemetry/nostd/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# include <stdexcept>
# include <string>

# include "opentelemetry/common/macros.h"
# include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -43,7 +44,7 @@ class string_view

string_view(const char *str) noexcept : length_(std::strlen(str)), data_(str) {}

string_view(const std::basic_string<char> &str) noexcept
string_view(const std::basic_string<char> &str OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND) noexcept
: length_(str.length()), data_(str.c_str())
{}

Expand Down
Loading

1 comment on commit 3efd3ce

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 3efd3ce Previous: 436a981 Ratio
BM_BaselineBuffer/1 13031086.921691895 ns/iter 2305220.127105713 ns/iter 5.65

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.