Skip to content

Commit

Permalink
Merge branch 'meter-filter-view' of github.com:lalitb/opentelemetry-c…
Browse files Browse the repository at this point in the history
…pp into meter-filter-view
  • Loading branch information
lalitb committed Feb 20, 2022
2 parents e0f11a7 + 009952d commit 7947f1e
Show file tree
Hide file tree
Showing 33 changed files with 1,227 additions and 647 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}")

include(CTest)
if(BUILD_TESTING)
add_definitions(-DENABLE_TEST)
if(EXISTS ${CMAKE_BINARY_DIR}/lib/libgtest.a)
# Prefer GTest from build tree. GTest is not always working with
# CMAKE_PREFIX_PATH
Expand Down Expand Up @@ -426,6 +427,7 @@ include_directories(api/include)
add_subdirectory(api)

if(NOT WITH_API_ONLY)
set(BUILD_TESTING ${BUILD_TESTING})
include_directories(sdk/include)
include_directories(sdk)
include_directories(ext/include)
Expand Down
12 changes: 12 additions & 0 deletions api/include/opentelemetry/context/runtime_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ class RuntimeContext
GetStorage() = storage;
}

/**
* Provide a pointer to const runtime context storage.
*
* The returned pointer can only be used for extending the lifetime of the runtime context
* storage.
*
*/
static nostd::shared_ptr<const RuntimeContextStorage> GetConstRuntimeContextStorage() noexcept
{
return GetRuntimeContextStorage();
}

