Skip to content

Commit

Permalink
Merge branch 'main' into fixes_2446
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff authored Jan 8, 2024
2 parents 25a2d19 + 74db5e6 commit 70aa3f3
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 26 deletions.
7 changes: 0 additions & 7 deletions api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ if(OPENTELEMETRY_INSTALL)
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(
DIRECTORY include/opentelemetry
DESTINATION include
FILES_MATCHING
PATTERN "*.h"
PATTERN "metrics" EXCLUDE)

install(
DIRECTORY include/opentelemetry
DESTINATION include
Expand Down
2 changes: 1 addition & 1 deletion docs/cpp-ostream-exporter-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public:
return sdktrace::ExportResult::kSuccess;
}

bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept
bool Shutdown(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept
{
isShutdown = true;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ class ElasticsearchLogRecordExporter final : public opentelemetry::sdk::logs::Lo
* @return return true when all data are exported, and false when timeout
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shutdown this exporter.
* @param timeout The maximum time to wait for the shutdown method to return
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
// Stores if this exporter had its Shutdown() method called
Expand Down
2 changes: 1 addition & 1 deletion exporters/elasticsearch/src/es_log_record_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ bool ElasticsearchLogRecordExporter::ForceFlush(
std::chrono::duration_cast<std::chrono::steady_clock::duration>(timeout);
if (timeout_steady <= std::chrono::steady_clock::duration::zero())
{
timeout_steady = std::chrono::steady_clock::duration::max();
timeout_steady = (std::chrono::steady_clock::duration::max)();
}

std::unique_lock<std::mutex> lk_cv(synchronization_data_->force_flush_cv_m);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class OtlpGrpcExporter final : public opentelemetry::sdk::trace::SpanExporter
* @return return true when all data are exported, and false when timeout
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shut down the exporter.
Expand All @@ -67,7 +67,7 @@ class OtlpGrpcExporter final : public opentelemetry::sdk::trace::SpanExporter
* @return return the status of this operation
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
// The configuration options associated with this exporter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ class OtlpGrpcLogRecordExporter : public opentelemetry::sdk::logs::LogRecordExpo
* @return return true when all data are exported, and false when timeout
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shutdown this exporter.
* @param timeout The maximum time to wait for the shutdown method to return.
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
// Configuration options for the exporter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporter final : public opentelemetry::sdk::t
* @return return true when all data are exported, and false when timeout
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shut down the exporter.
Expand All @@ -68,7 +68,7 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporter final : public opentelemetry::sdk::t
* @return return the status of this operation
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
// The configuration options associated with this exporter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ class OtlpHttpLogRecordExporter final : public opentelemetry::sdk::logs::LogReco
* @return return true when all data are exported, and false when timeout
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shutdown this exporter.
* @param timeout The maximum time to wait for the shutdown method to return
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
// Configuration options for the exporter
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/src/otlp_http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ bool OtlpHttpClient::ForceFlush(std::chrono::microseconds timeout) noexcept
std::chrono::duration_cast<std::chrono::steady_clock::duration>(timeout);
if (timeout_steady <= std::chrono::steady_clock::duration::zero())
{
timeout_steady = std::chrono::steady_clock::duration::max();
timeout_steady = (std::chrono::steady_clock::duration::max)();
}

while (timeout_steady > std::chrono::steady_clock::duration::zero())
Expand Down
91 changes: 91 additions & 0 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <algorithm>
#include <limits>
#include <regex>
#include <sstream>
Expand All @@ -13,6 +14,7 @@
#include "prometheus/metric_family.h"
#include "prometheus/metric_type.h"

#include "opentelemetry/common/macros.h"
#include "opentelemetry/exporters/prometheus/exporter_utils.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/sdk/resource/resource.h"
Expand Down Expand Up @@ -280,11 +282,13 @@ std::string PrometheusExporterUtils::SanitizeNames(std::string name)
return name;
}

#if OPENTELEMETRY_HAVE_WORKING_REGEX
std::regex INVALID_CHARACTERS_PATTERN("[^a-zA-Z0-9]");
std::regex CHARACTERS_BETWEEN_BRACES_PATTERN("\\{(.*?)\\}");
std::regex SANITIZE_LEADING_UNDERSCORES("^_+");
std::regex SANITIZE_TRAILING_UNDERSCORES("_+$");
std::regex SANITIZE_CONSECUTIVE_UNDERSCORES("[_]{2,}");
#endif

std::string PrometheusExporterUtils::GetEquivalentPrometheusUnit(
const std::string &raw_metric_unit_name)
Expand Down Expand Up @@ -360,7 +364,32 @@ std::string PrometheusExporterUtils::GetPrometheusPerUnit(const std::string &per

std::string PrometheusExporterUtils::RemoveUnitPortionInBraces(const std::string &unit)
{
#if OPENTELEMETRY_HAVE_WORKING_REGEX
return std::regex_replace(unit, CHARACTERS_BETWEEN_BRACES_PATTERN, "");
#else
bool in_braces = false;
std::string cleaned_unit;
cleaned_unit.reserve(unit.size());
for (auto c : unit)
{
if (in_braces)
{
if (c == '}')
{
in_braces = false;
}
}
else if (c == '{')
{
in_braces = true;
}
else
{
cleaned_unit += c;
}
}
return cleaned_unit;
#endif
}

std::string PrometheusExporterUtils::ConvertRateExpressedToPrometheusUnit(
Expand Down Expand Up @@ -389,12 +418,74 @@ std::string PrometheusExporterUtils::ConvertRateExpressedToPrometheusUnit(

std::string PrometheusExporterUtils::CleanUpString(const std::string &str)
{
#if OPENTELEMETRY_HAVE_WORKING_REGEX
std::string cleaned_string = std::regex_replace(str, INVALID_CHARACTERS_PATTERN, "_");
cleaned_string = std::regex_replace(cleaned_string, SANITIZE_CONSECUTIVE_UNDERSCORES, "_");
cleaned_string = std::regex_replace(cleaned_string, SANITIZE_TRAILING_UNDERSCORES, "");
cleaned_string = std::regex_replace(cleaned_string, SANITIZE_LEADING_UNDERSCORES, "");
return cleaned_string;
#else
std::string cleaned_string = str;
if (cleaned_string.empty())
{
return cleaned_string;
}
std::transform(cleaned_string.begin(), cleaned_string.end(), cleaned_string.begin(),
[](const char c) {
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
return c;
}
return '_';
});

std::string::size_type trim_start = 0;
std::string::size_type trim_end = 0;
bool previous_underscore = false;
for (std::string::size_type i = 0; i < cleaned_string.size(); ++i)
{
if (cleaned_string[i] == '_')
{
if (previous_underscore)
{
continue;
}

previous_underscore = true;
}
else
{
previous_underscore = false;
}

if (trim_end != i)
{
cleaned_string[trim_end] = cleaned_string[i];
}
++trim_end;
}

while (trim_end > 0 && cleaned_string[trim_end - 1] == '_')
{
--trim_end;
}
while (trim_start < trim_end && cleaned_string[trim_start] == '_')
{
++trim_start;
}

// All characters are underscore
if (trim_start >= trim_end)
{
return "_";
}
if (0 != trim_start || cleaned_string.size() != trim_end)
{
return cleaned_string.substr(trim_start, trim_end - trim_start);
}

return cleaned_string;
#endif
}

std::string PrometheusExporterUtils::MapToPrometheusName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class BatchLogRecordProcessor : public LogRecordProcessor
* NOTE: Timeout functionality not supported yet.
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shuts down the processor and does any cleanup required. Completely drains the buffer/queue of
Expand All @@ -82,7 +82,7 @@ class BatchLogRecordProcessor : public LogRecordProcessor
* NOTE: Timeout functionality not supported yet.
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Class destructor which invokes the Shutdown() method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MultiLogRecordProcessor : public LogRecordProcessor
* @return a result code indicating whether it succeeded, failed or timed out
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shuts down the processor and does any cleanup required.
Expand All @@ -55,7 +55,7 @@ class MultiLogRecordProcessor : public LogRecordProcessor
* @return true if the shutdown succeeded, false otherwise
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

private:
std::vector<std::unique_ptr<LogRecordProcessor>> processors_;
Expand Down
4 changes: 2 additions & 2 deletions sdk/include/opentelemetry/sdk/trace/batch_span_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class BatchSpanProcessor : public SpanProcessor
* NOTE: Timeout functionality not supported yet.
*/
bool ForceFlush(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shuts down the processor and does any cleanup required. Completely drains the buffer/queue of
Expand All @@ -79,7 +79,7 @@ class BatchSpanProcessor : public SpanProcessor
* NOTE: Timeout functionality not supported yet.
*/
bool Shutdown(
std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override;
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Class destructor which invokes the Shutdown() method. The Shutdown() method is supposed to be
Expand Down

0 comments on commit 70aa3f3

Please sign in to comment.