-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(profiling): endpoint profiling for stack v2 (#10882)
- Loading branch information
1 parent
2ea5415
commit ddf16b0
Showing
9 changed files
with
176 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
ddtrace/internal/datadog/profiling/stack_v2/include/thread_span_links.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <mutex> | ||
#include <stdint.h> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
namespace Datadog { | ||
|
||
struct Span | ||
{ | ||
uint64_t span_id; | ||
uint64_t local_root_span_id; | ||
std::string span_type; | ||
|
||
Span(uint64_t _span_id, uint64_t _local_root_span_id, std::string _span_type) | ||
: span_id(_span_id) | ||
, local_root_span_id(_local_root_span_id) | ||
, span_type(_span_type) | ||
{ | ||
} | ||
}; | ||
|
||
class ThreadSpanLinks | ||
{ | ||
public: | ||
static ThreadSpanLinks& get_instance() | ||
{ | ||
static ThreadSpanLinks instance; | ||
return instance; | ||
} | ||
|
||
// Delete Copy constructor and assignment operator to prevent copies | ||
ThreadSpanLinks(ThreadSpanLinks const&) = delete; | ||
ThreadSpanLinks& operator=(ThreadSpanLinks const&) = delete; | ||
|
||
void link_span(uint64_t thread_id, uint64_t span_id, uint64_t local_root_span_id, std::string span_type); | ||
|
||
const Span* get_active_span_from_thread_id(uint64_t thread_id); | ||
|
||
static void postfork_child(); | ||
|
||
private: | ||
std::mutex mtx; | ||
std::unordered_map<uint64_t, std::unique_ptr<Span>> thread_id_to_span; | ||
|
||
// Private Constructor/Destructor | ||
ThreadSpanLinks() = default; | ||
~ThreadSpanLinks() = default; | ||
|
||
void reset(); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
ddtrace/internal/datadog/profiling/stack_v2/src/thread_span_links.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "thread_span_links.hpp" | ||
|
||
#include <iostream> | ||
#include <mutex> | ||
#include <stdint.h> | ||
#include <string> | ||
|
||
namespace Datadog { | ||
void | ||
ThreadSpanLinks::link_span(uint64_t thread_id, uint64_t span_id, uint64_t local_root_span_id, std::string span_type) | ||
{ | ||
std::lock_guard<std::mutex> lock(mtx); | ||
|
||
if (thread_id_to_span.find(thread_id) == thread_id_to_span.end()) { | ||
thread_id_to_span[thread_id] = std::make_unique<Span>(span_id, local_root_span_id, span_type); | ||
} | ||
thread_id_to_span[thread_id]->span_id = span_id; | ||
thread_id_to_span[thread_id]->local_root_span_id = local_root_span_id; | ||
thread_id_to_span[thread_id]->span_type = span_type; | ||
} | ||
|
||
const Span* | ||
ThreadSpanLinks::get_active_span_from_thread_id(uint64_t thread_id) | ||
{ | ||
std::lock_guard<std::mutex> lock(mtx); | ||
|
||
if (thread_id_to_span.find(thread_id) == thread_id_to_span.end()) { | ||
return nullptr; | ||
} | ||
return thread_id_to_span[thread_id].get(); | ||
} | ||
|
||
void | ||
ThreadSpanLinks::reset() | ||
{ | ||
std::lock_guard<std::mutex> lock(mtx); | ||
thread_id_to_span.clear(); | ||
} | ||
|
||
void | ||
ThreadSpanLinks::postfork_child() | ||
{ | ||
// Explicitly destroy and reconstruct the mutex to avoid undefined behavior | ||
get_instance().mtx.~mutex(); | ||
new (&get_instance().mtx) std::mutex(); | ||
|
||
get_instance().reset(); | ||
} | ||
|
||
} // namespace Datadog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
releasenotes/notes/profiling-fix-stack-v2-endpoint-82a1e26366166b8d.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
fixes: | ||
- | | ||
profiling: enables endpoint profiling for stack v2, ``DD_PROFILING_STACK_V2_ENABLED`` | ||
is set. | ||