diff --git a/ddprof-lib/src/main/cpp/javaApi.cpp b/ddprof-lib/src/main/cpp/javaApi.cpp index 50009534c..6033255de 100644 --- a/ddprof-lib/src/main/cpp/javaApi.cpp +++ b/ddprof-lib/src/main/cpp/javaApi.cpp @@ -111,7 +111,7 @@ Java_com_datadoghq_profiler_JavaProfiler_execute0(JNIEnv *env, jobject unused, } extern "C" DLLEXPORT jstring JNICALL -Java_com_datadoghq_profiler_JavaProfiler_getStatus0(JNIEnv* env, +Java_com_datadoghq_profiler_JavaProfiler_getStatus0(JNIEnv* env, jobject unused) { char msg[2048]; int ret = Profiler::instance()->status((char*)msg, sizeof(msg) - 1); @@ -435,77 +435,83 @@ static otel_process_ctx_result* current_published_context = nullptr; extern "C" DLLEXPORT void JNICALL Java_com_datadoghq_profiler_OTelContext_setProcessCtx0(JNIEnv *env, jclass unused, - jstring serviceName, - jstring serviceId, - jstring environment) { - JniString service_name_str(env, serviceName); - JniString service_id_str(env, serviceId); - JniString environment_str(env, environment); + jstring env_data, + jstring hostname, + jstring runtime_id, + jstring service, + jstring version, + jstring tracer_version + ) { + JniString env_str(env, env_data); + JniString hostname_str(env, hostname); + JniString runtime_id_str(env, runtime_id); + JniString service_str(env, service); + JniString version_str(env, version); + JniString tracer_version_str(env, tracer_version); + otel_process_ctx_data data = { - const_cast(service_name_str.c_str()), - const_cast(service_id_str.c_str()), - const_cast(environment_str.c_str()) + .deployment_environment_name = const_cast(env_str.c_str()), + .host_name = const_cast(hostname_str.c_str()), + .service_instance_id = const_cast(runtime_id_str.c_str()), + .service_name = const_cast(service_str.c_str()), + .service_version = const_cast(version_str.c_str()), + .telemetry_sdk_language = const_cast("java"), + .telemetry_sdk_version = const_cast(tracer_version_str.c_str()), + .telemetry_sdk_name = const_cast("dd-trace-java"), + .resources = NULL // TODO: Arbitrary tags not supported yet for Java }; - - if (current_published_context == nullptr) { - // First time publishing - use publish - current_published_context = new otel_process_ctx_result(); - *current_published_context = otel_process_ctx_publish(data); - } else { - // Already have a published context - use update - otel_process_ctx_result new_result = otel_process_ctx_update(current_published_context, data); - *current_published_context = new_result; - } + + otel_process_ctx_result result = otel_process_ctx_publish(&data); } extern "C" DLLEXPORT jobject JNICALL Java_com_datadoghq_profiler_OTelContext_readProcessCtx0(JNIEnv *env, jclass unused) { #ifndef OTEL_PROCESS_CTX_NO_READ otel_process_ctx_read_result result = otel_process_ctx_read(); - + if (!result.success) { // Return null if reading failed return nullptr; } - + + // Convert C strings to Java strings + jstring jDeploymentEnvironmentName = result.data.deployment_environment_name ? + env->NewStringUTF(result.data.deployment_environment_name) : nullptr; + jstring jHostName = result.data.host_name ? + env->NewStringUTF(result.data.host_name) : nullptr; + jstring jServiceInstanceId = result.data.service_instance_id ? + env->NewStringUTF(result.data.service_instance_id) : nullptr; + jstring jServiceName = result.data.service_name ? + env->NewStringUTF(result.data.service_name) : nullptr; + jstring jServiceVersion = result.data.service_version ? + env->NewStringUTF(result.data.service_version) : nullptr; + jstring jTelemetrySdkLanguage = result.data.telemetry_sdk_language ? + env->NewStringUTF(result.data.telemetry_sdk_language) : nullptr; + jstring jTelemetrySdkVersion = result.data.telemetry_sdk_version ? + env->NewStringUTF(result.data.telemetry_sdk_version) : nullptr; + jstring jTelemetrySdkName = result.data.telemetry_sdk_name ? + env->NewStringUTF(result.data.telemetry_sdk_name) : nullptr; + // TODO: result.data.resources not supported yet for Java + + otel_process_ctx_read_drop(&result); + // Find the ProcessContext class jclass processContextClass = env->FindClass("com/datadoghq/profiler/OTelContext$ProcessContext"); if (!processContextClass) { - // Clean up allocated strings before returning - if (result.data.service_name) free((void*)result.data.service_name); - if (result.data.service_instance_id) free((void*)result.data.service_instance_id); - if (result.data.deployment_environment_name) free((void*)result.data.deployment_environment_name); return nullptr; } - + // Find the constructor - jmethodID constructor = env->GetMethodID(processContextClass, "", - "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); + jmethodID constructor = env->GetMethodID(processContextClass, "", + "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); if (!constructor) { - // Clean up allocated strings before returning - if (result.data.service_name) free((void*)result.data.service_name); - if (result.data.service_instance_id) free((void*)result.data.service_instance_id); - if (result.data.deployment_environment_name) free((void*)result.data.deployment_environment_name); return nullptr; } - - // Convert C strings to Java strings - jstring jServiceName = result.data.service_name ? - env->NewStringUTF(result.data.service_name) : nullptr; - jstring jServiceInstanceId = result.data.service_instance_id ? - env->NewStringUTF(result.data.service_instance_id) : nullptr; - jstring jDeploymentEnvironmentName = result.data.deployment_environment_name ? - env->NewStringUTF(result.data.deployment_environment_name) : nullptr; - - // Clean up the malloc'd strings - if (result.data.service_name) free((void*)result.data.service_name); - if (result.data.service_instance_id) free((void*)result.data.service_instance_id); - if (result.data.deployment_environment_name) free((void*)result.data.deployment_environment_name); - + // Create the ProcessContext object jobject processContext = env->NewObject(processContextClass, constructor, - jServiceName, jServiceInstanceId, jDeploymentEnvironmentName); - + jDeploymentEnvironmentName, jHostName, jServiceInstanceId, jServiceName, jServiceVersion, jTelemetrySdkLanguage, jTelemetrySdkVersion, jTelemetrySdkName); + return processContext; #else // If OTEL_PROCESS_CTX_NO_READ is defined, return null diff --git a/ddprof-lib/src/main/cpp/otel_process_ctx.cpp b/ddprof-lib/src/main/cpp/otel_process_ctx.cpp index c0f7513ae..d1cd08c18 100644 --- a/ddprof-lib/src/main/cpp/otel_process_ctx.cpp +++ b/ddprof-lib/src/main/cpp/otel_process_ctx.cpp @@ -1,8 +1,13 @@ -#ifdef __linux__ #include "otel_process_ctx.h" -#include #include +#ifdef __cplusplus + #include + using std::atomic_thread_fence; + using std::memory_order_seq_cst; +#else + #include +#endif #include #include #include @@ -19,6 +24,42 @@ #define PR_SET_VMA_ANON_NAME 0 #endif +static const otel_process_ctx_data empty_data = { + .deployment_environment_name = NULL, + .host_name = NULL, + .service_instance_id = NULL, + .service_name = NULL, + .service_version = NULL, + .telemetry_sdk_language = NULL, + .telemetry_sdk_version = NULL, + .telemetry_sdk_name = NULL, + .resources = NULL +}; + +#if (defined(OTEL_PROCESS_CTX_NOOP) && OTEL_PROCESS_CTX_NOOP) || !defined(__linux__) + // NOOP implementations when OTEL_PROCESS_CTX_NOOP is defined or not on Linux + + otel_process_ctx_result otel_process_ctx_publish(const otel_process_ctx_data *data) { + (void) data; // Suppress unused parameter warning + return (otel_process_ctx_result) {.success = false, .error_message = "OTEL_PROCESS_CTX_NOOP mode is enabled - no-op implementation (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; + } + + bool otel_process_ctx_drop(void) { + return true; // Nothing to do, this always succeeds + } + + #ifndef OTEL_PROCESS_CTX_NO_READ + otel_process_ctx_read_result otel_process_ctx_read(void) { + return (otel_process_ctx_read_result) {.success = false, .error_message = "OTEL_PROCESS_CTX_NOOP mode is enabled - no-op implementation (" __FILE__ ":" ADD_QUOTES(__LINE__) ")", .data = empty_data}; + } + + bool otel_process_ctx_read_drop(otel_process_ctx_read_result *result) { + (void) result; // Suppress unused parameter warning + return false; + } + #endif // OTEL_PROCESS_CTX_NO_READ +#else // OTEL_PROCESS_CTX_NOOP + /** * The process context data that's written into the published anonymous mapping. * @@ -41,7 +82,7 @@ typedef struct __attribute__((packed, aligned(8))) { * * It is used to store the all data for the process context and that needs to be kept around while the context is published. */ -struct otel_process_ctx_state { +typedef struct { // The pid of the process that published the context. pid_t publisher_pid; // The actual mapping of the process context. Note that because we `madvise(..., MADV_DONTFORK)` this mapping is not @@ -49,136 +90,170 @@ struct otel_process_ctx_state { otel_process_ctx_mapping *mapping; // The process context payload. char *payload; -}; +} otel_process_ctx_state; + +/** + * Only one context is active, so we keep its state as a global. + */ +static otel_process_ctx_state published_state; static otel_process_ctx_result otel_process_ctx_encode_payload(char **out, uint32_t *out_size, otel_process_ctx_data data); -// The `volatile` isn't strictly needed here but saves on a few casts below. -static void otel_process_ctx_state_drop(volatile otel_process_ctx_state *state) { - free(state->payload); - free((void *) state); +// We use a mapping size of 3 pages explicitly as a hint when running on legacy kernels that don't support the +// PR_SET_VMA_ANON_NAME prctl call; see below for more details. +static long size_for_mapping(void) { + long page_size_bytes = sysconf(_SC_PAGESIZE); + if (page_size_bytes < 4096) { + return -1; + } + return page_size_bytes * 2; } // The process context is designed to be read by an outside-of-process reader. Thus, for concurrency purposes the steps // on this method are ordered in a way to avoid races, or if not possible to avoid, to allow the reader to detect if there was a race. -otel_process_ctx_result otel_process_ctx_publish(otel_process_ctx_data data) { - volatile otel_process_ctx_state *state = (volatile otel_process_ctx_state *)calloc(1, sizeof(otel_process_ctx_state)); - if (!state) { - return (otel_process_ctx_result) {.success = false, .error_message = "Failed to allocate state (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; +otel_process_ctx_result otel_process_ctx_publish(const otel_process_ctx_data *data) { + // Step: Drop any previous context it if it exists + // No state should be around anywhere after this step. + if (!otel_process_ctx_drop_current()) { + return (otel_process_ctx_result) {.success = false, .error_message = "Failed to drop previous context (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; } - state->publisher_pid = getpid(); + // Step: Determine size for mapping + long mapping_size = size_for_mapping(); + if (mapping_size == -1) { + return (otel_process_ctx_result) {.success = false, .error_message = "Failed to get page size (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; + } // Step: Prepare the payload to be published // The payload SHOULD be ready and valid before trying to actually create the mapping. + if (!data) return (otel_process_ctx_result) {.success = false, .error_message = "otel_process_ctx_data is NULL (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; uint32_t payload_size = 0; - otel_process_ctx_result result = otel_process_ctx_encode_payload((char **)&state->payload, &payload_size, data); - if (!result.success) { - otel_process_ctx_state_drop(state); - return result; - } + otel_process_ctx_result result = otel_process_ctx_encode_payload(&published_state.payload, &payload_size, *data); + if (!result.success) return result; // Step: Create the mapping - otel_process_ctx_mapping *mapping = - (otel_process_ctx_mapping *)mmap(NULL, sizeof(otel_process_ctx_mapping), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (mapping == MAP_FAILED) { - otel_process_ctx_state_drop(state); + published_state.publisher_pid = getpid(); // This allows us to detect in forks that we shouldn't touch the mapping + published_state.mapping = (otel_process_ctx_mapping *) + mmap(NULL, mapping_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (published_state.mapping == MAP_FAILED) { + otel_process_ctx_drop_current(); return (otel_process_ctx_result) {.success = false, .error_message = "Failed to allocate mapping (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; } // Step: Setup MADV_DONTFORK // This ensures that the mapping is not propagated to child processes (they should call update/publish again). - if (madvise(mapping, sizeof(otel_process_ctx_mapping), MADV_DONTFORK) == -1) { - otel_process_ctx_state_drop(state); - - if (munmap(mapping, sizeof(otel_process_ctx_mapping)) == -1) { - return (otel_process_ctx_result) {.success = false, .error_message = "Failed to unmap mapping (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; - } else { + if (madvise(published_state.mapping, mapping_size, MADV_DONTFORK) == -1) { + if (otel_process_ctx_drop_current()) { return (otel_process_ctx_result) {.success = false, .error_message = "Failed to setup MADV_DONTFORK (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; + } else { + return (otel_process_ctx_result) {.success = false, .error_message = "Failed to drop previous context (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; } } - // (Store the mapping in the `volatile` state and stop using the local variable to force ordering below) - state->mapping = mapping; - mapping = NULL; - // Step: Populate the mapping // The payload and any extra fields must come first and not be reordered with the signature by the compiler. - // (In this implementation we guarantee this because `state` is declared `volatile`.) - *state->mapping = (otel_process_ctx_mapping) { + *published_state.mapping = (otel_process_ctx_mapping) { + .otel_process_ctx_signature = {0}, // Set in "Step: Populate the signature into the mapping" below .otel_process_ctx_version = 1, .otel_process_payload_size = payload_size, - .otel_process_payload = state->payload + .otel_process_payload = published_state.payload }; + // Step: Synchronization - Mapping has been filled and is missing signature + // Make sure the initialization of the mapping + payload above does not get reordered with setting the signature below. Setting + // the signature is what tells an outside reader that the context is fully published. + atomic_thread_fence(memory_order_seq_cst); + // Step: Populate the signature into the mapping // The signature must come last and not be reordered with the fields above by the compiler. After this step, external readers // can read the signature and know that the payload is ready to be read. - memcpy(state->mapping->otel_process_ctx_signature, "OTEL_CTX", sizeof(state->mapping->otel_process_ctx_signature)); + memcpy(published_state.mapping->otel_process_ctx_signature, "OTEL_CTX", sizeof(published_state.mapping->otel_process_ctx_signature)); - // TODO: Do we like this and want to keep it? - // Optional step: Change permissions on the mapping to only read permission - // We've observed the combination of anonymous mapping + single page + read-only permission is not very common, + // Step: Change permissions on the mapping to only read permission + // We've observed the combination of anonymous mapping + a given number of pages + read-only permission is not very common, // so this is left as a hint for when running on older kernels and the naming the mapping feature below isn't available. // For modern kernels, doing this is harmless so we do it unconditionally. - if (mprotect(state->mapping, sizeof(otel_process_ctx_mapping), PROT_READ) == -1) { - otel_process_ctx_state_drop(state); - - if (munmap(state->mapping, sizeof(otel_process_ctx_mapping)) == -1) { - return (otel_process_ctx_result) {.success = false, .error_message = "Failed to unmap mapping (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; - } else { + if (mprotect(published_state.mapping, mapping_size, PROT_READ) == -1) { + if (otel_process_ctx_drop_current()) { return (otel_process_ctx_result) {.success = false, .error_message = "Failed to change permissions on mapping (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; + } else { + return (otel_process_ctx_result) {.success = false, .error_message = "Failed to drop previous context (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; } } // Step: Name the mapping so outside readers can: // * Find it by name // * Hook on prctl to detect when new mappings are published - if (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, state->mapping, sizeof(otel_process_ctx_mapping), "OTEL_CTX") == -1) { + if (prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, published_state.mapping, mapping_size, "OTEL_CTX") == -1) { // Naming an anonymous mapping is a Linux 5.17+ feature. On earlier versions, this method call can fail. Thus it's OK // for this to fail because: // 1. Things that hook on prctl are still able to see this call, even though it's not supported (TODO: Confirm this is actually the case) // 2. As a fallback, on older kernels, it's possible to scan the mappings and look for the "OTEL_CTX" signature in the memory itself, - // after observing the mapping has the expected size and permissions. + // after observing the mapping has the expected number of pages and permissions. } // All done! - return (otel_process_ctx_result) {.success = true, .published_context = (otel_process_ctx_state *) state}; + return (otel_process_ctx_result) {.success = true, .error_message = NULL}; } -otel_process_ctx_result otel_process_ctx_update(otel_process_ctx_result *previous, otel_process_ctx_data data) { - if (!otel_process_ctx_drop(previous)) { - return (otel_process_ctx_result) {.success = false, .error_message = "Failed to drop previous context (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; +bool otel_process_ctx_drop_current(void) { + otel_process_ctx_state state = published_state; + + // Zero out the state and make sure no operations below are reordered with zeroing + published_state = (otel_process_ctx_state) {.publisher_pid = 0, .mapping = NULL, .payload = NULL}; + atomic_thread_fence(memory_order_seq_cst); + + // The mapping only exists if it was created by the current process; if it was inherited by a fork it doesn't exist anymore + // (due to the MADV_DONTFORK) and we don't need to do anything to it. + if (state.mapping != NULL && state.mapping != MAP_FAILED && getpid() == state.publisher_pid) { + long mapping_size = size_for_mapping(); + if (mapping_size == -1 || munmap(published_state.mapping, mapping_size) == -1) return false; } - return otel_process_ctx_publish(data); + // The payload may have been inherited from a parent. This is a regular malloc so we need to free it so we don't leak. + if (state.payload) free(state.payload); + + return true; } -bool otel_process_ctx_drop(otel_process_ctx_result *previous) { - if (!previous || !previous->success || !previous->published_context) { - return false; +static otel_process_ctx_result validate_and_calculate_payload_size(size_t *out_pairs_size, size_t *out_num_pairs, char **pairs) { + size_t num_entries = 0; + for (size_t i = 0; pairs[i] != NULL; i++) num_entries++; + if (num_entries % 2 != 0) { + return (otel_process_ctx_result) {.success = false, .error_message = "Value in otel_process_ctx_data is NULL (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; } + *out_num_pairs = num_entries / 2; - // The mapping only exists if it was created by the current process; if it was inherited by a fork it doesn't exist anymore - // (due to the MADV_DONTFORK) and we don't need to do anything to it. - if (getpid() == previous->published_context->publisher_pid) { - if (munmap(previous->published_context->mapping, sizeof(otel_process_ctx_mapping)) == -1) { - return false; + *out_pairs_size = 0; + for (size_t i = 0; i < *out_num_pairs; i++) { + size_t key_len = strlen(pairs[i * 2]); + if (key_len > INT16_MAX) { + return (otel_process_ctx_result) {.success = false, .error_message = "Length of key in otel_process_ctx_data exceeds INT16_MAX limit (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; } + size_t value_len = strlen(pairs[i * 2 + 1]); + if (value_len > INT16_MAX) { + return (otel_process_ctx_result) {.success = false, .error_message = "Length of value in otel_process_ctx_data exceeds INT16_MAX limit (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; + } + *out_pairs_size += 1 + 2 + key_len; // str 16 for key + *out_pairs_size += 1 + 2 + value_len; // str 16 for value } - otel_process_ctx_state_drop(previous->published_context); - previous->published_context = NULL; - - // Just to be nice to the caller, reset these as well - previous->success = false; - previous->error_message = "Context dropped"; + return (otel_process_ctx_result) {.success = true, .error_message = NULL}; +} - return true; +static void write_msgpack_string(char **ptr, const char *str) { + size_t len = strlen(str); + // Write str 16 header + *(*ptr)++ = 0xda; + *(*ptr)++ = (len >> 8) & 0xFF; // high byte of length + *(*ptr)++ = len & 0xFF; // low byte of length + memcpy(*ptr, str, len); + *ptr += len; } // TODO: The serialization format is still under discussion and is not considered stable yet. +// Comments **very** welcome: Should we use JSON instead? Or protobuf? // // Encode the payload as a msgpack map of string key/value pairs. // @@ -187,31 +262,36 @@ bool otel_process_ctx_drop(otel_process_ctx_result *previous) { // For extra compact code, it uses only a "map 16" encoding format with only "str 16" strings, rather than attempting to // use some of the other encoding alternatives. static otel_process_ctx_result otel_process_ctx_encode_payload(char **out, uint32_t *out_size, otel_process_ctx_data data) { - const char *pairs[][2] = { - {"service.name", data.service_name}, - {"service.instance.id", data.service_instance_id}, - {"deployment.environment.name", data.deployment_environment_name} + const char *pairs[] = { + "deployment.environment.name", data.deployment_environment_name, + "host.name", data.host_name, + "service.instance.id", data.service_instance_id, + "service.name", data.service_name, + "service.version", data.service_version, + "telemetry.sdk.language", data.telemetry_sdk_language, + "telemetry.sdk.version", data.telemetry_sdk_version, + "telemetry.sdk.name", data.telemetry_sdk_name, + NULL }; - const size_t num_pairs = sizeof(pairs) / sizeof(pairs[0]); + size_t num_pairs = 0, pairs_size = 0; + otel_process_ctx_result validation_result = validate_and_calculate_payload_size(&pairs_size, &num_pairs, (char **) pairs); + if (!validation_result.success) return validation_result; - // Validate + calculate size of payload - size_t total_size = 1 + 2; // map 16 header (1 byte + 2 bytes for count) - for (size_t i = 0; i < num_pairs; i++) { - size_t key_len = strlen(pairs[i][0]); - if (pairs[i][1] == NULL) { - return (otel_process_ctx_result) {.success = false, .error_message = "Value in otel_process_ctx_data is NULL (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; - } - size_t value_len = strlen(pairs[i][1]); - if (value_len > INT16_MAX) { - // Keys are hardcoded above so we know they have a valid length - return (otel_process_ctx_result) {.success = false, .error_message = "Length of value in otel_process_ctx_data exceeds INT16_MAX limit (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; - } - total_size += 1 + 2 + key_len; // str 16 for key - total_size += 1 + 2 + value_len; // str 16 for value + size_t resources_pairs_size = 0, resources_num_pairs = 0; + if (data.resources != NULL) { + validation_result = validate_and_calculate_payload_size(&resources_pairs_size, &resources_num_pairs, data.resources); + if (!validation_result.success) return validation_result; + } + + size_t total_pairs = num_pairs + resources_num_pairs; + size_t total_size = pairs_size + resources_pairs_size + 1 + 2; // map 16 header (1 byte + 2 bytes for count) + + if (total_pairs > INT16_MAX) { + return (otel_process_ctx_result) {.success = false, .error_message = "Total number of pairs exceeds INT16_MAX limit (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; } - char *encoded = (char *)calloc(total_size, 1); + char *encoded = (char *) calloc(total_size, 1); if (!encoded) { return (otel_process_ctx_result) {.success = false, .error_message = "Failed to allocate memory for payload (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; } @@ -219,32 +299,25 @@ static otel_process_ctx_result otel_process_ctx_encode_payload(char **out, uint3 // Write map 16 header (0xde) followed by count *ptr++ = 0xde; - *ptr++ = (num_pairs >> 8) & 0xFF; // high byte of count - *ptr++ = num_pairs & 0xFF; // low byte of count + *ptr++ = (total_pairs >> 8) & 0xFF; // high byte of count + *ptr++ = total_pairs & 0xFF; // low byte of count for (size_t i = 0; i < num_pairs; i++) { - size_t key_len = strlen(pairs[i][0]); - size_t value_len = strlen(pairs[i][1]); - - // Write key as str 16 - *ptr++ = 0xda; - *ptr++ = (key_len >> 8) & 0xFF; // high byte of length - *ptr++ = key_len & 0xFF; // low byte of length - memcpy(ptr, pairs[i][0], key_len); - ptr += key_len; - - // Write value as str 16 - *ptr++ = 0xda; - *ptr++ = (value_len >> 8) & 0xFF; // high byte of length - *ptr++ = value_len & 0xFF; // low byte of length - memcpy(ptr, pairs[i][1], value_len); - ptr += value_len; + write_msgpack_string(&ptr, pairs[i * 2]); // Write key + write_msgpack_string(&ptr, pairs[i * 2 + 1]); // Write value + } + + if (data.resources != NULL) { + for (size_t i = 0; i < resources_num_pairs; i++) { + write_msgpack_string(&ptr, data.resources[i * 2]); // Write key + write_msgpack_string(&ptr, data.resources[i * 2 + 1]); // Write value + } } *out = encoded; *out_size = (uint32_t) total_size; - return (otel_process_ctx_result) {.success = true }; + return (otel_process_ctx_result) {.success = true, .error_message = NULL}; } #ifndef OTEL_PROCESS_CTX_NO_READ @@ -268,20 +341,20 @@ static otel_process_ctx_result otel_process_ctx_encode_payload(char **out, uint3 static otel_process_ctx_mapping *try_finding_mapping(void) { char line[8192]; - void *result = NULL; + otel_process_ctx_mapping *result = NULL; FILE *fp = fopen("/proc/self/maps", "r"); - if (!fp) return (otel_process_ctx_mapping *)result; + if (!fp) return result; while (fgets(line, sizeof(line), fp)) { if (is_otel_process_ctx_mapping(line)) { - result = parse_mapping_start(line); + result = (otel_process_ctx_mapping *)parse_mapping_start(line); break; } } fclose(fp); - return (otel_process_ctx_mapping *) result; + return result; } // Simplified msgpack decoder to match the exact encoder above. If the msgpack string doesn't match the encoder, this will @@ -296,13 +369,25 @@ static otel_process_ctx_result otel_process_ctx_encode_payload(char **out, uint3 uint16_t count = ((uint8_t)*ptr << 8) | (uint8_t)*(ptr + 1); ptr += 2; - // We expect exactly 3 pairs - if (count != 3) return false; + // We expect at least 8 pairs (the standard fields) + if (count < 8) return false; // Initialize output data - data_out->service_name = NULL; - data_out->service_instance_id = NULL; data_out->deployment_environment_name = NULL; + data_out->host_name = NULL; + data_out->service_instance_id = NULL; + data_out->service_name = NULL; + data_out->service_version = NULL; + data_out->telemetry_sdk_language = NULL; + data_out->telemetry_sdk_version = NULL; + data_out->telemetry_sdk_name = NULL; + data_out->resources = NULL; + + // Allocate resources array with space for all pairs as a simplification (2 entries per pair + 1 for NULL terminator) + data_out->resources = (char **) calloc(count * 2 + 1, sizeof(char *)); + if (!data_out->resources) return false; + + int resources_index = 0; // Decode each key-value pair for (int i = 0; i < count; i++) { @@ -325,50 +410,101 @@ static otel_process_ctx_result otel_process_ctx_encode_payload(char **out, uint3 ptr += 2; // Read value - char *value = (char *)malloc(value_len + 1); + char *value = (char *) calloc(value_len + 1, 1); if (!value) return false; memcpy(value, ptr, value_len); value[value_len] = '\0'; ptr += value_len; // Assign to appropriate field based on key - if (key_len == strlen("service.name") && memcmp(key_not_terminated, "service.name", strlen("service.name")) == 0) { - data_out->service_name = value; + if (key_len == strlen("deployment.environment.name") && memcmp(key_not_terminated, "deployment.environment.name", strlen("deployment.environment.name")) == 0) { + data_out->deployment_environment_name = value; + } else if (key_len == strlen("host.name") && memcmp(key_not_terminated, "host.name", strlen("host.name")) == 0) { + data_out->host_name = value; } else if (key_len == strlen("service.instance.id") && memcmp(key_not_terminated, "service.instance.id", strlen("service.instance.id")) == 0) { data_out->service_instance_id = value; - } else if (key_len == strlen("deployment.environment.name") && memcmp(key_not_terminated, "deployment.environment.name", strlen("deployment.environment.name")) == 0) { - data_out->deployment_environment_name = value; + } else if (key_len == strlen("service.name") && memcmp(key_not_terminated, "service.name", strlen("service.name")) == 0) { + data_out->service_name = value; + } else if (key_len == strlen("service.version") && memcmp(key_not_terminated, "service.version", strlen("service.version")) == 0) { + data_out->service_version = value; + } else if (key_len == strlen("telemetry.sdk.language") && memcmp(key_not_terminated, "telemetry.sdk.language", strlen("telemetry.sdk.language")) == 0) { + data_out->telemetry_sdk_language = value; + } else if (key_len == strlen("telemetry.sdk.version") && memcmp(key_not_terminated, "telemetry.sdk.version", strlen("telemetry.sdk.version")) == 0) { + data_out->telemetry_sdk_version = value; + } else if (key_len == strlen("telemetry.sdk.name") && memcmp(key_not_terminated, "telemetry.sdk.name", strlen("telemetry.sdk.name")) == 0) { + data_out->telemetry_sdk_name = value; } else { - // Unknown key, clean up and fail - free(value); - return false; + // Unknown key, put it into resources + char *key = (char *) calloc(key_len + 1, 1); + if (!key) { + free(value); + return false; + } + memcpy(key, key_not_terminated, key_len); + key[key_len] = '\0'; + + data_out->resources[resources_index++] = key; + data_out->resources[resources_index++] = value; } } // Verify all required fields were found - return data_out->service_name != NULL && + return data_out->deployment_environment_name != NULL && + data_out->host_name != NULL && data_out->service_instance_id != NULL && - data_out->deployment_environment_name != NULL; + data_out->service_name != NULL && + data_out->service_version != NULL && + data_out->telemetry_sdk_language != NULL && + data_out->telemetry_sdk_version != NULL && + data_out->telemetry_sdk_name != NULL; + } + + void otel_process_ctx_read_data_drop(otel_process_ctx_data data) { + if (data.deployment_environment_name) free(data.deployment_environment_name); + if (data.host_name) free(data.host_name); + if (data.service_instance_id) free(data.service_instance_id); + if (data.service_name) free(data.service_name); + if (data.service_version) free(data.service_version); + if (data.telemetry_sdk_language) free(data.telemetry_sdk_language); + if (data.telemetry_sdk_version) free(data.telemetry_sdk_version); + if (data.telemetry_sdk_name) free(data.telemetry_sdk_name); + if (data.resources) { + for (int i = 0; data.resources[i] != NULL; i++) free(data.resources[i]); + free(data.resources); + } } otel_process_ctx_read_result otel_process_ctx_read(void) { otel_process_ctx_mapping *mapping = try_finding_mapping(); if (!mapping) { - return (otel_process_ctx_read_result) {.success = false, .error_message = "No OTEL_CTX mapping found (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; + return (otel_process_ctx_read_result) {.success = false, .error_message = "No OTEL_CTX mapping found (" __FILE__ ":" ADD_QUOTES(__LINE__) ")", .data = empty_data}; } if (strncmp(mapping->otel_process_ctx_signature, "OTEL_CTX", sizeof(mapping->otel_process_ctx_signature)) != 0 || mapping->otel_process_ctx_version != 1) { - return (otel_process_ctx_read_result) {.success = false, .error_message = "Invalid OTEL_CTX signature or version (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; + return (otel_process_ctx_read_result) {.success = false, .error_message = "Invalid OTEL_CTX signature or version (" __FILE__ ":" ADD_QUOTES(__LINE__) ")", .data = empty_data}; } - otel_process_ctx_data data = {0}; + otel_process_ctx_data data = empty_data; if (!otel_process_ctx_decode_payload(mapping->otel_process_payload, &data)) { - return (otel_process_ctx_read_result) {.success = false, .error_message = "Failed to decode payload (" __FILE__ ":" ADD_QUOTES(__LINE__) ")"}; + otel_process_ctx_read_data_drop(data); + return (otel_process_ctx_read_result) {.success = false, .error_message = "Failed to decode payload (" __FILE__ ":" ADD_QUOTES(__LINE__) ")", .data = empty_data}; } - return (otel_process_ctx_read_result) {.success = true, .data = data}; + return (otel_process_ctx_read_result) {.success = true, .error_message = NULL, .data = data}; + } + + bool otel_process_ctx_read_drop(otel_process_ctx_read_result *result) { + if (!result || !result->success) return false; + + // Free allocated strings in the data + otel_process_ctx_read_data_drop(result->data); + + // Reset the result to empty state + *result = (otel_process_ctx_read_result) {.success = false, .error_message = "Data dropped", .data = empty_data}; + + return true; } #endif // OTEL_PROCESS_CTX_NO_READ -#endif +#endif // OTEL_PROCESS_CTX_NOOP diff --git a/ddprof-lib/src/main/cpp/otel_process_ctx.h b/ddprof-lib/src/main/cpp/otel_process_ctx.h index 117b9bce0..fbb1b0a30 100644 --- a/ddprof-lib/src/main/cpp/otel_process_ctx.h +++ b/ddprof-lib/src/main/cpp/otel_process_ctx.h @@ -1,5 +1,10 @@ #pragma once +#define OTEL_PROCESS_CTX_VERSION_MAJOR 0 +#define OTEL_PROCESS_CTX_VERSION_MINOR 0 +#define OTEL_PROCESS_CTX_VERSION_PATCH 5 +#define OTEL_PROCESS_CTX_VERSION_STRING "0.0.5" + #ifdef __cplusplus extern "C" { #endif @@ -13,83 +18,98 @@ extern "C" { * process-level context sharing specification. (TODO Link) * * This reference implementation is Linux-only, as the specification currently only covers Linux. + * On non-Linux OS's (or when OTEL_PROCESS_CTX_NOOP is defined) no-op versions of functions are supplied. */ /** * Data that can be published as a process context. * - * Every string MUST be valid for the duration of the call to `otel_process_ctx_publish` or - * `otel_process_ctx_update`. Strings will be copied into the context. + * Every string MUST be valid for the duration of the call to `otel_process_ctx_publish`. + * Strings will be copied into the context. * * Strings MUST be: - * * Non-null + * * Non-NULL * * UTF-8 encoded * * Not longer than INT16_MAX bytes * * Strings MAY be: * * Empty + * + * The below fields map to usual datadog attributes as follows (TODO: Remove this once we share the header publicly) + * * deployment_environment_name -> env + * * host_name -> hostname + * * service_instance_id -> runtime-id + * * service_name -> service + * * service_version -> version + * * telemetry_sdk_language -> tracer_language + * * telemetry_sdk_version -> tracer_version + * * telemetry_sdk_name -> name of library (e.g. dd-trace-java) */ typedef struct { - // https://opentelemetry.io/docs/specs/semconv/registry/attributes/service/#service-name - char *service_name; - // https://opentelemetry.io/docs/specs/semconv/registry/attributes/service/#service-instance-id - char *service_instance_id; // https://opentelemetry.io/docs/specs/semconv/registry/attributes/deployment/#deployment-environment-name char *deployment_environment_name; + // https://opentelemetry.io/docs/specs/semconv/registry/attributes/host/#host-name + char *host_name; + // https://opentelemetry.io/docs/specs/semconv/registry/attributes/service/#service-instance-id + char *service_instance_id; + // https://opentelemetry.io/docs/specs/semconv/registry/attributes/service/#service-name + char *service_name; + // https://opentelemetry.io/docs/specs/semconv/registry/attributes/service/#service-version + char *service_version; + // https://opentelemetry.io/docs/specs/semconv/registry/attributes/telemetry/#telemetry-sdk-language + char *telemetry_sdk_language; + // https://opentelemetry.io/docs/specs/semconv/registry/attributes/telemetry/#telemetry-sdk-version + char *telemetry_sdk_version; + // https://opentelemetry.io/docs/specs/semconv/registry/attributes/telemetry/#telemetry-sdk-name + char *telemetry_sdk_name; + // Additional key/value pairs as resources https://opentelemetry.io/docs/specs/otel/resource/sdk/ + // Can be NULL if no resources are needed; if non-NULL, this array MUST be terminated with a NULL entry. + // Every even entry is a key, every odd entry is a value (E.g. "key1", "value1", "key2", "value2", NULL). + char **resources; } otel_process_ctx_data; -/** - * Opaque type representing the state of a published process context. - * - * Internally useful for dropping the context and any memory allocations related to it. - */ -typedef struct otel_process_ctx_state otel_process_ctx_state; +/** Number of entries in the `otel_process_ctx_data` struct. Can be used to easily detect when the struct is updated. */ +#define OTEL_PROCESS_CTX_DATA_ENTRIES sizeof(otel_process_ctx_data) / sizeof(char *) typedef struct { bool success; const char *error_message; // Static strings only, non-NULL if success is false - otel_process_ctx_state *published_context; // Non-NULL if success is true } otel_process_ctx_result; /** * Publishes a OpenTelemetry process context with the given data. * * The context should remain alive until the application exits (or is just about to exit). + * This method is NOT thread-safe. * - * @param data The data to publish. This data is copied into the context and only needs to be valid for the duration of - * the call. - * @return The result of the operation. - */ -otel_process_ctx_result otel_process_ctx_publish(otel_process_ctx_data data); - -/** - * Replaces the previous OpenTelemetry process context with the given data. - * - * This API is usually called when: + * Calling `publish` multiple times is supported and will replace a previous context (only one is published at any given + * time). Calling `publish` multiple times usually happens when: * * Some of the `otel_process_ctx_data` changes due to a live system reconfiguration for the same process * * The process is forked (to provide a new `service_instance_id`) * - * @param previous The previous context. This context is dropped before the new one is installed. - * This API can be called in a fork of the process that published the previous context, even though - * the context is not carried over into forked processes (although part of its memory allocations are). - * Must not be `NULL`. - * @param data The data to publish. This data is copied into the context and only needs to be valid for the duration of - * the call. + * This API can be called in a fork of the process that published the previous context, even though + * the context is not carried over into forked processes (although part of its memory allocations are). + * + * @param data Pointer to the data to publish. This data is copied into the context and only needs to be valid for the duration of + * the call. Must not be `NULL`. * @return The result of the operation. */ -otel_process_ctx_result otel_process_ctx_update(otel_process_ctx_result *previous, otel_process_ctx_data data); +otel_process_ctx_result otel_process_ctx_publish(const otel_process_ctx_data *data); /** - * Drops the previous OpenTelemetry process context. + * Drops the current OpenTelemetry process context, if any. + * + * This method is safe to call even there's no current context. + * This method is NOT thread-safe. + * + * This API can be called in a fork of the process that published the current context to clean memory allocations + * related to the parent's context (even though the context itself is not carried over into forked processes). * - * @param previous The previous context to drop. This API can be called in a fork of the process that published the - * previous context, to clean memory allocations related to the parent's context (even though the - * context is not carried over into forked processes). - * Must not be `NULL`. - * @return `true` if the context was successfully dropped, `false` otherwise. + * @return `true` if the context was successfully dropped or no context existed, `false` otherwise. */ -bool otel_process_ctx_drop(otel_process_ctx_result *previous); +bool otel_process_ctx_drop_current(void); +/** This can be disabled if no read support is required. */ #ifndef OTEL_PROCESS_CTX_NO_READ typedef struct { bool success; @@ -100,14 +120,22 @@ bool otel_process_ctx_drop(otel_process_ctx_result *previous); /** * Reads the current OpenTelemetry process context, if any. * - * Useful for debugging and testing purposes. Underlying returned strings in `data` are allocated using `malloc` and the - * caller is responsible for `free`ing them. + * Useful for debugging and testing purposes. Underlying returned strings in `data` are dynamically allocated using + * `malloc` and `otel_process_ctx_read_drop` must be called to free them. * * Thread-safety: This function assumes there is no concurrent mutation of the process context. * * @return The result of the operation. If successful, `data` contains the retrieved context data. */ otel_process_ctx_read_result otel_process_ctx_read(void); + + /** + * Drops the data resulting from a previous call to `otel_process_ctx_read`. + * + * @param result The result of a previous call to `otel_process_ctx_read`. Must not be `NULL`. + * @return `true` if the data was successfully dropped, `false` otherwise. + */ + bool otel_process_ctx_read_drop(otel_process_ctx_read_result *result); #endif #ifdef __cplusplus diff --git a/ddprof-lib/src/main/cpp/otel_process_ctx_macos.cpp b/ddprof-lib/src/main/cpp/otel_process_ctx_macos.cpp deleted file mode 100644 index a4cb2617d..000000000 --- a/ddprof-lib/src/main/cpp/otel_process_ctx_macos.cpp +++ /dev/null @@ -1,41 +0,0 @@ -// macOS stub implementation for otel_process_ctx -// The OpenTelemetry process context specification is Linux-only - -#ifdef __APPLE__ - -#include "otel_process_ctx.h" - -otel_process_ctx_result otel_process_ctx_publish(otel_process_ctx_data data) { - return (otel_process_ctx_result) { - .success = false, - .error_message = "OpenTelemetry process context is not supported on macOS" - }; -} - -otel_process_ctx_result otel_process_ctx_update(otel_process_ctx_result *previous, otel_process_ctx_data data) { - return (otel_process_ctx_result) { - .success = false, - .error_message = "OpenTelemetry process context is not supported on macOS" - }; -} - -bool otel_process_ctx_drop(otel_process_ctx_result *previous) { - // Always return true for no-op on macOS - if (previous) { - previous->success = false; - previous->error_message = "Context dropped (macOS stub)"; - previous->published_context = nullptr; - } - return true; -} - -#ifndef OTEL_PROCESS_CTX_NO_READ -otel_process_ctx_read_result otel_process_ctx_read(void) { - return (otel_process_ctx_read_result) { - .success = false, - .error_message = "OpenTelemetry process context reading is not supported on macOS" - }; -} -#endif // OTEL_PROCESS_CTX_NO_READ - -#endif // __APPLE__ \ No newline at end of file diff --git a/ddprof-lib/src/main/java/com/datadoghq/profiler/OTelContext.java b/ddprof-lib/src/main/java/com/datadoghq/profiler/OTelContext.java index 9665b2e3d..83f390d05 100644 --- a/ddprof-lib/src/main/java/com/datadoghq/profiler/OTelContext.java +++ b/ddprof-lib/src/main/java/com/datadoghq/profiler/OTelContext.java @@ -14,8 +14,7 @@ *

Platform Support: *

    *
  • Linux: Full support using anonymous memory mappings with prctl naming
  • - *
  • macOS: Limited support - API calls are no-ops
  • - *
  • Other platforms: Not supported
  • + *
  • Others: Limited support - API calls are no-ops
  • *
* *

Thread Safety: This class is thread-safe. All public methods can be @@ -27,11 +26,7 @@ * OTelContext context = OTelContext.getInstance(); * * // Set process context for external discovery - * context.setProcessContext( - * "my-service", // service name - * "instance-12345", // unique instance identifier - * "production" // deployment environment - * ); + * context.setProcessContext(...); * } * *

External Discovery: Once published, the process context can be @@ -63,7 +58,6 @@ public static OTelContext getInstance() { } private final LibraryLoader.Result libraryLoadResult; - private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); /** * Private constructor for singleton instance. @@ -111,46 +105,48 @@ private OTelContext() { * Reads the currently published OpenTelemetry process context, if any. * *

This method attempts to read back the process context that was previously - * published via {@link #setProcessContext(String, String, String)}. This is + * published via {@link #setProcessContext(String, String, String, String, String, String)}. This is * primarily useful for debugging and testing purposes. * *

Platform Support: Currently only supported on Linux. On other * platforms, this method will return null. - * - *

Thread Safety: This method assumes there is no concurrent mutation - * of the process context and is safe to call from any thread. - * + * * @return a ProcessContext object containing the current context data if * successfully read, or null if no context is published or reading failed * @since 1.30.0 */ - public ProcessContext readProcessContext() { - try { - readWriteLock.readLock().lock(); - return libraryLoadResult.succeeded ? readProcessCtx0() : null; - } finally { - readWriteLock.readLock().unlock(); - } + public synchronized ProcessContext readProcessContext() { + return libraryLoadResult.succeeded ? readProcessCtx0() : null; } /** * Represents the OpenTelemetry process context data. */ public static final class ProcessContext { - public final String serviceName; - public final String serviceInstanceId; public final String deploymentEnvironmentName; - - public ProcessContext(String serviceName, String serviceInstanceId, String deploymentEnvironmentName) { - this.serviceName = serviceName; - this.serviceInstanceId = serviceInstanceId; + public final String hostName; + public final String serviceInstanceId; + public final String serviceName; + public final String serviceVersion; + public final String telemetrySdkLanguage; + public final String telemetrySdkVersion; + public final String telemetrySdkName; + + public ProcessContext(String deploymentEnvironmentName, String hostName, String serviceInstanceId, String serviceName, String serviceVersion, String telemetrySdkLanguage, String telemetrySdkVersion, String telemetrySdkName) { this.deploymentEnvironmentName = deploymentEnvironmentName; + this.hostName = hostName; + this.serviceInstanceId = serviceInstanceId; + this.serviceName = serviceName; + this.serviceVersion = serviceVersion; + this.telemetrySdkLanguage = telemetrySdkLanguage; + this.telemetrySdkVersion = telemetrySdkVersion; + this.telemetrySdkName = telemetrySdkName; } @Override public String toString() { - return String.format("ProcessContext{serviceName='%s', serviceInstanceId='%s', deploymentEnvironmentName='%s'}", - serviceName, serviceInstanceId, deploymentEnvironmentName); + return String.format("ProcessContext{deploymentEnvironmentName='%s', hostName='%s', serviceInstanceId='%s', serviceName='%s', serviceVersion='%s', telemetrySdkLanguage='%s', telemetrySdkVersion='%s', telemetrySdkName='%s'}", + deploymentEnvironmentName, hostName, serviceInstanceId, serviceName, serviceVersion, telemetrySdkLanguage, telemetrySdkVersion, telemetrySdkName); } } @@ -175,38 +171,43 @@ public String toString() { *

Usage Example: *

{@code
      * OTelContext.getInstance().setProcessContext(
-     *     "order-service",        // Identifies the service
-     *     "pod-abc123",          // Unique instance ID (e.g., pod name, container ID)
-     *     "production"           // Environment (production, staging, dev, etc.)
+     *     "staging",           // env
+     *     "my-hostname",       // hostname
+     *     "instance-12345"     // runtime-id
+     *     "my-service",        // service
+     *     "1.0.0",             // version
+     *     "3.5.0"              // tracer-version
      * );
      * }
- * + * + * @param env the deployment environment name as defined by OpenTelemetry + * semantic conventions (deployment.environment.name). Must not be null. + * Examples: "production", "staging", "development", "test" + * @param hostname the hostname of the service as defined by OpenTelemetry + * semantic conventions (host.name). Must not be null. + * Examples: "my-hostname", "my-hostname.example.com" + * @param runtimeId the unique identifier for this specific instance of the service + * as defined by OpenTelemetry semantic conventions (service.instance.id). + * Must not be null. * @param service the logical name of the service as defined by OpenTelemetry * semantic conventions (service.name). Must not be null. * Examples: "order-service", "user-management", "payment-processor" - * @param runtimeId the unique identifier for this specific instance of the service - * as defined by OpenTelemetry semantic conventions (service.instance.id). - * Must not be null. Examples: pod name, container ID, hostname - * @param environment the deployment environment name as defined by OpenTelemetry - * semantic conventions (deployment.environment.name). Must not be null. - * Examples: "production", "staging", "development", "test" - * - * @throws RuntimeException if the native library failed to load during initialization - * + * @param version the version of the service as defined by OpenTelemetry + * semantic conventions (service.version). Must not be null. + * Examples: "1.0.0", "2.3.4" + * @param tracerVersion the version of the tracer as defined by OpenTelemetry + * semantic conventions (telemetry.sdk.version). Must not be null. + * Examples: "3.5.0", "4.2.0" + * * * @see OpenTelemetry Service Attributes * @see OpenTelemetry Deployment Attributes */ - public void setProcessContext(String service, String runtimeId, String environment) { - try { - readWriteLock.writeLock().lock(); - if (libraryLoadResult.succeeded ) { - setProcessCtx0(service, runtimeId, environment); - } - } finally { - readWriteLock.writeLock().unlock(); - } + public synchronized void setProcessContext(String env, String hostname, String runtimeId, String service, String version, String tracerVersion) { + if (libraryLoadResult.succeeded ) { + setProcessCtx0(env, hostname, runtimeId, service, version, tracerVersion); + } } - private static native void setProcessCtx0(String serviceName, String serviceId, String environment); + private static native void setProcessCtx0(String env, String hostname, String runtimeId, String service, String version, String tracerVersion); private static native ProcessContext readProcessCtx0(); -} \ No newline at end of file +} diff --git a/ddprof-test/src/test/java/com/datadoghq/profiler/context/ProcessContextTest.java b/ddprof-test/src/test/java/com/datadoghq/profiler/context/ProcessContextTest.java index 65a96bf41..fac9421a1 100644 --- a/ddprof-test/src/test/java/com/datadoghq/profiler/context/ProcessContextTest.java +++ b/ddprof-test/src/test/java/com/datadoghq/profiler/context/ProcessContextTest.java @@ -21,13 +21,16 @@ public class ProcessContextTest { @Test public void testProcessContextMappingCreation() throws IOException { Assumptions.assumeTrue(Platform.isLinux()); - - String serviceName = "test-service"; - String serviceId = "test-instance-123"; - String environment = "test-env"; - - OTelContext.getInstance().setProcessContext(serviceName, serviceId, environment); - + + String env = "test-env"; + String hostname = "test-hostname"; + String runtimeId = "test-instance-123"; + String service = "test-service"; + String version = "1.0.0"; + String tracerVersion = "3.5.0"; + + OTelContext.getInstance().setProcessContext(env, hostname, runtimeId, service, version, tracerVersion); + OtelMappingInfo mapping = findOtelMapping(); assertNotNull(mapping, "OTEL mapping should exist after setProcessContext"); @@ -69,103 +72,39 @@ private OtelMappingInfo findOtelMapping() throws IOException { } return null; } - + private void verifyMappingPermissions(OtelMappingInfo mapping) { - assertTrue(mapping.permissions.contains("r"), + assertTrue(mapping.permissions.contains("r"), "OTEL mapping should have read permission, got: " + mapping.permissions); - assertFalse(mapping.permissions.contains("w"), + assertFalse(mapping.permissions.contains("w"), "OTEL mapping should not have write permission, got: " + mapping.permissions); - assertFalse(mapping.permissions.contains("x"), + assertFalse(mapping.permissions.contains("x"), "OTEL mapping should not have execute permission, got: " + mapping.permissions); - - long startAddr = Long.parseUnsignedLong(mapping.startAddress, 16); - long endAddr = Long.parseUnsignedLong(mapping.endAddress, 16); - long size = endAddr - startAddr; - assertTrue(size > 0, "OTEL mapping size should be positive, got: " + size + " bytes"); - } - - @Test - public void testMappingStructureCompliance() throws IOException { - Assumptions.assumeTrue(Platform.isLinux()); - - String serviceName = "test-service-structure"; - String serviceId = "test-instance-456"; - String environment = "test-env-structure"; - - OTelContext.getInstance().setProcessContext(serviceName, serviceId, environment); - - OtelMappingInfo mapping = findOtelMapping(); - assertNotNull(mapping, "OTEL mapping should exist"); - - verifyMappingStructure(mapping, serviceName, serviceId, environment); } @Test public void testNativeReadBackFunctionality() { Assumptions.assumeTrue(Platform.isLinux()); - - String serviceName = "test-service-read"; - String serviceId = "test-instance-789"; - String environment = "test-env-read"; - + + String env = "test-env"; + String hostname = "test-hostname"; + String runtimeId = "test-instance-123"; + String service = "test-service"; + String version = "1.0.0"; + String tracerVersion = "3.5.0"; + OTelContext context = OTelContext.getInstance(); - context.setProcessContext(serviceName, serviceId, environment); - + context.setProcessContext(env, hostname, runtimeId, service, version, tracerVersion); + OTelContext.ProcessContext readContext = context.readProcessContext(); - - assertNotNull(readContext, "Should be able to read back the published context"); - assertEquals(serviceName, readContext.serviceName, "Service name should match"); - assertEquals(serviceId, readContext.serviceInstanceId, "Service instance ID should match"); - assertEquals(environment, readContext.deploymentEnvironmentName, "Environment name should match"); - } - - private void verifyMappingStructure(OtelMappingInfo mapping, String expectedServiceName, - String expectedServiceId, String expectedEnvironment) throws IOException { - long startAddr = Long.parseUnsignedLong(mapping.startAddress, 16); - long endAddr = Long.parseUnsignedLong(mapping.endAddress, 16); - long size = endAddr - startAddr; - - assertTrue(size >= 24, "Mapping size should be at least 24 bytes, got: " + size); - - try (RandomAccessFile memFile = new RandomAccessFile("/proc/self/mem", "r")) { - memFile.seek(startAddr); - - byte[] mappingBytes = new byte[24]; - int bytesRead = memFile.read(mappingBytes); - assertEquals(24, bytesRead, "Should read 24 bytes"); - - String signature = new String(mappingBytes, 0, 8); - assertEquals("OTEL_CTX", signature, "Signature should be OTEL_CTX"); - - int version = ((mappingBytes[8] & 0xFF)) | - ((mappingBytes[9] & 0xFF) << 8) | - ((mappingBytes[10] & 0xFF) << 16) | - ((mappingBytes[11] & 0xFF) << 24); - assertEquals(1, version, "Version should be 1"); - - int payloadSize = ((mappingBytes[12] & 0xFF)) | - ((mappingBytes[13] & 0xFF) << 8) | - ((mappingBytes[14] & 0xFF) << 16) | - ((mappingBytes[15] & 0xFF) << 24); - assertTrue(payloadSize > 0, "Payload size should be positive"); - - int expectedSize = 3 + 3 + "service.name".length() + 3 + expectedServiceName.length() + - 3 + "service.instance.id".length() + 3 + expectedServiceId.length() + - 3 + "deployment.environment.name".length() + 3 + expectedEnvironment.length(); - - assertEquals(expectedSize, payloadSize, "Payload size should match expected size"); - - boolean hasPayload = false; - for (int i = 16; i < 24; i++) { - if (mappingBytes[i] != 0) { - hasPayload = true; - break; - } - } - assertTrue(hasPayload, "Payload pointer should be non-zero"); - - } catch (IOException e) { - fail("Failed to read OTEL mapping structure: " + e.getMessage()); - } + + assertEquals(env, readContext.deploymentEnvironmentName, "Environment name should match"); + assertEquals(hostname, readContext.hostName, "Host name should match"); + assertEquals(runtimeId, readContext.serviceInstanceId, "Service instance ID should match"); + assertEquals(service, readContext.serviceName, "Service name should match"); + assertEquals(version, readContext.serviceVersion, "Service version should match"); + assertEquals("java", readContext.telemetrySdkLanguage, "Tracer language should match"); + assertEquals(tracerVersion, readContext.telemetrySdkVersion, "Tracer version should match"); + assertEquals("dd-trace-java", readContext.telemetrySdkName, "Tracer name should match"); } -} \ No newline at end of file +} diff --git a/gradle/lock.properties b/gradle/lock.properties index 997c88202..7842d7640 100644 --- a/gradle/lock.properties +++ b/gradle/lock.properties @@ -1,5 +1,5 @@ ap.branch=dd/master ap.commit=5930966a92860f6e5d2d89ab6faab5815720bad9 -ctx_branch=ivoanjo/context-sharing-reference-impl -ctx_commit=0bcbd0c3f419770c6fb8ec1a043014583ae5269e +ctx_branch=main +ctx_commit=b33673d801b85a6c38fa0e9f1a139cb246737ce8