Skip to content

Commit 7fd7125

Browse files
ivoanjochristos68k
andauthored
Add experimental C/C++ implementation for process context OTEP (#23)
This PR adds an experimental C/C++ implementation for the "Process Context" OTEP being proposed in open-telemetry/opentelemetry-specification#4719 This implementation previously lived in https://github.com/ivoanjo/proc-level-demo/tree/main/anonmapping-clib and as discussed during the OTEL profiling SIG meeting we want to add it to this repository so it becomes easier to find and contribute to. I've made sure to include a README explaining how to use it. Here's the ultra-quick start (Linux-only): ```bash $ ./build.sh $ ./build/example_ctx --keep-running Published: service=my-service, instance=123d8444-2c7e-46e3-89f6-6217880f7123, env=prod, version=4.5.6, sdk=example_ctx.c/c/1.2.3, resources=resource.key1=resource.value1,resource.key2=resource.value2 Continuing forever, to exit press ctrl+c... TIP: You can now `sudo ./otel_process_ctx_dump.sh 267023` to see the context # In another shell $ sudo ./otel_process_ctx_dump.sh 267023 # Update this to match the PID from above Found OTEL context for PID 267023 Start address: 756f28ce1000 00000000 4f 54 45 4c 5f 43 54 58 02 00 00 00 0b 68 55 47 |OTEL_CTX.....hUG| 00000010 70 24 7d 18 50 01 00 00 a0 82 6d 7e 6a 5f 00 00 |p$}.P.....m~j_..| 00000020 Parsed struct: otel_process_ctx_signature : "OTEL_CTX" otel_process_ctx_version : 2 otel_process_ctx_published_at_ns : 1764606693650819083 (2025-12-01 16:31:33 GMT) otel_process_payload_size : 336 otel_process_payload : 0x00005f6a7e6d82a0 Payload dump (336 bytes): 00000000 0a 25 0a 1b 64 65 70 6c 6f 79 6d 65 6e 74 2e 65 |.%..deployment.e| 00000010 6e 76 69 72 6f 6e 6d 65 6e 74 2e 6e 61 6d 65 12 |nvironment.name.| ... Protobuf decode: attributes { key: "deployment.environment.name" value { string_value: "prod" } } attributes { key: "service.instance.id" value { string_value: "123d8444-2c7e-46e3-89f6-6217880f7123" } } attributes { key: "service.name" value { string_value: "my-service" } } ... ``` Note that because the upstream OTEP is still under discussion, this implementation is experimental and may need changes to match up with the final version of the OTEP. --------- Co-authored-by: Christos Kalkanis <christos.kalkanis@elastic.co>
1 parent 3395078 commit 7fd7125

File tree

11 files changed

+1681
-0
lines changed

11 files changed

+1681
-0
lines changed

process-context/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# OpenTelemetry Process Context (Under development)
2+
3+
This folder contains experimental implementations for the specification proposed in https://github.com/open-telemetry/opentelemetry-specification/pull/4719 .
4+
5+
The process context OTEP introduces a standard mechanism for OpenTelemetry SDKs to publish process-level resource attributes for access by out-of-process readers such as the OpenTelemetry eBPF Profiler.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(otel_process_ctx C CXX)
3+
4+
# Set C standard
5+
set(CMAKE_C_STANDARD 11)
6+
set(CMAKE_C_STANDARD_REQUIRED ON)
7+
8+
# Set C++ standard for C++ variants
9+
set(CMAKE_CXX_STANDARD 11)
10+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
11+
12+
# Note: We build here with both a C compiler as well as a C++ compiler because this implementation is expected to be
13+
# used with either, and we want to make sure we build cleanly on either (so we don't accidentally add C-isms that C++
14+
# doesn't like, or vice-versa)
15+
16+
# Compiler flags
17+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -O2 -fPIC -ggdb3")
18+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -O2 -fPIC -ggdb3")
19+
20+
# Source files
21+
set(SOURCES otel_process_ctx.c)
22+
configure_file(otel_process_ctx.c otel_process_ctx.cpp COPYONLY)
23+
set(SOURCES_CXX ${CMAKE_CURRENT_BINARY_DIR}/otel_process_ctx.cpp)
24+
25+
# Create shared library
26+
add_library(otel_process_ctx SHARED ${SOURCES})
27+
set_target_properties(otel_process_ctx PROPERTIES
28+
OUTPUT_NAME "otel_process_ctx"
29+
)
30+
31+
# Create static library
32+
add_library(otel_process_ctx_static STATIC ${SOURCES})
33+
set_target_properties(otel_process_ctx_static PROPERTIES
34+
OUTPUT_NAME "otel_process_ctx"
35+
)
36+
37+
# Create shared noop library
38+
add_library(otel_process_ctx_noop SHARED ${SOURCES})
39+
set_target_properties(otel_process_ctx_noop PROPERTIES
40+
OUTPUT_NAME "otel_process_ctx_noop"
41+
)
42+
target_compile_definitions(otel_process_ctx_noop PRIVATE OTEL_PROCESS_CTX_NOOP=1)
43+
44+
# Create static noop library
45+
add_library(otel_process_ctx_noop_static STATIC ${SOURCES})
46+
set_target_properties(otel_process_ctx_noop_static PROPERTIES
47+
OUTPUT_NAME "otel_process_ctx_noop"
48+
)
49+
target_compile_definitions(otel_process_ctx_noop_static PRIVATE OTEL_PROCESS_CTX_NOOP=1)
50+
51+
# Create C++ shared library
52+
add_library(otel_process_ctx_cpp SHARED ${SOURCES_CXX})
53+
set_target_properties(otel_process_ctx_cpp PROPERTIES
54+
OUTPUT_NAME "otel_process_ctx_cpp"
55+
CXX_STANDARD 11
56+
CXX_STANDARD_REQUIRED ON
57+
)
58+
59+
# Create C++ static library
60+
add_library(otel_process_ctx_cpp_static STATIC ${SOURCES_CXX})
61+
set_target_properties(otel_process_ctx_cpp_static PROPERTIES
62+
OUTPUT_NAME "otel_process_ctx_cpp"
63+
CXX_STANDARD 11
64+
CXX_STANDARD_REQUIRED ON
65+
)
66+
67+
# Create C++ shared noop library
68+
add_library(otel_process_ctx_cpp_noop SHARED ${SOURCES_CXX})
69+
set_target_properties(otel_process_ctx_cpp_noop PROPERTIES
70+
OUTPUT_NAME "otel_process_ctx_cpp_noop"
71+
CXX_STANDARD 11
72+
CXX_STANDARD_REQUIRED ON
73+
)
74+
target_compile_definitions(otel_process_ctx_cpp_noop PRIVATE OTEL_PROCESS_CTX_NOOP=1)
75+
76+
# Create C++ static noop library
77+
add_library(otel_process_ctx_cpp_noop_static STATIC ${SOURCES_CXX})
78+
set_target_properties(otel_process_ctx_cpp_noop_static PROPERTIES
79+
OUTPUT_NAME "otel_process_ctx_cpp_noop"
80+
CXX_STANDARD 11
81+
CXX_STANDARD_REQUIRED ON
82+
)
83+
target_compile_definitions(otel_process_ctx_cpp_noop_static PRIVATE OTEL_PROCESS_CTX_NOOP=1)
84+
85+
# Set include directories
86+
target_include_directories(otel_process_ctx PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
87+
target_include_directories(otel_process_ctx_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
88+
target_include_directories(otel_process_ctx_noop PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
89+
target_include_directories(otel_process_ctx_noop_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
90+
target_include_directories(otel_process_ctx_cpp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
91+
target_include_directories(otel_process_ctx_cpp_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
92+
target_include_directories(otel_process_ctx_cpp_noop PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
93+
target_include_directories(otel_process_ctx_cpp_noop_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
94+
95+
# Create example executable
96+
add_executable(example_ctx example_ctx.c)
97+
target_link_libraries(example_ctx otel_process_ctx)
98+
99+
# Create example noop executable
100+
add_executable(example_ctx_noop example_ctx.c)
101+
target_link_libraries(example_ctx_noop otel_process_ctx_noop)
102+
103+
# Install rules
104+
install(TARGETS otel_process_ctx otel_process_ctx_static otel_process_ctx_noop otel_process_ctx_noop_static
105+
otel_process_ctx_cpp otel_process_ctx_cpp_static otel_process_ctx_cpp_noop otel_process_ctx_cpp_noop_static
106+
example_ctx example_ctx_noop
107+
LIBRARY DESTINATION lib
108+
ARCHIVE DESTINATION lib
109+
RUNTIME DESTINATION bin
110+
)
111+
install(FILES otel_process_ctx.h DESTINATION include)

0 commit comments

Comments
 (0)