-
Notifications
You must be signed in to change notification settings - Fork 5
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
+137
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||
std::array<uint8_t, 32> data; | ||
}; | ||
} // namespace ddprof |
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
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 |
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
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.