Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default runtime-id tag #415

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/exporter_input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ struct ExporterInput {
std::string url; // url (can contain port and schema)
std::string port; // port appended to the host IP (ignored in agentless)
std::string debug_pprof_prefix; // local pprof prefix (debug)
bool do_export{true}; // prevent exports if needed (debug flag)
std::string runtime_id;
bool do_export{true}; // prevent exports if needed (debug flag)
std::string_view user_agent{
"ddprof"}; // ignored for now (override in shared lib)
std::string_view language{"native"}; // appended to the tags (set to native)
Expand Down
24 changes: 24 additions & 0 deletions include/uuid.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0. This product includes software
// developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present
// Datadog, Inc.

#pragma once

#include <array>
#include <cstdint>
#include <string>

namespace ddprof {
struct Uuid {
Uuid();
static constexpr int k_version_position = 12;
static constexpr int k_variant_position = 16;
static constexpr int k_version = 4;
[[nodiscard]] int get_version() const { return data[k_version_position]; }
[[nodiscard]] std::string to_string() const;
// We could make this smaller, as it is hexadecimal and 32 characters
// but we are keeping one byte per element for simplicity
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how this simplifies things, I would say either store chars and keeps it 32 bytes or store numbers and make it 16 bytes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it easier to reason on the values of every element.

std::array<uint8_t, 32> data;
};
} // namespace ddprof
13 changes: 13 additions & 0 deletions src/ddprof_cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "ddprof_defs.hpp"
#include "ddres.hpp"
#include "logger.hpp"
#include "uuid.hpp"
#include "version.hpp"

#include <chrono>
Expand Down Expand Up @@ -294,6 +295,17 @@ int DDProfCLI::parse(int argc, const char *argv[]) {
"A debug option to work without the Datadog agent.\n")
->group("")
->envname("DD_API_KEY"));
// In theory, the runtime_id should be read from tracing
// However the otel-tracers do not seem to provide this type of information
// To ensure timelines can be searched, we need to provide this
// Disclaimer: In full host, this does not really make sense.
// There should be several runtime_ids
extended_options.push_back(
app.add_option("--runtime_id,--runtime-id", exporter_input.runtime_id,
"Override the generated runtime_id.\n")
->default_val(Uuid().to_string())
->group("")
->envname("DD_PROFILING_RUNTIME_ID"));

