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

fix compiler warnings #1044

Merged
merged 11 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static const struct

inline const char *attr(uint32_t attr)
{
for (int i = 0; i < OTEL_CPP_TRACE_ATTRIBUTES_MAX; i++)
for (size_t i = 0; i < OTEL_CPP_TRACE_ATTRIBUTES_MAX; i++)
{
if (attribute_ids[i].attribute_id == attr)
return attribute_ids[i].attribute_key;
Expand Down
33 changes: 11 additions & 22 deletions exporters/otlp/test/otlp_grpc_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
# include <gtest/gtest.h>

# if defined(_MSC_VER)
# define putenv _putenv
# include "opentelemetry/sdk/common/env_variables.h"
using sdk::common::setenv;
using sdk::common::unsetenv;
# endif

using namespace testing;
Expand Down Expand Up @@ -139,16 +141,13 @@ TEST_F(OtlpGrpcExporterTestPeer, ConfigSslCredentialsTest)
TEST_F(OtlpGrpcExporterTestPeer, ConfigFromEnv)
{
const std::string cacert_str = "--begin and end fake cert--";
const std::string cacert_env = "OTEL_EXPORTER_OTLP_CERTIFICATE_STRING=" + cacert_str;
putenv(const_cast<char *>(cacert_env.data()));
char ssl_enable_env[] = "OTEL_EXPORTER_OTLP_SSL_ENABLE=True";
putenv(ssl_enable_env);
const std::string endpoint = "http://localhost:9999";
const std::string endpoint_env = "OTEL_EXPORTER_OTLP_ENDPOINT=" + endpoint;
putenv(const_cast<char *>(endpoint_env.data()));
putenv("OTEL_EXPORTER_OTLP_TIMEOUT=20050ms");
putenv("OTEL_EXPORTER_OTLP_HEADERS=k1=v1,k2=v2");
putenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS=k1=v3,k1=v4");
setenv("OTEL_EXPORTER_OTLP_CERTIFICATE_STRING", cacert_str.c_str(), 1);
setenv("OTEL_EXPORTER_OTLP_SSL_ENABLE", "True", 1);
const std::string endpoint = "http://localhost:9999";
setenv("OTEL_EXPORTER_OTLP_ENDPOINT", endpoint.c_str(), 1);
setenv("OTEL_EXPORTER_OTLP_TIMEOUT", "20050ms", 1);
setenv("OTEL_EXPORTER_OTLP_HEADERS", "k1=v1,k2=v2", 1);
setenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS", "k1=v3,k1=v4", 1);

std::unique_ptr<OtlpGrpcExporter> exporter(new OtlpGrpcExporter());
EXPECT_EQ(GetOptions(exporter).ssl_credentials_cacert_as_string, cacert_str);
Expand Down Expand Up @@ -177,23 +176,13 @@ TEST_F(OtlpGrpcExporterTestPeer, ConfigFromEnv)
++range.first;
EXPECT_TRUE(range.first == range.second);
}
# if defined(_MSC_VER)
putenv("OTEL_EXPORTER_OTLP_ENDPOINT=");
putenv("OTEL_EXPORTER_OTLP_CERTIFICATE_STRING=");
putenv("OTEL_EXPORTER_OTLP_SSL_ENABLE=");
putenv("OTEL_EXPORTER_OTLP_TIMEOUT=");
putenv("OTEL_EXPORTER_OTLP_HEADERS=");
putenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS=");

# else

unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT");
unsetenv("OTEL_EXPORTER_OTLP_CERTIFICATE_STRING");
unsetenv("OTEL_EXPORTER_OTLP_SSL_ENABLE");
unsetenv("OTEL_EXPORTER_OTLP_TIMEOUT");
unsetenv("OTEL_EXPORTER_OTLP_HEADERS");
unsetenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS");

# endif
}
# endif

Expand Down
37 changes: 11 additions & 26 deletions exporters/otlp/test/otlp_http_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
# include "nlohmann/json.hpp"

# if defined(_MSC_VER)
# define putenv _putenv
# include "opentelemetry/sdk/common/env_variables.h"
using sdk::common::setenv;
using sdk::common::unsetenv;
# endif

using namespace testing;

OPENTELEMETRY_BEGIN_NAMESPACE
Expand Down Expand Up @@ -348,10 +349,10 @@ TEST_F(OtlpHttpExporterTestPeer, ConfigJsonBytesMappingTest)
TEST_F(OtlpHttpExporterTestPeer, ConfigFromEnv)
{
const std::string url = "http://localhost:9999/v1/traces";
putenv("OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:9999");
putenv("OTEL_EXPORTER_OTLP_TIMEOUT=20s");
putenv("OTEL_EXPORTER_OTLP_HEADERS=k1=v1,k2=v2");
putenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS=k1=v3,k1=v4");
setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:9999", 1);
setenv("OTEL_EXPORTER_OTLP_TIMEOUT", "20s", 1);
setenv("OTEL_EXPORTER_OTLP_HEADERS", "k1=v1,k2=v2", 1);
setenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS", "k1=v3,k1=v4", 1);

std::unique_ptr<OtlpHttpExporter> exporter(new OtlpHttpExporter());
EXPECT_EQ(GetOptions(exporter).url, url);
Expand All @@ -378,28 +379,20 @@ TEST_F(OtlpHttpExporterTestPeer, ConfigFromEnv)
++range.first;
EXPECT_TRUE(range.first == range.second);
}
# if defined(_MSC_VER)
putenv("OTEL_EXPORTER_OTLP_ENDPOINT=");
putenv("OTEL_EXPORTER_OTLP_TIMEOUT=");
putenv("OTEL_EXPORTER_OTLP_HEADERS=");
putenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS=");

# else
unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT");
unsetenv("OTEL_EXPORTER_OTLP_TIMEOUT");
unsetenv("OTEL_EXPORTER_OTLP_HEADERS");
unsetenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS");

# endif
}

