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

[Obsolete] OTLP exporter #151

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
734c627
Add simple example to exporter dir for experimentation
nadiaciobanu Jun 19, 2020
23dc12e
Add trace service protobuf dependency
nadiaciobanu Jun 19, 2020
609bd1d
Try adding skylib, reformatting
nadiaciobanu Jun 19, 2020
0426132
Fix build by adding gRPC load to top of WORKSPACE
nadiaciobanu Jun 22, 2020
009776e
Add recordable to exporter
nadiaciobanu Jun 22, 2020
99e3672
Add basic recordable functions
nadiaciobanu Jun 22, 2020
e326783
Add start time
nadiaciobanu Jun 23, 2020
bb8b215
Add duration and ids
nadiaciobanu Jun 23, 2020
257d03a
Add recordable test
nadiaciobanu Jun 23, 2020
249b308
Move SetIds to its own test
nadiaciobanu Jun 24, 2020
b85b49a
Fix SetIds by not converting to hex
nadiaciobanu Jun 24, 2020
78cf8e0
Reformat tests
nadiaciobanu Jun 24, 2020
a53e871
Fix Recordable instantiation in test
nadiaciobanu Jun 24, 2020
91f454a
Merge branch 'master' into basic-otlp-recordable
nadiaciobanu Jun 24, 2020
ce61e40
Update code for new function (SetAttribute)
nadiaciobanu Jun 24, 2020
11ce576
Fix undefined reference error
nadiaciobanu Jun 24, 2020
bc98b81
Fix memory leak
nadiaciobanu Jun 25, 2020
a61c01f
Address review comments
nadiaciobanu Jun 25, 2020
23787a8
merge changes from basic-otlp-recordable
nadiaciobanu Jun 25, 2020
69c5c32
Return OTLP Recordable
nadiaciobanu Jun 25, 2020
de7c586
Return OTLP Recordable in MakeRecordable
nadiaciobanu Jun 26, 2020
2dbb94c
Use resource spans
nadiaciobanu Jun 26, 2020
9d39c2f
Get span from resource
nadiaciobanu Jun 26, 2020
e598dab
Export to collector
nadiaciobanu Jun 26, 2020
5b44b1b
Change port for collector
nadiaciobanu Jun 30, 2020
6ff134a
Set IDs in exporter
nadiaciobanu Jun 30, 2020
0468abe
Export traces to GCP!
nadiaciobanu Jun 30, 2020
4a5be17
Add otlp_exporter.cc file
nadiaciobanu Jun 30, 2020
a6b804d
Add test and simplify namespaces
nadiaciobanu Jun 30, 2020
8a580f6
Extend test, delete extra files
nadiaciobanu Jul 1, 2020
268d59c
Set IDs in test instead of exporter
nadiaciobanu Jul 1, 2020
6b428a3
Always Off Sampler (#125)
nholbrook Jun 26, 2020
3d50683
Always on sampler (#122)
ziqizh Jun 29, 2020
28b354c
Add compare operators to nostd::string_view (#124)
maxgolov Jun 29, 2020
b2f7b13
Start zPages README (#131)
jajanet Jun 30, 2020
b235655
Rebase
nadiaciobanu Jul 2, 2020
0abac8f
Update recordable for new proto defs
nadiaciobanu Jul 2, 2020
3d5e14d
Refactor export
nadiaciobanu Jul 2, 2020
4668266
Update OpenTelemetry protobuf definitions (#140)
nadiaciobanu Jul 2, 2020
a7a7693
Use mocks in test
nadiaciobanu Jul 2, 2020
ec36cb9
Update tests
nadiaciobanu Jul 2, 2020
c328581
Fix test
nadiaciobanu Jul 2, 2020
6a46525
Update workspace
nadiaciobanu Jul 2, 2020
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
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.0
3.3.0
25 changes: 25 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ workspace(name = "io_opentelemetry_cpp")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Load gRPC dependency
# Note that this dependency needs to be loaded first due to
# https://github.com/bazelbuild/bazel/issues/6664
http_archive(
name = "com_github_grpc_grpc",
sha256 = "d6af0859d3ae4693b1955e972aa2e590d6f4d44baaa82651467c6beea453e30e",
strip_prefix = "grpc-1.26.0-pre1",
urls = [
"https://github.com/grpc/grpc/archive/v1.26.0-pre1.tar.gz",
],
)

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()

load("@upb//bazel:repository_defs.bzl", "bazel_version_repository")

bazel_version_repository(name = "upb_bazel_version")

# Uses older protobuf version because of
# https://github.com/protocolbuffers/protobuf/issues/7179
http_archive(
Expand Down
64 changes: 57 additions & 7 deletions api/include/opentelemetry/nostd/string_view.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <algorithm>
#include <cstddef>
#include <cstring>
#include <ostream>
#include <stdexcept>
Expand All @@ -11,6 +12,9 @@
OPENTELEMETRY_BEGIN_NAMESPACE
namespace nostd
{

using Traits = std::char_traits<char>;

/**
* Back port of std::string_view to work with pre-cpp-17 compilers.
*
Expand All @@ -20,7 +24,9 @@ namespace nostd
class string_view
{
public:
static constexpr std::size_t npos = static_cast<std::size_t>(-1);
typedef std::size_t size_type;

static constexpr size_type npos = static_cast<size_type>(-1);

string_view() noexcept : length_(0), data_(nullptr) {}

Expand All @@ -30,25 +36,25 @@ class string_view
: length_(str.length()), data_(str.c_str())
{}

string_view(const char *str, size_t len) noexcept : length_(len), data_(str) {}
string_view(const char *str, size_type len) noexcept : length_(len), data_(str) {}

explicit operator std::string() const { return {data_, length_}; }

const char *data() const noexcept { return data_; }

bool empty() const noexcept { return length_ == 0; }

size_t length() const noexcept { return length_; }
size_type length() const noexcept { return length_; }

size_t size() const noexcept { return length_; }
size_type size() const noexcept { return length_; }

const char *begin() const noexcept { return data(); }

const char *end() const noexcept { return data() + length(); }

const char &operator[](std::size_t i) { return *(data() + i); }
const char &operator[](size_type i) { return *(data() + i); }

string_view substr(std::size_t pos, std::size_t n = npos) const
string_view substr(size_type pos, size_type n = npos) const
{
if (pos > length_)
{
Expand All @@ -62,11 +68,55 @@ class string_view
return string_view(data_ + pos, n);
}

int compare(string_view v) const noexcept
{
size_type len = std::min(size(), v.size());
int result = Traits::compare(data(), v.data(), len);
if (result == 0)
result = size() == v.size() ? 0 : (size() < v.size() ? -1 : 1);
return result;
};

int compare(size_type pos1, size_type count1, string_view v) const
{
return substr(pos1, count1).compare(v);
};

int compare(size_type pos1, size_type count1, string_view v, size_type pos2, size_type count2) const
{
return substr(pos1, count1).compare(v.substr(pos2, count2));
};

int compare(const char *s) const
{
return compare(string_view(s));
};

int compare(size_type pos1, size_type count1, const char *s) const
{
return substr(pos1, count1).compare(string_view(s));
};

int compare(size_type pos1, size_type count1, const char *s, size_type count2) const
{
return substr(pos1, count1).compare(string_view(s, count2));
};

bool operator<(const string_view v) const noexcept
{
return compare(v) < 0;
}

bool operator>(const string_view v) const noexcept
{
return compare(v) > 0;
}

private:
// Note: uses the same binary layout as libstdc++'s std::string_view
// See
// https://github.com/gcc-mirror/gcc/blob/e0c554e4da7310df83bb1dcc7b8e6c4c9c5a2a4f/libstdc%2B%2B-v3/include/std/string_view#L466-L467
size_t length_;
size_type length_;
const char *data_;
};

Expand Down
30 changes: 30 additions & 0 deletions api/test/nostd/string_view_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <gtest/gtest.h>

#include "map"

using opentelemetry::nostd::string_view;

TEST(StringViewTest, DefaultConstruction)
Expand Down Expand Up @@ -72,3 +74,31 @@ TEST(StringViewTest, SubstrOutOfRange)
EXPECT_DEATH({ s.substr(10); }, "");
#endif
}

TEST(StringViewTest, Compare)
{
string_view s1 = "aaa";
string_view s2 = "bbb";
string_view s3 = "aaa";

// Equals
EXPECT_EQ(s1, s3);
EXPECT_EQ(s1, s1);

// Less then
EXPECT_LT(s1, s2);

// Greater then
EXPECT_GT(s2, s1);
}

TEST(StringViewTest, MapKeyOrdering)
{
std::map<string_view, size_t> m = {{"bbb", 2}, {"aaa", 1}, {"ccc", 3}};
size_t i = 1;
for (const auto &kv : m)
{
EXPECT_EQ(kv.second, i);
i++;
}
}
25 changes: 25 additions & 0 deletions bazel/opentelemetry_proto.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package(default_visibility = ["//visibility:public"])

load("@rules_proto//proto:defs.bzl", "proto_library")

load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library")

proto_library(
name = "common_proto",
srcs = [
Expand Down Expand Up @@ -58,3 +60,26 @@ cc_proto_library(
name = "trace_proto_cc",
deps = [":trace_proto"],
)

proto_library(
name = "trace_service_proto",
srcs = [
"opentelemetry/proto/collector/trace/v1/trace_service.proto",
],
deps = [
":trace_proto",
],
)

cc_proto_library(
name = "trace_service_proto_cc",
deps = [":trace_service_proto"],
)

cc_grpc_library(
name = "trace_service_grpc_cc",
srcs = [":trace_service_proto"],
grpc_only = True,
deps = [":trace_service_proto_cc"],
generate_mocks = True,
)
38 changes: 38 additions & 0 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,44 @@ cc_library(
include_prefix = "exporters/otlp",
deps = [
"//sdk/src/trace",
"//sdk/src/common:random",
"@com_github_opentelemetry_proto//:trace_proto_cc",
],
)

cc_library(
name = "otlp_exporter",
srcs = [
'otlp_exporter.h',
'otlp_exporter.cc',
],
deps = [
":recordable",
"//api",
"//sdk/src/trace",
"@com_github_opentelemetry_proto//:trace_proto_cc",
"@com_github_opentelemetry_proto//:trace_service_proto_cc",

# For gRPC
"@com_github_opentelemetry_proto//:trace_service_grpc_cc",
"@com_github_grpc_grpc//:grpc++",
],
)

cc_test(
name = "recordable_test",
srcs = ["recordable_test.cc"],
deps = [
":recordable",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "otlp_exporter_test",
srcs = ["otlp_exporter_test.cc"],
deps = [
":otlp_exporter",
"@com_google_googletest//:gtest_main",
],
)
8 changes: 8 additions & 0 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
add_library(opentelemetry_exporter_otprotocol recordable.cc)
target_link_libraries(opentelemetry_exporter_otprotocol
$<TARGET_OBJECTS:opentelemetry_proto>)

add_executable(recordable_test recordable_test.cc)
target_link_libraries(recordable_test
${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
opentelemetry_exporter_otprotocol
protobuf::libprotobuf)
gtest_add_tests(TARGET recordable_test TEST_PREFIX exporter. TEST_LIST recordable_test)
71 changes: 71 additions & 0 deletions exporters/otlp/otlp_exporter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "otlp_exporter.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{

const std::string KCollectorAddress = "localhost:55678";

// ----------------------------- Helper functions ------------------------------

// Add span protobufs contained in recordables to request
void PopulateRequest(const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &spans,
proto::collector::trace::v1::ExportTraceServiceRequest *request)
{
auto resource_span = request->add_resource_spans();
auto instrumentation_lib = resource_span->add_instrumentation_library_spans();

for (auto &recordable : spans) {
auto rec = std::unique_ptr<Recordable>(
static_cast<Recordable *>(recordable.release()));

proto::trace::v1::Span* span = instrumentation_lib->add_spans();
span->CopyFrom(rec->span());
}
}

// Establish connection to OpenTelemetry Collector
std::unique_ptr<proto::collector::trace::v1::TraceService::Stub> MakeServiceStub()
{
auto channel = grpc::CreateChannel(KCollectorAddress, grpc::InsecureChannelCredentials());
return proto::collector::trace::v1::TraceService::NewStub(channel);
}

// -------------------------------- Contructors --------------------------------

OtlpExporter::OtlpExporter(): OtlpExporter(MakeServiceStub()) {}

OtlpExporter::OtlpExporter(
std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> stub):
trace_service_stub_(std::move(stub)) {}

// ----------------------------- Exporter methods ------------------------------

std::unique_ptr<sdk::trace::Recordable> OtlpExporter::MakeRecordable() noexcept
{
return std::unique_ptr<sdk::trace::Recordable>(new Recordable);
}

sdk::trace::ExportResult OtlpExporter::Export(
const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &spans) noexcept
{
proto::collector::trace::v1::ExportTraceServiceRequest request;

PopulateRequest(spans, &request);

grpc::ClientContext context;
proto::collector::trace::v1::ExportTraceServiceResponse response;

grpc::Status status = trace_service_stub_->Export(&context, request, &response);

if(!status.ok()){
std::cerr << "OTLP trace exporter: Export() failed\n";
return sdk::trace::ExportResult::kFailure;
}
return sdk::trace::ExportResult::kSuccess;
}
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
41 changes: 41 additions & 0 deletions exporters/otlp/otlp_exporter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include "opentelemetry/sdk/trace/exporter.h"
#include "opentelemetry/sdk/trace/span_data.h"
#include "opentelemetry/proto/collector/trace/v1/trace_service.pb.h"
#include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h"
#include "opentelemetry/proto/trace/v1/trace.pb.h"
#include "src/common/random.h"
#include "recordable.h"

#include <grpcpp/grpcpp.h>
#include <iostream>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{
class OtlpExporter final : public opentelemetry::sdk::trace::SpanExporter
{
public:
OtlpExporter();

std::unique_ptr<sdk::trace::Recordable> MakeRecordable() noexcept override;

sdk::trace::ExportResult Export(
const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &spans) noexcept override;

void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override {};

private:
// For testing
friend class OtlpExporterTestPeer;

std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> trace_service_stub_;

OtlpExporter(std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> stub);
};
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Loading