Skip to content

Commit 89ffc47

Browse files
feat(profiling): add metric on sample count
1 parent b7a2d2a commit 89ffc47

File tree

12 files changed

+127
-21
lines changed

12 files changed

+127
-21
lines changed

ddtrace/internal/datadog/profiling/dd_wrapper/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ add_library(
4545
src/code_provenance.cpp
4646
src/code_provenance_interface.cpp
4747
src/ddup_interface.cpp
48+
src/profiler_stats.cpp
4849
src/profile.cpp
4950
src/sample.cpp
5051
src/sample_manager.cpp

ddtrace/internal/datadog/profiling/dd_wrapper/include/profile.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#pragma once
22

33
#include "constants.hpp"
4+
#include "profiler_stats.hpp"
45
#include "types.hpp"
56

67
#include <atomic>
7-
#include <memory>
88
#include <mutex>
9-
#include <string>
10-
#include <string_view>
119
#include <vector>
1210

1311
extern "C"
@@ -45,6 +43,8 @@ class Profile
4543
// cannot be used until it's initialized by libdatadog
4644
ddog_prof_Profile cur_profile{};
4745

46+
Datadog::ProfilerStats profiler_stats{};
47+
4848
public:
4949
// State management
5050
void one_time_init(SampleType type, unsigned int _max_nframes);
@@ -54,6 +54,9 @@ class Profile
5454
// Getters
5555
size_t get_sample_type_length();
5656
ddog_prof_Profile& profile_borrow();
57+
ProfilerStats& profiler_stats_borrow();
58+
59+
void profiler_stats_release();
5760
void profile_release();
5861

5962
// constref getters
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
5+
#include <string_view>
6+
7+
namespace Datadog {
8+
9+
class ProfilerStats
10+
{
11+
private:
12+
size_t sample_count = 0;
13+
size_t sampling_event_count = 0;
14+
15+
public:
16+
ProfilerStats() = default;
17+
~ProfilerStats() = default;
18+
19+
void increment_sample_count(size_t k_sample_count = 1);
20+
size_t get_sample_count();
21+
22+
void increment_sampling_event_count(size_t k_sampling_event_count = 1);
23+
size_t get_sampling_event_count();
24+
25+
std::string_view get_internal_metadata_json();
26+
27+
void reset_state();
28+
};
29+
30+
} // namespace Datadog

ddtrace/internal/datadog/profiling/dd_wrapper/include/sample.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "libdatadog_helpers.hpp"
44
#include "profile.hpp"
5+
#include "profiler_stats.hpp"
56
#include "types.hpp"
67

78
#include <string>
@@ -135,6 +136,7 @@ class Sample
135136
bool flush_sample(bool reverse_locations = false);
136137

137138
static ddog_prof_Profile& profile_borrow();
139+
static ProfilerStats& profiler_stats_borrow();
138140
static void profile_release();
139141
static void postfork_child();
140142
Sample(SampleType _type_mask, unsigned int _max_nframes);

ddtrace/internal/datadog/profiling/dd_wrapper/include/uploader.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#pragma once
22

3-
#include "sample.hpp"
4-
#include "types.hpp"
3+
#include "profiler_stats.hpp"
54

65
#include <atomic>
7-
#include <memory>
86
#include <mutex>
97

108
extern "C"
@@ -27,7 +25,7 @@ class Uploader
2725
bool export_to_file(ddog_prof_EncodedProfile* encoded);
2826

2927
public:
30-
bool upload(ddog_prof_Profile& profile);
28+
bool upload(ddog_prof_Profile& profile, Datadog::ProfilerStats& profiler_stats);
3129
static void cancel_inflight();
3230
static void lock();
3331
static void unlock();

ddtrace/internal/datadog/profiling/dd_wrapper/include/uploader_builder.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

3+
#include "constants.hpp"
34
#include "uploader.hpp"
45

5-
#include <mutex>
66
#include <string>
77
#include <string_view>
88
#include <unordered_map>

ddtrace/internal/datadog/profiling/dd_wrapper/src/ddup_interface.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include "ddup_interface.hpp"
22

3-
#include "code_provenance.hpp"
43
#include "libdatadog_helpers.hpp"
5-
#include "profile.hpp"
4+
#include "profiler_stats.hpp"
65
#include "sample.hpp"
76
#include "sample_manager.hpp"
87
#include "uploader.hpp"
@@ -351,7 +350,8 @@ ddup_upload() // cppcheck-suppress unusedFunction
351350
// be modified. It gets released and cleared after uploading.
352351
// * Uploading cancels inflight uploads. There are better ways to do this, but this is what
353352
// we have for now.
354-
uploader.upload(Datadog::Sample::profile_borrow());
353+
uploader.upload(Datadog::Sample::profile_borrow(), Datadog::Sample::profiler_stats_borrow());
354+
Datadog::Sample::profiler_stats_borrow().reset_state();
355355
Datadog::Sample::profile_release();
356356
return true;
357357
}

ddtrace/internal/datadog/profiling/dd_wrapper/src/profile.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Datadog::Profile::reset_profile()
5555
ddog_Error_drop(&err);
5656
return false;
5757
}
58+
59+
profiler_stats.reset_state();
5860
return true;
5961
}
6062

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "profiler_stats.hpp"
2+
3+
#include <charconv>
4+
#include <string>
5+
6+
namespace {
7+
8+
void
9+
append_to_string(std::string& s, size_t value)
10+
{
11+
char buf[128];
12+
auto [ptr, ec] = std::to_chars(std::begin(buf), std::end(buf), value);
13+
s.append(buf, ptr);
14+
}
15+
16+
} // namespace
17+
18+
void
19+
Datadog::ProfilerStats::increment_sample_count(size_t k_sample_count)
20+
{
21+
sample_count += k_sample_count;
22+
}
23+
24+
void
25+
Datadog::ProfilerStats::increment_sampling_event_count(size_t k_sampling_event_count)
26+
{
27+
sampling_event_count += k_sampling_event_count;
28+
}
29+
30+
size_t
31+
Datadog::ProfilerStats::get_sampling_event_count()
32+
{
33+
return sampling_event_count;
34+
}
35+
36+
size_t
37+
Datadog::ProfilerStats::get_sample_count()
38+
{
39+
return sample_count;
40+
}
41+
42+
void
43+
Datadog::ProfilerStats::reset_state()
44+
{
45+
sample_count = 0;
46+
}
47+
48+
std::string_view
49+
Datadog::ProfilerStats::get_internal_metadata_json()
50+
{
51+
static std::string result;
52+
result.reserve(2048);
53+
54+
result = "{";
55+
56+
result += "\"recorded_samples\": ";
57+
append_to_string(result, sample_count);
58+
59+
result += "}";
60+
61+
return result;
62+
}

ddtrace/internal/datadog/profiling/dd_wrapper/src/sample.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
#include "sample.hpp"
22

3-
#include "code_provenance.hpp"
4-
53
#include <algorithm>
64
#include <chrono>
75
#include <string_view>
8-
#include <thread>
96

107
Datadog::internal::StringArena::StringArena()
118
{
@@ -532,6 +529,12 @@ Datadog::Sample::profile_borrow()
532529
return profile_state.profile_borrow();
533530
}
534531

532+
Datadog::ProfilerStats&
533+
Datadog::Sample::profiler_stats_borrow()
534+
{
535+
return profile_state.profiler_stats_borrow();
536+
}
537+
535538
void
536539
Datadog::Sample::profile_release()
537540
{

0 commit comments

Comments
 (0)