-
Notifications
You must be signed in to change notification settings - Fork 548
Add OTLP exporter #152
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
Merged
reyang
merged 13 commits into
open-telemetry:master
from
nadiaciobanu:basic-otlp-exporter
Jul 8, 2020
Merged
Add OTLP exporter #152
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2332a1b
Add exporter
nadiaciobanu e3a748b
Address review comment
nadiaciobanu e3dc330
Fix build by updating gRPC version
nadiaciobanu a92bcd0
Attempt to fix build on MacOS by updating gRPC version
nadiaciobanu fcda784
Change gRPC version for MacOS
nadiaciobanu 26c0ab8
Address review comments
nadiaciobanu 06cba20
MacOS build fix
nadiaciobanu a2e952e
Add option to bazelrc for MacOs
nadiaciobanu 4aff53e
Address more review comments
nadiaciobanu da2d5a5
Address more comments
nadiaciobanu 7841ddd
Fix newline
nadiaciobanu 337985f
Trigger GitHub Actions
nadiaciobanu 80b4985
Merge branch 'master' into basic-otlp-exporter
nadiaciobanu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #include "otlp_exporter.h" | ||
| #include "recordable.h" | ||
|
|
||
| #include <grpcpp/grpcpp.h> | ||
| #include <iostream> | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace exporter | ||
| { | ||
| namespace otlp | ||
| { | ||
|
|
||
| const std::string kCollectorAddress = "localhost:55678"; | ||
|
|
||
| // ----------------------------- Helper functions ------------------------------ | ||
|
|
||
| /** | ||
| * Add span protobufs contained in recordables to request. | ||
| * @param spans the spans to export | ||
| * @param request the current 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())); | ||
| *instrumentation_lib->add_spans() = std::move(rec->span()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create service stub to communicate with the 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 Exporter] Export() failed: " << status.error_message() << "\n"; | ||
| return sdk::trace::ExportResult::kFailure; | ||
| } | ||
| return sdk::trace::ExportResult::kSuccess; | ||
| } | ||
| } // namespace otlp | ||
| } // namespace exporter | ||
| OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #pragma once | ||
|
|
||
| #include "opentelemetry/sdk/trace/exporter.h" | ||
| #include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace exporter | ||
| { | ||
| namespace otlp | ||
| { | ||
| /** | ||
| * The OTLP exporter exports span data in OpenTelemetry Protocol (OTLP) format. | ||
| */ | ||
| class OtlpExporter final : public opentelemetry::sdk::trace::SpanExporter | ||
| { | ||
| public: | ||
| /** | ||
| * Create an OtlpExporter. This constructor initializes a service stub to be | ||
| * used for exporting. | ||
| */ | ||
| OtlpExporter(); | ||
|
nadiaciobanu marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Create a span recordable. | ||
| * @return a newly initialized Recordable object | ||
| */ | ||
| std::unique_ptr<sdk::trace::Recordable> MakeRecordable() noexcept override; | ||
|
|
||
| /** | ||
| * Export a batch of span recordables in OTLP format. | ||
| * @param spans a span of unique pointers to span recordables | ||
| */ | ||
| sdk::trace::ExportResult Export( | ||
| const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &spans) noexcept override; | ||
|
|
||
| /** | ||
| * Shut down the exporter. | ||
| * @param timeout an optional timeout, the default timeout of 0 means that no | ||
| * timeout is applied. | ||
| */ | ||
| void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override {}; | ||
|
|
||
| private: | ||
| // For testing | ||
| friend class OtlpExporterTestPeer; | ||
|
|
||
| // Store service stub internally. Useful for testing. | ||
| std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> trace_service_stub_; | ||
|
|
||
| /** | ||
| * Create an OtlpExporter using the specified service stub. | ||
| * Only tests can call this constructor directly. | ||
| * @param stub the service stub to be used for exporting | ||
| */ | ||
| OtlpExporter(std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> stub); | ||
| }; | ||
| } // namespace otlp | ||
| } // namespace exporter | ||
| OPENTELEMETRY_END_NAMESPACE | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #include "otlp_exporter.h" | ||
| #include "opentelemetry/proto/collector/trace/v1/trace_service_mock.grpc.pb.h" | ||
| #include "opentelemetry/sdk/trace/simple_processor.h" | ||
| #include "opentelemetry/sdk/trace/tracer_provider.h" | ||
| #include "opentelemetry/trace/provider.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| using namespace testing; | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace exporter | ||
| { | ||
| namespace otlp | ||
| { | ||
|
|
||
| class OtlpExporterTestPeer : public ::testing::Test | ||
| { | ||
| public: | ||
| std::unique_ptr<sdk::trace::SpanExporter> GetExporter( | ||
| std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> &stub_interface) | ||
| { | ||
| return std::unique_ptr<sdk::trace::SpanExporter>(new OtlpExporter(std::move(stub_interface))); | ||
| } | ||
| }; | ||
|
|
||
| // Call Export() directly | ||
| TEST_F(OtlpExporterTestPeer, ExportUnitTest) | ||
| { | ||
| auto mock_stub = new proto::collector::trace::v1::MockTraceServiceStub(); | ||
| std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> stub_interface( | ||
| mock_stub); | ||
| auto exporter = GetExporter(stub_interface); | ||
|
|
||
| auto recordable_1 = exporter->MakeRecordable(); | ||
| recordable_1->SetName("Test span 1"); | ||
| auto recordable_2 = exporter->MakeRecordable(); | ||
| recordable_2->SetName("Test span 2"); | ||
|
|
||
| // Test successful RPC | ||
| nostd::span<std::unique_ptr<sdk::trace::Recordable>> batch_1(&recordable_1, 1); | ||
| EXPECT_CALL(*mock_stub, Export(_, _, _)).Times(Exactly(1)).WillOnce(Return(grpc::Status::OK)); | ||
| auto result = exporter->Export(batch_1); | ||
| EXPECT_EQ(sdk::trace::ExportResult::kSuccess, result); | ||
|
|
||
| // Test failed RPC | ||
| nostd::span<std::unique_ptr<sdk::trace::Recordable>> batch_2(&recordable_2, 1); | ||
| EXPECT_CALL(*mock_stub, Export(_, _, _)) | ||
| .Times(Exactly(1)) | ||
| .WillOnce(Return(grpc::Status::CANCELLED)); | ||
| result = exporter->Export(batch_2); | ||
| EXPECT_EQ(sdk::trace::ExportResult::kFailure, result); | ||
| } | ||
|
|
||
| // Create spans, let processor call Export() | ||
| TEST_F(OtlpExporterTestPeer, ExportIntegrationTest) | ||
| { | ||
| auto mock_stub = new proto::collector::trace::v1::MockTraceServiceStub(); | ||
| std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> stub_interface( | ||
| mock_stub); | ||
|
|
||
| auto exporter = GetExporter(stub_interface); | ||
|
|
||
| auto processor = std::shared_ptr<sdk::trace::SpanProcessor>( | ||
| new sdk::trace::SimpleSpanProcessor(std::move(exporter))); | ||
| auto provider = | ||
| nostd::shared_ptr<trace::TracerProvider>(new sdk::trace::TracerProvider(processor)); | ||
| auto tracer = provider->GetTracer("test"); | ||
|
|
||
| EXPECT_CALL(*mock_stub, Export(_, _, _)) | ||
| .Times(AtLeast(1)) | ||
| .WillRepeatedly(Return(grpc::Status::OK)); | ||
|
|
||
| auto parent_span = tracer->StartSpan("Test parent span"); | ||
| auto child_span = tracer->StartSpan("Test child span"); | ||
| child_span->End(); | ||
| parent_span->End(); | ||
| } | ||
| } // namespace otlp | ||
| } // namespace exporter | ||
| OPENTELEMETRY_END_NAMESPACE |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.