Skip to content
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

Visual Studio compiler warnings clean-up #315

Merged
merged 19 commits into from
Sep 9, 2020
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/include/opentelemetry/context/context.h
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
#include "opentelemetry/context/context_value.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/nostd/string_view.h"
#include <cstring>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace context
@@ -65,7 +66,7 @@ class Context
{
if (key.size() == data->key_length_)
{
if (memcmp(key.data(), data->key_, data->key_length_) == 0)
if (std::memcmp(key.data(), data->key_, data->key_length_) == 0)
{
return data->value_;
}
@@ -81,7 +82,7 @@ class Context
{
if (key.size() == data->key_length_)
{
if (memcmp(key.data(), data->key_, data->key_length_) == 0)
if (std::memcmp(key.data(), data->key_, data->key_length_) == 0)
{
return true;
}
20 changes: 20 additions & 0 deletions api/include/opentelemetry/nostd/string_view.h
Original file line number Diff line number Diff line change
@@ -118,7 +118,12 @@ class string_view
inline bool operator==(string_view lhs, string_view rhs) noexcept
{
return lhs.length() == rhs.length() &&
#if _MSC_VER == 1900
// Avoid SCL error in Visual Studio 2015
(std::memcmp(lhs.data(), rhs.data(), lhs.length()) == 0);
#else
std::equal(lhs.data(), lhs.data() + lhs.length(), rhs.data());
#endif
}

inline bool operator==(string_view lhs, const std::string &rhs) noexcept
@@ -172,3 +177,18 @@ inline std::ostream &operator<<(std::ostream &os, string_view s)
}
} // namespace nostd
OPENTELEMETRY_END_NAMESPACE

namespace std {
template <>
struct hash<OPENTELEMETRY_NAMESPACE::nostd::string_view>
{
std::size_t operator()(const OPENTELEMETRY_NAMESPACE::nostd::string_view& k) const
{
// TODO: for C++17 that has native support for std::basic_string_view it would
// be more performance-efficient to provide a zero-copy hash.
auto s = std::string(k.data(), k.size());
return std::hash<std::string>{}(s);
}
};
}
#endif
3 changes: 3 additions & 0 deletions api/include/opentelemetry/version.h
Original file line number Diff line number Diff line change
@@ -12,4 +12,7 @@

#define OPENTELEMETRY_END_NAMESPACE \
}}

#define OPENTELEMETRY_NAMESPACE opentelemetry :: OPENTELEMETRY_CONCAT(v, OPENTELEMETRY_ABI_VERSION_NO)

// clang-format on
1 change: 1 addition & 0 deletions api/test/metrics/noop_metrics_test.cc
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
#include "opentelemetry/metrics/sync_instruments.h"

#include <memory>
#include <array>

OPENTELEMETRY_BEGIN_NAMESPACE

7 changes: 5 additions & 2 deletions api/test/nostd/utility_test.cc
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

#include <type_traits>
#include <vector>
#include <tuple>

#include <gtest/gtest.h>

@@ -20,7 +21,8 @@ TEST(UtilityTest, Data)
std::vector<int> v = {1, 2, 3};
int array[3] = {1, 2, 3};
std::initializer_list<int> list{1, 2, 3};
int x;
int x = 0;
std::ignore = x;