private:
static nostd::shared_ptr<RuntimeContextStorage> GetRuntimeContextStorage() noexcept
{
Expand Down
2 changes: 1 addition & 1 deletion ci/do_ci.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $VCPKG_DIR="$SRC_DIR\vcpkg"

switch ($action) {
"bazel.build" {
bazel build $BAZEL_OPTIONS -- //...
bazel build --copt=-DENABLE_TEST $BAZEL_OPTIONS -- //...
$exit = $LASTEXITCODE
if ($exit -ne 0) {
exit $exit
Expand Down
2 changes: 1 addition & 1 deletion ci/do_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mkdir -p "${BUILD_DIR}"
[ -z "${PLUGIN_DIR}" ] && export PLUGIN_DIR=$HOME/plugin
mkdir -p "${PLUGIN_DIR}"

BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW"
BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --copt=-DENABLE_TEST"
BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors"

# https://github.com/bazelbuild/bazel/issues/4341
Expand Down
8 changes: 7 additions & 1 deletion ci/setup_thrift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set -e
export DEBIAN_FRONTEND=noninteractive
export THRIFT_VERSION=0.14.1

apt update

if ! type cmake > /dev/null; then
#cmake not installed, exiting
exit 1
Expand All @@ -12,7 +14,7 @@ export BUILD_DIR=/tmp/
export INSTALL_DIR=/usr/local/

apt install -y --no-install-recommends \
libboost-all-dev \
libboost-locale-dev \
libevent-dev \
libssl-dev \
ninja-build
Expand All @@ -38,6 +40,10 @@ cmake -G Ninja .. \
-DBUILD_JAVA=OFF \
-DBUILD_TESTING=OFF \
-DBUILD_TUTORIALS=OFF \
-DWITH_STDTHREADS=ON \
-DWITH_BOOSTTHREADS=OFF \
-DWITH_BOOST_FUNCTIONAL=OFF \
-DWITH_BOOST_SMART_PTR=OFF \
..

ninja -j $(nproc)
Expand Down
33 changes: 32 additions & 1 deletion exporters/ostream/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,39 @@ cc_library(
],
)

cc_library(
name = "ostream_metric_exporter",
srcs = [
"src/metric_exporter.cc",
],
hdrs = [
"include/opentelemetry/exporters/ostream/metric_exporter.h",
],
strip_include_prefix = "include",
tags = [
"metrics",
"ostream",
],
deps = [
"//sdk/src/metrics",
],
)

cc_test(
name = "ostream_metric_test",
srcs = ["test/ostream_metric_test.cc"],
tags = [
"ostream",
"test",
],
deps = [
":ostream_metric_exporter",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "ostream_metrics_test",
name = "ostream_metrics_test_deprecated",
srcs = ["test/ostream_metrics_test.cc"],
tags = [
"ostream",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW

# include <iostream>
# include <string>
# include "opentelemetry/common/spin_lock_mutex.h"
# include "opentelemetry/nostd/span.h"
# include "opentelemetry/sdk/metrics/instruments.h"
# include "opentelemetry/sdk/metrics/metric_exporter.h"
# include "opentelemetry/sdk/metrics/recordable.h"
# include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace metrics
{

/**
* The OStreamMetricExporter exports record data through an ostream
*/
class OStreamMetricExporter final : public opentelemetry::sdk::metrics::MetricExporter
{
public:
/**
* Create an OStreamMetricExporter. This constructor takes in a reference to an ostream that the
* export() function will send span data into.
* The default ostream is set to stdout
*/
explicit OStreamMetricExporter(std::ostream &sout = std::cout) noexcept;

/**
* Export
* @param records a span of unique pointers to metrics data
*/
sdk::common::ExportResult Export(
const nostd::span<std::unique_ptr<opentelemetry::sdk::metrics::MetricData>> &records) noexcept
override;

/**
* Force flush the exporter.
*/
bool ForceFlush(
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shut down the exporter.
* @param timeout an optional timeout, the default timeout of 0 means that no
* timeout is applied.
* @return return the status of this operation
*/
bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override;

private:
std::ostream &sout_;
bool is_shutdown_ = false;
mutable opentelemetry::common::SpinLockMutex lock_;
bool isShutdown() const noexcept;
void printPointData(opentelemetry::sdk::metrics::PointType &point_data);
};
} // namespace metrics
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
#endif
150 changes: 150 additions & 0 deletions exporters/ostream/src/metric_exporter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#ifndef ENABLE_METRICS_PREVIEW
# include "opentelemetry/exporters/ostream/metric_exporter.h"
# include <algorithm>
# include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
# include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h"
# include "opentelemetry/sdk_config.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace metrics
{

OStreamMetricExporter::OStreamMetricExporter(std::ostream &sout) noexcept : sout_(sout) {}

sdk::common::ExportResult OStreamMetricExporter::Export(
const nostd::span<std::unique_ptr<opentelemetry::sdk::metrics::MetricData>> &records) noexcept
{
if (isShutdown())
{
OTEL_INTERNAL_LOG_ERROR("[OStream Metric] Exporting "
<< records.size() << " records(s) failed, exporter is shutdown");
return sdk::common::ExportResult::kFailure;
}

for (auto &record : records)
{
sout_ << "{"
<< "\n name : " << record->instrumentation_library_->GetName()
<< "\n version : " << record->instrumentation_library_->GetVersion();
printPointData(record->point_data_);
sout_ << "\n}\n";
}
return sdk::common::ExportResult::kSuccess;
}

template <typename T>
inline void printVec(std::ostream &os, std::vector<T> &vec)
{
os << '[';
if (vec.size() > 1)
{
std::copy(vec.begin(), vec.end() - 1, std::ostream_iterator<T>(os, ", "));
}
if (!vec.empty())
{
os << vec.back();
}
os << ']';
}

void OStreamMetricExporter::printPointData(opentelemetry::sdk::metrics::PointType &point_data)
{
if (nostd::holds_alternative<sdk::metrics::SumPointData>(point_data))
{
auto sum_point_data = nostd::get<sdk::metrics::SumPointData>(point_data);
sout_ << "\n type : SumPointData";
sout_ << "\n start timestamp : "
<< std::to_string(sum_point_data.start_epoch_nanos_.time_since_epoch().count());
sout_ << "\n end timestamp : "
<< std::to_string(sum_point_data.end_epoch_nanos_.time_since_epoch().count());
sout_ << "\n value : ";
if (nostd::holds_alternative<double>(sum_point_data.value_))
{
sout_ << nostd::get<double>(sum_point_data.value_);
}
else if (nostd::holds_alternative<long>(sum_point_data.value_))
{
sout_ << nostd::get<long>(sum_point_data.value_);
}
}
else if (nostd::holds_alternative<sdk::metrics::HistogramPointData>(point_data))
{
auto histogram_point_data = nostd::get<sdk::metrics::HistogramPointData>(point_data);
sout_ << "\n type : HistogramPointData";
sout_ << "\n timestamp : "
<< std::to_string(histogram_point_data.epoch_nanos_.time_since_epoch().count());
sout_ << "\n count : " << histogram_point_data.count_;
sout_ << "\n sum : ";
if (nostd::holds_alternative<double>(histogram_point_data.sum_))
{
sout_ << nostd::get<double>(histogram_point_data.sum_);
}
else if (nostd::holds_alternative<long>(histogram_point_data.sum_))
{
sout_ << nostd::get<long>(histogram_point_data.sum_);
}

sout_ << "\n buckets : ";
if (nostd::holds_alternative<std::vector<double>>(histogram_point_data.boundaries_))
{
auto &double_boundaries = nostd::get<std::vector<double>>(histogram_point_data.boundaries_);
printVec(sout_, double_boundaries);
}
else if (nostd::holds_alternative<std::vector<long>>(histogram_point_data.boundaries_))
{
auto &long_boundaries = nostd::get<std::vector<long>>(histogram_point_data.boundaries_);
printVec(sout_, long_boundaries);
}

sout_ << "\n counts : ";
printVec(sout_, histogram_point_data.counts_);
}
else if (nostd::holds_alternative<sdk::metrics::LastValuePointData>(point_data))
{
auto last_point_data = nostd::get<sdk::metrics::LastValuePointData>(point_data);
sout_ << "\n type : LastValuePointData";
sout_ << "\n timestamp : "
<< std::to_string(last_point_data.epoch_nanos_.time_since_epoch().count())
<< std::boolalpha << "\n valid : " << last_point_data.is_lastvalue_valid_;
sout_ << "\n value : ";
if (nostd::holds_alternative<double>(last_point_data.value_))
{
sout_ << nostd::get<double>(last_point_data.value_);
}
else if (nostd::holds_alternative<long>(last_point_data.value_))
{
sout_ << nostd::get<long>(last_point_data.value_);
}
}
else if (nostd::holds_alternative<sdk::metrics::DropPointData>(point_data))
{}
}

bool OStreamMetricExporter::ForceFlush(std::chrono::microseconds timeout) noexcept
{
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
return true;
}

bool OStreamMetricExporter::Shutdown(std::chrono::microseconds timeout) noexcept
{
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
is_shutdown_ = true;
return true;
}

bool OStreamMetricExporter::isShutdown() const noexcept
{
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
return is_shutdown_;
}

} // namespace metrics
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
#endif
Loading

0 comments on commit 7947f1e

Please sign in to comment.