Skip to content

Commit 3efd3ce

Browse files
owentmarcalff
andauthored
[EXPORTER] OTLP file: use fopen, fwrite, fflush and fclose which 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]>
1 parent 436a981 commit 3efd3ce

File tree

3 files changed

+218
-29
lines changed

3 files changed

+218
-29
lines changed

api/include/opentelemetry/common/macros.h

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,73 @@
33

44
#pragma once
55

6+
/*
7+
OPENTELEMETRY_HAVE_BUILTIN&OPENTELEMETRY_HAVE_FEATURE
8+
9+
Checks whether the compiler supports a Clang Feature Checking Macro, and if
10+
so, checks whether it supports the provided builtin function "x" where x
11+
is one of the functions noted in
12+
https://clang.llvm.org/docs/LanguageExtensions.html
13+
14+
Note: Use this macro to avoid an extra level of #ifdef __has_builtin check.
15+
http://releases.llvm.org/3.3/tools/clang/docs/LanguageExtensions.html
16+
*/
17+
#if !defined(OPENTELEMETRY_HAVE_BUILTIN)
18+
# ifdef __has_builtin
19+
# define OPENTELEMETRY_HAVE_BUILTIN(x) __has_builtin(x)
20+
# else
21+
# define OPENTELEMETRY_HAVE_BUILTIN(x) 0
22+
# endif
23+
#endif
24+
25+
#if !defined(OPENTELEMETRY_HAVE_FEATURE)
26+
# ifdef __has_feature
27+
# define OPENTELEMETRY_HAVE_FEATURE(f) __has_feature(f)
28+
# else
29+
# define OPENTELEMETRY_HAVE_FEATURE(f) 0
30+
# endif
31+
#endif
32+
33+
/*
34+
has feature
35+
36+
OPENTELEMETRY_HAVE_ATTRIBUTE
37+
38+
A function-like feature checking macro that is a wrapper around
39+
`__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
40+
nonzero constant integer if the attribute is supported or 0 if not.
41+
42+
It evaluates to zero if `__has_attribute` is not defined by the compiler.
43+
44+
GCC: https://gcc.gnu.org/gcc-5/changes.html
45+
Clang: https://clang.llvm.org/docs/LanguageExtensions.html
46+
*/
47+
#if !defined(OPENTELEMETRY_HAVE_ATTRIBUTE)
48+
# ifdef __has_attribute
49+
# define OPENTELEMETRY_HAVE_ATTRIBUTE(x) __has_attribute(x)
50+
# else
51+
# define OPENTELEMETRY_HAVE_ATTRIBUTE(x) 0
52+
# endif
53+
#endif
54+
55+
/*
56+
OPENTELEMETRY_HAVE_CPP_ATTRIBUTE
57+
58+
A function-like feature checking macro that accepts C++11 style attributes.
59+
It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6
60+
(https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't
61+
find `__has_cpp_attribute`, will evaluate to 0.
62+
*/
63+
#if !defined(OPENTELEMETRY_HAVE_CPP_ATTRIBUTE)
64+
# if defined(__cplusplus) && defined(__has_cpp_attribute)
65+
// NOTE: requiring __cplusplus above should not be necessary, but
66+
// works around https://bugs.llvm.org/show_bug.cgi?id=23435.
67+
# define OPENTELEMETRY_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
68+
# else
69+
# define OPENTELEMETRY_HAVE_CPP_ATTRIBUTE(x) 0
70+
# endif
71+
#endif
72+
673
/*
774
Expected usage pattern:
875
@@ -319,3 +386,107 @@ point.
319386
# define OPENTELEMETRY_EXPORT
320387

321388
#endif
389+
390+
/*
391+
OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function
392+
parameter or implicit object parameter is retained by the return value of the
393+
annotated function (or, for a parameter of a constructor, in the value of the
394+
constructed object). This attribute causes warnings to be produced if a
395+
temporary object does not live long enough.
396+
397+
When applied to a reference parameter, the referenced object is assumed to be
398+
retained by the return value of the function. When applied to a non-reference
399+
parameter (for example, a pointer or a class type), all temporaries
400+
referenced by the parameter are assumed to be retained by the return value of
401+
the function.
402+
403+
See also the upstream documentation:
404+
https://clang.llvm.org/docs/AttributeReference.html#lifetimebound
405+
*/
406+
#ifndef OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND
407+
# if OPENTELEMETRY_HAVE_CPP_ATTRIBUTE(clang::lifetimebound)
408+
# define OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND [[clang::lifetimebound]]
409+
# elif OPENTELEMETRY_HAVE_ATTRIBUTE(lifetimebound)
410+
# define OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND __attribute__((lifetimebound))
411+
# else
412+
# define OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND
413+
# endif
414+
#endif
415+
416+
// OPENTELEMETRY_HAVE_MEMORY_SANITIZER
417+
//
418+
// MemorySanitizer (MSan) is a detector of uninitialized reads. It consists of
419+
// a compiler instrumentation module and a run-time library.
420+
#ifndef OPENTELEMETRY_HAVE_MEMORY_SANITIZER
421+
# if !defined(__native_client__) && OPENTELEMETRY_HAVE_FEATURE(memory_sanitizer)
422+
# define OPENTELEMETRY_HAVE_MEMORY_SANITIZER 1
423+
# else
424+
# define OPENTELEMETRY_HAVE_MEMORY_SANITIZER 0
425+
# endif
426+
#endif
427+
428+
#if OPENTELEMETRY_HAVE_MEMORY_SANITIZER && OPENTELEMETRY_HAVE_ATTRIBUTE(no_sanitize_memory)
429+
# define OPENTELEMETRY_SANITIZER_NO_MEMORY \
430+
__attribute__((no_sanitize_memory)) // __attribute__((no_sanitize("memory")))
431+
#else
432+
# define OPENTELEMETRY_SANITIZER_NO_MEMORY
433+
#endif
434+
435+
// OPENTELEMETRY_HAVE_THREAD_SANITIZER
436+
//
437+
// ThreadSanitizer (TSan) is a fast data race detector.
438+
#ifndef OPENTELEMETRY_HAVE_THREAD_SANITIZER
439+
# if defined(__SANITIZE_THREAD__)
440+
# define OPENTELEMETRY_HAVE_THREAD_SANITIZER 1
441+
# elif OPENTELEMETRY_HAVE_FEATURE(thread_sanitizer)
442+
# define OPENTELEMETRY_HAVE_THREAD_SANITIZER 1
443+
# else
444+
# define OPENTELEMETRY_HAVE_THREAD_SANITIZER 0
445+
# endif
446+
#endif
447+
448+
#if OPENTELEMETRY_HAVE_THREAD_SANITIZER && OPENTELEMETRY_HAVE_ATTRIBUTE(no_sanitize_thread)
449+
# define OPENTELEMETRY_SANITIZER_NO_THREAD \
450+
__attribute__((no_sanitize_thread)) // __attribute__((no_sanitize("thread")))
451+
#else
452+
# define OPENTELEMETRY_SANITIZER_NO_THREAD
453+
#endif
454+
455+
// OPENTELEMETRY_HAVE_ADDRESS_SANITIZER
456+
//
457+
// AddressSanitizer (ASan) is a fast memory error detector.
458+
#ifndef OPENTELEMETRY_HAVE_ADDRESS_SANITIZER
459+
# if defined(__SANITIZE_ADDRESS__)
460+
# define OPENTELEMETRY_HAVE_ADDRESS_SANITIZER 1
461+
# elif OPENTELEMETRY_HAVE_FEATURE(address_sanitizer)
462+
# define OPENTELEMETRY_HAVE_ADDRESS_SANITIZER 1
463+
# else
464+
# define OPENTELEMETRY_HAVE_ADDRESS_SANITIZER 0
465+
# endif
466+
#endif
467+
468+
// OPENTELEMETRY_HAVE_HWADDRESS_SANITIZER
469+
//
470+
// Hardware-Assisted AddressSanitizer (or HWASAN) is even faster than asan
471+
// memory error detector which can use CPU features like ARM TBI, Intel LAM or
472+
// AMD UAI.
473+
#ifndef OPENTELEMETRY_HAVE_HWADDRESS_SANITIZER
474+
# if defined(__SANITIZE_HWADDRESS__)
475+
# define OPENTELEMETRY_HAVE_HWADDRESS_SANITIZER 1
476+
# elif OPENTELEMETRY_HAVE_FEATURE(hwaddress_sanitizer)
477+
# define OPENTELEMETRY_HAVE_HWADDRESS_SANITIZER 1
478+
# else
479+
# define OPENTELEMETRY_HAVE_HWADDRESS_SANITIZER 0
480+
# endif
481+
#endif
482+
483+
#if OPENTELEMETRY_HAVE_ADDRESS_SANITIZER && OPENTELEMETRY_HAVE_ATTRIBUTE(no_sanitize_address)
484+
# define OPENTELEMETRY_SANITIZER_NO_ADDRESS \
485+
__attribute__((no_sanitize_address)) // __attribute__((no_sanitize("address")))
486+
#elif OPENTELEMETRY_HAVE_ADDRESS_SANITIZER && defined(_MSC_VER) && _MSC_VER >= 1928
487+
# define OPENTELEMETRY_SANITIZER_NO_ADDRESS __declspec(no_sanitize_address)
488+
#elif OPENTELEMETRY_HAVE_HWADDRESS_SANITIZER && OPENTELEMETRY_HAVE_ATTRIBUTE(no_sanitize)
489+
# define OPENTELEMETRY_SANITIZER_NO_ADDRESS __attribute__((no_sanitize("hwaddress")))
490+
#else
491+
# define OPENTELEMETRY_SANITIZER_NO_ADDRESS
492+
#endif

api/include/opentelemetry/nostd/string_view.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# include <stdexcept>
1919
# include <string>
2020

21+
# include "opentelemetry/common/macros.h"
2122
# include "opentelemetry/version.h"
2223

2324
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -43,7 +44,7 @@ class string_view
4344

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

46-
string_view(const std::basic_string<char> &str) noexcept
47+
string_view(const std::basic_string<char> &str OPENTELEMETRY_ATTRIBUTE_LIFETIME_BOUND) noexcept
4748
: length_(str.length()), data_(str.c_str())
4849
{}
4950

0 commit comments

Comments
 (0)