Skip to content

Commit

Permalink
Merge branch 'main' into otl_grpc_exporter-export-after-shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored Nov 2, 2021
2 parents 78cd874 + 52f6198 commit 3166535
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 91 deletions.
53 changes: 50 additions & 3 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ build systems for opentelemetry-cpp.
exporter need nlohmann-json to build. This is covered in the build
instructions for each of these components.

### Building as Standalone CMake Project
### Building as standalone CMake Project

1. Getting the opentelementry-cpp source:

Expand Down Expand Up @@ -115,7 +115,7 @@ build systems for opentelemetry-cpp.
$
```

### Incorporating Into An Existing CMake Project
### Incorporating into an existing CMake Project

To use the library from a CMake project, you can locate it directly with
`find_package` and use the imported targets from generated package
Expand Down Expand Up @@ -151,7 +151,7 @@ SDK with their unittests. We use 3.7.2 in our build system.

To install Bazel, consult the [Installing Bazel](https://docs.bazel.build/versions/3.7.0/install.html) guide.

### Building as Standalone Bazel Project
### Building as standalone Bazel Project

1. Getting the opentelementry-cpp source:

Expand Down Expand Up @@ -189,3 +189,50 @@ To install Bazel, consult the [Installing Bazel](https://docs.bazel.build/versio
```

4. The build artifacts will be located under `bazel-bin`

### Incorporating into an existing Bazel Project

- WORKSPACE file:

```console
http_archive(
name = "io_opentelemetry_cpp",
sha256 = "<sha256>",
strip_prefix = "opentelemetry-cpp-1.0.1",
urls = [
"https://github.com/open-telemetry/opentelemetry-cpp/archive/refs/tags/v1.0.1.tar.gz"
],
)

# Load OpenTelemetry dependencies after load.
load("@io_opentelemetry_cpp//bazel:repository.bzl", "opentelemetry_cpp_deps")

opentelemetry_cpp_deps()

# Load gRPC dependencies after load.
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")

grpc_deps()

# Load extra gRPC dependencies due to https://github.com/grpc/grpc/issues/20511
load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")

grpc_extra_deps()

```

- Component level BUILD file:

```console
cc_library(
name = "<name>"
...
deps = [
"@io_opentelemetry_cpp//api",
"@io_opentelemetry_cpp//exporters/otlp:otlp_exporter",
"@io_opentelemetry_cpp//sdk/src/trace",
...
],
...
)
```
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
2 changes: 1 addition & 1 deletion bazel/repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def opentelemetry_cpp_deps():
maybe(
http_archive,
name = "curl",
build_file = "@//bazel:curl.BUILD",
build_file = "@io_opentelemetry_cpp//bazel:curl.BUILD",
sha256 = "ba98332752257b47b9dea6d8c0ad25ec1745c20424f1dd3ff2c99ab59e97cf91",
strip_prefix = "curl-7.73.0",
urls = ["https://curl.haxx.se/download/curl-7.73.0.tar.gz"],
Expand Down
33 changes: 11 additions & 22 deletions exporters/otlp/test/otlp_grpc_exporter_test.cc
100644 → 100755
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 opentelemetry::sdk::common::setenv;
using opentelemetry::sdk::common::unsetenv;
# endif

using namespace testing;
Expand Down Expand Up @@ -164,16 +166,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 @@ -202,23 +201,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
100644 → 100755
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 opentelemetry::sdk::common::setenv;
using opentelemetry::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
100644 → 100755
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 opentelemetry::sdk::common::setenv;
using opentelemetry::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
2 changes: 1 addition & 1 deletion sdk/include/opentelemetry/sdk/logs/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Logger final : public opentelemetry::logs::Logger
const opentelemetry::common::KeyValueIterable &attributes,
opentelemetry::trace::TraceId trace_id,
opentelemetry::trace::SpanId span_id,
trace::TraceFlags trace_flags,
opentelemetry::trace::TraceFlags trace_flags,
opentelemetry::common::SystemTimestamp timestamp) noexcept override;

private:
Expand Down
Loading

0 comments on commit 3166535

Please sign in to comment.