-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add compression library to helper * Implement sampler and processor trigger
- Loading branch information
1 parent
b5d3b66
commit 3b04614
Showing
20 changed files
with
688 additions
and
12 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
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,105 @@ | ||
// Unless explicitly stated otherwise all files in this repository are | ||
// dual-licensed under the Apache-2.0 License or BSD-3-Clause License. | ||
// | ||
// This product includes software developed at Datadog | ||
// (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. | ||
|
||
#include "compression.hpp" | ||
#include <cstdint> | ||
#include <string> | ||
#include <zlib.h> | ||
|
||
namespace dds { | ||
|
||
namespace { | ||
constexpr int64_t encoding = -0xf; | ||
constexpr int max_round_decompression = 100; | ||
// Taken from PHP approach | ||
// https://heap.space/xref/PHP-7.3/ext/zlib/php_zlib.h?r=8d3f8ca1#36 | ||
size_t estimate_compressed_size(size_t in_len) | ||
{ | ||
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) | ||
return (((size_t)((double)in_len * (double)1.015)) + 10 + 8 + 4 + 1); | ||
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) | ||
} | ||
} // namespace | ||
|
||
// The implementation of this function is based on how PHP does | ||
// https://heap.space/xref/PHP-7.3/ext/zlib/zlib.c?r=9afce019#336 | ||
std::optional<std::string> compress(const std::string &text) | ||
{ | ||
std::string ret_string; | ||
z_stream strm = {}; | ||
|
||
if (text.length() == 0) { | ||
return std::nullopt; | ||
} | ||
|
||
if (Z_OK == deflateInit2(&strm, -1, Z_DEFLATED, encoding, MAX_MEM_LEVEL, | ||
Z_DEFAULT_STRATEGY)) { | ||
auto size = estimate_compressed_size(text.length()); | ||
ret_string.resize(size); | ||
|
||
// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) | ||
strm.next_in = reinterpret_cast<const Bytef *>(text.data()); | ||
strm.next_out = reinterpret_cast<Bytef *>(ret_string.data()); | ||
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) | ||
strm.avail_in = text.length(); | ||
strm.avail_out = size; | ||
|
||
if (Z_STREAM_END == deflate(&strm, Z_FINISH)) { | ||
deflateEnd(&strm); | ||
/* size buffer down to actual length */ | ||
ret_string.resize(strm.total_out); | ||
ret_string.shrink_to_fit(); | ||
return ret_string; | ||
} | ||
deflateEnd(&strm); | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
// Taken from PHP approach | ||
// https://heap.space/xref/PHP-7.3/ext/zlib/zlib.c?r=9afce019#422 | ||
std::optional<std::string> uncompress(const std::string &compressed) | ||
{ | ||
int round = 0; | ||
size_t used = 0; | ||
size_t free; | ||
size_t capacity; | ||
z_stream strm = {}; | ||
|
||
if (compressed.length() < 1 || Z_OK != inflateInit2(&strm, encoding)) { | ||
return std::nullopt; | ||
} | ||
|
||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) | ||
strm.next_in = reinterpret_cast<const Bytef *>(compressed.data()); | ||
strm.avail_in = compressed.length(); | ||
std::string output; | ||
int status = Z_OK; | ||
|
||
capacity = strm.avail_in; | ||
output.resize(capacity); | ||
while ((Z_BUF_ERROR == status || (Z_OK == status && strm.avail_in > 0)) && | ||
++round < max_round_decompression) { | ||
strm.avail_out = free = capacity - used; | ||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) | ||
strm.next_out = reinterpret_cast<uint8_t *>(output.data()) + used; | ||
status = inflate(&strm, Z_NO_FLUSH); | ||
used += free - strm.avail_out; | ||
capacity += (output.size() >> 3) + 1; | ||
output.resize(capacity); | ||
} | ||
if (status == Z_STREAM_END) { | ||
inflateEnd(&strm); | ||
output.resize(used); | ||
output.shrink_to_fit(); | ||
return output; | ||
} | ||
inflateEnd(&strm); | ||
|
||
return std::nullopt; | ||
} | ||
|
||
} // namespace dds |
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,16 @@ | ||
// Unless explicitly stated otherwise all files in this repository are | ||
// dual-licensed under the Apache-2.0 License or BSD-3-Clause License. | ||
// | ||
// This product includes software developed at Datadog | ||
// (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. | ||
#pragma once | ||
|
||
#include <optional> | ||
#include <string> | ||
|
||
namespace dds { | ||
|
||
std::optional<std::string> compress(const std::string &text); | ||
std::optional<std::string> uncompress(const std::string &compressed); | ||
|
||
} // namespace dds |
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,91 @@ | ||
// Unless explicitly stated otherwise all files in this repository are | ||
// dual-licensed under the Apache-2.0 License or BSD-3-Clause License. | ||
// | ||
// This product includes software developed at Datadog | ||
// (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc. | ||
|
||
#pragma once | ||
|
||
#include <atomic> | ||
#include <cmath> | ||
#include <cstdint> | ||
#include <iostream> | ||
#include <mutex> | ||
#include <optional> | ||
|
||
namespace dds { | ||
static const double min_rate = 0.0001; | ||
class sampler { | ||
public: | ||
sampler(double sample_rate) : sample_rate_(sample_rate) | ||
{ | ||
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) | ||
if (sample_rate_ <= 0) { | ||
sample_rate_ = 0; | ||
} else if (sample_rate_ > 1) { | ||
sample_rate_ = 1; | ||
} else if (sample_rate_ < min_rate) { | ||
sample_rate_ = min_rate; | ||
} | ||
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) | ||
} | ||
class scope { | ||
public: | ||
explicit scope(std::atomic<bool> &concurrent) : concurrent_(&concurrent) | ||
{ | ||
*concurrent_ = true; | ||
} | ||
|
||
scope(const scope &) = delete; | ||
scope &operator=(const scope &) = delete; | ||
scope(scope &&oth) noexcept | ||
{ | ||
concurrent_ = oth.concurrent_; | ||
oth.concurrent_ = nullptr; | ||
} | ||
scope &operator=(scope &&oth) | ||
{ | ||
concurrent_ = oth.concurrent_; | ||
oth.concurrent_ = nullptr; | ||
|
||
return *this; | ||
} | ||
|
||
~scope() | ||
{ | ||
if (concurrent_ != nullptr) { | ||
*concurrent_ = false; | ||
} | ||
} | ||
|
||
protected: | ||
std::atomic<bool> *concurrent_; | ||
}; | ||
|
||
std::optional<scope> get() | ||
{ | ||
const std::lock_guard<std::mutex> lock_guard(mtx_); | ||
|
||
std::optional<scope> result = std::nullopt; | ||
|
||
if (!concurrent_ && floor(request_ * sample_rate_) != | ||
floor((request_ + 1) * sample_rate_)) { | ||
result = {scope{concurrent_}}; | ||
} | ||
|
||
if (request_ < UINT_MAX) { | ||
request_++; | ||
} else { | ||
request_ = 1; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
protected: | ||
unsigned request_{1}; | ||
double sample_rate_; | ||
std::atomic<bool> concurrent_{false}; | ||
std::mutex mtx_; | ||
}; | ||
} // namespace dds |
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
Oops, something went wrong.