diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_exporter.h index 16a5252f0b..bf2ad4a5d4 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_exporter.h @@ -8,6 +8,15 @@ 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. */ @@ -15,11 +24,15 @@ 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 @@ -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; diff --git a/exporters/otlp/src/otlp_exporter.cc b/exporters/otlp/src/otlp_exporter.cc index 0de17e0114..74851976ca 100644 --- a/exporters/otlp/src/otlp_exporter.cc +++ b/exporters/otlp/src/otlp_exporter.cc @@ -10,8 +10,6 @@ namespace exporter namespace otlp { -const std::string kCollectorAddress = "localhost:55678"; - // ----------------------------- Helper functions ------------------------------ /** @@ -35,19 +33,24 @@ void PopulateRequest(const nostd::span> /** * Create service stub to communicate with the OpenTelemetry Collector. */ -std::unique_ptr MakeServiceStub() +std::unique_ptr 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 stub) - : trace_service_stub_(std::move(stub)) + : options_(OtlpExporterOptions()), trace_service_stub_(std::move(stub)) {} // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/otlp/test/otlp_exporter_test.cc b/exporters/otlp/test/otlp_exporter_test.cc index 581eb8f125..a79046f1d3 100644 --- a/exporters/otlp/test/otlp_exporter_test.cc +++ b/exporters/otlp/test/otlp_exporter_test.cc @@ -23,6 +23,12 @@ class OtlpExporterTestPeer : public ::testing::Test { return std::unique_ptr(new OtlpExporter(std::move(stub_interface))); } + + // Get the options associated with the given exporter. + const OtlpExporterOptions &GetOptions(std::unique_ptr &exporter) + { + return exporter->options_; + } }; // Call Export() directly @@ -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 exporter(new OtlpExporter(opts)); + EXPECT_EQ(GetOptions(exporter).endpoint, "localhost:45454"); +} } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE