Skip to content

Commit 5728dc1

Browse files
committed
Integrate examples CMakeLists.txt with the main CMakeList.txt
1 parent 220927e commit 5728dc1

File tree

13 files changed

+72
-69
lines changed

13 files changed

+72
-69
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Build OpenTelemetry-Matlab
1919
run: |
2020
cd opentelemetry-matlab
21-
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${{ env.OPENTELEMETRY_MATLAB_INSTALL }}
21+
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DWITH_EXAMPLES=ON -DCMAKE_INSTALL_PREFIX=${{ env.OPENTELEMETRY_MATLAB_INSTALL }}
2222
cmake --build build --config Release --target install
2323
- name: Run tests
2424
env:

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# build directories
22
build
3-
examples/webread/build
4-
examples/context_propagation/build
53

64
# Autosave files
75
*.asv

CMakeLists.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ else()
5656
endif()
5757

5858
if(NOT DEFINED VCPKG_INSTALLED_DIR)
59-
set(DVCPKG_INSTALLED_DIR ${CMAKE_BINARY_DIR}/vcpkg_installed)
59+
set(VCPKG_INSTALLED_DIR ${CMAKE_BINARY_DIR}/vcpkg_installed)
6060
endif()
6161

6262
# ######################################
@@ -71,6 +71,7 @@ endif()
7171
if(APPLE)
7272
option(SKIP_OTEL_CPP_PATCH "Whether to skip patching OpenTelemetry-cpp" OFF)
7373
endif()
74+
option(WITH_EXAMPLES "Whether to build examples" OFF)
7475

7576
# set vcpkg features depending on specified options
7677
set(VCPKG_MANIFEST_FEATURES "") # start with empty
@@ -269,7 +270,7 @@ endif()
269270
target_compile_options(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTLP_MACROS} ${CUSTOM_CXX_FLAGS})
270271

271272
# link against OpenTelemetry-cpp libraries and their dependencies
272-
target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_common${CMAKE_STATIC_LIBRARY_SUFFIX}
273+
set(OTEL_CPP_LINK_LIBRARIES ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_common${CMAKE_STATIC_LIBRARY_SUFFIX}
273274
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_otlp_recordable${CMAKE_STATIC_LIBRARY_SUFFIX}
274275
${OTEL_CPP_PREFIX}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}opentelemetry_proto${OTEL_PROTO_LIBRARY_SUFFIX}
275276
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_resources${CMAKE_STATIC_LIBRARY_SUFFIX}
@@ -278,19 +279,21 @@ target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PRE
278279
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_logs${CMAKE_STATIC_LIBRARY_SUFFIX}
279280
${Protobuf_LIBRARIES})
280281
if(WITH_OTLP_HTTP)
281-
target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_http${CMAKE_STATIC_LIBRARY_SUFFIX}
282+
set(OTEL_CPP_LINK_LIBRARIES ${OTEL_CPP_LINK_LIBRARIES} ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_http${CMAKE_STATIC_LIBRARY_SUFFIX}
282283
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_http_client${CMAKE_STATIC_LIBRARY_SUFFIX}
283284
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_http_client_curl${CMAKE_STATIC_LIBRARY_SUFFIX}
284285
${CURL_LIBRARIES})
285286
endif()
286287
if(WITH_OTLP_GRPC)
287-
target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_grpc${CMAKE_STATIC_LIBRARY_SUFFIX}
288+
set(OTEL_CPP_LINK_LIBRARIES ${OTEL_CPP_LINK_LIBRARIES} ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_grpc${CMAKE_STATIC_LIBRARY_SUFFIX}
288289
${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_grpc_client${CMAKE_STATIC_LIBRARY_SUFFIX}
289290
${OTEL_CPP_PREFIX}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}opentelemetry_proto_grpc${OTEL_PROTO_LIBRARY_SUFFIX}
290291
gRPC::grpc++
291292
absl::synchronization)
292293
endif()
293294

295+
target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_LINK_LIBRARIES})
296+
294297
# On Linux, when linking with certain static libraries, need to force include entire archive to avoid the linker mistakenly leaving out symbols
295298
if(UNIX AND NOT APPLE AND NOT CYGWIN)
296299
set(OPENTELEMETRY_PROXY_LINK_OPTIONS -Wl,--whole-archive
@@ -343,6 +346,7 @@ elseif(UNIX AND NOT CYGWIN)
343346
set(OPENTELEMETRY_PROXY_RUNTIME_LIBRARIES ${OTEL_CPP_RUNTIME})
344347
endif()
345348

349+
346350
# ##############################
347351
# OpenTelemetry MEX Gateway
348352
# ##############################
@@ -395,3 +399,10 @@ endif()
395399

396400
# Install dependent runtime libraries
397401
install(FILES ${OPENTELEMETRY_PROXY_RUNTIME_LIBRARIES} DESTINATION +libmexclass/+proxy)
402+
403+
# ##############################
404+
# Subdirectories
405+
# ##############################
406+
if(WITH_EXAMPLES)
407+
add_subdirectory(examples)
408+
endif()

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Requires MATLAB release R2022b or newer
1212
- [MATLAB](https://www.mathworks.com/products/matlab.html)
1313

1414
### 3rd Party Products:
15-
- [Opentelemetry C++](https://github.com/open-telemetry/opentelemetry-cpp)
15+
- [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector-releases/releases)
16+
- [OpenTelemetry C++](https://github.com/open-telemetry/opentelemetry-cpp)
1617
- [vcpkg C/C++ dependency manager](https://vcpkg.io)
1718

1819
## Installation
@@ -57,6 +58,8 @@ otelcol --config <otelcol-config-yaml>
5758
```
5859
4. If your collector is configured to display the data, you should see your span displayed.
5960

61+
For more examples, see the "examples" folder.
62+
6063
## Help
6164
To view documentation of individual function, type "help \<function_name>\". For example,
6265
```

examples/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_subdirectory(context_propagation)
3+
add_subdirectory(webread)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# C++ target
3+
set(CONTEXTPROP_EXAMPLE_TARGET contextprop_example_client)
4+
add_executable(${CONTEXTPROP_EXAMPLE_TARGET} cpp/client.cc)
5+
6+
target_include_directories(${CONTEXTPROP_EXAMPLE_TARGET} PRIVATE ${OTEL_CPP_PREFIX}/include)
7+
target_link_libraries(${CONTEXTPROP_EXAMPLE_TARGET} PRIVATE ${OTEL_CPP_LINK_LIBRARIES})
8+
9+
# MATLAB target
10+
find_package(Matlab REQUIRED COMPONENTS MCC_COMPILER MAIN_PROGRAM)
11+
12+
set(CONTEXTPROP_EXAMPLE_DEPLOYNAME mymagic)
13+
set(CONTEXTPROP_EXAMPLE_MATLAB_SOURCE ${CMAKE_CURRENT_LIST_DIR}/matlab/${CONTEXTPROP_EXAMPLE_DEPLOYNAME}.m)
14+
set(CONTEXTPROP_EXAMPLE_ROUTES ../../../examples/context_propagation/matlab/routes.json) #somehow, only relative paths are allowed
15+
matlab_get_version_from_matlab_run(${Matlab_MAIN_PROGRAM} Matlab_LIST_VERSION)
16+
if(DEFINED Matlab_LIST_VERSION AND ${Matlab_LIST_VERSION} VERSION_GREATER_EQUAL 23.2.0)
17+
# since MATLAB R2023b, route mapping can be specified at the archive level
18+
set(ARCHIVE_ROUTES ",ROUTES:${CONTEXTPROP_EXAMPLE_ROUTES}")
19+
else()
20+
set(ARCHIVE_ROUTES "")
21+
endif()
22+
install(CODE "execute_process(COMMAND ${Matlab_MCC_COMPILER} -W CTF:${CONTEXTPROP_EXAMPLE_DEPLOYNAME}${ARCHIVE_ROUTES} -U ${CONTEXTPROP_EXAMPLE_MATLAB_SOURCE} -a ${CMAKE_INSTALL_PREFIX} -a ${CMAKE_INSTALL_PREFIX}/+libmexclass/+proxy WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})")

examples/context_propagation/README.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@
33
In this example, a C++ client calls a MATLAB function hosted on MATLAB Production Server that returns a magic square matrix. Both the C++ client and the MATLAB code are instrumented with OpenTelemetry, and their generated spans form a single trace.
44

55
## Building the Example
6-
1. Build the C++ client, which requires an installed opentelemetry-cpp package and a vcpkg library manager
6+
1. Enable WITH_EXAMPLES when building OpenTelemetry-Matlab
77
```
8-
cd cpp
9-
cmake -S . -B build -DCMAKE_PREFIX_PATH=<opentelemetry-cpp_install_root>/lib/cmake/opentelemetry-cpp -DCMAKE_TOOLCHAIN_FILE=<vcpkg_root>/scripts/buildsystems/vcpkg.cmake
10-
cmake --build build --config Release --target <all_target>
8+
cmake -S . -B build -DWITH_EXAMPLES=ON -DCMAKE_INSTALL_PREFIX=<opentelemetry-matlab-installdir>
9+
cmake --build build --config Release
1110
```
12-
where <all_target> is "all" on most platforms but is "ALL_BUILD" on Windows with Visual Studio.
13-
2. Build MATLAB code into a deployable archive. Use the following MATLAB command:
14-
```
15-
cd matlab
16-
mcc -W CTF:mymagic -U mymagic.m -a <opentelemetry-cpp_install_root> -a <opentelemetry-cpp_install_root>\+libmexclass\+proxy
17-
```
18-
3. [Create](https://www.mathworks.com/help/mps/server/creating-a-server.html) and [start](https://www.mathworks.com/help/mps/qs/starting-and-stopping.html) a MATLAB Production Server instance.
19-
4. [Deploy](https://www.mathworks.com/help/mps/qs/share-a-ctf-archive-on-the-server-instance.html) archive to server instance by copying to the auto_deploy directory.
20-
5. [Copy](https://www.mathworks.com/help/mps/server/use-web-handler-for-custom-routes-and-custom-payloads.html) matlab/routes.json to the config directory of the server instance.
11+
The built examples can be found in build/examples/context_propagation and subdirectories.
12+
2. [Create](https://www.mathworks.com/help/mps/server/creating-a-server.html) and [start](https://www.mathworks.com/help/mps/qs/starting-and-stopping.html) a MATLAB Production Server instance.
13+
3. [Deploy](https://www.mathworks.com/help/mps/qs/share-a-ctf-archive-on-the-server-instance.html) archive to server instance by copying to the auto_deploy directory.
14+
4. [Copy](https://www.mathworks.com/help/mps/server/use-web-handler-for-custom-routes-and-custom-payloads.html) matlab/routes.json to the config directory of the server instance.
2115
6. Start an instance of [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector).
2216
7. Start the C++ client.
2317
```

examples/context_propagation/cpp/CMakeLists.txt

Lines changed: 0 additions & 16 deletions
This file was deleted.

examples/context_propagation/cpp/client.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ void sendRequest(const std::string &url)
6969
std::string span_name = url_parser.path_;
7070
auto span = get_tracer("http-client")
7171
->StartSpan(span_name,
72-
{{SemanticConventions::kHttpUrl, url_parser.url_},
73-
{SemanticConventions::kHttpScheme, url_parser.scheme_},
74-
{SemanticConventions::kHttpMethod, "POST"}},
72+
{{SemanticConventions::kUrlFull, url_parser.url_},
73+
{SemanticConventions::kUrlScheme, url_parser.scheme_},
74+
{SemanticConventions::kHttpRequestMethod, "POST"}},
7575
options);
7676
auto scope = get_tracer("http-client")->WithActiveSpan(span);
7777

@@ -87,7 +87,7 @@ void sendRequest(const std::string &url)
8787
{
8888
// set span attributes
8989
auto status_code = result.GetResponse().GetStatusCode();
90-
span->SetAttribute(SemanticConventions::kHttpStatusCode, status_code);
90+
span->SetAttribute(SemanticConventions::kHttpResponseStatusCode, status_code);
9191
result.GetResponse().ForEachHeader(
9292
[&span](nostd::string_view header_name, nostd::string_view header_value) {
9393
span->SetAttribute("http.header." + std::string(header_name.data()), header_value);
@@ -124,7 +124,7 @@ int main(int argc, char *argv[])
124124
{
125125
InitTracer();
126126
constexpr char default_host[] = "localhost";
127-
constexpr char default_path[] = "/magic";
127+
constexpr char default_path[] = "/mymagic/magic";
128128
constexpr uint16_t default_port = 9910;
129129
uint16_t port;
130130

examples/webread/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
add_executable(http_server cpp/server.cc)
3+
4+
target_include_directories(http_server PRIVATE ${OTEL_CPP_PREFIX}/include)
5+
target_link_libraries(http_server PRIVATE ${OTEL_CPP_LINK_LIBRARIES})

0 commit comments

Comments
 (0)