TEST_F(OtlpHttpExporterTestPeer, ConfigFromTracesEnv)
{
const std::string url = "http://localhost:9999/v1/traces";
putenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:9999/v1/traces");
putenv("OTEL_EXPORTER_OTLP_TIMEOUT=20s");
putenv("OTEL_EXPORTER_OTLP_HEADERS=k1=v1,k2=v2");
putenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS=k1=v3,k1=v4");
setenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", url.c_str(), 1);
setenv("OTEL_EXPORTER_OTLP_TIMEOUT", "20s", 1);
setenv("OTEL_EXPORTER_OTLP_HEADERS", "k1=v1,k2=v2", 1);
setenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS", "k1=v3,k1=v4", 1);

std::unique_ptr<OtlpHttpExporter> exporter(new OtlpHttpExporter());
EXPECT_EQ(GetOptions(exporter).url, url);
Expand All @@ -426,19 +419,11 @@ TEST_F(OtlpHttpExporterTestPeer, ConfigFromTracesEnv)
++range.first;
EXPECT_TRUE(range.first == range.second);
}
# if defined(_MSC_VER)
putenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=");
putenv("OTEL_EXPORTER_OTLP_TIMEOUT=");
putenv("OTEL_EXPORTER_OTLP_HEADERS=");
putenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS=");

# else
unsetenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT");
unsetenv("OTEL_EXPORTER_OTLP_TIMEOUT");
unsetenv("OTEL_EXPORTER_OTLP_HEADERS");
unsetenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS");

# endif
}
# endif

Expand Down
36 changes: 11 additions & 25 deletions exporters/otlp/test/otlp_http_log_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
# include "nlohmann/json.hpp"

# if defined(_MSC_VER)
# define putenv _putenv
# include "opentelemetry/sdk/common/env_variables.h"
using sdk::common::setenv;
using sdk::common::unsetenv;
# endif

using namespace testing;
Expand Down Expand Up @@ -390,10 +392,10 @@ TEST_F(OtlpHttpLogExporterTestPeer, ConfigJsonBytesMappingTest)
TEST_F(OtlpHttpLogExporterTestPeer, ConfigFromEnv)
{
const std::string url = "http://localhost:9999/v1/logs";
putenv("OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:9999");
putenv("OTEL_EXPORTER_OTLP_TIMEOUT=20s");
putenv("OTEL_EXPORTER_OTLP_HEADERS=k1=v1,k2=v2");
putenv("OTEL_EXPORTER_OTLP_LOGS_HEADERS=k1=v3,k1=v4");
setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:9999", 1);
setenv("OTEL_EXPORTER_OTLP_TIMEOUT", "20s", 1);
setenv("OTEL_EXPORTER_OTLP_HEADERS", "k1=v1,k2=v2", 1);
setenv("OTEL_EXPORTER_OTLP_LOGS_HEADERS", "k1=v3,k1=v4", 1);

std::unique_ptr<OtlpHttpLogExporter> exporter(new OtlpHttpLogExporter());
EXPECT_EQ(GetOptions(exporter).url, url);
Expand All @@ -420,28 +422,20 @@ TEST_F(OtlpHttpLogExporterTestPeer, ConfigFromEnv)
++range.first;
EXPECT_TRUE(range.first == range.second);
}
# if defined(_MSC_VER)
putenv("OTEL_EXPORTER_OTLP_ENDPOINT=");
putenv("OTEL_EXPORTER_OTLP_TIMEOUT=");
putenv("OTEL_EXPORTER_OTLP_HEADERS=");
putenv("OTEL_EXPORTER_OTLP_LOGS_HEADERS=");

# else
unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT");
unsetenv("OTEL_EXPORTER_OTLP_TIMEOUT");
unsetenv("OTEL_EXPORTER_OTLP_HEADERS");
unsetenv("OTEL_EXPORTER_OTLP_LOGS_HEADERS");

# endif
}

TEST_F(OtlpHttpLogExporterTestPeer, ConfigFromLogsEnv)
{
const std::string url = "http://localhost:9999/v1/logs";
putenv("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://localhost:9999/v1/logs");
putenv("OTEL_EXPORTER_OTLP_TIMEOUT=20s");
putenv("OTEL_EXPORTER_OTLP_HEADERS=k1=v1,k2=v2");
putenv("OTEL_EXPORTER_OTLP_LOGS_HEADERS=k1=v3,k1=v4");
setenv("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT", url.c_str(), 1);
setenv("OTEL_EXPORTER_OTLP_TIMEOUT", "20s", 1);
setenv("OTEL_EXPORTER_OTLP_HEADERS", "k1=v1,k2=v2", 1);
setenv("OTEL_EXPORTER_OTLP_LOGS_HEADERS", "k1=v3,k1=v4", 1);

std::unique_ptr<OtlpHttpLogExporter> exporter(new OtlpHttpLogExporter());
EXPECT_EQ(GetOptions(exporter).url, url);
Expand All @@ -468,19 +462,11 @@ TEST_F(OtlpHttpLogExporterTestPeer, ConfigFromLogsEnv)
++range.first;
EXPECT_TRUE(range.first == range.second);
}
# if defined(_MSC_VER)
putenv("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=");
putenv("OTEL_EXPORTER_OTLP_TIMEOUT=");
putenv("OTEL_EXPORTER_OTLP_HEADERS=");
putenv("OTEL_EXPORTER_OTLP_LOGS_HEADERS=");

# else
unsetenv("OTEL_EXPORTER_OTLP_LOGS_ENDPOINT");
unsetenv("OTEL_EXPORTER_OTLP_TIMEOUT");
unsetenv("OTEL_EXPORTER_OTLP_HEADERS");
unsetenv("OTEL_EXPORTER_OTLP_LOGS_HEADERS");

# endif
}

TEST_F(OtlpHttpLogExporterTestPeer, DefaultEndpoint)
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/test/otlp_recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ TEST(OtlpRecordable, SetResource)

auto proto_resource = rec.ProtoResource();
bool found_service_name = false;
for (size_t i = 0; i < proto_resource.attributes_size(); i++)
for (int i = 0; i < proto_resource.attributes_size(); i++)
{
auto attr = proto_resource.attributes(static_cast<int>(i));
if (attr.key() == service_name_key && attr.value().string_value() == service_name)
Expand Down
12 changes: 12 additions & 0 deletions sdk/include/opentelemetry/sdk/common/env_variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ namespace sdk
namespace common
{

#if defined(_MSC_VER)
inline int setenv(const char *name, const char *value, int)
{
return _putenv_s(name, value);
}

inline int unsetenv(const char *name)
{
return setenv(name, "", 1);
}
#endif

// Returns the env variable set.
inline const std::string GetEnvironmentVariable(const char *env_var_name)
{
Expand Down
24 changes: 11 additions & 13 deletions sdk/test/resource/resource_test.cc
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

#include <gtest/gtest.h>

#if defined(_MSC_VER)
# define putenv _putenv
#endif
# if defined(_MSC_VER)
# include "opentelemetry/sdk/common/env_variables.h"
using sdk::common::setenv;
using sdk::common::unsetenv;
# endif

using namespace opentelemetry::sdk::resource;

Expand Down Expand Up @@ -45,6 +47,7 @@ TEST(ResourceTest, create_without_servicename)
{
EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
if (expected_attributes.find(e.first) != expected_attributes.end())
{
if (e.first == "version")
EXPECT_EQ(opentelemetry::nostd::get<uint32_t>(expected_attributes.find(e.first)->second),
opentelemetry::nostd::get<uint32_t>(e.second));
Expand All @@ -54,6 +57,7 @@ TEST(ResourceTest, create_without_servicename)
else
EXPECT_EQ(opentelemetry::nostd::get<std::string>(expected_attributes.find(e.first)->second),
opentelemetry::nostd::get<std::string>(e.second));
}
lalitb marked this conversation as resolved.
Show resolved Hide resolved
}
EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name
}
Expand Down Expand Up @@ -106,8 +110,10 @@ TEST(ResourceTest, create_with_emptyatrributes)
{
EXPECT_TRUE(expected_attributes.find(e.first) != expected_attributes.end());
if (expected_attributes.find(e.first) != expected_attributes.end())
{
EXPECT_EQ(opentelemetry::nostd::get<std::string>(expected_attributes.find(e.first)->second),
opentelemetry::nostd::get<std::string>(e.second));
}
}
EXPECT_EQ(received_attributes.size(), expected_attributes.size()); // for missing service.name
}
Expand Down Expand Up @@ -170,8 +176,7 @@ TEST(ResourceTest, OtelResourceDetector)
{
std::map<std::string, std::string> expected_attributes = {{"k", "v"}};

char env[] = "OTEL_RESOURCE_ATTRIBUTES=k=v";
putenv(env);
setenv("OTEL_RESOURCE_ATTRIBUTES", "k=v", 1);

OTELResourceDetector detector;
auto resource = detector.Detect();
Expand All @@ -186,21 +191,14 @@ TEST(ResourceTest, OtelResourceDetector)
}
}
EXPECT_EQ(received_attributes.size(), expected_attributes.size());
# if defined(_MSC_VER)
putenv("OTEL_RESOURCE_ATTRIBUTES=");
# else

unsetenv("OTEL_RESOURCE_ATTRIBUTES");
# endif
}

TEST(ResourceTest, OtelResourceDetectorEmptyEnv)
{
std::map<std::string, std::string> expected_attributes = {};
# if defined(_MSC_VER)
putenv("OTEL_RESOURCE_ATTRIBUTES=");
# else
unsetenv("OTEL_RESOURCE_ATTRIBUTES");
# endif
OTELResourceDetector detector;
auto resource = detector.Detect();
auto received_attributes = resource.GetAttributes();
Expand Down