diff --git a/.devcontainer/Dockerfile.dev b/.devcontainer/Dockerfile.dev new file mode 100644 index 0000000000..6eec367f88 --- /dev/null +++ b/.devcontainer/Dockerfile.dev @@ -0,0 +1,28 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +FROM otel/cpp_format_tools + +ARG GRPC_VERSION=v1.55.0 +ARG PROTOBUF_VERSION=23.4 +ARG ABSEIL_CPP_VERSION=20240116.1 + +ENV PROTOBUF_VERSION=${PROTOBUF_VERSION} +ENV ABSEIL_CPP_VERSION=${ABSEIL_CPP_VERSION} + +COPY ci /opt/ci + +RUN apt update && apt install -y wget \ + ninja-build \ + libcurl4-openssl-dev \ + markdownlint + +RUN cd /opt/ci && bash setup_cmake.sh +RUN cd /opt/ci && bash setup_ci_environment.sh +RUN cd /opt && bash ci/setup_googletest.sh \ + && bash ci/setup_grpc.sh -r ${GRPC_VERSION} + +ADD https://github.com/bazelbuild/bazelisk/releases/download/v1.22.1/bazelisk-linux-amd64 /usr/local/bin + +RUN git config --global core.autocrlf input \ + && chmod +x /usr/local/bin/bazelisk-linux-amd64 diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000000..8615497085 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,26 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 +// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: +// https://github.com/microsoft/vscode-dev-containers/tree/v0.162.0/containers/javascript-node +{ + "name": "opentelemetry-cpp", + "build": { + "context": "..", + "dockerfile": "Dockerfile.dev", + "args": { + "GRPC_VERSION": "v1.55.0", + "PROTOBUF_VERSION": "23.4", + "ABSEIL_CPP_VERSION":"20240116.1" + } + }, + "settings": { + "terminal.integrated.shell.linux": "/bin/sh" + }, + "extensions": [ + "ms-vscode.cpptools", + "ms-azuretools.vscode-docker", + "ms-vscode.cpptools-extension-pack" + ], + + "remoteUser": "root" +} diff --git a/.github/dependabot.yml b/.github/dependabot.yml index d907392962..5cfb4cb898 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -6,3 +6,8 @@ updates: interval: "daily" labels: - "GHA" + + - package-ecosystem: "devcontainers" + directory: "/" + schedule: + interval: daily diff --git a/.github/workflows/iwyu.yml b/.github/workflows/iwyu.yml index ae4c5fb96b..2aa91e0741 100644 --- a/.github/workflows/iwyu.yml +++ b/.github/workflows/iwyu.yml @@ -31,6 +31,10 @@ jobs: libgtest-dev \ libbenchmark-dev + - name: setup grpc + run: | + sudo ./ci/setup_grpc.sh + - name: Prepare CMake run: | TOPDIR=`pwd` @@ -41,12 +45,14 @@ jobs: -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE="include-what-you-use;-w;-Xiwyu;--mapping_file=${TOPDIR}/.iwyu.imp;" \ -DBUILD_TESTING=ON \ -DBUILD_W3CTRACECONTEXT_TEST=ON \ - -DWITH_OTLP_GRPC=OFF \ + -DWITH_OTLP_GRPC=ON \ -DWITH_OTLP_HTTP=ON \ -DWITH_OTLP_FILE=ON \ + -DWITH_OPENTRACING=ON \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ + -DWITH_THREAD_INSTRUMENTATION=ON \ -DWITH_ZIPKIN=ON \ - -DWITH_PROMETHEUS=OFF \ + -DWITH_PROMETHEUS=ON \ .. - name: iwyu_tool @@ -67,7 +73,7 @@ jobs: readonly WARNING_COUNT=`grep -c "include-what-you-use reported diagnostics:" iwyu.log` echo "include-what-you-use reported ${WARNING_COUNT} warning(s)" # Acceptable limit, to decrease over time down to 0 - readonly WARNING_LIMIT=180 + readonly WARNING_LIMIT=10 # FAIL the build if WARNING_COUNT > WARNING_LIMIT if [ $WARNING_COUNT -gt $WARNING_LIMIT ] ; then exit 1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 52f280eb22..1ca60c83e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,69 @@ Increment the: * [EXPORTER] Fix scope attributes missing from otlp traces metrics [#3185](https://github.com/open-telemetry/opentelemetry-cpp/pull/3185) +* [EXPORTER] Fix throw in OtlpGrpcMetricExporter with shared grpc client + [#3243](https://github.com/open-telemetry/opentelemetry-cpp/pull/3243) + +* [SDK] Better control of threads executed by opentelemetry-cpp + [#3175](https://github.com/open-telemetry/opentelemetry-cpp/pull/3175) + +New features: + +* [SDK] Better control of threads executed by opentelemetry-cpp + [#3175](https://github.com/open-telemetry/opentelemetry-cpp/pull/3175) + + * This feature provides a way for applications, + when configuring the SDK and exporters, + to participate in the execution path + of internal opentelemetry-cpp threads. + + * The opentelemetry-cpp library provides the following: + + * a new ThreadInstrumentation interface, + * new runtime options structures, to optionally configure the SDK: + * BatchSpanProcessorRuntimeOptions + * PeriodicExportingMetricReaderRuntimeOptions + * BatchLogRecordProcessorRuntimeOptions + * new runtime options structures, + to optionally configure the OTLP HTTP exporters: + * OtlpHttpExporterRuntimeOptions + * OtlpHttpMetricExporterRuntimeOptions + * OtlpHttpLogRecordExporterRuntimeOptions + * new ThreadInstrumentation parameters, + to optionally configure the CURL HttpClient + * new runtime options structures, + to optionally configure the OTLP FILE exporters: + * OtlpFileExporterRuntimeOptions + * OtlpFileMetricExporterRuntimeOptions + * OtlpFileLogRecordExporterRuntimeOptions + * new runtime options structure, + to optionally configure the OTLP FILE client: + * OtlpFileClientRuntimeOptions + + * Using the optional runtime options structures, + an application can subclass the ThreadInstrumentation interface, + and be notified of specific events of interest during the execution + of an internal opentelemetry-cpp thread. + + * This allows an application to call, for example: + + * pthread_setaffinity_np(), for better performances, + * setns(), to control the network namespace used by HTTP CURL connections + * pthread_setname_np(), for better observability from the operating system + * many more specific apis, as needed + + * See the documentation for ThreadInstrumentation for details. + + * A new example program, example_otlp_instrumented_http, + shows how to use the feature, + and add application logic in the thread execution code path. + + * Note that this feature is experimental, + protected by a WITH_THREAD_INSTRUMENTATION_PREVIEW + flag in CMake. Various runtime options structures, + as well as the thread instrumentation interface, + may change without notice before this feature is declared stable. + ## [1.18 2024-11-25] * [EXPORTER] Fix crash in ElasticsearchLogRecordExporter diff --git a/CMakeLists.txt b/CMakeLists.txt index 408ee43046..aa76c997bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -300,6 +300,10 @@ option(WITH_ASYNC_EXPORT_PREVIEW "Whether to enable async export" OFF) option(WITH_METRICS_EXEMPLAR_PREVIEW "Whether to enable exemplar within metrics" OFF) +# Experimental, so behind feature flag by default +option(WITH_THREAD_INSTRUMENTATION_PREVIEW + "Whether to enable thread instrumentation" OFF) + option(OPENTELEMETRY_SKIP_DYNAMIC_LOADING_TESTS "Whether to build test libraries that are always linked as shared libs" OFF) @@ -365,11 +369,16 @@ if(WITH_PROMETHEUS) if(EXISTS ${PROJECT_SOURCE_DIR}/third_party/prometheus-cpp/.git) set(SAVED_ENABLE_TESTING ${ENABLE_TESTING}) set(SAVED_CMAKE_CXX_CLANG_TIDY ${CMAKE_CXX_CLANG_TIDY}) + set(SAVED_CMAKE_CXX_INCLUDE_WHAT_YOU_USE + ${CMAKE_CXX_INCLUDE_WHAT_YOU_USE}) set(ENABLE_TESTING OFF) set(CMAKE_CXX_CLANG_TIDY "") + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "") add_subdirectory(third_party/prometheus-cpp) set(ENABLE_TESTING ${SAVED_ENABLE_TESTING}) set(CMAKE_CXX_CLANG_TIDY ${SAVED_CMAKE_CXX_CLANG_TIDY}) + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE + ${SAVED_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}) else() message( FATAL_ERROR @@ -711,8 +720,13 @@ if(WITH_OPENTRACING) if(EXISTS "${PROJECT_SOURCE_DIR}/${OPENTRACING_DIR}/.git") set(SAVED_BUILD_TESTING ${BUILD_TESTING}) set(BUILD_TESTING OFF) + set(SAVED_CMAKE_CXX_INCLUDE_WHAT_YOU_USE + ${CMAKE_CXX_INCLUDE_WHAT_YOU_USE}) + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "") add_subdirectory(${OPENTRACING_DIR}) set(BUILD_TESTING ${SAVED_BUILD_TESTING}) + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE + ${SAVED_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}) else() message( FATAL_ERROR diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 95b4e226f8..bf837f6bd1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -60,6 +60,91 @@ bazel build //examples/simple:example_simple bazel-bin/examples/simple/example_simple ``` +### DevContainer Setup for Project + +This guide provides instructions on how to set up and use the development +container (`devcontainer`) environment to streamline testing and development +for this project. With the DevContainer, you can work in a consistent environment +configured with all the necessary dependencies and tools. + +#### Prerequisites + +Before getting started, ensure you have the following installed: + +* **Docker**: DevContainers require Docker for containerization. +* **Visual Studio Code (VSCode)** with the **Remote - Containers** extension. + +#### Getting Started + +* **Open the Project in DevContainer**: + + Open the project in VSCode. When prompted to "Reopen in Container," select + this option. If you’re not prompted, you can manually open the container by + selecting **Remote-Containers: Reopen in Container** from the command palette + (`F1` or `Ctrl+Shift+P`). + +* **Container Setup**: + + The DevContainer environment will automatically build based on the configuration + files provided (e.g., `.devcontainer/devcontainer.json`). This setup will install + required dependencies, tools, and environment variables needed for the project. + +#### Available Commands + +Once inside the DevContainer, you can use the following commands to run tests +and CI workflows. + +##### 1. Run Tests with Bazelisk + +To run tests with Bazelisk using specific compilation options, use: + +```bash +bazelisk-linux-amd64 test --copt=-DENABLE_LOGS_PREVIEW +--test_output=errors --cache_test_results=no --copt=-DENABLE_TEST //exporters/otlp/... +``` + +###### Command Breakdown + +* `--copt=-DENABLE_LOGS_PREVIEW`: Enables preview logs. +* `--test_output=errors`: Shows only the errors in the test output. +* `--cache_test_results=no`: Forces Bazel to re-run tests without caching. +* `--copt=-DENABLE_TEST`: Enables testing capabilities for the target code. +* `//exporters/otlp/...`: Specifies the test target path. + +##### 2. Run CI Script + +You can also run the CI script provided to perform testing with the +following command as an +example: + +```bash +bash ci/do_ci.sh cmake.exporter.otprotocol.test +``` + +This command initiates the CI pipeline, executing tests specifically for the +**cmake.exporter.otprotocol** module. + +#### Troubleshooting + +If you encounter issues: + +* **Rebuild the DevContainer**: From the command palette, run + **Remote-Containers: Rebuild Container** to reinitialize the environment. +* **Check Bazelisk and CI Script Logs**: Inspect logs for any configuration or + dependency issues. + +#### Additional Notes + +* You can adjust compiler options (`--copt`) as needed to test additional flags + or enable/disable specific features. +* The test results will be displayed in the terminal within the DevContainer for + easy debugging. + +#### Resources + +* **Bazelisk Documentation**: [https://github.com/bazelbuild/bazelisk](https://github.com/bazelbuild/bazelisk) +* **VSCode DevContainer Documentation**: [https://code.visualstudio.com/docs/remote/containers](https://code.visualstudio.com/docs/remote/containers) + ## Pull Requests ### How to Send Pull Requests diff --git a/api/CMakeLists.txt b/api/CMakeLists.txt index 78e97ad029..b3c97808f6 100644 --- a/api/CMakeLists.txt +++ b/api/CMakeLists.txt @@ -126,6 +126,11 @@ if(WITH_METRICS_EXEMPLAR_PREVIEW) INTERFACE ENABLE_METRICS_EXEMPLAR_PREVIEW) endif() +if(WITH_THREAD_INSTRUMENTATION_PREVIEW) + target_compile_definitions(opentelemetry_api + INTERFACE ENABLE_THREAD_INSTRUMENTATION_PREVIEW) +endif() + if(WITH_OTLP_HTTP_COMPRESSION) target_compile_definitions(opentelemetry_api INTERFACE ENABLE_OTLP_COMPRESSION_PREVIEW) diff --git a/ci/README.md b/ci/README.md index 65bea032d7..39d9a19acc 100644 --- a/ci/README.md +++ b/ci/README.md @@ -1,7 +1,10 @@ # Building and running tests as a developer CI tests can be run on docker by invoking the script `./ci/run_docker.sh -./ci/do_ci.sh {TARGET}` where the targets are: +./ci/do_ci.sh {TARGET}`or inside +[devcontainer](../CONTRIBUTING.md#devcontainer-setup-for-project) +by invoking the script +`./ci/do_ci.sh {TARGET}` where the targets are: * `cmake.test`: build cmake targets and run tests. * `cmake.maintainer.test`: build with cmake and test, in maintainer mode. diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 11dbe4ef80..1fd1a37f80 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -63,20 +63,9 @@ mkdir -p "${BUILD_DIR}" [ -z "${PLUGIN_DIR}" ] && export PLUGIN_DIR=$HOME/plugin mkdir -p "${PLUGIN_DIR}" -IWYU="" MAKE_COMMAND="make -k -j \$(nproc)" -# Temporarily disable the IWYU build. -# It fails in Ubuntu 24-04 CI with: -# Error running 'iwyu': Segmentation fault -# -# if [[ "${CXX}" == *clang* ]]; then -# MAKE_COMMAND="make -k CXX=include-what-you-use CXXFLAGS=\"-Xiwyu --error_always\" -j \$(nproc)" -# IWYU="-DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=iwyu" -# fi - echo "make command: ${MAKE_COMMAND}" -echo "IWYU option: ${IWYU}" BAZEL_OPTIONS_DEFAULT="--copt=-DENABLE_METRICS_EXEMPLAR_PREVIEW" BAZEL_OPTIONS="$BAZEL_OPTIONS_DEFAULT" @@ -131,7 +120,7 @@ elif [[ "$1" == "cmake.maintainer.sync.test" ]]; then -DOTELCPP_MAINTAINER_MODE=ON \ -DWITH_NO_DEPRECATED_CODE=ON \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ - ${IWYU} \ + -DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \ "${SRC_DIR}" eval "$MAKE_COMMAND" make test @@ -153,7 +142,7 @@ elif [[ "$1" == "cmake.maintainer.async.test" ]]; then -DOTELCPP_MAINTAINER_MODE=ON \ -DWITH_NO_DEPRECATED_CODE=ON \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ - ${IWYU} \ + -DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \ "${SRC_DIR}" eval "$MAKE_COMMAND" make test @@ -176,6 +165,7 @@ elif [[ "$1" == "cmake.maintainer.cpp11.async.test" ]]; then -DOTELCPP_MAINTAINER_MODE=ON \ -DWITH_NO_DEPRECATED_CODE=ON \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ + -DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \ "${SRC_DIR}" make -k -j $(nproc) make test @@ -199,7 +189,7 @@ elif [[ "$1" == "cmake.maintainer.abiv2.test" ]]; then -DWITH_ABI_VERSION_1=OFF \ -DWITH_ABI_VERSION_2=ON \ -DWITH_OTLP_HTTP_COMPRESSION=ON \ - ${IWYU} \ + -DWITH_THREAD_INSTRUMENTATION_PREVIEW=ON \ "${SRC_DIR}" eval "$MAKE_COMMAND" make test @@ -247,7 +237,6 @@ elif [[ "$1" == "cmake.c++20.test" ]]; then -DCMAKE_CXX_FLAGS="-Werror $CXXFLAGS" \ -DWITH_ASYNC_EXPORT_PREVIEW=ON \ -DWITH_STL=CXX20 \ - ${IWYU} \ "${SRC_DIR}" eval "$MAKE_COMMAND" make test @@ -259,7 +248,6 @@ elif [[ "$1" == "cmake.c++23.test" ]]; then -DCMAKE_CXX_FLAGS="-Werror $CXXFLAGS" \ -DWITH_ASYNC_EXPORT_PREVIEW=ON \ -DWITH_STL=CXX23 \ - ${IWYU} \ "${SRC_DIR}" eval "$MAKE_COMMAND" make test @@ -272,7 +260,6 @@ elif [[ "$1" == "cmake.c++14.stl.test" ]]; then -DCMAKE_CXX_FLAGS="-Werror $CXXFLAGS" \ -DWITH_ASYNC_EXPORT_PREVIEW=ON \ -DWITH_STL=CXX14 \ - ${IWYU} \ "${SRC_DIR}" eval "$MAKE_COMMAND" make test @@ -285,7 +272,6 @@ elif [[ "$1" == "cmake.c++17.stl.test" ]]; then -DCMAKE_CXX_FLAGS="-Werror $CXXFLAGS" \ -DWITH_ASYNC_EXPORT_PREVIEW=ON \ -DWITH_STL=CXX17 \ - ${IWYU} \ "${SRC_DIR}" eval "$MAKE_COMMAND" make test @@ -298,7 +284,6 @@ elif [[ "$1" == "cmake.c++20.stl.test" ]]; then -DCMAKE_CXX_FLAGS="-Werror $CXXFLAGS" \ -DWITH_ASYNC_EXPORT_PREVIEW=ON \ -DWITH_STL=CXX20 \ - ${IWYU} \ "${SRC_DIR}" eval "$MAKE_COMMAND" make test @@ -311,7 +296,6 @@ elif [[ "$1" == "cmake.c++23.stl.test" ]]; then -DCMAKE_CXX_FLAGS="-Werror $CXXFLAGS" \ -DWITH_ASYNC_EXPORT_PREVIEW=ON \ -DWITH_STL=CXX23 \ - ${IWYU} \ "${SRC_DIR}" eval "$MAKE_COMMAND" make test diff --git a/ci/run_docker.sh b/ci/run_docker.sh index d3b78c3af9..1f499e935e 100755 --- a/ci/run_docker.sh +++ b/ci/run_docker.sh @@ -7,7 +7,7 @@ set -e BUILD_IMAGE=opentelemetry-cpp-build docker image inspect "$BUILD_IMAGE" &> /dev/null || { - docker build -t "$BUILD_IMAGE" ci + docker build -t "$BUILD_IMAGE" -f .devcontainer/Dockerfile.dev . } if [[ $# -ge 1 ]]; then diff --git a/cmake/opentelemetry-proto.cmake b/cmake/opentelemetry-proto.cmake index 0ff346d249..2f7431e02a 100644 --- a/cmake/opentelemetry-proto.cmake +++ b/cmake/opentelemetry-proto.cmake @@ -342,6 +342,9 @@ if(WITH_OTLP_GRPC) ${LOGS_SERVICE_GRPC_PB_CPP_FILE} ${METRICS_SERVICE_GRPC_PB_CPP_FILE}) set_target_version(opentelemetry_proto_grpc) + # Disable include-what-you-use on generated code. + set_target_properties(opentelemetry_proto_grpc PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "") + list(APPEND OPENTELEMETRY_PROTO_TARGETS opentelemetry_proto_grpc) target_link_libraries(opentelemetry_proto_grpc PUBLIC opentelemetry_proto) diff --git a/examples/grpc/CMakeLists.txt b/examples/grpc/CMakeLists.txt index 21325663ab..015f4a2287 100644 --- a/examples/grpc/CMakeLists.txt +++ b/examples/grpc/CMakeLists.txt @@ -25,6 +25,9 @@ add_library(example_grpc_proto ${example_grpc_srcs} ${example_grpc_hdrs} patch_protobuf_targets(example_grpc_proto) +# Disable include-what-you-use on generated code. +set_target_properties(example_grpc_proto PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "") + include_directories( ${CMAKE_SOURCE_DIR}/exporters/ostream/include ${CMAKE_SOURCE_DIR}/ext/include ${CMAKE_SOURCE_DIR}/api/include/ ${CMAKE_SOURCE_DIR/}) diff --git a/examples/grpc/client.cc b/examples/grpc/client.cc index 49be22c155..ab11d86045 100644 --- a/examples/grpc/client.cc +++ b/examples/grpc/client.cc @@ -5,16 +5,32 @@ // ambiguity with `nostd::variant` if compiled with Visual Studio 2015. Other // modern compilers are unaffected. #include +#include +#include + +#include +#include +#include +#include +#include +#include "opentelemetry/context/propagation/global_propagator.h" +#include "opentelemetry/context/propagation/text_map_propagator.h" +#include "opentelemetry/context/runtime_context.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/trace/propagation/http_trace_context.h" +#include "opentelemetry/trace/span.h" +#include "opentelemetry/trace/span_metadata.h" +#include "opentelemetry/trace/span_startoptions.h" +#include "opentelemetry/trace/tracer.h" + #ifdef BAZEL_BUILD # include "examples/grpc/protos/messages.grpc.pb.h" +# include "examples/grpc/protos/messages.pb.h" #else # include "messages.grpc.pb.h" +# include "messages.pb.h" #endif -#include -#include -#include - #include "opentelemetry/semconv/incubating/rpc_attributes.h" #include "opentelemetry/semconv/network_attributes.h" #include "tracer_common.h" diff --git a/examples/grpc/server.cc b/examples/grpc/server.cc index 6aa543a8a1..1c23b27a0d 100644 --- a/examples/grpc/server.cc +++ b/examples/grpc/server.cc @@ -1,28 +1,40 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#ifdef BAZEL_BUILD -# include "examples/grpc/protos/messages.grpc.pb.h" -#else -# include "messages.grpc.pb.h" -#endif - -#include "opentelemetry/semconv/incubating/rpc_attributes.h" -#include "opentelemetry/trace/context.h" -#include "opentelemetry/trace/span_context_kv_iterable_view.h" -#include "tracer_common.h" - -#include +#include #include #include #include - -#include -#include +#include +#include +#include +#include +#include #include -#include #include -#include +#include + +#include "opentelemetry/context/propagation/global_propagator.h" +#include "opentelemetry/context/propagation/text_map_propagator.h" +#include "opentelemetry/context/runtime_context.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/variant.h" +#include "opentelemetry/semconv/incubating/rpc_attributes.h" +#include "opentelemetry/trace/context.h" +#include "opentelemetry/trace/span.h" +#include "opentelemetry/trace/span_context.h" +#include "opentelemetry/trace/span_metadata.h" +#include "opentelemetry/trace/span_startoptions.h" +#include "opentelemetry/trace/tracer.h" +#include "tracer_common.h" + +#ifdef BAZEL_BUILD +# include "examples/grpc/protos/messages.grpc.pb.h" +# include "examples/grpc/protos/messages.pb.h" +#else +# include "messages.grpc.pb.h" +# include "messages.pb.h" +#endif using grpc::Server; using grpc::ServerBuilder; diff --git a/examples/otlp/BUILD b/examples/otlp/BUILD index fd4e0dc171..b27cbef32d 100644 --- a/examples/otlp/BUILD +++ b/examples/otlp/BUILD @@ -168,3 +168,25 @@ cc_binary( "//sdk/src/metrics", ], ) + +cc_binary( + name = "example_otlp_instrumented_http", + srcs = [ + "http_instrumented_main.cc", + ], + tags = [ + "examples", + "otlp", + "otlp_http", + ], + deps = [ + "//api", + "//examples/common/logs_foo_library:common_logs_foo_library", + "//examples/common/metrics_foo_library:common_metrics_foo_library", + "//exporters/otlp:otlp_http_exporter", + "//exporters/otlp:otlp_http_log_record_exporter", + "//exporters/otlp:otlp_http_metric_exporter", + "//sdk/src/metrics", + "//sdk/src/trace", + ], +) diff --git a/examples/otlp/CMakeLists.txt b/examples/otlp/CMakeLists.txt index 145fafca87..170017c965 100644 --- a/examples/otlp/CMakeLists.txt +++ b/examples/otlp/CMakeLists.txt @@ -99,6 +99,29 @@ if(WITH_OTLP_HTTP) opentelemetry_exporter_otlp_http_log) endif() + # ALL, instrumented + + add_executable(example_otlp_instrumented_http http_instrumented_main.cc) + + # Note: common_logs_foo_library provide traces and logs + target_link_libraries( + example_otlp_instrumented_http ${CMAKE_THREAD_LIBS_INIT} + common_metrics_foo_library common_logs_foo_library) + + if(DEFINED OPENTELEMETRY_BUILD_DLL) + target_link_libraries(example_otlp_instrumented_http opentelemetry_cpp + opentelemetry_common) + else() + target_link_libraries( + example_otlp_instrumented_http + opentelemetry_trace + opentelemetry_metrics + opentelemetry_logs + opentelemetry_exporter_otlp_http + opentelemetry_exporter_otlp_http_metric + opentelemetry_exporter_otlp_http_log) + endif() + endif() if(WITH_OTLP_FILE) diff --git a/examples/otlp/http_instrumented_main.cc b/examples/otlp/http_instrumented_main.cc new file mode 100644 index 0000000000..19f7a2d572 --- /dev/null +++ b/examples/otlp/http_instrumented_main.cc @@ -0,0 +1,361 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include +#include +#include +#include + +#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" +#include "opentelemetry/logs/logger_provider.h" +#include "opentelemetry/logs/provider.h" +#include "opentelemetry/metrics/meter_provider.h" +#include "opentelemetry/metrics/provider.h" +#include "opentelemetry/sdk/logs/batch_log_record_processor_factory.h" +#include "opentelemetry/sdk/logs/logger_provider.h" +#include "opentelemetry/sdk/logs/logger_provider_factory.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h" +#include "opentelemetry/sdk/metrics/meter_context.h" +#include "opentelemetry/sdk/metrics/meter_context_factory.h" +#include "opentelemetry/sdk/metrics/meter_provider_factory.h" +#include "opentelemetry/sdk/metrics/metric_reader.h" +#include "opentelemetry/sdk/metrics/push_metric_exporter.h" +#include "opentelemetry/sdk/trace/batch_span_processor_factory.h" +#include "opentelemetry/sdk/trace/batch_span_processor_options.h" +#include "opentelemetry/sdk/trace/batch_span_processor_runtime_options.h" +#include "opentelemetry/sdk/trace/processor.h" +#include "opentelemetry/sdk/trace/tracer_provider.h" +#include "opentelemetry/sdk/trace/tracer_provider_factory.h" +#include "opentelemetry/trace/provider.h" +#include "opentelemetry/trace/tracer_provider.h" + +#include +#include +#include "opentelemetry/sdk/logs/batch_log_record_processor_options.h" +#include "opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h" +#include "opentelemetry/sdk/logs/exporter.h" +#include "opentelemetry/sdk/logs/processor.h" +#include "opentelemetry/sdk/metrics/meter_provider.h" +#include "opentelemetry/sdk/trace/exporter.h" + +#ifdef BAZEL_BUILD +# include "examples/common/logs_foo_library/foo_library.h" +# include "examples/common/metrics_foo_library/foo_library.h" +#else +# include "logs_foo_library/foo_library.h" +# include "metrics_foo_library/foo_library.h" +#endif + +namespace +{ + +std::mutex serialize; + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + +/** + The purpose of MyThreadInstrumentation is to demonstrate + how notifications are delivered to the application. + + Printing to std::cout is useful for debugging, + to understand the overall thread execution in the library. + + In production, a real application would instead: + - set thread priorities / CPU affinity + - set thread local storage keys + - set a thread name to the operating system + - set network namespaces + in the OnXXX() code here. +*/ +class MyThreadInstrumentation : public opentelemetry::sdk::common::ThreadInstrumentation +{ +public: + MyThreadInstrumentation(const std::string &thread_name, + const std::string &network_name, + const std::string &priority) + : thread_name_(thread_name), network_name_(network_name), priority_(priority) + {} + ~MyThreadInstrumentation() override = default; + + void OnStart() override + { + std::lock_guard lock_guard(serialize); + std::cout << "OnStart() thread " << thread_name_ << ", id " << std::this_thread::get_id(); + if (!network_name_.empty()) + { + std::cout << ", network_name " << network_name_; + } + if (!priority_.empty()) + { + std::cout << ", priority " << priority_; + } + std::cout << std::endl << std::flush; + } + + void OnEnd() override + { + std::lock_guard lock_guard(serialize); + std::cout << "OnEnd() thread " << thread_name_ << ", id " << std::this_thread::get_id() + << std::endl + << std::flush; + } + + void BeforeWait() override + { + std::lock_guard lock_guard(serialize); + std::cout << "BeforeWait() thread " << thread_name_ << ", id " << std::this_thread::get_id() + << ", waiting" << std::endl + << std::flush; + } + + void AfterWait() override + { + std::lock_guard lock_guard(serialize); + std::cout << "AfterWait() thread " << thread_name_ << ", id " << std::this_thread::get_id() + << ", done waiting" << std::endl + << std::flush; + } + + void BeforeLoad() override + { + std::lock_guard lock_guard(serialize); + std::cout << "BeforeLoad() thread " << thread_name_ << ", id " << std::this_thread::get_id() + << ", about to work" << std::endl + << std::flush; + } + + void AfterLoad() override + { + std::lock_guard lock_guard(serialize); + std::cout << "AfterLoad() thread " << thread_name_ << ", id " << std::this_thread::get_id() + << ", done working" << std::endl + << std::flush; + } + +private: + std::string thread_name_; + std::string network_name_; + std::string priority_; +}; + +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + +opentelemetry::exporter::otlp::OtlpHttpExporterOptions tracer_opts; +opentelemetry::exporter::otlp::OtlpHttpMetricExporterOptions meter_opts; +opentelemetry::exporter::otlp::OtlpHttpLogRecordExporterOptions logger_opts; + +std::shared_ptr tracer_provider; +std::shared_ptr meter_provider; +std::shared_ptr logger_provider; + +void InitTracer() +{ + // Create OTLP exporter instance + opentelemetry::exporter::otlp::OtlpHttpExporterRuntimeOptions exp_rt_opts; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + auto exp_instr = std::shared_ptr( + new MyThreadInstrumentation("OtlpHttpExporter", "trace-net", "high")); + exp_rt_opts.thread_instrumentation = exp_instr; +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + auto exporter = + opentelemetry::exporter::otlp::OtlpHttpExporterFactory::Create(tracer_opts, exp_rt_opts); + + // Create Processor instance + opentelemetry::sdk::trace::BatchSpanProcessorOptions pro_opts; + opentelemetry::sdk::trace::BatchSpanProcessorRuntimeOptions pro_rt_opts; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + auto pro_instr = std::shared_ptr( + new MyThreadInstrumentation("BatchSpanProcessor", "", "high")); + pro_rt_opts.thread_instrumentation = pro_instr; +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + auto processor = opentelemetry::sdk::trace::BatchSpanProcessorFactory::Create( + std::move(exporter), pro_opts, pro_rt_opts); + + // Create Provider instance + tracer_provider = opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(processor)); + + // Set the global trace provider + std::shared_ptr api_provider = tracer_provider; + opentelemetry::trace::Provider::SetTracerProvider(api_provider); +} + +void CleanupTracer() +{ + // We call ForceFlush to prevent to cancel running exportings, It's optional. + if (tracer_provider) + { + tracer_provider->ForceFlush(); + tracer_provider->Shutdown(); + } + + tracer_provider.reset(); + std::shared_ptr none; + opentelemetry::trace::Provider::SetTracerProvider(none); +} + +void InitMetrics() +{ + // Create OTLP exporter instance + opentelemetry::exporter::otlp::OtlpHttpMetricExporterRuntimeOptions exp_rt_opts; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + auto exp_instr = std::shared_ptr( + new MyThreadInstrumentation("OtlpHttpMetricExporter", "metric-net", "medium")); + exp_rt_opts.thread_instrumentation = exp_instr; +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + auto exporter = + opentelemetry::exporter::otlp::OtlpHttpMetricExporterFactory::Create(meter_opts, exp_rt_opts); + + std::string version{"1.2.0"}; + std::string schema{"https://opentelemetry.io/schemas/1.2.0"}; + + // Initialize and set the global MeterProvider + opentelemetry::sdk::metrics::PeriodicExportingMetricReaderOptions reader_options; + reader_options.export_interval_millis = std::chrono::milliseconds(1000); + reader_options.export_timeout_millis = std::chrono::milliseconds(500); + + opentelemetry::sdk::metrics::PeriodicExportingMetricReaderRuntimeOptions reader_rt_opts; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + auto reader_periodic_instr = std::shared_ptr( + new MyThreadInstrumentation("PeriodicExportingMetricReader(periodic)", "", "medium")); + auto reader_collect_instr = std::shared_ptr( + new MyThreadInstrumentation("PeriodicExportingMetricReader(collect)", "", "medium")); + reader_rt_opts.periodic_thread_instrumentation = reader_periodic_instr; + reader_rt_opts.collect_thread_instrumentation = reader_collect_instr; +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + auto reader = opentelemetry::sdk::metrics::PeriodicExportingMetricReaderFactory::Create( + std::move(exporter), reader_options, reader_rt_opts); + + auto context = opentelemetry::sdk::metrics::MeterContextFactory::Create(); + context->AddMetricReader(std::move(reader)); + + meter_provider = opentelemetry::sdk::metrics::MeterProviderFactory::Create(std::move(context)); + std::shared_ptr api_provider = meter_provider; + + opentelemetry::metrics::Provider::SetMeterProvider(api_provider); +} + +void CleanupMetrics() +{ + // We call ForceFlush to prevent to cancel running exportings, It's optional. + if (meter_provider) + { + meter_provider->ForceFlush(); + meter_provider->Shutdown(); + } + + meter_provider.reset(); + std::shared_ptr none; + opentelemetry::metrics::Provider::SetMeterProvider(none); +} + +void InitLogger() +{ + // Create OTLP exporter instance + opentelemetry::exporter::otlp::OtlpHttpLogRecordExporterRuntimeOptions exp_rt_opts; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + auto exp_instr = std::shared_ptr( + new MyThreadInstrumentation("OtlpHttpLogRecordExporter", "log-net", "low")); + exp_rt_opts.thread_instrumentation = exp_instr; +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + auto exporter = opentelemetry::exporter::otlp::OtlpHttpLogRecordExporterFactory::Create( + logger_opts, exp_rt_opts); + + // Create Processor instance + opentelemetry::sdk::logs::BatchLogRecordProcessorOptions pro_opts; + opentelemetry::sdk::logs::BatchLogRecordProcessorRuntimeOptions pro_rt_opts; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + auto pro_instr = std::shared_ptr( + new MyThreadInstrumentation("BatchLogRecordProcessor", "", "low")); + pro_rt_opts.thread_instrumentation = pro_instr; +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + auto processor = opentelemetry::sdk::logs::BatchLogRecordProcessorFactory::Create( + std::move(exporter), pro_opts, pro_rt_opts); + + logger_provider = opentelemetry::sdk::logs::LoggerProviderFactory::Create(std::move(processor)); + + std::shared_ptr api_provider = logger_provider; + opentelemetry::logs::Provider::SetLoggerProvider(api_provider); +} + +void CleanupLogger() +{ + // We call ForceFlush to prevent to cancel running exportings, It's optional. + if (logger_provider) + { + logger_provider->ForceFlush(); + logger_provider->Shutdown(); + } + + logger_provider.reset(); + std::shared_ptr none; + opentelemetry::logs::Provider::SetLoggerProvider(none); +} + +} // namespace + +/* + Usage: + - example_otlp_instrumented_http + - example_otlp_instrumented_http +*/ +int main(int argc, char *argv[]) +{ + if (argc > 1) + { + tracer_opts.url = argv[1]; + } + else + { + tracer_opts.url = "http://localhost:4318/v1/traces"; + } + + if (argc > 2) + { + meter_opts.url = argv[2]; + } + else + { + meter_opts.url = "http://localhost:4318/v1/metrics"; + } + + if (argc > 3) + { + logger_opts.url = argv[3]; + } + else + { + logger_opts.url = "http://localhost:4318/v1/logs"; + } + + std::cout << "Initializing opentelemetry-cpp" << std::endl << std::flush; + + InitTracer(); + InitMetrics(); + InitLogger(); + + std::cout << "Application payload" << std::endl << std::flush; + + foo_library(); + + std::string name{"otlp_http_metric_example"}; + foo_library::observable_counter_example(name); + + std::cout << "Shutting down opentelemetry-cpp" << std::endl << std::flush; + + CleanupLogger(); + CleanupMetrics(); + CleanupTracer(); + + std::cout << "Done" << std::endl << std::flush; + return 0; +} diff --git a/examples/prometheus/main.cc b/examples/prometheus/main.cc index 8a559f0f57..5af0294066 100644 --- a/examples/prometheus/main.cc +++ b/examples/prometheus/main.cc @@ -1,19 +1,26 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include #include +#include #include +#include +#include "opentelemetry/common/attribute_value.h" #include "opentelemetry/exporters/prometheus/exporter_factory.h" #include "opentelemetry/exporters/prometheus/exporter_options.h" +#include "opentelemetry/metrics/meter_provider.h" #include "opentelemetry/metrics/provider.h" -#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" -#include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h" -#include "opentelemetry/sdk/metrics/meter.h" +#include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/sdk/metrics/meter_provider.h" #include "opentelemetry/sdk/metrics/meter_provider_factory.h" +#include "opentelemetry/sdk/metrics/metric_reader.h" +#include "opentelemetry/sdk/metrics/view/instrument_selector.h" #include "opentelemetry/sdk/metrics/view/instrument_selector_factory.h" +#include "opentelemetry/sdk/metrics/view/meter_selector.h" #include "opentelemetry/sdk/metrics/view/meter_selector_factory.h" +#include "opentelemetry/sdk/metrics/view/view.h" #include "opentelemetry/sdk/metrics/view/view_factory.h" #ifdef BAZEL_BUILD diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index eb3d23d579..cdaba37e4d 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -155,6 +155,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_http_exporter.h", "include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h", "include/opentelemetry/exporters/otlp/otlp_http_exporter_options.h", + "include/opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], @@ -180,6 +181,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_environment.h", "include/opentelemetry/exporters/otlp/otlp_file_client.h", "include/opentelemetry/exporters/otlp/otlp_file_client_options.h", + "include/opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], @@ -210,6 +212,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_file_exporter.h", "include/opentelemetry/exporters/otlp/otlp_file_exporter_factory.h", "include/opentelemetry/exporters/otlp/otlp_file_exporter_options.h", + "include/opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], @@ -270,6 +273,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h", "include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h", "include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h", + "include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], @@ -298,6 +302,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_file_metric_exporter.h", "include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_factory.h", "include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_options.h", + "include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], @@ -326,6 +331,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h", "include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h", "include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h", + "include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], @@ -354,6 +360,7 @@ cc_library( "include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h", "include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h", "include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h", + "include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h", "include/opentelemetry/exporters/otlp/protobuf_include_prefix.h", "include/opentelemetry/exporters/otlp/protobuf_include_suffix.h", ], diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client.h index e377b140b6..987606ecdb 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client.h @@ -7,6 +7,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_file_client_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/version.h" @@ -35,7 +36,8 @@ class OtlpFileClient /** * Create an OtlpFileClient using the given options. */ - explicit OtlpFileClient(OtlpFileClientOptions &&options); + explicit OtlpFileClient(OtlpFileClientOptions &&options, + OtlpFileClientRuntimeOptions &&runtime_options); ~OtlpFileClient(); @@ -80,6 +82,8 @@ class OtlpFileClient // The configuration options associated with this file client. const OtlpFileClientOptions options_; + // The runtime options associated with this file client. + const OtlpFileClientRuntimeOptions runtime_options_; opentelemetry::nostd::shared_ptr backend_; }; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h new file mode 100644 index 0000000000..2fc4c2e31c --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h @@ -0,0 +1,31 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP FILE client runtime options. + */ +struct OtlpFileClientRuntimeOptions +{ + OtlpFileClientRuntimeOptions() = default; + ~OtlpFileClientRuntimeOptions() = default; + + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter.h index b49dcef77f..e2b3b4bd6c 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter.h @@ -8,6 +8,7 @@ #include "opentelemetry/exporters/otlp/otlp_file_client.h" #include "opentelemetry/exporters/otlp/otlp_file_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/trace/exporter.h" @@ -36,6 +37,12 @@ class OPENTELEMETRY_EXPORT OtlpFileExporter final : public opentelemetry::sdk::t */ explicit OtlpFileExporter(const OtlpFileExporterOptions &options); + /** + * Create an OtlpFileExporter using the given options. + */ + explicit OtlpFileExporter(const OtlpFileExporterOptions &options, + const OtlpFileExporterRuntimeOptions &runtime_options); + /** * Create a span recordable. * @return a newly initialized Recordable object @@ -70,6 +77,8 @@ class OPENTELEMETRY_EXPORT OtlpFileExporter final : public opentelemetry::sdk::t private: // The configuration options associated with this exporter. const OtlpFileExporterOptions options_; + // The runtime options associated with this exporter. + const OtlpFileExporterRuntimeOptions runtime_options_; // Object that stores the file context. std::unique_ptr file_client_; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_factory.h index bf4ba3288f..326fba4f26 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_file_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/version.h" @@ -31,6 +32,13 @@ class OPENTELEMETRY_EXPORT OtlpFileExporterFactory */ static std::unique_ptr Create( const OtlpFileExporterOptions &options); + + /** + * Create an OtlpFileExporter using the given options. + */ + static std::unique_ptr Create( + const OtlpFileExporterOptions &options, + const OtlpFileExporterRuntimeOptions &runtime_options); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h new file mode 100644 index 0000000000..dcd30239a7 --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h @@ -0,0 +1,27 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/version.h" + +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP File traces exporter runtime options. + */ +struct OPENTELEMETRY_EXPORT OtlpFileExporterRuntimeOptions : public OtlpFileClientRuntimeOptions +{ + OtlpFileExporterRuntimeOptions() = default; + ~OtlpFileExporterRuntimeOptions() = default; +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h index 6fb9f6d844..23af07c027 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h @@ -8,6 +8,7 @@ #include "opentelemetry/exporters/otlp/otlp_file_client.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/logs/exporter.h" @@ -37,6 +38,14 @@ class OtlpFileLogRecordExporter final : public opentelemetry::sdk::logs::LogReco */ OtlpFileLogRecordExporter(const OtlpFileLogRecordExporterOptions &options); + /** + * Create an OtlpFileLogRecordExporter with user specified options. + * @param options An object containing the user's configuration options. + * @param runtime_options An object containing the user's runtime options. + */ + OtlpFileLogRecordExporter(const OtlpFileLogRecordExporterOptions &options, + const OtlpFileLogRecordExporterRuntimeOptions &runtime_options); + /** * Creates a recordable that stores the data in a JSON object */ @@ -69,6 +78,8 @@ class OtlpFileLogRecordExporter final : public opentelemetry::sdk::logs::LogReco private: // Configuration options for the exporter const OtlpFileLogRecordExporterOptions options_; + // Runtime options for the exporter + const OtlpFileLogRecordExporterRuntimeOptions runtime_options_; // Object that stores the file context. std::unique_ptr file_client_; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h index 663eeb189a..5a72043de6 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/version.h" @@ -31,6 +32,13 @@ class OPENTELEMETRY_EXPORT OtlpFileLogRecordExporterFactory */ static std::unique_ptr Create( const OtlpFileLogRecordExporterOptions &options); + + /** + * Create an OtlpFileExporter using the given options. + */ + static std::unique_ptr Create( + const OtlpFileLogRecordExporterOptions &options, + const OtlpFileLogRecordExporterRuntimeOptions &runtime_options); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h new file mode 100644 index 0000000000..4c10ab298f --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h @@ -0,0 +1,27 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP File log record exporter runtime options. + */ +struct OPENTELEMETRY_EXPORT OtlpFileLogRecordExporterRuntimeOptions + : public OtlpFileClientRuntimeOptions +{ + OtlpFileLogRecordExporterRuntimeOptions() = default; + ~OtlpFileLogRecordExporterRuntimeOptions() = default; +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter.h index e03e544de9..7439c40323 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter.h @@ -8,6 +8,7 @@ #include "opentelemetry/exporters/otlp/otlp_file_client.h" #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/metrics/export/metric_producer.h" #include "opentelemetry/sdk/metrics/instruments.h" @@ -36,6 +37,14 @@ class OtlpFileMetricExporter final : public opentelemetry::sdk::metrics::PushMet */ OtlpFileMetricExporter(const OtlpFileMetricExporterOptions &options); + /** + * Create an OtlpFileMetricExporter with user specified options. + * @param options An object containing the user's configuration options. + * @param runtime_options An object containing the user's runtime options. + */ + OtlpFileMetricExporter(const OtlpFileMetricExporterOptions &options, + const OtlpFileMetricExporterRuntimeOptions &runtime_options); + /** * Get the AggregationTemporality for exporter * @@ -61,6 +70,8 @@ class OtlpFileMetricExporter final : public opentelemetry::sdk::metrics::PushMet // Configuration options for the exporter const OtlpFileMetricExporterOptions options_; + // Runtime options for the exporter + const OtlpFileMetricExporterRuntimeOptions runtime_options_; // Aggregation Temporality Selector const sdk::metrics::AggregationTemporalitySelector aggregation_temporality_selector_; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_factory.h index a34c057fad..355c4693bc 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" #include "opentelemetry/version.h" @@ -31,6 +32,13 @@ class OPENTELEMETRY_EXPORT OtlpFileMetricExporterFactory */ static std::unique_ptr Create( const OtlpFileMetricExporterOptions &options); + + /** + * Create an OtlpFileExporter using the given options. + */ + static std::unique_ptr Create( + const OtlpFileMetricExporterOptions &options, + const OtlpFileMetricExporterRuntimeOptions &runtime_options); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h new file mode 100644 index 0000000000..be8f3e2caa --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h @@ -0,0 +1,27 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP File metrics exporter runtime options. + */ +struct OPENTELEMETRY_EXPORT OtlpFileMetricExporterRuntimeOptions + : public OtlpFileClientRuntimeOptions +{ + OtlpFileMetricExporterRuntimeOptions() = default; + ~OtlpFileMetricExporterRuntimeOptions() = default; +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client_factory.h index ded0eadd8e..133f4643e0 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client_factory.h @@ -5,8 +5,9 @@ #include +#include "opentelemetry/exporters/otlp/otlp_grpc_client.h" #include "opentelemetry/exporters/otlp/otlp_grpc_client_options.h" -#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -14,9 +15,6 @@ namespace exporter namespace otlp { -class OtlpGrpcClientReferenceGuard; -class OtlpGrpcClient; - /** * Factory class for OtlpGrpcClient. */ diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_utils.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_utils.h index 504b0e9df3..a5e4e018fa 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_utils.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_utils.h @@ -3,9 +3,9 @@ #pragma once -#include +#include -#include "opentelemetry/sdk/version/version.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index 3753704bb3..1668f0381d 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -20,6 +20,7 @@ #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/exporter_utils.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" // forward declare google::protobuf::Message @@ -83,28 +84,33 @@ struct OtlpHttpClientOptions // User agent std::string user_agent; - inline OtlpHttpClientOptions(nostd::string_view input_url, - bool input_ssl_insecure_skip_verify, - nostd::string_view input_ssl_ca_cert_path, - nostd::string_view input_ssl_ca_cert_string, - nostd::string_view input_ssl_client_key_path, - nostd::string_view input_ssl_client_key_string, - nostd::string_view input_ssl_client_cert_path, - nostd::string_view input_ssl_client_cert_string, - nostd::string_view input_ssl_min_tls, - nostd::string_view input_ssl_max_tls, - nostd::string_view input_ssl_cipher, - nostd::string_view input_ssl_cipher_suite, - HttpRequestContentType input_content_type, - JsonBytesMappingKind input_json_bytes_mapping, - nostd::string_view input_compression, - bool input_use_json_name, - bool input_console_debug, - std::chrono::system_clock::duration input_timeout, - const OtlpHeaders &input_http_headers, - std::size_t input_concurrent_sessions = 64, - std::size_t input_max_requests_per_connection = 8, - nostd::string_view input_user_agent = GetOtlpDefaultUserAgent()) + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); + + inline OtlpHttpClientOptions( + nostd::string_view input_url, + bool input_ssl_insecure_skip_verify, + nostd::string_view input_ssl_ca_cert_path, + nostd::string_view input_ssl_ca_cert_string, + nostd::string_view input_ssl_client_key_path, + nostd::string_view input_ssl_client_key_string, + nostd::string_view input_ssl_client_cert_path, + nostd::string_view input_ssl_client_cert_string, + nostd::string_view input_ssl_min_tls, + nostd::string_view input_ssl_max_tls, + nostd::string_view input_ssl_cipher, + nostd::string_view input_ssl_cipher_suite, + HttpRequestContentType input_content_type, + JsonBytesMappingKind input_json_bytes_mapping, + nostd::string_view input_compression, + bool input_use_json_name, + bool input_console_debug, + std::chrono::system_clock::duration input_timeout, + const OtlpHeaders &input_http_headers, + const std::shared_ptr &input_thread_instrumentation, + std::size_t input_concurrent_sessions = 64, + std::size_t input_max_requests_per_connection = 8, + nostd::string_view input_user_agent = GetOtlpDefaultUserAgent()) : url(input_url), ssl_options(input_url, input_ssl_insecure_skip_verify, @@ -127,7 +133,8 @@ struct OtlpHttpClientOptions http_headers(input_http_headers), max_concurrent_requests(input_concurrent_sessions), max_requests_per_connection(input_max_requests_per_connection), - user_agent(input_user_agent) + user_agent(input_user_agent), + thread_instrumentation(input_thread_instrumentation) {} }; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h index 38f1d9e306..454959dd45 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h @@ -8,6 +8,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/trace/exporter.h" @@ -36,6 +37,12 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporter final : public opentelemetry::sdk::t */ explicit OtlpHttpExporter(const OtlpHttpExporterOptions &options); + /** + * Create an OtlpHttpExporter using the given options. + */ + OtlpHttpExporter(const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options); + /** * Create a span recordable. * @return a newly initialized Recordable object @@ -69,7 +76,9 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporter final : public opentelemetry::sdk::t private: // The configuration options associated with this exporter. - const OtlpHttpExporterOptions options_; + OtlpHttpExporterOptions options_; + // The runtime options associated with this exporter. + OtlpHttpExporterRuntimeOptions runtime_options_; // Object that stores the HTTP sessions that have been created std::unique_ptr http_client_; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h index 9e072a0534..bf42d26354 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/version.h" @@ -31,6 +32,13 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporterFactory */ static std::unique_ptr Create( const OtlpHttpExporterOptions &options); + + /** + * Create an OtlpHttpExporter using the given options. + */ + static std::unique_ptr Create( + const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h new file mode 100644 index 0000000000..02ec76acf9 --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h @@ -0,0 +1,31 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP HTTP traces exporter runtime options. + */ +struct OPENTELEMETRY_EXPORT OtlpHttpExporterRuntimeOptions +{ + OtlpHttpExporterRuntimeOptions() = default; + ~OtlpHttpExporterRuntimeOptions() = default; + + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h index 9439e5725b..aba430522e 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h @@ -8,6 +8,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/logs/exporter.h" @@ -37,6 +38,14 @@ class OtlpHttpLogRecordExporter final : public opentelemetry::sdk::logs::LogReco */ OtlpHttpLogRecordExporter(const OtlpHttpLogRecordExporterOptions &options); + /** + * Create an OtlpHttpLogRecordExporter with user specified options. + * @param options An object containing the user's configuration options. + * @param runtime_options An object containing the user's runtime options. + */ + OtlpHttpLogRecordExporter(const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options); + /** * Creates a recordable that stores the data in a JSON object */ @@ -68,7 +77,9 @@ class OtlpHttpLogRecordExporter final : public opentelemetry::sdk::logs::LogReco private: // Configuration options for the exporter - const OtlpHttpLogRecordExporterOptions options_; + OtlpHttpLogRecordExporterOptions options_; + // Runtime options for the exporter + OtlpHttpLogRecordExporterRuntimeOptions runtime_options_; // Object that stores the HTTP sessions that have been created std::unique_ptr http_client_; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h index c1e9de9ae1..d01eb4259a 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/version.h" @@ -31,6 +32,13 @@ class OPENTELEMETRY_EXPORT OtlpHttpLogRecordExporterFactory */ static std::unique_ptr Create( const OtlpHttpLogRecordExporterOptions &options); + + /** + * Create a OtlpHttpLogRecordExporter. + */ + static std::unique_ptr Create( + const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h new file mode 100644 index 0000000000..b5ef2ed967 --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h @@ -0,0 +1,31 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP HTTP log record exporter runtime options. + */ +struct OPENTELEMETRY_EXPORT OtlpHttpLogRecordExporterRuntimeOptions +{ + OtlpHttpLogRecordExporterRuntimeOptions() = default; + ~OtlpHttpLogRecordExporterRuntimeOptions() = default; + + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h index d74177e5c9..8733a740a9 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h @@ -8,6 +8,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/metrics/export/metric_producer.h" #include "opentelemetry/sdk/metrics/instruments.h" @@ -36,6 +37,14 @@ class OtlpHttpMetricExporter final : public opentelemetry::sdk::metrics::PushMet */ OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptions &options); + /** + * Create an OtlpHttpMetricExporter with user specified options. + * @param options An object containing the user's configuration options. + * @param runtime_options An object containing the user's runtime options. + */ + OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options); + /** * Get the AggregationTemporality for exporter * @@ -58,7 +67,9 @@ class OtlpHttpMetricExporter final : public opentelemetry::sdk::metrics::PushMet private: // Configuration options for the exporter - const OtlpHttpMetricExporterOptions options_; + OtlpHttpMetricExporterOptions options_; + // Runtime options for the exporter + OtlpHttpMetricExporterRuntimeOptions runtime_options_; // Aggregation Temporality Selector const sdk::metrics::AggregationTemporalitySelector aggregation_temporality_selector_; diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h index a290d91fd0..cb026cbe1e 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" #include "opentelemetry/version.h" @@ -31,6 +32,13 @@ class OPENTELEMETRY_EXPORT OtlpHttpMetricExporterFactory */ static std::unique_ptr Create( const OtlpHttpMetricExporterOptions &options); + + /** + * Create a OtlpHttpMetricExporter. + */ + static std::unique_ptr Create( + const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h new file mode 100644 index 0000000000..c0e91a342c --- /dev/null +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h @@ -0,0 +1,31 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +/** + * Struct to hold OTLP HTTP metrics exporter runtime options. + */ +struct OPENTELEMETRY_EXPORT OtlpHttpMetricExporterRuntimeOptions +{ + OtlpHttpMetricExporterRuntimeOptions() = default; + ~OtlpHttpMetricExporterRuntimeOptions() = default; + + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/src/otlp_file_client.cc b/exporters/otlp/src/otlp_file_client.cc index 7817dde85e..dab212871c 100644 --- a/exporters/otlp/src/otlp_file_client.cc +++ b/exporters/otlp/src/otlp_file_client.cc @@ -20,6 +20,8 @@ #include #include +// IWYU pragma: no_include + #if defined(HAVE_GSL) # include #else @@ -60,6 +62,7 @@ # include # include +# include # include # define FS_ACCESS(x) access(x, F_OK) @@ -104,12 +107,14 @@ #include "opentelemetry/exporters/otlp/otlp_file_client.h" #include "opentelemetry/exporters/otlp/otlp_file_client_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/base64.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" // clang-format off @@ -970,8 +975,9 @@ void ConvertListFieldToJson(nlohmann::json &value, class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender { public: - explicit OtlpFileSystemBackend(const OtlpFileClientFileSystemOptions &options) - : options_(options), is_initialized_{false} + explicit OtlpFileSystemBackend(const OtlpFileClientFileSystemOptions &options, + const OtlpFileClientRuntimeOptions &runtime_options) + : options_(options), runtime_options_(runtime_options), is_initialized_{false} { file_->is_shutdown.store(false); file_->rotate_index = 0; @@ -1444,11 +1450,20 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender std::shared_ptr concurrency_file = file_; std::chrono::microseconds flush_interval = options_.flush_interval; - file_->background_flush_thread.reset(new std::thread([concurrency_file, flush_interval]() { + auto thread_instrumentation = runtime_options_.thread_instrumentation; + file_->background_flush_thread.reset(new std::thread([concurrency_file, flush_interval, + thread_instrumentation]() { std::chrono::system_clock::time_point last_free_job_timepoint = std::chrono::system_clock::now(); std::size_t last_record_count = 0; +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (thread_instrumentation != nullptr) + { + thread_instrumentation->OnStart(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + while (true) { std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); @@ -1463,11 +1478,25 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender break; } +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (thread_instrumentation != nullptr) + { + thread_instrumentation->BeforeWait(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + { std::unique_lock lk(concurrency_file->background_thread_waker_lock); concurrency_file->background_thread_waker_cv.wait_for(lk, flush_interval); } +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (thread_instrumentation != nullptr) + { + thread_instrumentation->AfterWait(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + { std::size_t current_record_count = concurrency_file->record_count.load(std::memory_order_acquire); @@ -1496,6 +1525,14 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender std::lock_guard lock_guard_inner{concurrency_file->background_thread_lock}; background_flush_thread.swap(concurrency_file->background_flush_thread); } + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (thread_instrumentation != nullptr) + { + thread_instrumentation->OnEnd(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + if (background_flush_thread && background_flush_thread->joinable()) { background_flush_thread->detach(); @@ -1519,6 +1556,7 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileSystemBackend : public OtlpFileAppender private: OtlpFileClientFileSystemOptions options_; + OtlpFileClientRuntimeOptions runtime_options_; struct FileStats { @@ -1571,13 +1609,16 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpFileOstreamBackend : public OtlpFileAppende std::reference_wrapper os_; }; -OtlpFileClient::OtlpFileClient(OtlpFileClientOptions &&options) - : is_shutdown_(false), options_(std::move(options)) +OtlpFileClient::OtlpFileClient(OtlpFileClientOptions &&options, + OtlpFileClientRuntimeOptions &&runtime_options) + : is_shutdown_(false), + options_(std::move(options)), + runtime_options_(std::move(runtime_options)) { if (nostd::holds_alternative(options_.backend_options)) { backend_ = opentelemetry::nostd::shared_ptr(new OtlpFileSystemBackend( - nostd::get(options_.backend_options))); + nostd::get(options_.backend_options), runtime_options_)); } else if (nostd::holds_alternative>(options_.backend_options)) { diff --git a/exporters/otlp/src/otlp_file_exporter.cc b/exporters/otlp/src/otlp_file_exporter.cc index b98a6b2007..158f152fa3 100644 --- a/exporters/otlp/src/otlp_file_exporter.cc +++ b/exporters/otlp/src/otlp_file_exporter.cc @@ -11,6 +11,7 @@ #include "opentelemetry/exporters/otlp/otlp_file_client_options.h" #include "opentelemetry/exporters/otlp/otlp_file_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_recordable.h" #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" #include "opentelemetry/nostd/span.h" @@ -35,7 +36,15 @@ namespace otlp OtlpFileExporter::OtlpFileExporter() : OtlpFileExporter(OtlpFileExporterOptions()) {} OtlpFileExporter::OtlpFileExporter(const OtlpFileExporterOptions &options) - : options_(options), file_client_(new OtlpFileClient(OtlpFileClientOptions(options))) + : OtlpFileExporter(options, OtlpFileExporterRuntimeOptions()) +{} + +OtlpFileExporter::OtlpFileExporter(const OtlpFileExporterOptions &options, + const OtlpFileExporterRuntimeOptions &runtime_options) + : options_(options), + runtime_options_(runtime_options), + file_client_(new OtlpFileClient(OtlpFileClientOptions(options), + OtlpFileExporterRuntimeOptions(runtime_options))) {} // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_file_exporter_factory.cc b/exporters/otlp/src/otlp_file_exporter_factory.cc index 245b3a9152..57c327a0c7 100644 --- a/exporters/otlp/src/otlp_file_exporter_factory.cc +++ b/exporters/otlp/src/otlp_file_exporter_factory.cc @@ -2,9 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 #include "opentelemetry/exporters/otlp/otlp_file_exporter_factory.h" - #include "opentelemetry/exporters/otlp/otlp_file_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_exporter_runtime_options.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -21,7 +22,16 @@ std::unique_ptr OtlpFileExporterFactory std::unique_ptr OtlpFileExporterFactory::Create( const OtlpFileExporterOptions &options) { - std::unique_ptr exporter(new OtlpFileExporter(options)); + OtlpFileExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr OtlpFileExporterFactory::Create( + const OtlpFileExporterOptions &options, + const OtlpFileExporterRuntimeOptions &runtime_options) +{ + std::unique_ptr exporter( + new OtlpFileExporter(options, runtime_options)); return exporter; } diff --git a/exporters/otlp/src/otlp_file_log_record_exporter.cc b/exporters/otlp/src/otlp_file_log_record_exporter.cc index 7df1c70b29..8767a7fd60 100644 --- a/exporters/otlp/src/otlp_file_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_file_log_record_exporter.cc @@ -11,6 +11,7 @@ #include "opentelemetry/exporters/otlp/otlp_file_client_options.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_log_recordable.h" #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" #include "opentelemetry/nostd/span.h" @@ -38,7 +39,16 @@ OtlpFileLogRecordExporter::OtlpFileLogRecordExporter() OtlpFileLogRecordExporter::OtlpFileLogRecordExporter( const OtlpFileLogRecordExporterOptions &options) - : options_(options), file_client_(new OtlpFileClient(OtlpFileClientOptions(options))) + : OtlpFileLogRecordExporter(options, OtlpFileLogRecordExporterRuntimeOptions()) +{} + +OtlpFileLogRecordExporter::OtlpFileLogRecordExporter( + const OtlpFileLogRecordExporterOptions &options, + const OtlpFileLogRecordExporterRuntimeOptions &runtime_options) + : options_(options), + runtime_options_(runtime_options), + file_client_(new OtlpFileClient(OtlpFileClientOptions(options), + OtlpFileLogRecordExporterRuntimeOptions(runtime_options))) {} // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc b/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc index 145e52262a..38aeb3b8e2 100644 --- a/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc +++ b/exporters/otlp/src/otlp_file_log_record_exporter_factory.cc @@ -4,6 +4,8 @@ #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_log_record_exporter_runtime_options.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -20,9 +22,18 @@ OtlpFileLogRecordExporterFactory::Create() std::unique_ptr OtlpFileLogRecordExporterFactory::Create(const OtlpFileLogRecordExporterOptions &options) +{ + OtlpFileLogRecordExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr +OtlpFileLogRecordExporterFactory::Create( + const OtlpFileLogRecordExporterOptions &options, + const OtlpFileLogRecordExporterRuntimeOptions &runtime_options) { std::unique_ptr exporter( - new OtlpFileLogRecordExporter(options)); + new OtlpFileLogRecordExporter(options, runtime_options)); return exporter; } diff --git a/exporters/otlp/src/otlp_file_metric_exporter.cc b/exporters/otlp/src/otlp_file_metric_exporter.cc index 78aea66956..eafc588d40 100644 --- a/exporters/otlp/src/otlp_file_metric_exporter.cc +++ b/exporters/otlp/src/otlp_file_metric_exporter.cc @@ -10,8 +10,10 @@ #include "opentelemetry/exporters/otlp/otlp_file_client.h" #include "opentelemetry/exporters/otlp/otlp_file_client_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_metric_utils.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/common/global_log_handler.h" @@ -37,10 +39,18 @@ OtlpFileMetricExporter::OtlpFileMetricExporter() {} OtlpFileMetricExporter::OtlpFileMetricExporter(const OtlpFileMetricExporterOptions &options) + : OtlpFileMetricExporter(options, OtlpFileMetricExporterRuntimeOptions()) +{} + +OtlpFileMetricExporter::OtlpFileMetricExporter( + const OtlpFileMetricExporterOptions &options, + const OtlpFileMetricExporterRuntimeOptions &runtime_options) : options_(options), + runtime_options_(runtime_options), aggregation_temporality_selector_{ OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, - file_client_(new OtlpFileClient(OtlpFileClientOptions(options))) + file_client_(new OtlpFileClient(OtlpFileClientOptions(options), + OtlpFileClientRuntimeOptions(runtime_options))) {} // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_file_metric_exporter_factory.cc b/exporters/otlp/src/otlp_file_metric_exporter_factory.cc index d474bbb914..7b52153e6b 100644 --- a/exporters/otlp/src/otlp_file_metric_exporter_factory.cc +++ b/exporters/otlp/src/otlp_file_metric_exporter_factory.cc @@ -4,6 +4,8 @@ #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter.h" #include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_metric_exporter_runtime_options.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -20,9 +22,17 @@ OtlpFileMetricExporterFactory::Create() std::unique_ptr OtlpFileMetricExporterFactory::Create(const OtlpFileMetricExporterOptions &options) +{ + OtlpFileMetricExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr +OtlpFileMetricExporterFactory::Create(const OtlpFileMetricExporterOptions &options, + const OtlpFileMetricExporterRuntimeOptions &runtime_options) { std::unique_ptr exporter( - new OtlpFileMetricExporter(options)); + new OtlpFileMetricExporter(options, runtime_options)); return exporter; } diff --git a/exporters/otlp/src/otlp_grpc_metric_exporter.cc b/exporters/otlp/src/otlp_grpc_metric_exporter.cc index 1723dd557a..2ed7b4ed6c 100644 --- a/exporters/otlp/src/otlp_grpc_metric_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_metric_exporter.cc @@ -50,6 +50,8 @@ OtlpGrpcMetricExporter::OtlpGrpcMetricExporter( OtlpGrpcMetricExporter::OtlpGrpcMetricExporter(const OtlpGrpcMetricExporterOptions &options, const std::shared_ptr &client) : options_(options), + aggregation_temporality_selector_{ + OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, client_(client), client_reference_guard_(OtlpGrpcClientFactory::CreateReferenceGuard()) { @@ -62,6 +64,8 @@ OtlpGrpcMetricExporter::OtlpGrpcMetricExporter( std::unique_ptr stub, const std::shared_ptr &client) : options_(OtlpGrpcMetricExporterOptions()), + aggregation_temporality_selector_{ + OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, client_(client), client_reference_guard_(OtlpGrpcClientFactory::CreateReferenceGuard()), metrics_service_stub_(std::move(stub)) diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 6e55164dae..7499574bcb 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -666,7 +666,7 @@ void ConvertListFieldToJson(nlohmann::json &value, OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options) : is_shutdown_(false), options_(options), - http_client_(http_client::HttpClientFactory::Create()), + http_client_(http_client::HttpClientFactory::Create(options.thread_instrumentation)), start_session_counter_(0), finished_session_counter_(0) { diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index 84845099b9..95383d164a 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -12,12 +12,14 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_recordable.h" #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/trace/recordable.h" #include "opentelemetry/version.h" @@ -38,6 +40,40 @@ OtlpHttpExporter::OtlpHttpExporter() : OtlpHttpExporter(OtlpHttpExporterOptions( OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options) : options_(options), + runtime_options_(), + http_client_(new OtlpHttpClient(OtlpHttpClientOptions( + options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + std::shared_ptr{nullptr} +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ))) +{} + +OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options) + : options_(options), + runtime_options_(runtime_options), http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, options.ssl_insecure_skip_verify, options.ssl_ca_cert_path, @@ -56,7 +92,8 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options) options.use_json_name, options.console_debug, options.timeout, - options.http_headers + options.http_headers, + runtime_options.thread_instrumentation #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, @@ -68,18 +105,18 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options) OtlpHttpExporter::OtlpHttpExporter(std::unique_ptr http_client) : options_(OtlpHttpExporterOptions()), http_client_(std::move(http_client)) { - OtlpHttpExporterOptions &options = const_cast(options_); - options.url = http_client_->GetOptions().url; - options.content_type = http_client_->GetOptions().content_type; - options.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; - options.use_json_name = http_client_->GetOptions().use_json_name; - options.console_debug = http_client_->GetOptions().console_debug; - options.timeout = http_client_->GetOptions().timeout; - options.http_headers = http_client_->GetOptions().http_headers; + options_.url = http_client_->GetOptions().url; + options_.content_type = http_client_->GetOptions().content_type; + options_.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; + options_.use_json_name = http_client_->GetOptions().use_json_name; + options_.console_debug = http_client_->GetOptions().console_debug; + options_.timeout = http_client_->GetOptions().timeout; + options_.http_headers = http_client_->GetOptions().http_headers; #ifdef ENABLE_ASYNC_EXPORT - options.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; - options.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; + options_.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; + options_.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; #endif + runtime_options_.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; } // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_http_exporter_factory.cc b/exporters/otlp/src/otlp_http_exporter_factory.cc index 7d892bc337..eca3552146 100644 --- a/exporters/otlp/src/otlp_http_exporter_factory.cc +++ b/exporters/otlp/src/otlp_http_exporter_factory.cc @@ -4,6 +4,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -20,7 +21,16 @@ std::unique_ptr OtlpHttpExporterFactory std::unique_ptr OtlpHttpExporterFactory::Create( const OtlpHttpExporterOptions &options) { - std::unique_ptr exporter(new OtlpHttpExporter(options)); + OtlpHttpExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr OtlpHttpExporterFactory::Create( + const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options) +{ + std::unique_ptr exporter( + new OtlpHttpExporter(options, runtime_options)); return exporter; } diff --git a/exporters/otlp/src/otlp_http_log_record_exporter.cc b/exporters/otlp/src/otlp_http_log_record_exporter.cc index 0dddfc02cb..98865fdfcd 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter.cc @@ -12,12 +12,14 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_log_recordable.h" #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/logs/recordable.h" #include "opentelemetry/version.h" @@ -41,6 +43,41 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter() OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( const OtlpHttpLogRecordExporterOptions &options) : options_(options), + runtime_options_(), + http_client_(new OtlpHttpClient(OtlpHttpClientOptions( + options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + std::shared_ptr{nullptr} +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ))) +{} + +OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( + const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options) + : options_(options), + runtime_options_(runtime_options), http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, options.ssl_insecure_skip_verify, options.ssl_ca_cert_path, @@ -59,7 +96,8 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( options.use_json_name, options.console_debug, options.timeout, - options.http_headers + options.http_headers, + runtime_options.thread_instrumentation #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, @@ -71,19 +109,18 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter(std::unique_ptr http_client) : options_(OtlpHttpLogRecordExporterOptions()), http_client_(std::move(http_client)) { - OtlpHttpLogRecordExporterOptions &options = - const_cast(options_); - options.url = http_client_->GetOptions().url; - options.content_type = http_client_->GetOptions().content_type; - options.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; - options.use_json_name = http_client_->GetOptions().use_json_name; - options.console_debug = http_client_->GetOptions().console_debug; - options.timeout = http_client_->GetOptions().timeout; - options.http_headers = http_client_->GetOptions().http_headers; + options_.url = http_client_->GetOptions().url; + options_.content_type = http_client_->GetOptions().content_type; + options_.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; + options_.use_json_name = http_client_->GetOptions().use_json_name; + options_.console_debug = http_client_->GetOptions().console_debug; + options_.timeout = http_client_->GetOptions().timeout; + options_.http_headers = http_client_->GetOptions().http_headers; #ifdef ENABLE_ASYNC_EXPORT - options.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; - options.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; + options_.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; + options_.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; #endif + runtime_options_.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; } // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc b/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc index c224acc053..54e78bf4e9 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc @@ -4,6 +4,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -20,9 +21,18 @@ OtlpHttpLogRecordExporterFactory::Create() std::unique_ptr OtlpHttpLogRecordExporterFactory::Create(const OtlpHttpLogRecordExporterOptions &options) +{ + OtlpHttpLogRecordExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr +OtlpHttpLogRecordExporterFactory::Create( + const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options) { std::unique_ptr exporter( - new OtlpHttpLogRecordExporter(options)); + new OtlpHttpLogRecordExporter(options, runtime_options)); return exporter; } diff --git a/exporters/otlp/src/otlp_http_metric_exporter.cc b/exporters/otlp/src/otlp_http_metric_exporter.cc index a7e8e1d2be..f59138f981 100644 --- a/exporters/otlp/src/otlp_http_metric_exporter.cc +++ b/exporters/otlp/src/otlp_http_metric_exporter.cc @@ -13,10 +13,12 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_metric_utils.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/metrics/export/metric_producer.h" #include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/version.h" @@ -40,6 +42,43 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter() OtlpHttpMetricExporter::OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptions &options) : options_(options), + runtime_options_(), + aggregation_temporality_selector_{ + OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, + http_client_(new OtlpHttpClient(OtlpHttpClientOptions( + options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + std::shared_ptr{nullptr} +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ))) +{} + +OtlpHttpMetricExporter::OtlpHttpMetricExporter( + const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options) + : options_(options), + runtime_options_(runtime_options), aggregation_temporality_selector_{ OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, @@ -60,7 +99,8 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptio options.use_json_name, options.console_debug, options.timeout, - options.http_headers + options.http_headers, + runtime_options.thread_instrumentation #ifdef ENABLE_ASYNC_EXPORT , options.max_concurrent_requests, @@ -75,18 +115,18 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter(std::unique_ptr h OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, http_client_(std::move(http_client)) { - OtlpHttpMetricExporterOptions &options = const_cast(options_); - options.url = http_client_->GetOptions().url; - options.content_type = http_client_->GetOptions().content_type; - options.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; - options.use_json_name = http_client_->GetOptions().use_json_name; - options.console_debug = http_client_->GetOptions().console_debug; - options.timeout = http_client_->GetOptions().timeout; - options.http_headers = http_client_->GetOptions().http_headers; + options_.url = http_client_->GetOptions().url; + options_.content_type = http_client_->GetOptions().content_type; + options_.json_bytes_mapping = http_client_->GetOptions().json_bytes_mapping; + options_.use_json_name = http_client_->GetOptions().use_json_name; + options_.console_debug = http_client_->GetOptions().console_debug; + options_.timeout = http_client_->GetOptions().timeout; + options_.http_headers = http_client_->GetOptions().http_headers; #ifdef ENABLE_ASYNC_EXPORT - options.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; - options.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; + options_.max_concurrent_requests = http_client_->GetOptions().max_concurrent_requests; + options_.max_requests_per_connection = http_client_->GetOptions().max_requests_per_connection; #endif + runtime_options_.thread_instrumentation = http_client_->GetOptions().thread_instrumentation; } // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/src/otlp_http_metric_exporter_factory.cc b/exporters/otlp/src/otlp_http_metric_exporter_factory.cc index 883ce2c503..1bd6d8a644 100644 --- a/exporters/otlp/src/otlp_http_metric_exporter_factory.cc +++ b/exporters/otlp/src/otlp_http_metric_exporter_factory.cc @@ -4,6 +4,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -20,9 +21,17 @@ OtlpHttpMetricExporterFactory::Create() std::unique_ptr OtlpHttpMetricExporterFactory::Create(const OtlpHttpMetricExporterOptions &options) +{ + OtlpHttpMetricExporterRuntimeOptions runtime_options; + return Create(options, runtime_options); +} + +std::unique_ptr +OtlpHttpMetricExporterFactory::Create(const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options) { std::unique_ptr exporter( - new OtlpHttpMetricExporter(options)); + new OtlpHttpMetricExporter(options, runtime_options)); return exporter; } diff --git a/exporters/otlp/test/otlp_file_client_test.cc b/exporters/otlp/test/otlp_file_client_test.cc index 368e7f67c2..20ba905c47 100644 --- a/exporters/otlp/test/otlp_file_client_test.cc +++ b/exporters/otlp/test/otlp_file_client_test.cc @@ -16,9 +16,12 @@ #include #include +// IWYU pragma: no_include + #include "opentelemetry/common/timestamp.h" #include "opentelemetry/exporters/otlp/otlp_file_client.h" #include "opentelemetry/exporters/otlp/otlp_file_client_options.h" +#include "opentelemetry/exporters/otlp/otlp_file_client_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_recordable.h" #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" #include "opentelemetry/nostd/shared_ptr.h" @@ -158,7 +161,8 @@ TEST(OtlpFileClientTest, Shutdown) opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest request; auto client = std::unique_ptr( new opentelemetry::exporter::otlp::OtlpFileClient( - opentelemetry::exporter::otlp::OtlpFileClientOptions())); + opentelemetry::exporter::otlp::OtlpFileClientOptions(), + opentelemetry::exporter::otlp::OtlpFileClientRuntimeOptions())); ASSERT_FALSE(client->IsShutdown()); ASSERT_TRUE(client->Shutdown()); ASSERT_TRUE(client->IsShutdown()); @@ -181,10 +185,11 @@ TEST(OtlpFileClientTest, ExportToOstreamTest) std::stringstream output_stream; opentelemetry::exporter::otlp::OtlpFileClientOptions opts; + opentelemetry::exporter::otlp::OtlpFileClientRuntimeOptions rt_opts; opts.backend_options = std::ref(output_stream); auto client = std::unique_ptr( - new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts))); + new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts), std::move(rt_opts))); client->Export(request, 1); { @@ -280,10 +285,11 @@ TEST(OtlpFileClientTest, ExportToFileSystemRotateIndexTest) backend_opts.rotate_size = 3; opentelemetry::exporter::otlp::OtlpFileClientOptions opts; + opentelemetry::exporter::otlp::OtlpFileClientRuntimeOptions rt_opts; opts.backend_options = backend_opts; auto client = std::unique_ptr( - new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts))); + new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts), std::move(rt_opts))); // Write 5 records with rotatation index 1,2,3,1,2 for (int i = 0; i < 4; ++i) @@ -401,10 +407,11 @@ TEST(OtlpFileClientTest, ExportToFileSystemRotateByTimeTest) backend_opts.file_size = 1500; opentelemetry::exporter::otlp::OtlpFileClientOptions opts; + opentelemetry::exporter::otlp::OtlpFileClientRuntimeOptions rt_opts; opts.backend_options = backend_opts; auto client = std::unique_ptr( - new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts))); + new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts), std::move(rt_opts))); auto start_time = std::chrono::system_clock::now(); client->Export(request, 1); @@ -508,11 +515,12 @@ TEST(OtlpFileClientTest, ConfigTest) { { opentelemetry::exporter::otlp::OtlpFileClientOptions opts; + opentelemetry::exporter::otlp::OtlpFileClientRuntimeOptions rt_opts; opts.console_debug = true; opts.backend_options = std::ref(std::cout); auto client = std::unique_ptr( - new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts))); + new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts), std::move(rt_opts))); ASSERT_TRUE(client->GetOptions().console_debug); ASSERT_TRUE(opentelemetry::nostd::holds_alternative>( @@ -524,11 +532,12 @@ TEST(OtlpFileClientTest, ConfigTest) backend_opts.file_pattern = "test_file_pattern.jsonl"; opentelemetry::exporter::otlp::OtlpFileClientOptions opts; + opentelemetry::exporter::otlp::OtlpFileClientRuntimeOptions rt_opts; opts.console_debug = false; opts.backend_options = backend_opts; auto client = std::unique_ptr( - new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts))); + new opentelemetry::exporter::otlp::OtlpFileClient(std::move(opts), std::move(rt_opts))); ASSERT_FALSE(client->GetOptions().console_debug); ASSERT_TRUE(opentelemetry::nostd::holds_alternative< diff --git a/exporters/otlp/test/otlp_grpc_exporter_factory_test.cc b/exporters/otlp/test/otlp_grpc_exporter_factory_test.cc index a2fb62dab0..8885c3e947 100644 --- a/exporters/otlp/test/otlp_grpc_exporter_factory_test.cc +++ b/exporters/otlp/test/otlp_grpc_exporter_factory_test.cc @@ -3,7 +3,6 @@ #include -#include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h" @@ -15,6 +14,10 @@ # error "protobuf should not be included" #endif +/* + Implementation, this requires protobuf. +*/ +#include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h" OPENTELEMETRY_BEGIN_NAMESPACE diff --git a/exporters/otlp/test/otlp_grpc_log_record_exporter_factory_test.cc b/exporters/otlp/test/otlp_grpc_log_record_exporter_factory_test.cc index 0d0ab17d62..8e8261334c 100644 --- a/exporters/otlp/test/otlp_grpc_log_record_exporter_factory_test.cc +++ b/exporters/otlp/test/otlp_grpc_log_record_exporter_factory_test.cc @@ -3,7 +3,6 @@ #include -#include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_options.h" @@ -15,6 +14,10 @@ # error "protobuf should not be included" #endif +/* + Implementation, this requires protobuf. +*/ +#include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h" OPENTELEMETRY_BEGIN_NAMESPACE diff --git a/exporters/otlp/test/otlp_grpc_metric_exporter_factory_test.cc b/exporters/otlp/test/otlp_grpc_metric_exporter_factory_test.cc index ecad49bee1..76af86b1bb 100644 --- a/exporters/otlp/test/otlp_grpc_metric_exporter_factory_test.cc +++ b/exporters/otlp/test/otlp_grpc_metric_exporter_factory_test.cc @@ -3,7 +3,6 @@ #include -#include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter_options.h" @@ -15,6 +14,10 @@ # error "protobuf should not be included" #endif +/* + Implementation, this requires protobuf. +*/ +#include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h" OPENTELEMETRY_BEGIN_NAMESPACE diff --git a/exporters/otlp/test/otlp_grpc_metric_exporter_test.cc b/exporters/otlp/test/otlp_grpc_metric_exporter_test.cc index 6689233a9d..d36def10f5 100644 --- a/exporters/otlp/test/otlp_grpc_metric_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_metric_exporter_test.cc @@ -25,6 +25,9 @@ # include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" +# include "opentelemetry/exporters/otlp/otlp_grpc_client.h" +# include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" + # include "opentelemetry/sdk/trace/simple_processor.h" # include "opentelemetry/sdk/trace/tracer_provider.h" # include "opentelemetry/trace/provider.h" @@ -50,12 +53,34 @@ class OtlpGrpcMetricExporterTestPeer : public ::testing::Test { public: std::unique_ptr GetExporter( - std::unique_ptr &stub_interface) + const OtlpGrpcMetricExporterOptions &options) + { + return std::unique_ptr(new OtlpGrpcMetricExporter(options)); + } + + std::unique_ptr GetExporter( + std::unique_ptr stub_interface) { return std::unique_ptr( new OtlpGrpcMetricExporter(std::move(stub_interface))); } + std::unique_ptr GetExporter( + std::unique_ptr stub_interface, + std::shared_ptr client) + { + return std::unique_ptr( + new OtlpGrpcMetricExporter(std::move(stub_interface), std::move(client))); + } + + std::unique_ptr GetExporter( + const OtlpGrpcMetricExporterOptions &options, + std::shared_ptr client) + { + return std::unique_ptr( + new OtlpGrpcMetricExporter(options, std::move(client))); + } + // Get the options associated with the given exporter. const OtlpGrpcMetricExporterOptions &GetOptions(std::unique_ptr &exporter) { @@ -204,6 +229,35 @@ TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigUnknownInsecureFromEnv) } # endif +TEST_F(OtlpGrpcMetricExporterTestPeer, CheckGetAggregationTemporality) +{ + auto options = OtlpGrpcMetricExporterOptions(); + options.aggregation_temporality = PreferredAggregationTemporality::kCumulative; + + auto client = OtlpGrpcClientFactory::Create(options); + + auto exporter0 = GetExporter(options); + auto exporter1 = GetExporter(client->MakeMetricsServiceStub()); + auto exporter2 = GetExporter(options, client); + auto exporter3 = GetExporter(client->MakeMetricsServiceStub(), client); + + EXPECT_EQ( + opentelemetry::sdk::metrics::AggregationTemporality::kCumulative, + exporter0->GetAggregationTemporality(opentelemetry::sdk::metrics::InstrumentType::kCounter)); + + EXPECT_EQ( + opentelemetry::sdk::metrics::AggregationTemporality::kCumulative, + exporter1->GetAggregationTemporality(opentelemetry::sdk::metrics::InstrumentType::kCounter)); + + EXPECT_EQ( + opentelemetry::sdk::metrics::AggregationTemporality::kCumulative, + exporter2->GetAggregationTemporality(opentelemetry::sdk::metrics::InstrumentType::kCounter)); + + EXPECT_EQ( + opentelemetry::sdk::metrics::AggregationTemporality::kCumulative, + exporter3->GetAggregationTemporality(opentelemetry::sdk::metrics::InstrumentType::kCounter)); +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 4c4a6dbeb2..0824a59a06 100644 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -54,6 +54,7 @@ static nostd::span MakeSpan(T (&array)[N]) OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_type, bool async_mode) { + std::shared_ptr not_instrumented; OtlpHttpExporterOptions options; options.content_type = content_type; options.console_debug = true; @@ -70,7 +71,7 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t "", /* ssl_cipher */ "", /* ssl_cipher_suite */ options.content_type, options.json_bytes_mapping, options.compression, options.use_json_name, - options.console_debug, options.timeout, options.http_headers); + options.console_debug, options.timeout, options.http_headers, not_instrumented); if (!async_mode) { otlp_http_client_options.max_concurrent_requests = 0; diff --git a/exporters/otlp/test/otlp_http_log_record_exporter_test.cc b/exporters/otlp/test/otlp_http_log_record_exporter_test.cc index 35a0bb62e2..606f162024 100644 --- a/exporters/otlp/test/otlp_http_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_record_exporter_test.cc @@ -54,6 +54,7 @@ static nostd::span MakeSpan(T (&array)[N]) OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_type, bool async_mode) { + std::shared_ptr not_instrumented; OtlpHttpLogRecordExporterOptions options; options.content_type = content_type; options.console_debug = true; @@ -69,7 +70,7 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t "", /* ssl_cipher */ "", /* ssl_cipher_suite */ options.content_type, options.json_bytes_mapping, options.compression, options.use_json_name, - options.console_debug, options.timeout, options.http_headers); + options.console_debug, options.timeout, options.http_headers, not_instrumented); if (!async_mode) { otlp_http_client_options.max_concurrent_requests = 0; diff --git a/exporters/otlp/test/otlp_http_metric_exporter_test.cc b/exporters/otlp/test/otlp_http_metric_exporter_test.cc index 06dbad6ff9..420946ff70 100644 --- a/exporters/otlp/test/otlp_http_metric_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_metric_exporter_test.cc @@ -1,9 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include #include -#include #include #include #include @@ -13,6 +11,7 @@ #include #include #include +#include "gmock/gmock.h" #include "opentelemetry/common/timestamp.h" #include "opentelemetry/exporters/otlp/otlp_environment.h" @@ -26,6 +25,7 @@ #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/sdk/common/exporter_utils.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" #include "opentelemetry/sdk/metrics/data/metric_data.h" #include "opentelemetry/sdk/metrics/data/point_data.h" @@ -39,6 +39,7 @@ // clang-format off #include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" // IWYU pragma: keep +// IWYU pragma: no_include "net/proto2/public/repeated_field.h" #include #include "opentelemetry/proto/collector/metrics/v1/metrics_service.pb.h" #include "opentelemetry/proto/common/v1/common.pb.h" @@ -74,6 +75,7 @@ static IntegerType JsonToInteger(nlohmann::json value) OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_type, bool async_mode) { + std::shared_ptr not_instrumented; OtlpHttpMetricExporterOptions options; options.content_type = content_type; options.console_debug = true; @@ -89,7 +91,7 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t "", /* ssl_cipher */ "", /* ssl_cipher_suite */ options.content_type, options.json_bytes_mapping, options.compression, options.use_json_name, - options.console_debug, options.timeout, options.http_headers); + options.console_debug, options.timeout, options.http_headers, not_instrumented); if (!async_mode) { otlp_http_client_options.max_concurrent_requests = 0; diff --git a/exporters/otlp/test/otlp_log_recordable_test.cc b/exporters/otlp/test/otlp_log_recordable_test.cc index b2e965b185..571f600b22 100644 --- a/exporters/otlp/test/otlp_log_recordable_test.cc +++ b/exporters/otlp/test/otlp_log_recordable_test.cc @@ -24,6 +24,7 @@ // clang-format off #include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" // IWYU pragma: keep +// IWYU pragma: no_include "net/proto2/public/repeated_field.h" #include "opentelemetry/proto/common/v1/common.pb.h" #include "opentelemetry/proto/logs/v1/logs.pb.h" #include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" // IWYU pragma: keep diff --git a/exporters/otlp/test/otlp_recordable_test.cc b/exporters/otlp/test/otlp_recordable_test.cc index 8309811ae0..c2449c2c25 100644 --- a/exporters/otlp/test/otlp_recordable_test.cc +++ b/exporters/otlp/test/otlp_recordable_test.cc @@ -38,6 +38,7 @@ // clang-format off #include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" // IWYU pragma: keep +// IWYU pragma: no_include "net/proto2/public/repeated_field.h" #include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h" #include "opentelemetry/proto/common/v1/common.pb.h" #include "opentelemetry/proto/resource/v1/resource.pb.h" diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/collector.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/collector.h index cb94fe7654..12cc26e6f8 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/collector.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/collector.h @@ -3,14 +3,13 @@ #pragma once -#include +#include +#include #include #include -#include -#include -#include "opentelemetry/exporters/prometheus/exporter_utils.h" #include "opentelemetry/sdk/metrics/metric_reader.h" +#include "opentelemetry/version.h" namespace prometheus_client = ::prometheus; diff --git a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h index 34bdc4a0f8..3cdca002fc 100644 --- a/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h +++ b/exporters/prometheus/include/opentelemetry/exporters/prometheus/exporter.h @@ -3,16 +3,13 @@ #pragma once -#include -#include -#include - #include +#include +#include #include "opentelemetry/exporters/prometheus/collector.h" #include "opentelemetry/exporters/prometheus/exporter_options.h" -#include "opentelemetry/nostd/span.h" -#include "opentelemetry/sdk/common/env_variables.h" +#include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/version.h" diff --git a/exporters/prometheus/src/collector.cc b/exporters/prometheus/src/collector.cc index 53d880ee01..2387baf578 100644 --- a/exporters/prometheus/src/collector.cc +++ b/exporters/prometheus/src/collector.cc @@ -1,8 +1,18 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include +#include +#include +#include + #include "opentelemetry/exporters/prometheus/collector.h" +#include "opentelemetry/exporters/prometheus/exporter_utils.h" +#include "opentelemetry/nostd/function_ref.h" #include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/metrics/export/metric_producer.h" +#include "opentelemetry/sdk/metrics/metric_reader.h" +#include "opentelemetry/version.h" namespace metric_sdk = opentelemetry::sdk::metrics; diff --git a/exporters/prometheus/src/exporter.cc b/exporters/prometheus/src/exporter.cc index eeab82d6bb..12fef5acd9 100644 --- a/exporters/prometheus/src/exporter.cc +++ b/exporters/prometheus/src/exporter.cc @@ -1,8 +1,19 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include +#include +#include +#include +#include +#include + +#include "opentelemetry/exporters/prometheus/collector.h" #include "opentelemetry/exporters/prometheus/exporter.h" +#include "opentelemetry/exporters/prometheus/exporter_options.h" #include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/metrics/instruments.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE diff --git a/exporters/prometheus/src/exporter_factory.cc b/exporters/prometheus/src/exporter_factory.cc index 93fca80977..f736dc755d 100644 --- a/exporters/prometheus/src/exporter_factory.cc +++ b/exporters/prometheus/src/exporter_factory.cc @@ -1,11 +1,12 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include +#include #include "opentelemetry/exporters/prometheus/exporter.h" #include "opentelemetry/exporters/prometheus/exporter_factory.h" #include "opentelemetry/exporters/prometheus/exporter_options.h" +#include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE diff --git a/exporters/prometheus/src/exporter_utils.cc b/exporters/prometheus/src/exporter_utils.cc index 598c88cb44..c874faa654 100644 --- a/exporters/prometheus/src/exporter_utils.cc +++ b/exporters/prometheus/src/exporter_utils.cc @@ -1,24 +1,36 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include +#include +#include #include +#include +#include +#include +#include #include +#include #include #include #include +#include #include -#include #include #include -#include "prometheus/metric_family.h" -#include "prometheus/metric_type.h" - -#include "opentelemetry/common/macros.h" +#include "opentelemetry/common/timestamp.h" #include "opentelemetry/exporters/prometheus/exporter_utils.h" +#include "opentelemetry/nostd/variant.h" +#include "opentelemetry/sdk/common/attribute_utils.h" #include "opentelemetry/sdk/common/global_log_handler.h" +#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" +#include "opentelemetry/sdk/metrics/data/metric_data.h" +#include "opentelemetry/sdk/metrics/data/point_data.h" #include "opentelemetry/sdk/metrics/export/metric_producer.h" +#include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/version.h" namespace prometheus_client = ::prometheus; namespace metric_sdk = opentelemetry::sdk::metrics; diff --git a/exporters/prometheus/test/collector_test.cc b/exporters/prometheus/test/collector_test.cc index a31d23ab72..f0626c8a0b 100644 --- a/exporters/prometheus/test/collector_test.cc +++ b/exporters/prometheus/test/collector_test.cc @@ -1,16 +1,20 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include +#include +#include +#include +#include +#include + #include "opentelemetry/exporters/prometheus/collector.h" #include "opentelemetry/metrics/meter_provider.h" -#include "opentelemetry/version.h" +#include "opentelemetry/sdk/metrics/export/metric_producer.h" +#include "opentelemetry/sdk/metrics/instruments.h" +#include "opentelemetry/sdk/metrics/metric_reader.h" #include "prometheus_test_helper.h" -#include -#include -#include -#include - using opentelemetry::exporter::metrics::PrometheusCollector; using opentelemetry::sdk::metrics::MetricProducer; using opentelemetry::sdk::metrics::ResourceMetrics; diff --git a/exporters/prometheus/test/exporter_test.cc b/exporters/prometheus/test/exporter_test.cc index b268ac8514..aa2e0de4bf 100644 --- a/exporters/prometheus/test/exporter_test.cc +++ b/exporters/prometheus/test/exporter_test.cc @@ -2,12 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 #include +#include -#include "opentelemetry/exporters/prometheus/collector.h" #include "opentelemetry/exporters/prometheus/exporter.h" +#include "opentelemetry/exporters/prometheus/exporter_options.h" #include "opentelemetry/sdk/metrics/instruments.h" -#include "opentelemetry/version.h" -#include "prometheus_test_helper.h" /** * PrometheusExporterTest is a friend class of PrometheusExporter. diff --git a/exporters/prometheus/test/exporter_utils_test.cc b/exporters/prometheus/test/exporter_utils_test.cc index 76ec869a6f..5a597f2b29 100644 --- a/exporters/prometheus/test/exporter_utils_test.cc +++ b/exporters/prometheus/test/exporter_utils_test.cc @@ -2,13 +2,25 @@ // SPDX-License-Identifier: Apache-2.0 #include - +#include +#include +#include +#include +#include +#include +#include #include -#include "prometheus/metric_family.h" -#include "prometheus/metric_type.h" +#include +#include "opentelemetry/common/timestamp.h" #include "opentelemetry/exporters/prometheus/exporter_utils.h" +#include "opentelemetry/nostd/unique_ptr.h" +#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" +#include "opentelemetry/sdk/metrics/data/metric_data.h" +#include "opentelemetry/sdk/metrics/export/metric_producer.h" +#include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/version.h" #include "prometheus_test_helper.h" using opentelemetry::exporter::metrics::PrometheusExporterUtils; diff --git a/exporters/prometheus/test/prometheus_test_helper.h b/exporters/prometheus/test/prometheus_test_helper.h index b1aaf70d8c..1bf1f17657 100644 --- a/exporters/prometheus/test/prometheus_test_helper.h +++ b/exporters/prometheus/test/prometheus_test_helper.h @@ -3,6 +3,8 @@ #pragma once +#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h" +#include "opentelemetry/sdk/resource/resource.h" #include "opentelemetry/version.h" namespace metric_sdk = opentelemetry::sdk::metrics; diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h index bef15997fc..cd88c9f7c0 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h @@ -23,6 +23,7 @@ #include "opentelemetry/nostd/function_ref.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -302,6 +303,7 @@ class HttpClient : public opentelemetry::ext::http::client::HttpClient public: // The call (curl_global_init) is not thread safe. Ensure this is called only once. HttpClient(); + HttpClient(const std::shared_ptr &thread_instrumentation); ~HttpClient() override; std::shared_ptr CreateSession( @@ -355,6 +357,7 @@ class HttpClient : public opentelemetry::ext::http::client::HttpClient std::mutex background_thread_m_; std::unique_ptr background_thread_; + std::shared_ptr background_thread_instrumentation_; std::chrono::milliseconds scheduled_delay_milliseconds_; std::chrono::milliseconds background_thread_wait_for_; diff --git a/ext/include/opentelemetry/ext/http/client/http_client_factory.h b/ext/include/opentelemetry/ext/http/client/http_client_factory.h index 43e15cf255..c1a326b7e6 100644 --- a/ext/include/opentelemetry/ext/http/client/http_client_factory.h +++ b/ext/include/opentelemetry/ext/http/client/http_client_factory.h @@ -2,7 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once + #include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace ext @@ -17,6 +20,8 @@ class HttpClientFactory static std::shared_ptr CreateSync(); static std::shared_ptr Create(); + static std::shared_ptr Create( + const std::shared_ptr &thread_instrumentation); }; } // namespace client } // namespace http diff --git a/ext/src/http/client/curl/http_client_curl.cc b/ext/src/http/client/curl/http_client_curl.cc index 51ad3dc413..b9f46f30bd 100644 --- a/ext/src/http/client/curl/http_client_curl.cc +++ b/ext/src/http/client/curl/http_client_curl.cc @@ -25,6 +25,7 @@ #include "opentelemetry/ext/http/common/url_parser.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" #ifdef ENABLE_OTLP_COMPRESSION_PREVIEW @@ -269,6 +270,18 @@ HttpClient::HttpClient() : multi_handle_(curl_multi_init()), next_session_id_{0}, max_sessions_per_connection_{8}, + background_thread_instrumentation_(nullptr), + scheduled_delay_milliseconds_{std::chrono::milliseconds(256)}, + background_thread_wait_for_{std::chrono::minutes{1}}, + curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()) +{} + +HttpClient::HttpClient( + const std::shared_ptr &thread_instrumentation) + : multi_handle_(curl_multi_init()), + next_session_id_{0}, + max_sessions_per_connection_{8}, + background_thread_instrumentation_(thread_instrumentation), scheduled_delay_milliseconds_{std::chrono::milliseconds(256)}, background_thread_wait_for_{std::chrono::minutes{1}}, curl_global_initializer_(HttpCurlGlobalInitializer::GetInstance()) @@ -429,6 +442,13 @@ bool HttpClient::MaybeSpawnBackgroundThread() background_thread_.reset(new std::thread( [](HttpClient *self) { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (self->background_thread_instrumentation_ != nullptr) + { + self->background_thread_instrumentation_->OnStart(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + int still_running = 1; std::chrono::system_clock::time_point last_free_job_timepoint = std::chrono::system_clock::now(); @@ -447,6 +467,13 @@ bool HttpClient::MaybeSpawnBackgroundThread() } else if (still_running || need_wait_more) { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (self->background_thread_instrumentation_ != nullptr) + { + self->background_thread_instrumentation_->BeforeWait(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + // curl_multi_poll is added from libcurl 7.66.0, before 7.68.0, we can only wait util // timeout to do the rest jobs #if LIBCURL_VERSION_NUM >= 0x074200 @@ -459,6 +486,13 @@ bool HttpClient::MaybeSpawnBackgroundThread() static_cast(self->scheduled_delay_milliseconds_.count()), nullptr); #endif + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (self->background_thread_instrumentation_ != nullptr) + { + self->background_thread_instrumentation_->AfterWait(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } do @@ -557,6 +591,13 @@ bool HttpClient::MaybeSpawnBackgroundThread() // If there is no pending jobs, we can stop the background thread. if (still_running == 0) { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (self->background_thread_instrumentation_ != nullptr) + { + self->background_thread_instrumentation_->OnEnd(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + if (self->background_thread_) { self->background_thread_->detach(); diff --git a/ext/src/http/client/curl/http_client_factory_curl.cc b/ext/src/http/client/curl/http_client_factory_curl.cc index fd0a7d6a81..b339c2d93c 100644 --- a/ext/src/http/client/curl/http_client_factory_curl.cc +++ b/ext/src/http/client/curl/http_client_factory_curl.cc @@ -6,6 +6,7 @@ #include "opentelemetry/ext/http/client/curl/http_client_curl.h" #include "opentelemetry/ext/http/client/http_client.h" #include "opentelemetry/ext/http/client/http_client_factory.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" namespace http_client = opentelemetry::ext::http::client; @@ -14,6 +15,12 @@ std::shared_ptr http_client::HttpClientFactory::Create( return std::make_shared(); } +std::shared_ptr http_client::HttpClientFactory::Create( + const std::shared_ptr &thread_instrumentation) +{ + return std::make_shared(thread_instrumentation); +} + std::shared_ptr http_client::HttpClientFactory::CreateSync() { return std::make_shared(); diff --git a/opentracing-shim/include/opentelemetry/opentracingshim/propagation.h b/opentracing-shim/include/opentelemetry/opentracingshim/propagation.h index 4e44276a3d..3eceedb35f 100644 --- a/opentracing-shim/include/opentelemetry/opentracingshim/propagation.h +++ b/opentracing-shim/include/opentelemetry/opentracingshim/propagation.h @@ -5,13 +5,14 @@ #pragma once +#include "opentracing/propagation.h" +#include "opentracing/value.h" + #include "opentelemetry/baggage/baggage.h" #include "opentelemetry/common/attribute_value.h" #include "opentelemetry/context/propagation/text_map_propagator.h" #include "opentelemetry/nostd/function_ref.h" #include "opentelemetry/nostd/string_view.h" -#include "opentracing/propagation.h" -#include "opentracing/value.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace opentracingshim diff --git a/opentracing-shim/include/opentelemetry/opentracingshim/shim_utils.h b/opentracing-shim/include/opentelemetry/opentracingshim/shim_utils.h index 3436473870..f072168702 100644 --- a/opentracing-shim/include/opentelemetry/opentracingshim/shim_utils.h +++ b/opentracing-shim/include/opentelemetry/opentracingshim/shim_utils.h @@ -5,11 +5,34 @@ #pragma once +#include +#include +#include +#include +#include +#include +#include + +#include "opentracing/propagation.h" +#include "opentracing/span.h" +#include "opentracing/string_view.h" +#include "opentracing/tracer.h" +#include "opentracing/value.h" +#include "opentracing/variant/recursive_wrapper.hpp" + +#include "opentelemetry/baggage/baggage.h" +#include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/common/key_value_iterable.h" +#include "opentelemetry/common/key_value_iterable_view.h" +#include "opentelemetry/nostd/function_ref.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/opentracingshim/span_context_shim.h" - #include "opentelemetry/semconv/incubating/opentracing_attributes.h" -#include "opentelemetry/trace/tracer.h" -#include "opentracing/tracer.h" +#include "opentelemetry/trace/span_context.h" +#include "opentelemetry/trace/span_context_kv_iterable.h" +#include "opentelemetry/trace/span_startoptions.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace opentracingshim diff --git a/opentracing-shim/include/opentelemetry/opentracingshim/span_context_shim.h b/opentracing-shim/include/opentelemetry/opentracingshim/span_context_shim.h index 55cdd5d86d..8eba4a6377 100644 --- a/opentracing-shim/include/opentelemetry/opentracingshim/span_context_shim.h +++ b/opentracing-shim/include/opentelemetry/opentracingshim/span_context_shim.h @@ -5,9 +5,16 @@ #pragma once +#include +#include + +#include "opentracing/span.h" + #include "opentelemetry/baggage/baggage.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/trace/span_context.h" -#include "opentracing/span.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace opentracingshim diff --git a/opentracing-shim/include/opentelemetry/opentracingshim/span_shim.h b/opentracing-shim/include/opentelemetry/opentracingshim/span_shim.h index 4fe48852c1..f15a07f2dc 100644 --- a/opentracing-shim/include/opentelemetry/opentracingshim/span_shim.h +++ b/opentracing-shim/include/opentelemetry/opentracingshim/span_shim.h @@ -5,14 +5,24 @@ #pragma once -#include "opentelemetry/opentracingshim/span_context_shim.h" -#include "opentelemetry/opentracingshim/tracer_shim.h" +#include +#include +#include +#include + +#include "opentracing/span.h" +#include "opentracing/string_view.h" +#include "opentracing/tracer.h" +#include "opentracing/util.h" +#include "opentracing/value.h" -#include "opentelemetry/baggage/baggage.h" -#include "opentelemetry/common/attribute_value.h" #include "opentelemetry/common/spin_lock_mutex.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/span.h" +#include "opentelemetry/opentracingshim/span_context_shim.h" +#include "opentelemetry/opentracingshim/tracer_shim.h" #include "opentelemetry/trace/span.h" -#include "opentracing/span.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace opentracingshim diff --git a/opentracing-shim/include/opentelemetry/opentracingshim/tracer_shim.h b/opentracing-shim/include/opentelemetry/opentracingshim/tracer_shim.h index 7f2d492efd..fcce5a6341 100644 --- a/opentracing-shim/include/opentelemetry/opentracingshim/tracer_shim.h +++ b/opentracing-shim/include/opentelemetry/opentracingshim/tracer_shim.h @@ -5,10 +5,21 @@ #pragma once +#include +#include + +#include "opentracing/expected/expected.hpp" +#include "opentracing/propagation.h" +#include "opentracing/span.h" +#include "opentracing/string_view.h" +#include "opentracing/tracer.h" + #include "opentelemetry/context/propagation/text_map_propagator.h" +#include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/trace/provider.h" #include "opentelemetry/trace/tracer.h" -#include "opentracing/tracer.h" +#include "opentelemetry/trace/tracer_provider.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace opentracingshim diff --git a/opentracing-shim/src/shim_utils.cc b/opentracing-shim/src/shim_utils.cc index 6cfb9a5328..e3f6fc72cf 100644 --- a/opentracing-shim/src/shim_utils.cc +++ b/opentracing-shim/src/shim_utils.cc @@ -3,9 +3,28 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "opentelemetry/opentracingshim/shim_utils.h" +#include +#include +#include +#include +#include +#include + +#include "opentracing/propagation.h" +#include "opentracing/span.h" +#include "opentracing/tracer.h" +#include "opentelemetry/baggage/baggage.h" #include "opentelemetry/baggage/baggage_context.h" +#include "opentelemetry/common/timestamp.h" +#include "opentelemetry/context/runtime_context.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/variant.h" +#include "opentelemetry/opentracingshim/shim_utils.h" +#include "opentelemetry/opentracingshim/span_context_shim.h" +#include "opentelemetry/trace/span_context.h" +#include "opentelemetry/trace/span_startoptions.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace opentracingshim diff --git a/opentracing-shim/src/span_context_shim.cc b/opentracing-shim/src/span_context_shim.cc index cd1069e9d9..153d40154e 100644 --- a/opentracing-shim/src/span_context_shim.cc +++ b/opentracing-shim/src/span_context_shim.cc @@ -3,7 +3,21 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include +#include +#include +#include + +#include "opentracing/span.h" + +#include "opentelemetry/baggage/baggage.h" +#include "opentelemetry/nostd/function_ref.h" +#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/opentracingshim/span_context_shim.h" +#include "opentelemetry/trace/span_context.h" +#include "opentelemetry/trace/span_id.h" +#include "opentelemetry/trace/trace_id.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace opentracingshim diff --git a/opentracing-shim/src/span_shim.cc b/opentracing-shim/src/span_shim.cc index afc974f057..6bbac7ee80 100644 --- a/opentracing-shim/src/span_shim.cc +++ b/opentracing-shim/src/span_shim.cc @@ -3,14 +3,32 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "opentelemetry/opentracingshim/span_shim.h" +#include +#include +#include +#include +#include +#include +#include + +#include "opentracing/ext/tags.h" +#include "opentracing/span.h" +#include "opentracing/string_view.h" +#include "opentracing/util.h" +#include "opentracing/value.h" + +#include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/common/spin_lock_mutex.h" +#include "opentelemetry/common/timestamp.h" +#include "opentelemetry/nostd/span.h" +#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/opentracingshim/shim_utils.h" #include "opentelemetry/opentracingshim/span_context_shim.h" -#include "opentelemetry/opentracingshim/tracer_shim.h" - +#include "opentelemetry/opentracingshim/span_shim.h" #include "opentelemetry/semconv/exception_attributes.h" +#include "opentelemetry/trace/span.h" #include "opentelemetry/trace/span_metadata.h" -#include "opentracing/ext/tags.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace opentracingshim diff --git a/opentracing-shim/src/tracer_shim.cc b/opentracing-shim/src/tracer_shim.cc index e06961a88e..8c1ae8328a 100644 --- a/opentracing-shim/src/tracer_shim.cc +++ b/opentracing-shim/src/tracer_shim.cc @@ -3,15 +3,39 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "opentelemetry/opentracingshim/tracer_shim.h" -#include "opentelemetry/opentracingshim/propagation.h" -#include "opentelemetry/opentracingshim/shim_utils.h" -#include "opentelemetry/opentracingshim/span_shim.h" +#include +#include +#include +#include +#include +#include +#include + +#include "opentracing/expected/expected.hpp" +#include "opentracing/ext/tags.h" +#include "opentracing/propagation.h" +#include "opentracing/span.h" +#include "opentracing/string_view.h" +#include "opentracing/tracer.h" +#include "opentracing/value.h" #include "opentelemetry/baggage/baggage_context.h" #include "opentelemetry/context/propagation/global_propagator.h" +#include "opentelemetry/context/propagation/text_map_propagator.h" +#include "opentelemetry/context/runtime_context.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/opentracingshim/propagation.h" +#include "opentelemetry/opentracingshim/shim_utils.h" +#include "opentelemetry/opentracingshim/span_context_shim.h" +#include "opentelemetry/opentracingshim/span_shim.h" +#include "opentelemetry/opentracingshim/tracer_shim.h" #include "opentelemetry/trace/context.h" -#include "opentracing/ext/tags.h" +#include "opentelemetry/trace/default_span.h" +#include "opentelemetry/trace/span.h" +#include "opentelemetry/trace/span_context.h" +#include "opentelemetry/trace/tracer.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace opentracingshim diff --git a/opentracing-shim/test/propagation_test.cc b/opentracing-shim/test/propagation_test.cc index eb59630405..497f0eb0ae 100644 --- a/opentracing-shim/test/propagation_test.cc +++ b/opentracing-shim/test/propagation_test.cc @@ -3,11 +3,25 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "shim_mocks.h" - +#include +#include +#include +#include +#include +#include + +#include "opentracing/expected/expected.hpp" +#include "opentracing/propagation.h" +#include "opentracing/string_view.h" +#include "opentracing/util.h" + +#include "opentelemetry/baggage/baggage.h" +#include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/nostd/function_ref.h" +#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/opentracingshim/propagation.h" -#include +#include "shim_mocks.h" namespace baggage = opentelemetry::baggage; namespace common = opentelemetry::common; diff --git a/opentracing-shim/test/shim_mocks.h b/opentracing-shim/test/shim_mocks.h index b39b2b3781..485e166233 100644 --- a/opentracing-shim/test/shim_mocks.h +++ b/opentracing-shim/test/shim_mocks.h @@ -5,6 +5,11 @@ #pragma once +#include +#include + +#include "opentracing/propagation.h" + #include "opentelemetry/baggage/baggage_context.h" #include "opentelemetry/context/propagation/text_map_propagator.h" #include "opentelemetry/trace/span.h" @@ -12,10 +17,6 @@ #include "opentelemetry/trace/span_metadata.h" #include "opentelemetry/trace/tracer.h" #include "opentelemetry/trace/tracer_provider.h" -#include "opentracing/propagation.h" - -#include -#include namespace trace_api = opentelemetry::trace; namespace baggage = opentelemetry::baggage; diff --git a/opentracing-shim/test/shim_utils_test.cc b/opentracing-shim/test/shim_utils_test.cc index d053273984..6af28d60cb 100644 --- a/opentracing-shim/test/shim_utils_test.cc +++ b/opentracing-shim/test/shim_utils_test.cc @@ -3,13 +3,40 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "shim_mocks.h" - -#include "opentelemetry/opentracingshim/shim_utils.h" - +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "opentracing/propagation.h" +#include "opentracing/span.h" +#include "opentracing/string_view.h" #include "opentracing/tracer.h" +#include "opentracing/util.h" +#include "opentracing/value.h" +#include "opentracing/variant/recursive_wrapper.hpp" + +#include "opentelemetry/baggage/baggage.h" +#include "opentelemetry/baggage/baggage_context.h" +#include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/common/key_value_iterable.h" +#include "opentelemetry/common/timestamp.h" +#include "opentelemetry/context/runtime_context.h" +#include "opentelemetry/nostd/function_ref.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/nostd/variant.h" +#include "opentelemetry/opentracingshim/shim_utils.h" +#include "opentelemetry/opentracingshim/span_context_shim.h" +#include "opentelemetry/trace/span_context.h" +#include "opentelemetry/trace/span_startoptions.h" -#include +#include "shim_mocks.h" namespace trace_api = opentelemetry::trace; namespace baggage = opentelemetry::baggage; diff --git a/opentracing-shim/test/span_context_shim_test.cc b/opentracing-shim/test/span_context_shim_test.cc index 93978f079a..9433f3a1cb 100644 --- a/opentracing-shim/test/span_context_shim_test.cc +++ b/opentracing-shim/test/span_context_shim_test.cc @@ -3,14 +3,23 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "opentelemetry/opentracingshim/span_context_shim.h" - -#include "opentelemetry/baggage/baggage.h" -#include "opentelemetry/trace/span_context.h" +#include +#include +#include +#include +#include +#include #include "opentracing/noop.h" +#include "opentracing/span.h" +#include "opentracing/tracer.h" +#include "opentracing/value.h" -#include +#include "opentelemetry/baggage/baggage.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/unique_ptr.h" +#include "opentelemetry/opentracingshim/span_context_shim.h" +#include "opentelemetry/trace/span_context.h" namespace trace_api = opentelemetry::trace; namespace baggage = opentelemetry::baggage; diff --git a/opentracing-shim/test/span_shim_test.cc b/opentracing-shim/test/span_shim_test.cc index d73f3e0061..0b1aaf80c3 100644 --- a/opentracing-shim/test/span_shim_test.cc +++ b/opentracing-shim/test/span_shim_test.cc @@ -3,12 +3,37 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "shim_mocks.h" - +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "opentracing/span.h" +#include "opentracing/string_view.h" +#include "opentracing/util.h" +#include "opentracing/value.h" + +#include "opentelemetry/baggage/baggage.h" +#include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/common/timestamp.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/nostd/unique_ptr.h" +#include "opentelemetry/nostd/variant.h" #include "opentelemetry/opentracingshim/span_shim.h" #include "opentelemetry/opentracingshim/tracer_shim.h" +#include "opentelemetry/trace/span.h" +#include "opentelemetry/trace/span_metadata.h" -#include +#include "shim_mocks.h" namespace trace_api = opentelemetry::trace; namespace baggage = opentelemetry::baggage; diff --git a/opentracing-shim/test/tracer_shim_test.cc b/opentracing-shim/test/tracer_shim_test.cc index 2d3f2618da..7e48f47d0c 100644 --- a/opentracing-shim/test/tracer_shim_test.cc +++ b/opentracing-shim/test/tracer_shim_test.cc @@ -3,16 +3,46 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "shim_mocks.h" - +#include +#include +#include +#include +#include +#include +#include +#include + +#include "opentracing/expected/expected.hpp" +#include "opentracing/noop.h" +#include "opentracing/propagation.h" +#include "opentracing/span.h" +#include "opentracing/tracer.h" +#include "opentracing/util.h" +#include "opentracing/value.h" + +#include "opentelemetry/baggage/baggage.h" +#include "opentelemetry/common/key_value_iterable.h" +#include "opentelemetry/context/propagation/text_map_propagator.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/span.h" +#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/opentracingshim/shim_utils.h" #include "opentelemetry/opentracingshim/span_context_shim.h" -#include "opentelemetry/opentracingshim/span_shim.h" #include "opentelemetry/opentracingshim/tracer_shim.h" +#include "opentelemetry/trace/default_span.h" +#include "opentelemetry/trace/provider.h" +#include "opentelemetry/trace/span.h" +#include "opentelemetry/trace/span_context.h" +#include "opentelemetry/trace/span_context_kv_iterable.h" +#include "opentelemetry/trace/span_id.h" +#include "opentelemetry/trace/span_metadata.h" +#include "opentelemetry/trace/span_startoptions.h" +#include "opentelemetry/trace/trace_flags.h" +#include "opentelemetry/trace/trace_id.h" +#include "opentelemetry/trace/tracer.h" +#include "opentelemetry/trace/tracer_provider.h" -#include "opentracing/noop.h" - -#include +#include "shim_mocks.h" namespace trace_api = opentelemetry::trace; namespace nostd = opentelemetry::nostd; diff --git a/sdk/include/opentelemetry/sdk/common/global_log_handler.h b/sdk/include/opentelemetry/sdk/common/global_log_handler.h index eadd408762..d11e1e0b7d 100644 --- a/sdk/include/opentelemetry/sdk/common/global_log_handler.h +++ b/sdk/include/opentelemetry/sdk/common/global_log_handler.h @@ -99,37 +99,28 @@ class GlobalLogHandler * * By default, a default LogHandler is returned. */ - static inline const nostd::shared_ptr &GetLogHandler() noexcept - { - return GetHandlerAndLevel().first; - } + static nostd::shared_ptr GetLogHandler() noexcept; /** * Changes the singleton LogHandler. * This should be called once at the start of application before creating any Provider * instance. */ - static inline void SetLogHandler(const nostd::shared_ptr &eh) noexcept - { - GetHandlerAndLevel().first = eh; - } + static void SetLogHandler(const nostd::shared_ptr &eh) noexcept; /** * Returns the singleton log level. * * By default, a default log level is returned. */ - static inline LogLevel GetLogLevel() noexcept { return GetHandlerAndLevel().second; } + static LogLevel GetLogLevel() noexcept; /** * Changes the singleton Log level. * This should be called once at the start of application before creating any Provider * instance. */ - static inline void SetLogLevel(LogLevel level) noexcept { GetHandlerAndLevel().second = level; } - -private: - static std::pair, LogLevel> &GetHandlerAndLevel() noexcept; + static void SetLogLevel(LogLevel level) noexcept; }; } // namespace internal_log @@ -142,24 +133,23 @@ OPENTELEMETRY_END_NAMESPACE * To ensure that GlobalLogHandler is the first one to be initialized (and so last to be * destroyed), it is first used inside the constructors of TraceProvider, MeterProvider * and LoggerProvider for debug logging. */ -#define OTEL_INTERNAL_LOG_DISPATCH(level, message, attributes) \ - do \ - { \ - using opentelemetry::sdk::common::internal_log::GlobalLogHandler; \ - using opentelemetry::sdk::common::internal_log::LogHandler; \ - if (level > GlobalLogHandler::GetLogLevel()) \ - { \ - break; \ - } \ - const opentelemetry::nostd::shared_ptr &log_handler = \ - GlobalLogHandler::GetLogHandler(); \ - if (!log_handler) \ - { \ - break; \ - } \ - std::stringstream tmp_stream; \ - tmp_stream << message; \ - log_handler->Handle(level, __FILE__, __LINE__, tmp_stream.str().c_str(), attributes); \ +#define OTEL_INTERNAL_LOG_DISPATCH(level, message, attributes) \ + do \ + { \ + using opentelemetry::sdk::common::internal_log::GlobalLogHandler; \ + using opentelemetry::sdk::common::internal_log::LogHandler; \ + if (level > GlobalLogHandler::GetLogLevel()) \ + { \ + break; \ + } \ + opentelemetry::nostd::shared_ptr log_handler = GlobalLogHandler::GetLogHandler(); \ + if (!log_handler) \ + { \ + break; \ + } \ + std::stringstream tmp_stream; \ + tmp_stream << message; \ + log_handler->Handle(level, __FILE__, __LINE__, tmp_stream.str().c_str(), attributes); \ } while (false); #define OTEL_INTERNAL_LOG_GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 diff --git a/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h b/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h new file mode 100644 index 0000000000..7886af71b5 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/common/thread_instrumentation.h @@ -0,0 +1,92 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace common +{ + +/** + * Thread instrumentation interface. + * When the opentelemetry-cpp library executes internal threads, + * the application linking with the opentelemetry-cpp library + * may want to have some control on how the thread executes. + * + * There are many different use cases for this, + * listing a few to illustrate. + * + * (1) The application may need to initialize thread local storage, + * in an application specific way, because application code that + * relies on thread local storage will be executed in the thread code path. + * For example, custom samplers for traces, or custom observable instruments + * for metrics, may expect specific thread local storage keys to exist. + * + * (2) The application may want opentelemetry-cpp threads to be observable + * in a given way when exposed to the operating system. + * For example, a linux application may want to give a name to + * opentelemetry-cpp threads, + * using [pthread_setname_np(3)](https://man7.org/linux/man-pages/man3/pthread_setname_np.3.html), + * to help troubleshooting. + * + * (3) The application may want specific opentelemetry-cpp threads to use + * application defined specific named network. + * For example, a linux application may want to use + * [setns(2)](https://man7.org/linux/man-pages/man2/setns.2.html) + * on a per exporter basis, so that different exporters uses different networks. + * + * (4) The application may want to bind specific opentelemetry-cpp threads + * to specific CPUs, for performance reasons. + * For example, a linux application may want to use + * [sched_setaffinity(2)](https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html) + * on a per thread basis. + * + * Providing dedicated opentelemetry-cpp interfaces in the SDK or exporters, + * to support these use cases, is not practical, because the code involved + * is highly platform dependent and use case dependent. + * + * Instead, the opentelemetry-cpp library provide hooks for applications + * to implement their own, arbitrary, platform specific, logic. + * This is done by implementing the ThreadInstrumentation interface + * in the application, and providing a given ThreadInstrumentation object + * when initializing the SDK or exporters. + * + * The opentelemetry-cpp library calls the following extension points, + * when a ThreadInstrumentation is provided. + * + * Upon thread creation and termination, the methods OnStart() and OnEnd() + * are invoked, respectively. + * + * When a thread is to block and wait, for example on a timer, + * the methods BeforeWait() and AfterWait() are invoked. + * + * When a thread is to perform a chunk of work, + * for example to process all the available data in an exporter, + * the methods BeforeLoad() and AfterLoad() are invoked. + */ +class ThreadInstrumentation +{ +public: + ThreadInstrumentation() = default; + virtual ~ThreadInstrumentation() = default; + +/* + * This feature is experimental, protected by a _PREVIEW flag. + */ +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + virtual void OnStart() {} + virtual void OnEnd() {} + virtual void BeforeWait() {} + virtual void AfterWait() {} + virtual void BeforeLoad() {} + virtual void AfterLoad() {} +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ +}; + +} // namespace common +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h index b52476071a..9f36b18fca 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h @@ -14,6 +14,7 @@ #include "opentelemetry/sdk/common/circular_buffer.h" #include "opentelemetry/sdk/logs/batch_log_record_processor_options.h" +#include "opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/sdk/logs/processor.h" #include "opentelemetry/sdk/logs/recordable.h" @@ -54,12 +55,24 @@ class BatchLogRecordProcessor : public LogRecordProcessor * Creates a batch log processor by configuring the specified exporter and other parameters * as per the official, language-agnostic opentelemetry specs. * - * @param exporter - The backend exporter to pass the logs to - * @param options - The batch SpanProcessor options. + * @param exporter The backend exporter to pass the logs to + * @param options The batch SpanProcessor configuration options. */ explicit BatchLogRecordProcessor(std::unique_ptr &&exporter, const BatchLogRecordProcessorOptions &options); + /** + * Creates a batch log processor by configuring the specified exporter and other parameters + * as per the official, language-agnostic opentelemetry specs. + * + * @param exporter The backend exporter to pass the logs to + * @param options The batch SpanProcessor configuration options. + * @param runtime_options The batch SpanProcessor runtime options. + */ + explicit BatchLogRecordProcessor(std::unique_ptr &&exporter, + const BatchLogRecordProcessorOptions &options, + const BatchLogRecordProcessorRuntimeOptions &runtime_options); + /** Makes a new recordable **/ std::unique_ptr MakeRecordable() noexcept override; @@ -157,6 +170,7 @@ class BatchLogRecordProcessor : public LogRecordProcessor std::shared_ptr synchronization_data_; /* The background worker thread */ + std::shared_ptr worker_thread_instrumentation_; std::thread worker_thread_; }; diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_factory.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_factory.h index 983f2e7508..14be61450a 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_factory.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/sdk/logs/batch_log_record_processor_options.h" +#include "opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/sdk/logs/processor.h" #include "opentelemetry/version.h" @@ -28,6 +29,14 @@ class OPENTELEMETRY_EXPORT BatchLogRecordProcessorFactory */ static std::unique_ptr Create(std::unique_ptr &&exporter, const BatchLogRecordProcessorOptions &options); + + /** + * Create a BatchLogRecordProcessor. + */ + static std::unique_ptr Create( + std::unique_ptr &&exporter, + const BatchLogRecordProcessorOptions &options, + const BatchLogRecordProcessorRuntimeOptions &runtime_options); }; } // namespace logs diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h new file mode 100644 index 0000000000..9201ea1298 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h @@ -0,0 +1,30 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ + +namespace logs +{ + +/** + * Struct to hold batch SpanProcessor runtime options. + */ +struct BatchLogRecordProcessorRuntimeOptions +{ + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace logs +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h index d72ec08839..bb8db1ac09 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h @@ -11,7 +11,9 @@ #include #include +#include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h" #include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" @@ -28,7 +30,11 @@ class PeriodicExportingMetricReader : public MetricReader public: PeriodicExportingMetricReader(std::unique_ptr exporter, - const PeriodicExportingMetricReaderOptions &option); + const PeriodicExportingMetricReaderOptions &options); + + PeriodicExportingMetricReader(std::unique_ptr exporter, + const PeriodicExportingMetricReaderOptions &options, + const PeriodicExportingMetricReaderRuntimeOptions &runtime_options); AggregationTemporality GetAggregationTemporality( InstrumentType instrument_type) const noexcept override; @@ -47,15 +53,17 @@ class PeriodicExportingMetricReader : public MetricReader void DoBackgroundWork(); bool CollectAndExportOnce(); - /* The background worker thread */ - std::thread worker_thread_; - /* Synchronization primitives */ std::atomic is_force_wakeup_background_worker_{false}; std::atomic force_flush_pending_sequence_{0}; std::atomic force_flush_notified_sequence_{0}; std::condition_variable cv_, force_flush_cv_; std::mutex cv_m_, force_flush_m_; + + /* The background worker thread */ + std::shared_ptr worker_thread_instrumentation_; + std::shared_ptr collect_thread_instrumentation_; + std::thread worker_thread_; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h index 8cb439599e..4325edfc34 100644 --- a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h" #include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" #include "opentelemetry/version.h" @@ -20,7 +21,12 @@ class OPENTELEMETRY_EXPORT PeriodicExportingMetricReaderFactory { public: static std::unique_ptr Create(std::unique_ptr exporter, - const PeriodicExportingMetricReaderOptions &option); + const PeriodicExportingMetricReaderOptions &options); + + static std::unique_ptr Create( + std::unique_ptr exporter, + const PeriodicExportingMetricReaderOptions &options, + const PeriodicExportingMetricReaderRuntimeOptions &runtime_options); }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h new file mode 100644 index 0000000000..f816fc05a0 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h @@ -0,0 +1,30 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +/** + * Struct to hold PeriodicExortingMetricReader runtime options. + */ +struct PeriodicExportingMetricReaderRuntimeOptions +{ + std::shared_ptr periodic_thread_instrumentation = + std::shared_ptr(nullptr); + std::shared_ptr collect_thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h b/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h index e069a461e6..3a78d6f33c 100644 --- a/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h +++ b/sdk/include/opentelemetry/sdk/trace/batch_span_processor.h @@ -14,6 +14,7 @@ #include "opentelemetry/sdk/common/circular_buffer.h" #include "opentelemetry/sdk/trace/batch_span_processor_options.h" +#include "opentelemetry/sdk/trace/batch_span_processor_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/recordable.h" @@ -37,12 +38,24 @@ class BatchSpanProcessor : public SpanProcessor * Creates a batch span processor by configuring the specified exporter and other parameters * as per the official, language-agnostic opentelemetry specs. * - * @param exporter - The backend exporter to pass the ended spans to. - * @param options - The batch SpanProcessor options. + * @param exporter The backend exporter to pass the ended spans to. + * @param options The batch SpanProcessor configuration options. */ BatchSpanProcessor(std::unique_ptr &&exporter, const BatchSpanProcessorOptions &options); + /** + * Creates a batch span processor by configuring the specified exporter and other parameters + * as per the official, language-agnostic opentelemetry specs. + * + * @param exporter The backend exporter to pass the ended spans to. + * @param options The batch SpanProcessor configuration options. + * @param runtime_options The batch SpanProcessor runtime options. + */ + BatchSpanProcessor(std::unique_ptr &&exporter, + const BatchSpanProcessorOptions &options, + const BatchSpanProcessorRuntimeOptions &runtime_options); + /** * Requests a Recordable(Span) from the configured exporter. * @@ -158,6 +171,7 @@ class BatchSpanProcessor : public SpanProcessor std::shared_ptr synchronization_data_; /* The background worker thread */ + std::shared_ptr worker_thread_instrumentation_; std::thread worker_thread_; }; diff --git a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_factory.h b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_factory.h index bfec277b1d..4ceddc6ae3 100644 --- a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_factory.h +++ b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_factory.h @@ -6,6 +6,7 @@ #include #include "opentelemetry/sdk/trace/batch_span_processor_options.h" +#include "opentelemetry/sdk/trace/batch_span_processor_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/version.h" @@ -27,6 +28,14 @@ class OPENTELEMETRY_EXPORT BatchSpanProcessorFactory */ static std::unique_ptr Create(std::unique_ptr &&exporter, const BatchSpanProcessorOptions &options); + + /** + * Create a BatchSpanProcessor. + */ + static std::unique_ptr Create( + std::unique_ptr &&exporter, + const BatchSpanProcessorOptions &options, + const BatchSpanProcessorRuntimeOptions &runtime_options); }; } // namespace trace diff --git a/sdk/include/opentelemetry/sdk/trace/batch_span_processor_runtime_options.h b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_runtime_options.h new file mode 100644 index 0000000000..25674649e7 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/trace/batch_span_processor_runtime_options.h @@ -0,0 +1,29 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/sdk/common/thread_instrumentation.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ + +namespace trace +{ + +/** + * Struct to hold batch SpanProcessor runtime options. + */ +struct BatchSpanProcessorRuntimeOptions +{ + std::shared_ptr thread_instrumentation = + std::shared_ptr(nullptr); +}; + +} // namespace trace +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/common/global_log_handler.cc b/sdk/src/common/global_log_handler.cc index f60527eee8..0883e5dfab 100644 --- a/sdk/src/common/global_log_handler.cc +++ b/sdk/src/common/global_log_handler.cc @@ -13,6 +13,38 @@ namespace common namespace internal_log { +namespace +{ +struct GlobalLogHandlerData +{ + nostd::shared_ptr handler; + LogLevel log_level; + + GlobalLogHandlerData() + : handler(nostd::shared_ptr(new DefaultLogHandler)), log_level(LogLevel::Warning) + {} + ~GlobalLogHandlerData() { is_singleton_destroyed = true; } + + GlobalLogHandlerData(const GlobalLogHandlerData &) = delete; + GlobalLogHandlerData(GlobalLogHandlerData &&) = delete; + + GlobalLogHandlerData &operator=(const GlobalLogHandlerData &) = delete; + GlobalLogHandlerData &operator=(GlobalLogHandlerData &&) = delete; + + static GlobalLogHandlerData &Instance() noexcept; + static bool is_singleton_destroyed; +}; + +bool GlobalLogHandlerData::is_singleton_destroyed = false; + +GlobalLogHandlerData &GlobalLogHandlerData::Instance() noexcept +{ + static GlobalLogHandlerData instance; + return instance; +} + +} // namespace + LogHandler::~LogHandler() {} void DefaultLogHandler::Handle(LogLevel level, @@ -57,11 +89,43 @@ void NoopLogHandler::Handle(LogLevel, const sdk::common::AttributeMap &) noexcept {} -std::pair, LogLevel> &GlobalLogHandler::GetHandlerAndLevel() noexcept +nostd::shared_ptr GlobalLogHandler::GetLogHandler() noexcept +{ + if OPENTELEMETRY_UNLIKELY_CONDITION (GlobalLogHandlerData::is_singleton_destroyed) + { + return nostd::shared_ptr(); + } + + return GlobalLogHandlerData::Instance().handler; +} + +void GlobalLogHandler::SetLogHandler(const nostd::shared_ptr &eh) noexcept +{ + if OPENTELEMETRY_UNLIKELY_CONDITION (GlobalLogHandlerData::is_singleton_destroyed) + { + return; + } + + GlobalLogHandlerData::Instance().handler = eh; +} + +LogLevel GlobalLogHandler::GetLogLevel() noexcept { - static std::pair, LogLevel> handler_and_level{ - nostd::shared_ptr(new DefaultLogHandler), LogLevel::Warning}; - return handler_and_level; + if OPENTELEMETRY_UNLIKELY_CONDITION (GlobalLogHandlerData::is_singleton_destroyed) + { + return LogLevel::None; + } + + return GlobalLogHandlerData::Instance().log_level; +} + +void GlobalLogHandler::SetLogLevel(LogLevel level) noexcept +{ + if OPENTELEMETRY_UNLIKELY_CONDITION (GlobalLogHandlerData::is_singleton_destroyed) + { + return; + } + GlobalLogHandlerData::Instance().log_level = level; } } // namespace internal_log diff --git a/sdk/src/logs/batch_log_record_processor.cc b/sdk/src/logs/batch_log_record_processor.cc index 2ff8c26971..2080136303 100644 --- a/sdk/src/logs/batch_log_record_processor.cc +++ b/sdk/src/logs/batch_log_record_processor.cc @@ -21,6 +21,7 @@ #include "opentelemetry/sdk/common/circular_buffer_range.h" #include "opentelemetry/sdk/logs/batch_log_record_processor.h" #include "opentelemetry/sdk/logs/batch_log_record_processor_options.h" +#include "opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/sdk/logs/recordable.h" #include "opentelemetry/version.h" @@ -44,8 +45,12 @@ BatchLogRecordProcessor::BatchLogRecordProcessor( max_export_batch_size_(max_export_batch_size), buffer_(max_queue_size_), synchronization_data_(std::make_shared()), - worker_thread_(&BatchLogRecordProcessor::DoBackgroundWork, this) -{} + worker_thread_instrumentation_(nullptr), + worker_thread_() +{ + // Make sure the constructor is complete before giving 'this' to a thread. + worker_thread_ = std::thread(&BatchLogRecordProcessor::DoBackgroundWork, this); +} BatchLogRecordProcessor::BatchLogRecordProcessor(std::unique_ptr &&exporter, const BatchLogRecordProcessorOptions &options) @@ -55,8 +60,29 @@ BatchLogRecordProcessor::BatchLogRecordProcessor(std::unique_ptr()), - worker_thread_(&BatchLogRecordProcessor::DoBackgroundWork, this) -{} + worker_thread_instrumentation_(nullptr), + worker_thread_() +{ + // Make sure the constructor is complete before giving 'this' to a thread. + worker_thread_ = std::thread(&BatchLogRecordProcessor::DoBackgroundWork, this); +} + +BatchLogRecordProcessor::BatchLogRecordProcessor( + std::unique_ptr &&exporter, + const BatchLogRecordProcessorOptions &options, + const BatchLogRecordProcessorRuntimeOptions &runtime_options) + : exporter_(std::move(exporter)), + max_queue_size_(options.max_queue_size), + scheduled_delay_millis_(options.schedule_delay_millis), + max_export_batch_size_(options.max_export_batch_size), + buffer_(options.max_queue_size), + synchronization_data_(std::make_shared()), + worker_thread_instrumentation_(runtime_options.thread_instrumentation), + worker_thread_() +{ + // Make sure the constructor is complete before giving 'this' to a thread. + worker_thread_ = std::thread(&BatchLogRecordProcessor::DoBackgroundWork, this); +} std::unique_ptr BatchLogRecordProcessor::MakeRecordable() noexcept { @@ -151,10 +177,24 @@ bool BatchLogRecordProcessor::ForceFlush(std::chrono::microseconds timeout) noex void BatchLogRecordProcessor::DoBackgroundWork() { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->OnStart(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + auto timeout = scheduled_delay_millis_; while (true) { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->BeforeWait(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + // Wait for `timeout` milliseconds std::unique_lock lk(synchronization_data_->cv_m); synchronization_data_->cv.wait_for(lk, timeout, [this] { @@ -168,6 +208,13 @@ void BatchLogRecordProcessor::DoBackgroundWork() synchronization_data_->is_force_wakeup_background_worker.store(false, std::memory_order_release); +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->AfterWait(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + if (synchronization_data_->is_shutdown.load() == true) { DrainQueue(); @@ -182,10 +229,24 @@ void BatchLogRecordProcessor::DoBackgroundWork() // Subtract the duration of this export call from the next `timeout`. timeout = scheduled_delay_millis_ - duration; } + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->OnEnd(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } void BatchLogRecordProcessor::Export() { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->BeforeLoad(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + do { std::vector> records_arr; @@ -224,6 +285,13 @@ void BatchLogRecordProcessor::Export() nostd::span>(records_arr.data(), records_arr.size())); NotifyCompletion(notify_force_flush, exporter_, synchronization_data_); } while (true); + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->AfterLoad(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } void BatchLogRecordProcessor::NotifyCompletion( diff --git a/sdk/src/logs/batch_log_record_processor_factory.cc b/sdk/src/logs/batch_log_record_processor_factory.cc index a6e29e4967..b76f7b20d0 100644 --- a/sdk/src/logs/batch_log_record_processor_factory.cc +++ b/sdk/src/logs/batch_log_record_processor_factory.cc @@ -7,6 +7,7 @@ #include "opentelemetry/sdk/logs/batch_log_record_processor.h" #include "opentelemetry/sdk/logs/batch_log_record_processor_factory.h" #include "opentelemetry/sdk/logs/batch_log_record_processor_options.h" +#include "opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/sdk/logs/processor.h" #include "opentelemetry/version.h" @@ -20,9 +21,18 @@ namespace logs std::unique_ptr BatchLogRecordProcessorFactory::Create( std::unique_ptr &&exporter, const BatchLogRecordProcessorOptions &options) +{ + BatchLogRecordProcessorRuntimeOptions runtime_options; + return Create(std::move(exporter), options, runtime_options); +} + +std::unique_ptr BatchLogRecordProcessorFactory::Create( + std::unique_ptr &&exporter, + const BatchLogRecordProcessorOptions &options, + const BatchLogRecordProcessorRuntimeOptions &runtime_options) { std::unique_ptr processor( - new BatchLogRecordProcessor(std::move(exporter), options)); + new BatchLogRecordProcessor(std::move(exporter), options, runtime_options)); return processor; } diff --git a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc index 4b0a676961..f9970312e4 100644 --- a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc @@ -19,6 +19,7 @@ #include "opentelemetry/sdk/metrics/export/metric_producer.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h" #include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" #include "opentelemetry/version.h" @@ -42,10 +43,32 @@ namespace metrics PeriodicExportingMetricReader::PeriodicExportingMetricReader( std::unique_ptr exporter, - const PeriodicExportingMetricReaderOptions &option) + const PeriodicExportingMetricReaderOptions &options) : exporter_{std::move(exporter)}, - export_interval_millis_{option.export_interval_millis}, - export_timeout_millis_{option.export_timeout_millis} + export_interval_millis_{options.export_interval_millis}, + export_timeout_millis_{options.export_timeout_millis}, + worker_thread_instrumentation_(nullptr), + collect_thread_instrumentation_(nullptr) +{ + if (export_interval_millis_ <= export_timeout_millis_) + { + OTEL_INTERNAL_LOG_WARN( + "[Periodic Exporting Metric Reader] Invalid configuration: " + "export_timeout_millis_ should be less than export_interval_millis_, using default values"); + export_interval_millis_ = kExportIntervalMillis; + export_timeout_millis_ = kExportTimeOutMillis; + } +} + +PeriodicExportingMetricReader::PeriodicExportingMetricReader( + std::unique_ptr exporter, + const PeriodicExportingMetricReaderOptions &options, + const PeriodicExportingMetricReaderRuntimeOptions &runtime_options) + : exporter_{std::move(exporter)}, + export_interval_millis_{options.export_interval_millis}, + export_timeout_millis_{options.export_timeout_millis}, + worker_thread_instrumentation_(runtime_options.periodic_thread_instrumentation), + collect_thread_instrumentation_(runtime_options.collect_thread_instrumentation) { if (export_interval_millis_ <= export_timeout_millis_) { @@ -69,18 +92,50 @@ void PeriodicExportingMetricReader::OnInitialized() noexcept void PeriodicExportingMetricReader::DoBackgroundWork() { - std::unique_lock lk(cv_m_); +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->OnStart(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + do { - auto start = std::chrono::steady_clock::now(); + auto start = std::chrono::steady_clock::now(); + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->BeforeLoad(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + auto status = CollectAndExportOnce(); + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->AfterLoad(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + if (!status) { OTEL_INTERNAL_LOG_ERROR("[Periodic Exporting Metric Reader] Collect-Export Cycle Failure.") } + auto end = std::chrono::steady_clock::now(); auto export_time_ms = std::chrono::duration_cast(end - start); auto remaining_wait_interval_ms = export_interval_millis_ - export_time_ms; + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->BeforeWait(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + + std::unique_lock lk(cv_m_); cv_.wait_for(lk, remaining_wait_interval_ms, [this]() { if (is_force_wakeup_background_worker_.load(std::memory_order_acquire)) { @@ -89,7 +144,22 @@ void PeriodicExportingMetricReader::DoBackgroundWork() } return IsShutdown(); }); + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->AfterWait(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + } while (IsShutdown() != true); + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->OnEnd(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } bool PeriodicExportingMetricReader::CollectAndExportOnce() @@ -108,6 +178,14 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce() task_thread.reset( new std::thread([this, &cancel_export_for_timeout, sender = std::move(sender)] { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (collect_thread_instrumentation_ != nullptr) + { + collect_thread_instrumentation_->OnStart(); + collect_thread_instrumentation_->BeforeLoad(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + this->Collect([this, &cancel_export_for_timeout](ResourceMetrics &metric_data) { if (cancel_export_for_timeout.load(std::memory_order_acquire)) { @@ -121,6 +199,14 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce() }); const_cast &>(sender).set_value(); + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (collect_thread_instrumentation_ != nullptr) + { + collect_thread_instrumentation_->AfterLoad(); + collect_thread_instrumentation_->OnEnd(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ })); std::future_status status; diff --git a/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc b/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc index 6c85caaa2a..b1542d4209 100644 --- a/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader_factory.cc @@ -7,6 +7,7 @@ #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h" #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h" +#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_runtime_options.h" #include "opentelemetry/sdk/metrics/metric_reader.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" #include "opentelemetry/version.h" @@ -19,10 +20,19 @@ namespace metrics std::unique_ptr PeriodicExportingMetricReaderFactory::Create( std::unique_ptr exporter, - const PeriodicExportingMetricReaderOptions &option) + const PeriodicExportingMetricReaderOptions &options) +{ + PeriodicExportingMetricReaderRuntimeOptions runtime_options; + return Create(std::move(exporter), options, runtime_options); +} + +std::unique_ptr PeriodicExportingMetricReaderFactory::Create( + std::unique_ptr exporter, + const PeriodicExportingMetricReaderOptions &options, + const PeriodicExportingMetricReaderRuntimeOptions &runtime_options) { std::unique_ptr reader( - new PeriodicExportingMetricReader(std::move(exporter), option)); + new PeriodicExportingMetricReader(std::move(exporter), options, runtime_options)); return reader; } diff --git a/sdk/src/trace/batch_span_processor.cc b/sdk/src/trace/batch_span_processor.cc index 0d608404ba..9a16724e4d 100644 --- a/sdk/src/trace/batch_span_processor.cc +++ b/sdk/src/trace/batch_span_processor.cc @@ -22,6 +22,7 @@ #include "opentelemetry/sdk/common/global_log_handler.h" #include "opentelemetry/sdk/trace/batch_span_processor.h" #include "opentelemetry/sdk/trace/batch_span_processor_options.h" +#include "opentelemetry/sdk/trace/batch_span_processor_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/sdk/trace/recordable.h" @@ -45,8 +46,28 @@ BatchSpanProcessor::BatchSpanProcessor(std::unique_ptr &&exporter, max_export_batch_size_(options.max_export_batch_size), buffer_(max_queue_size_), synchronization_data_(std::make_shared()), - worker_thread_(&BatchSpanProcessor::DoBackgroundWork, this) -{} + worker_thread_instrumentation_(nullptr), + worker_thread_() +{ + // Make sure the constructor is complete before giving 'this' to a thread. + worker_thread_ = std::thread(&BatchSpanProcessor::DoBackgroundWork, this); +} + +BatchSpanProcessor::BatchSpanProcessor(std::unique_ptr &&exporter, + const BatchSpanProcessorOptions &options, + const BatchSpanProcessorRuntimeOptions &runtime_options) + : exporter_(std::move(exporter)), + max_queue_size_(options.max_queue_size), + schedule_delay_millis_(options.schedule_delay_millis), + max_export_batch_size_(options.max_export_batch_size), + buffer_(max_queue_size_), + synchronization_data_(std::make_shared()), + worker_thread_instrumentation_(runtime_options.thread_instrumentation), + worker_thread_() +{ + // Make sure the constructor is complete before giving 'this' to a thread. + worker_thread_ = std::thread(&BatchSpanProcessor::DoBackgroundWork, this); +} std::unique_ptr BatchSpanProcessor::MakeRecordable() noexcept { @@ -148,10 +169,24 @@ bool BatchSpanProcessor::ForceFlush(std::chrono::microseconds timeout) noexcept void BatchSpanProcessor::DoBackgroundWork() { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->OnStart(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + auto timeout = schedule_delay_millis_; while (true) { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->BeforeWait(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + // Wait for `timeout` milliseconds std::unique_lock lk(synchronization_data_->cv_m); synchronization_data_->cv.wait_for(lk, timeout, [this] { @@ -165,10 +200,17 @@ void BatchSpanProcessor::DoBackgroundWork() synchronization_data_->is_force_wakeup_background_worker.store(false, std::memory_order_release); +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->AfterWait(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + if (synchronization_data_->is_shutdown.load() == true) { DrainQueue(); - return; + break; } auto start = std::chrono::steady_clock::now(); @@ -179,10 +221,24 @@ void BatchSpanProcessor::DoBackgroundWork() // Subtract the duration of this export call from the next `timeout`. timeout = schedule_delay_millis_ - duration; } + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->OnEnd(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } void BatchSpanProcessor::Export() { +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->BeforeLoad(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ + do { std::vector> spans_arr; @@ -221,6 +277,13 @@ void BatchSpanProcessor::Export() exporter_->Export(nostd::span>(spans_arr.data(), spans_arr.size())); NotifyCompletion(notify_force_flush, exporter_, synchronization_data_); } while (true); + +#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW + if (worker_thread_instrumentation_ != nullptr) + { + worker_thread_instrumentation_->AfterLoad(); + } +#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */ } void BatchSpanProcessor::NotifyCompletion( diff --git a/sdk/src/trace/batch_span_processor_factory.cc b/sdk/src/trace/batch_span_processor_factory.cc index a21b056cee..caaafbacb7 100644 --- a/sdk/src/trace/batch_span_processor_factory.cc +++ b/sdk/src/trace/batch_span_processor_factory.cc @@ -7,6 +7,7 @@ #include "opentelemetry/sdk/trace/batch_span_processor.h" #include "opentelemetry/sdk/trace/batch_span_processor_factory.h" #include "opentelemetry/sdk/trace/batch_span_processor_options.h" +#include "opentelemetry/sdk/trace/batch_span_processor_runtime_options.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/sdk/trace/processor.h" #include "opentelemetry/version.h" @@ -16,11 +17,22 @@ namespace sdk { namespace trace { + std::unique_ptr BatchSpanProcessorFactory::Create( std::unique_ptr &&exporter, const BatchSpanProcessorOptions &options) { - std::unique_ptr processor(new BatchSpanProcessor(std::move(exporter), options)); + BatchSpanProcessorRuntimeOptions runtime_options; + return Create(std::move(exporter), options, runtime_options); +} + +std::unique_ptr BatchSpanProcessorFactory::Create( + std::unique_ptr &&exporter, + const BatchSpanProcessorOptions &options, + const BatchSpanProcessorRuntimeOptions &runtime_options) +{ + std::unique_ptr processor( + new BatchSpanProcessor(std::move(exporter), options, runtime_options)); return processor; } diff --git a/sdk/test/common/BUILD b/sdk/test/common/BUILD index b08bcc0976..de4c02ec56 100644 --- a/sdk/test/common/BUILD +++ b/sdk/test/common/BUILD @@ -160,6 +160,20 @@ cc_test( ], ) +cc_test( + name = "global_log_handle_singleton_lifetime_test", + srcs = [ + "global_log_handle_singleton_lifetime_test.cc", + ], + tags = ["test"], + deps = [ + "//api", + "//sdk:headers", + "//sdk/src/common:global_log_handler", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "attributemap_hash_test", srcs = [ diff --git a/sdk/test/common/CMakeLists.txt b/sdk/test/common/CMakeLists.txt index 00ad67a5bf..fab78de90e 100644 --- a/sdk/test/common/CMakeLists.txt +++ b/sdk/test/common/CMakeLists.txt @@ -11,6 +11,7 @@ foreach( attribute_utils_test attributemap_hash_test global_log_handle_test + global_log_handle_singleton_lifetime_test env_var_test) add_executable(${testname} "${testname}.cc") diff --git a/sdk/test/common/global_log_handle_singleton_lifetime_test.cc b/sdk/test/common/global_log_handle_singleton_lifetime_test.cc new file mode 100644 index 0000000000..5b163dc6b9 --- /dev/null +++ b/sdk/test/common/global_log_handle_singleton_lifetime_test.cc @@ -0,0 +1,107 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include +#include +#include +#include + +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/sdk/common/attribute_utils.h" +#include "opentelemetry/sdk/common/global_log_handler.h" + +class GlobalLogHandlerChecker +{ +public: + GlobalLogHandlerChecker() {} + ~GlobalLogHandlerChecker() + { + using opentelemetry::sdk::common::internal_log::GlobalLogHandler; + auto handle = GlobalLogHandler::GetLogHandler(); + if (handle && custom_handler_destroyed) + { + OTEL_INTERNAL_LOG_ERROR("GlobalLogHandler should be destroyed here"); + abort(); + } + std::cout << "GlobalLogHandlerChecker destroyed and check pass.\n"; + } + + void Print() { std::cout << "GlobalLogHandlerChecker constructed\n"; } + + GlobalLogHandlerChecker(const GlobalLogHandlerChecker &) = delete; + GlobalLogHandlerChecker(GlobalLogHandlerChecker &&) = delete; + GlobalLogHandlerChecker &operator=(const GlobalLogHandlerChecker &) = delete; + GlobalLogHandlerChecker &operator=(GlobalLogHandlerChecker &&) = delete; + + static bool custom_handler_destroyed; +}; +bool GlobalLogHandlerChecker::custom_handler_destroyed = false; + +namespace +{ +static GlobalLogHandlerChecker &ConstructChecker() +{ + static GlobalLogHandlerChecker checker; + return checker; +} +} // namespace + +class CustomLogHandler : public opentelemetry::sdk::common::internal_log::LogHandler +{ +public: + ~CustomLogHandler() override + { + GlobalLogHandlerChecker::custom_handler_destroyed = true; + std::cout << "Custom Gobal Log Handle destroyed\n"; + } + + void Handle(opentelemetry::sdk::common::internal_log::LogLevel level, + const char *, + int, + const char *msg, + const opentelemetry::sdk::common::AttributeMap &) noexcept override + { + if (level == opentelemetry::sdk::common::internal_log::LogLevel::Debug) + { + std::cout << "Custom Gobal Log Handle[Debug]: " << msg << "\n"; + } + else if (level == opentelemetry::sdk::common::internal_log::LogLevel::Error) + { + std::cout << "Custom Gobal Log Handle[Error]: " << msg << "\n"; + } + else if (level == opentelemetry::sdk::common::internal_log::LogLevel::Info) + { + std::cout << "Custom Gobal Log Handle[Info]: " << msg << "\n"; + } + else if (level == opentelemetry::sdk::common::internal_log::LogLevel::Warning) + { + std::cout << "Custom Gobal Log Handle[Warning]: " << msg << "\n"; + } + ++count; + } + + size_t count = 0; +}; + +TEST(GlobalLogHandleSingletonTest, Lifetime) +{ + // Setup a new static variable which will be destroyed after the global handle + ConstructChecker().Print(); + + using opentelemetry::sdk::common::internal_log::GlobalLogHandler; + using opentelemetry::sdk::common::internal_log::LogHandler; + + auto custom_log_handler = opentelemetry::nostd::shared_ptr(new CustomLogHandler{}); + opentelemetry::sdk::common::internal_log::GlobalLogHandler::SetLogHandler(custom_log_handler); + auto before_count = static_cast(custom_log_handler.get())->count; + opentelemetry::sdk::common::AttributeMap attributes = { + {"url", "https://opentelemetry.io/"}, {"content-length", 0}, {"content-type", "text/html"}}; + OTEL_INTERNAL_LOG_ERROR("Error message"); + OTEL_INTERNAL_LOG_DEBUG("Debug message. Headers:", attributes); + EXPECT_EQ(before_count + 1, static_cast(custom_log_handler.get())->count); + + { + auto handle = GlobalLogHandler::GetLogHandler(); + EXPECT_TRUE(!!handle); + } +}