Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -8,18 +8,31 @@ namespace exporter
{
namespace otlp
{
/**
* Struct to hold OTLP exporter options.
*/
struct OtlpExporterOptions
{
// The endpoint to export to. By default the OpenTelemetry Collector's default endpoint.
std::string endpoint = "localhost:55680";
};

/**
* 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.
* Create an OtlpExporter using all default options.
*/
OtlpExporter();

/**
* Create an OtlpExporter using the given options.
*/
OtlpExporter(const OtlpExporterOptions &options);

/**
* Create a span recordable.
* @return a newly initialized Recordable object
Expand All @@ -42,6 +55,9 @@ class OtlpExporter final : public opentelemetry::sdk::trace::SpanExporter
std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override{};

private:
// The configuration options associated with this exporter.
const OtlpExporterOptions options_;

// For testing
friend class OtlpExporterTestPeer;

Expand Down
15 changes: 9 additions & 6 deletions exporters/otlp/src/otlp_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace exporter
namespace otlp
{

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

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

/**
Expand All @@ -35,19 +33,24 @@ void PopulateRequest(const nostd::span<std::unique_ptr<sdk::trace::Recordable>>
/**
* Create service stub to communicate with the OpenTelemetry Collector.
*/
std::unique_ptr<proto::collector::trace::v1::TraceService::Stub> MakeServiceStub()
std::unique_ptr<proto::collector::trace::v1::TraceService::Stub> MakeServiceStub(
std::string endpoint)
{
auto channel = grpc::CreateChannel(kCollectorAddress, grpc::InsecureChannelCredentials());
auto channel = grpc::CreateChannel(endpoint, grpc::InsecureChannelCredentials());
return proto::collector::trace::v1::TraceService::NewStub(channel);
}

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

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

OtlpExporter::OtlpExporter(const OtlpExporterOptions &options)
: options_(options), trace_service_stub_(MakeServiceStub(options.endpoint))
{}

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

// ----------------------------- Exporter methods ------------------------------
Expand Down
15 changes: 15 additions & 0 deletions exporters/otlp/test/otlp_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class OtlpExporterTestPeer : public ::testing::Test
{
return std::unique_ptr<sdk::trace::SpanExporter>(new OtlpExporter(std::move(stub_interface)));
}

// Get the options associated with the given exporter.
const OtlpExporterOptions &GetOptions(std::unique_ptr<OtlpExporter> &exporter)
{
return exporter->options_;
}
};

// Call Export() directly
Expand Down Expand Up @@ -77,6 +83,15 @@ TEST_F(OtlpExporterTestPeer, ExportIntegrationTest)
child_span->End();
parent_span->End();
}

// Test exporter configuration options
TEST_F(OtlpExporterTestPeer, ConfigTest)
{
OtlpExporterOptions opts;
opts.endpoint = "localhost:45454";
std::unique_ptr<OtlpExporter> exporter(new OtlpExporter(opts));
EXPECT_EQ(GetOptions(exporter).endpoint, "localhost:45454");
}
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE