forked from Netflix/spectator-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace invalid characters in ids (Netflix#106)
It is easy enough to break the spectatord line protocol, if any of the control characters (`:,=`) are inserted in unexpected places. Since metric names and tags are often programmatically generated, we want to only construct id strings which are valid.
- Loading branch information
1 parent
8d9ab60
commit 5c85b31
Showing
16 changed files
with
222 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
.vscode/ | ||
bazel-* | ||
build-* | ||
spectator/valid_chars.inc |
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
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
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
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,34 @@ | ||
#include "stateless_meters.h" | ||
#include "test_publisher.h" | ||
#include <gtest/gtest.h> | ||
|
||
namespace { | ||
|
||
using spectator::Id; | ||
using spectator::MonotonicCounterUint; | ||
using spectator::Tags; | ||
using spectator::TestPublisher; | ||
|
||
TEST(MonotonicCounterUint, Set) { | ||
TestPublisher publisher; | ||
auto id = std::make_shared<Id>("ctr", Tags{}); | ||
auto id2 = std::make_shared<Id>("ctr2", Tags{{"key", "val"}}); | ||
MonotonicCounterUint<TestPublisher> c{id, &publisher}; | ||
MonotonicCounterUint<TestPublisher> c2{id2, &publisher}; | ||
|
||
c.Set(42); | ||
c2.Set(2); | ||
c.Set(-1); | ||
std::vector<std::string> expected = {"U:ctr:42", "U:ctr2,key=val:2", "U:ctr:18446744073709551615"}; | ||
EXPECT_EQ(publisher.SentMessages(), expected); | ||
} | ||
|
||
TEST(MonotonicCounterUint, InvalidTags) { | ||
TestPublisher publisher; | ||
// test with a single tag, because tags order is not guaranteed in a flat_hash_map | ||
auto id = std::make_shared<Id>("test`!@#$%^&*()-=~_+[]{}\\|;:'\",<.>/?foo", | ||
Tags{{"tag1,:=", "value1,:="}}); | ||
MonotonicCounterUint c{id, &publisher}; | ||
EXPECT_EQ("U:test______^____-_~______________.___foo,tag1___=value1___:", c.GetPrefix()); | ||
} | ||
} // namespace |
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
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,48 @@ | ||
// generate the atlas valid charsets | ||
|
||
#include <array> | ||
#include <fstream> | ||
|
||
void dump_array(std::ostream& os, const std::string& name, const std::array<bool, 256>& chars) { | ||
os << "static constexpr std::array<bool, 256> " << name << " = {{"; | ||
|
||
os << chars[0]; | ||
for (auto i = 1u; i < chars.size(); ++i) { | ||
os << ", " << chars[i]; | ||
} | ||
|
||
os << "}};\n"; | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
std::ofstream of; | ||
if (argc > 1) { | ||
of.open(argv[1]); | ||
} else { | ||
of.open("/dev/stdout"); | ||
} | ||
|
||
// default false | ||
std::array<bool, 256> charsAllowed{}; | ||
for (int i = 0; i < 256; ++i) { | ||
charsAllowed[i] = false; | ||
} | ||
|
||
// configure allowed characters | ||
charsAllowed['.'] = true; | ||
charsAllowed['-'] = true; | ||
|
||
for (auto ch = '0'; ch <= '9'; ++ch) { | ||
charsAllowed[ch] = true; | ||
} | ||
for (auto ch = 'a'; ch <= 'z'; ++ch) { | ||
charsAllowed[ch] = true; | ||
} | ||
for (auto ch = 'A'; ch <= 'Z'; ++ch) { | ||
charsAllowed[ch] = true; | ||
} | ||
charsAllowed['~'] = true; | ||
charsAllowed['^'] = true; | ||
|
||
dump_array(of, "kAtlasChars", charsAllowed); | ||
} |