extended_options.push_back(
app.add_option("--cpu_affinity,--cpu-affinity", cpu_affinity,
Expand Down Expand Up @@ -474,6 +486,7 @@ void DDProfCLI::print() const {
PRINT_NFO(" - host: %s", exporter_input.host.c_str());
PRINT_NFO(" - port: %s", exporter_input.port.c_str());
PRINT_NFO(" - do_export: %s", exporter_input.do_export ? "true" : "false");
PRINT_NFO(" - runtime_id: %s", exporter_input.runtime_id.c_str());

if (!tags.empty()) {
PRINT_NFO("Tags: %s", tags.c_str());
Expand Down
3 changes: 3 additions & 0 deletions src/exporter/ddprof_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ DDRes fill_stable_tags(const UserTags *user_tags,
exporter->_input.profiler_version));
}

DDRES_CHECK_FWD(
add_single_tag(tags_exporter, "runtime-id", exporter->_input.runtime_id));

for (const auto &el : user_tags->_tags) {
DDRES_CHECK_FWD(add_single_tag(tags_exporter, el.first, el.second));
}
Expand Down
59 changes: 59 additions & 0 deletions src/uuid.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0. This product includes software
// developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present
// Datadog, Inc.

#include <random>

#include "absl/strings/str_format.h"
#include "uuid.hpp"

namespace ddprof {
// NOLINTBEGIN(readability-magic-numbers)
Uuid::Uuid() {
std::random_device rd;
std::array<int32_t, 4> seed_data;
// Generate seed data
// as we only are using 4 integers, we avoid using the mersenne twister
std::generate_n(seed_data.data(), seed_data.size(), std::ref(rd));

// fill the data array with the seed data
// we do not need to worry about distribution as we limit to 15 with the mask
int index = 0;
for (const auto &seed : seed_data) {
data[index++] = (seed >> 28) & 0x0F;
data[index++] = (seed >> 24) & 0x0F;
data[index++] = (seed >> 20) & 0x0F;
data[index++] = (seed >> 16) & 0x0F;
data[index++] = (seed >> 12) & 0x0F;
data[index++] = (seed >> 8) & 0x0F;
data[index++] = (seed >> 4) & 0x0F;
data[index++] = seed & 0x0F;
}

// Set the version to 4 (UUID version 4)
data[k_version_position] = k_version;

// Variant is of the form 10XX
// So we set the first bits, then we use the random
// This should be random from 8 to 11 (8 to b)
data[k_variant_position] = 0x8 | (data[k_variant_position] & 0x03);
}

// we could loop instead to make things more readable,
// though that would be worse to understand the format
std::string Uuid::to_string() const {
return absl::StrFormat("%x%x%x%x%x%x%x%x-"
"%x%x%x%x-"
"%x%x%x%x-"
"%x%x%x%x-"
"%x%x%x%x%x%x%x%x%x%x%x%x",
data[0], data[1], data[2], data[3], data[4], data[5],
data[6], data[7], data[8], data[9], data[10], data[11],
data[12], data[13], data[14], data[15], data[16],
data[17], data[18], data[19], data[20], data[21],
data[22], data[23], data[24], data[25], data[26],
data[27], data[28], data[29], data[30], data[31]);
}
// NOLINTEND(readability-magic-numbers)
} // namespace ddprof
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ add_unit_test(
../src/logger_setup.cc
../src/perf_watcher.cc
../src/presets.cc
../src/uuid.cc
../src/tracepoint_config.cc
ddprof_context-ut.cc
LIBRARIES DDProf::Parser CLI11
Expand Down Expand Up @@ -370,6 +371,7 @@ add_unit_test(
../src/ddprof_cmdline_watcher.cc
../src/perf_watcher.cc
../src/tracepoint_config.cc
../src/uuid.cc
LIBRARIES DDProf::Parser CLI11)

add_unit_test(pthread_tls-ut pthread_tls-ut.cc)
Expand All @@ -392,6 +394,8 @@ add_unit_test(
add_unit_test(ddprof_module_lib-ut ddprof_module_lib-ut.cc ../src/ddprof_module_lib.cc
../src/build_id.cc ../src/dso.cc LIBRARIES ${ELFUTILS_LIBRARIES})

add_unit_test(uuid-ut ../src/uuid.cc ./uuid-ut.cc)

add_benchmark(savecontext-bench savecontext-bench.cc ../src/lib/pthread_fixes.cc
../src/lib/savecontext.cc ../src/lib/saveregisters.cc LIBRARIES llvm-demangle)

Expand Down
32 changes: 32 additions & 0 deletions test/uuid-ut.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0. This product includes software
// developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present
// Datadog, Inc.

#include <gtest/gtest.h>

#include "loghandle.hpp"
#include "uuid.hpp"

namespace ddprof {
TEST(Uuid, simple_class) {
ddprof::LogHandle loghandle;
Uuid uuid;
std::string uuid_str = uuid.to_string();
LG_DBG("uuid: %s", uuid_str.c_str());
EXPECT_EQ(uuid.get_version(), 4);
// Should be of the form
// 07a931f2-c8b5-4527-a80a-b7405be05c68
EXPECT_EQ(uuid_str.size(), 36);
EXPECT_EQ(uuid_str[8], '-');
EXPECT_EQ(uuid_str[13], '-');
EXPECT_EQ(uuid_str[14], '4');
EXPECT_TRUE(uuid_str[19] == '8' || uuid_str[19] == '9' ||
uuid_str[19] == 'a' || uuid_str[19] == 'b');
EXPECT_EQ(uuid_str[18], '-');
EXPECT_EQ(uuid_str[23], '-');

std::string uuid_2 = ddprof::Uuid().to_string();
EXPECT_NE(uuid.to_string(), uuid_2);
}
} // namespace ddprof