EXPECT_EQ(opentelemetry::nostd::data(v), v.data());
EXPECT_EQ(opentelemetry::nostd::data(array), array);
@@ -32,7 +34,8 @@ TEST(UtilityTest, Size)
{
std::vector<int> v = {1, 2, 3};
int array[3] = {1, 2, 3};
int x;
int x = 0;
std::ignore = x;

EXPECT_EQ(opentelemetry::nostd::size(v), v.size());
EXPECT_EQ(opentelemetry::nostd::size(array), 3);
2 changes: 1 addition & 1 deletion api/test/trace/key_value_iterable_view_test.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "opentelemetry/trace/key_value_iterable_view.h"

#include <map>

#include "opentelemetry/nostd/type_traits.h"
#include <gtest/gtest.h>

using namespace opentelemetry;
6 changes: 4 additions & 2 deletions ci/do_ci.sh
Original file line number Diff line number Diff line change
@@ -106,8 +106,10 @@ elif [[ "$1" == "bazel.test" ]]; then
bazel test $BAZEL_TEST_OPTIONS //...
exit 0
elif [[ "$1" == "bazel.legacy.test" ]]; then
bazel build $BAZEL_OPTIONS -- //... -//exporters/otlp/...
bazel test $BAZEL_TEST_OPTIONS -- //... -//exporters/otlp/...
# we uses C++ future and async() function to test the Prometheus Exporter functionality,
# that make this test always fail. ignore Prometheus exporter here.
bazel build $BAZEL_OPTIONS -- //... -//exporters/otlp/... -//exporters/prometheus/...
bazel test $BAZEL_TEST_OPTIONS -- //... -//exporters/otlp/... -//exporters/prometheus/...
exit 0
elif [[ "$1" == "bazel.noexcept" ]]; then
# there are some exceptions and error handling code from the Prometheus Client
10 changes: 8 additions & 2 deletions ci/setup_cmake.sh
Original file line number Diff line number Diff line change
@@ -4,13 +4,19 @@ set -e

export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install --no-install-recommends --no-install-suggests -y \

set +e
echo \
cmake \
libbenchmark-dev \
libgtest-dev \
zlib1g-dev \
sudo \
libcurl4-openssl-dev
libcurl4-openssl-dev \
nlohmann-json-dev \
nlohmann-json3 \
nlohmann-json3-dev | xargs -n 1 apt-get install --ignore-missing --no-install-recommends --no-install-suggests -y
set -e

# Follows these instructions for setting up gtest
# https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/
5 changes: 5 additions & 0 deletions ci/setup_windows_ci_environment.ps1
Original file line number Diff line number Diff line change
@@ -11,5 +11,10 @@ $VCPKG_DIR=(Get-Item -Path ".\").FullName
# Patched Google Benchmark can be shared between vs2017 and vs2019 compilers
./vcpkg install --overlay-ports="$PSScriptRoot\ports" benchmark:x64-windows

# Google Test
./vcpkg install gtest:x64-windows

# nlohmann-json
./vcpkg install nlohmann-json:x64-windows

Pop-Location
13 changes: 9 additions & 4 deletions examples/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
add_library(foo_library foo_library/foo_library.cc)
target_link_libraries(foo_library ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api)
include_directories(
${CMAKE_BINARY_DIR}/generated/third_party/opentelemetry-proto)
include_directories(${CMAKE_SOURCE_DIR}/exporters/otlp/include)

add_library(otlp_foo_library foo_library/foo_library.cc)
target_link_libraries(otlp_foo_library ${CMAKE_THREAD_LIBS_INIT}
opentelemetry_api)

add_executable(example_otlp main.cc)
target_link_libraries(example_otlp ${CMAKE_THREAD_LIBS_INIT} foo_library
opentelemetry_trace)
target_link_libraries(example_otlp ${CMAKE_THREAD_LIBS_INIT} otlp_foo_library
opentelemetry_trace opentelemetry_exporter_otprotocol)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <iostream>
#include <string>
#include "opentelemetry/sdk/metrics/aggregator/exact_aggregator.h"
#include "opentelemetry/sdk/metrics/aggregator/gauge_aggregator.h"
#include "opentelemetry/sdk/metrics/aggregator/histogram_aggregator.h"
@@ -83,7 +84,7 @@ class OStreamMetricsExporter final : public sdkmetrics::MetricsExporter
else
{
auto vec = agg->get_checkpoint();
int size = vec.size();
size_t size = vec.size();
int i = 1;

sout_ << "\n values : " << '[';
@@ -104,8 +105,8 @@ class OStreamMetricsExporter final : public sdkmetrics::MetricsExporter
auto boundaries = agg->get_boundaries();
auto counts = agg->get_counts();

int boundaries_size = boundaries.size();
int counts_size = counts.size();
size_t boundaries_size = boundaries.size();
size_t counts_size = counts.size();

sout_ << "\n buckets : " << '[';

@@ -134,8 +135,8 @@ class OStreamMetricsExporter final : public sdkmetrics::MetricsExporter
auto boundaries = agg->get_boundaries();
auto counts = agg->get_counts();

int boundaries_size = boundaries.size();
int counts_size = counts.size();
size_t boundaries_size = boundaries.size();
size_t counts_size = counts.size();

sout_ << "\n buckets : " << '[';

Original file line number Diff line number Diff line change
@@ -146,8 +146,8 @@ class OStreamSpanExporter final : public sdktrace::SpanExporter

void printAttributes(std::unordered_map<std::string, sdktrace::SpanDataAttributeValue> map)
{
int size = map.size();
int i = 1;
size_t size = map.size();
size_t i = 1;
for (auto kv : map)
{
sout_ << kv.first << ": ";
29 changes: 29 additions & 0 deletions exporters/prometheus/BUILD
Original file line number Diff line number Diff line change
@@ -14,6 +14,24 @@

package(default_visibility = ["//visibility:public"])

cc_library(
name = "prometheus_collector",
srcs = [
"src/prometheus_collector.cc",
],
hdrs = [
"include/opentelemetry/exporters/prometheus/prometheus_collector.h",
"include/opentelemetry/exporters/prometheus/prometheus_exporter_utils.h",
],
strip_include_prefix = "include",
deps = [
":prometheus_utils",
"//api",
"//sdk:headers",
"@com_github_jupp0r_prometheus_cpp//core",
],
)

cc_library(
name = "prometheus_utils",
srcs = [
@@ -30,6 +48,17 @@ cc_library(
],
)

cc_test(
name = "prometheus_collector_test",
srcs = [
"test/prometheus_collector_test.cc",
],
deps = [
":prometheus_collector",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "prometheus_exporter_utils_test",
srcs = [
4 changes: 3 additions & 1 deletion exporters/prometheus/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -15,7 +15,9 @@
include_directories(include)

find_package(prometheus-cpp CONFIG REQUIRED)
add_library(prometheus_exporter src/prometheus_exporter_utils.cc)

add_library(prometheus_exporter src/prometheus_collector.cc
src/prometheus_exporter_utils.cc)

if(BUILD_TESTING)
add_subdirectory(test)
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <memory>
#include <mutex>
#include <vector>

#include "opentelemetry/sdk/metrics/record.h"
#include "prometheus/collectable.h"
#include "prometheus/metric_family.h"
#include "prometheus_exporter_utils.h"

namespace prometheus_client = ::prometheus;
namespace metric_sdk = opentelemetry::sdk::metrics;

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace prometheus
{
/**
* The Prometheus Collector maintains the intermediate collection in Prometheus Exporter
*/
class PrometheusCollector : public prometheus_client::Collectable
{
public:
/**
* Default Constructor.
*
* This constructor initializes the collection for metrics to export
* in this class with default capacity
*/
explicit PrometheusCollector(int max_collection_size = 2048);

/**
* Collects all metrics data from metricsToCollect collection.
*
* @return all metrics in the metricsToCollect snapshot
*/
std::vector<prometheus_client::MetricFamily> Collect() const override;

/**
* This function is called by export() function and add the collection of
* records to the metricsToCollect collection
*
* @param records a collection of records to add to the metricsToCollect collection
*/
void AddMetricData(const std::vector<metric_sdk::Record> &records);

/**
* Get the current collection in the collector.
*
* @return the current metricsToCollect collection
*/
std::vector<metric_sdk::Record> GetCollection();

/**
* Gets the maximum size of the collection.
*
* @return max collection size
*/
int GetMaxCollectionSize() const;

private:
/**
* Collection of metrics data from the export() function, and to be export
* to user when they send a pull request. This collection is a pointer
* to a collection so Collect() is able to clear the collection, even
* though it is a const function.
*/
std::unique_ptr<std::vector<metric_sdk::Record>> metrics_to_collect_;

/**
* Maximum size of the metricsToCollect collection.
*/
int max_collection_size_;

/*
* Lock when operating the metricsToCollect collection
*/
mutable std::mutex collection_lock_;
};
} // namespace prometheus
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Loading