|
| 1 | +/* |
| 2 | + * Copyright 2026, Datadog, Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "context_api.h" |
| 18 | +#include "context.h" |
| 19 | +#include "guards.h" |
| 20 | +#include "otel_context.h" |
| 21 | +#include "otel_process_ctx.h" |
| 22 | +#include "profiler.h" |
| 23 | +#include "thread.h" |
| 24 | +#include <cstring> |
| 25 | + |
| 26 | +// Reserved attribute index for local root span ID in OTEL attrs_data. |
| 27 | +// Only used within this translation unit; not part of the public ContextApi header. |
| 28 | +static const uint8_t LOCAL_ROOT_SPAN_ATTR_INDEX = 0; |
| 29 | + |
| 30 | +/** |
| 31 | + * Initialize context TLS for the current thread on first use. |
| 32 | + * Must be called with signals blocked to prevent musl TLS deadlock: |
| 33 | + * on musl, the first write to a TLS variable triggers lazy slot allocation, |
| 34 | + * which acquires an internal lock that is also held during signal delivery, |
| 35 | + * causing deadlock if a signal fires mid-init. |
| 36 | + * The OtelThreadContextRecord is already zero-initialized by the ProfiledThread ctor. |
| 37 | + */ |
| 38 | +void ContextApi::initializeContextTLS(ProfiledThread* thrd) { |
| 39 | + SignalBlocker blocker; |
| 40 | + // Set the TLS pointer permanently to this thread's record. |
| 41 | + // This first write triggers musl's TLS slot initialization (see above). |
| 42 | + // The pointer remains stable for the thread's lifetime; external profilers |
| 43 | + // rely solely on the valid flag for consistency, not pointer nullness. |
| 44 | + otel_thread_ctx_v1 = thrd->getOtelContextRecord(); |
| 45 | + thrd->markContextInitialized(); |
| 46 | +} |
| 47 | + |
| 48 | +bool ContextApi::get(u64& span_id, u64& root_span_id) { |
| 49 | + ProfiledThread* thrd = ProfiledThread::currentSignalSafe(); |
| 50 | + if (thrd == nullptr || !thrd->isContextInitialized()) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + OtelThreadContextRecord* record = thrd->getOtelContextRecord(); |
| 55 | + if (__atomic_load_n(&record->valid, __ATOMIC_ACQUIRE) != 1) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + u64 val = 0; |
| 59 | + for (int i = 0; i < 8; i++) { val = (val << 8) | record->span_id[i]; } |
| 60 | + span_id = val; |
| 61 | + |
| 62 | + root_span_id = thrd->getOtelLocalRootSpanId(); |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | +Context ContextApi::snapshot() { |
| 67 | + ProfiledThread* thrd = ProfiledThread::currentSignalSafe(); |
| 68 | + if (thrd == nullptr) { |
| 69 | + return {}; |
| 70 | + } |
| 71 | + size_t numAttrs = Profiler::instance()->numContextAttributes(); |
| 72 | + return thrd->snapshotContext(numAttrs); |
| 73 | +} |
| 74 | + |
| 75 | +void ContextApi::registerAttributeKeys(const char** keys, int count) { |
| 76 | + // Clip to DD_TAGS_CAPACITY: that is the actual sidecar slot limit and the |
| 77 | + // maximum keyIndex accepted by ThreadContext.setContextAttribute. |
| 78 | + int n = count < (int)DD_TAGS_CAPACITY ? count : (int)DD_TAGS_CAPACITY; |
| 79 | + |
| 80 | + // Build NULL-terminated key array for the process context config. |
| 81 | + // Index LOCAL_ROOT_SPAN_ATTR_INDEX (0) is reserved for local_root_span_id; user keys start at index 1. |
| 82 | + // otel_process_ctx_publish copies all strings, so no strdup is needed. |
| 83 | + const char* key_ptrs[DD_TAGS_CAPACITY + 2]; // +1 reserved, +1 null |
| 84 | + key_ptrs[LOCAL_ROOT_SPAN_ATTR_INDEX] = "datadog.local_root_span_id"; |
| 85 | + for (int i = 0; i < n; i++) { |
| 86 | + key_ptrs[i + 1] = keys[i]; |
| 87 | + } |
| 88 | + key_ptrs[n + 1] = nullptr; |
| 89 | + |
| 90 | + otel_thread_ctx_config_data config = { |
| 91 | + .schema_version = "tlsdesc_v1_dev", |
| 92 | + .attribute_key_map = key_ptrs, |
| 93 | + }; |
| 94 | + |
| 95 | +#ifndef OTEL_PROCESS_CTX_NO_READ |
| 96 | + otel_process_ctx_read_result read_result = otel_process_ctx_read(); |
| 97 | + if (read_result.success) { |
| 98 | + otel_process_ctx_data data = { |
| 99 | + .deployment_environment_name = read_result.data.deployment_environment_name, |
| 100 | + .service_instance_id = read_result.data.service_instance_id, |
| 101 | + .service_name = read_result.data.service_name, |
| 102 | + .service_version = read_result.data.service_version, |
| 103 | + .telemetry_sdk_language = read_result.data.telemetry_sdk_language, |
| 104 | + .telemetry_sdk_version = read_result.data.telemetry_sdk_version, |
| 105 | + .telemetry_sdk_name = read_result.data.telemetry_sdk_name, |
| 106 | + .resource_attributes = read_result.data.resource_attributes, |
| 107 | + .extra_attributes = read_result.data.extra_attributes, |
| 108 | + .thread_ctx_config = &config, |
| 109 | + }; |
| 110 | + |
| 111 | + otel_process_ctx_publish(&data); |
| 112 | + otel_process_ctx_read_drop(&read_result); |
| 113 | + } |
| 114 | +#endif |
| 115 | +} |
0 commit comments