Skip to content

Commit e102d16

Browse files
committed
Add experimental C/C++ implementation for process context OTEP
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.
1 parent 41b470a commit e102d16

File tree

12 files changed

+1693
-0
lines changed

12 files changed

+1693
-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: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
set(SOURCES_CXX otel_process_ctx.cpp)
23+
24+
# Create shared library
25+
add_library(otel_process_ctx SHARED ${SOURCES})
26+
set_target_properties(otel_process_ctx PROPERTIES
27+
OUTPUT_NAME "otel_process_ctx"
28+
)
29+
30+
# Create static library
31+
add_library(otel_process_ctx_static STATIC ${SOURCES})
32+
set_target_properties(otel_process_ctx_static PROPERTIES
33+
OUTPUT_NAME "otel_process_ctx"
34+
)
35+
36+
# Create shared noop library
37+
add_library(otel_process_ctx_noop SHARED ${SOURCES})
38+
set_target_properties(otel_process_ctx_noop PROPERTIES
39+
OUTPUT_NAME "otel_process_ctx_noop"
40+
)
41+
target_compile_definitions(otel_process_ctx_noop PRIVATE OTEL_PROCESS_CTX_NOOP=1)
42+
43+
# Create static noop library
44+
add_library(otel_process_ctx_noop_static STATIC ${SOURCES})
45+
set_target_properties(otel_process_ctx_noop_static PROPERTIES
46+
OUTPUT_NAME "otel_process_ctx_noop"
47+
)
48+
target_compile_definitions(otel_process_ctx_noop_static PRIVATE OTEL_PROCESS_CTX_NOOP=1)
49+
50+
# Create C++ shared library
51+
add_library(otel_process_ctx_cpp SHARED ${SOURCES_CXX})
52+
set_target_properties(otel_process_ctx_cpp PROPERTIES
53+
OUTPUT_NAME "otel_process_ctx_cpp"
54+
CXX_STANDARD 11
55+
CXX_STANDARD_REQUIRED ON
56+
)
57+
58+
# Create C++ static library
59+
add_library(otel_process_ctx_cpp_static STATIC ${SOURCES_CXX})
60+
set_target_properties(otel_process_ctx_cpp_static PROPERTIES
61+
OUTPUT_NAME "otel_process_ctx_cpp"
62+
CXX_STANDARD 11
63+
CXX_STANDARD_REQUIRED ON
64+
)
65+
66+
# Create C++ shared noop library
67+
add_library(otel_process_ctx_cpp_noop SHARED ${SOURCES_CXX})
68+
set_target_properties(otel_process_ctx_cpp_noop PROPERTIES
69+
OUTPUT_NAME "otel_process_ctx_cpp_noop"
70+
CXX_STANDARD 11
71+
CXX_STANDARD_REQUIRED ON
72+
)
73+
target_compile_definitions(otel_process_ctx_cpp_noop PRIVATE OTEL_PROCESS_CTX_NOOP=1)
74+
75+
# Create C++ static noop library
76+
add_library(otel_process_ctx_cpp_noop_static STATIC ${SOURCES_CXX})
77+
set_target_properties(otel_process_ctx_cpp_noop_static PROPERTIES
78+
OUTPUT_NAME "otel_process_ctx_cpp_noop"
79+
CXX_STANDARD 11
80+
CXX_STANDARD_REQUIRED ON
81+
)
82+
target_compile_definitions(otel_process_ctx_cpp_noop_static PRIVATE OTEL_PROCESS_CTX_NOOP=1)
83+
84+
# Set include directories
85+
target_include_directories(otel_process_ctx PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
86+
target_include_directories(otel_process_ctx_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
87+
target_include_directories(otel_process_ctx_noop PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
88+
target_include_directories(otel_process_ctx_noop_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
89+
target_include_directories(otel_process_ctx_cpp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
90+
target_include_directories(otel_process_ctx_cpp_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
91+
target_include_directories(otel_process_ctx_cpp_noop PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
92+
target_include_directories(otel_process_ctx_cpp_noop_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
93+
94+
# Create example executable
95+
add_executable(example_ctx example_ctx.c)
96+
target_link_libraries(example_ctx otel_process_ctx)
97+
98+
# Create example noop executable
99+
add_executable(example_ctx_noop example_ctx.c)
100+
target_link_libraries(example_ctx_noop otel_process_ctx_noop)
101+
102+
# Install rules
103+
install(TARGETS otel_process_ctx otel_process_ctx_static otel_process_ctx_noop otel_process_ctx_noop_static
104+
otel_process_ctx_cpp otel_process_ctx_cpp_static otel_process_ctx_cpp_noop otel_process_ctx_cpp_noop_static
105+
example_ctx example_ctx_noop
106+
LIBRARY DESTINATION lib
107+
ARCHIVE DESTINATION lib
108+
RUNTIME DESTINATION bin
109+
)
110+
install(FILES otel_process_ctx.h DESTINATION include)

0 commit comments

Comments
 (0)