Skip to content

Commit

Permalink
Delegate all API calls of gRPC into `opentelemetry_exporter_otlp_grpc…
Browse files Browse the repository at this point in the history
…_client`, and make it contains all symbols needed.

Signed-off-by: owent <admin@owent.net>
  • Loading branch information
owent committed Feb 24, 2023
1 parent 7c0826e commit 399ed00
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 37 deletions.
38 changes: 19 additions & 19 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug on Windows",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/build/<path-to-bin-file>",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false
},
{
"name": "Debug on Linux",
"type": "gdb",
"request": "launch",
"target": "${workspaceFolder}/bazel-bin/<path to the bin file>",
"cwd": "${workspaceRoot}",
"valuesFormatting": "parseText"
}
{
"name": "Debug on Windows",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/build/exporters/ostream/Debug/ostream_log_test.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false
},
{
"name": "Debug on Linux",
"type": "gdb",
"request": "launch",
"target": "${workspaceFolder}/bazel-bin/<path to the bin file>",
"cwd": "${workspaceRoot}",
"valuesFormatting": "parseText"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@

#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"

#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"

#include "opentelemetry/proto/collector/metrics/v1/metrics_service.grpc.pb.h"
#include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h"

#ifdef ENABLE_LOGS_PREVIEW
# include "opentelemetry/proto/collector/logs/v1/logs_service.grpc.pb.h"
#endif

#include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
Expand All @@ -33,14 +44,49 @@ class OtlpGrpcClient
const OtlpGrpcExporterOptions &options);

/**
* Create service stub to communicate with the OpenTelemetry Collector.
* Create gRPC CompletionQueue to async call RPC.
*/
static std::unique_ptr<grpc::CompletionQueue> MakeCompletionQueue();

/**
* Create trace service stub to communicate with the OpenTelemetry Collector.
*/
static std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface>
MakeTraceServiceStub(const OtlpGrpcExporterOptions &options);

/**
* Create metrics service stub to communicate with the OpenTelemetry Collector.
*/
static std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface>
MakeMetricsServiceStub(const OtlpGrpcExporterOptions &options);

#ifdef ENABLE_LOGS_PREVIEW
/**
* Create logs service stub to communicate with the OpenTelemetry Collector.
*/
template <class ServiceType>
static std::unique_ptr<typename ServiceType::Stub> MakeServiceStub(
const OtlpGrpcExporterOptions &options)
{
return ServiceType::NewStub(MakeChannel(options));
}
static std::unique_ptr<proto::collector::logs::v1::LogsService::StubInterface>
MakeLogsServiceStub(const OtlpGrpcExporterOptions &options);
#endif

static grpc::Status DelegateExport(
proto::collector::trace::v1::TraceService::StubInterface *stub,
grpc::ClientContext *context,
const proto::collector::trace::v1::ExportTraceServiceRequest &request,
proto::collector::trace::v1::ExportTraceServiceResponse *response);

static grpc::Status DelegateExport(
proto::collector::metrics::v1::MetricsService::StubInterface *stub,
grpc::ClientContext *context,
const proto::collector::metrics::v1::ExportMetricsServiceRequest &request,
proto::collector::metrics::v1::ExportMetricsServiceResponse *response);

#ifdef ENABLE_LOGS_PREVIEW
static grpc::Status DelegateExport(
proto::collector::logs::v1::LogsService::StubInterface *stub,
grpc::ClientContext *context,
const proto::collector::logs::v1::ExportLogsServiceRequest &request,
proto::collector::logs::v1::ExportLogsServiceResponse *response);
#endif
};
} // namespace otlp
} // namespace exporter
Expand Down
54 changes: 54 additions & 0 deletions exporters/otlp/src/otlp_grpc_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,60 @@ std::unique_ptr<grpc::ClientContext> OtlpGrpcClient::MakeClientContext(
return context;
}

std::unique_ptr<grpc::CompletionQueue> OtlpGrpcClient::MakeCompletionQueue()
{
return std::unique_ptr<grpc::CompletionQueue>(new grpc::CompletionQueue());
}

std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface>
OtlpGrpcClient::MakeTraceServiceStub(const OtlpGrpcExporterOptions &options)
{
return proto::collector::trace::v1::TraceService::NewStub(MakeChannel(options));
}

std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface>
OtlpGrpcClient::MakeMetricsServiceStub(const OtlpGrpcExporterOptions &options)
{
return proto::collector::metrics::v1::MetricsService::NewStub(MakeChannel(options));
}

#ifdef ENABLE_LOGS_PREVIEW
std::unique_ptr<proto::collector::logs::v1::LogsService::StubInterface>
OtlpGrpcClient::MakeLogsServiceStub(const OtlpGrpcExporterOptions &options)
{
return proto::collector::logs::v1::LogsService::NewStub(MakeChannel(options));
}
#endif

grpc::Status OtlpGrpcClient::DelegateExport(
proto::collector::trace::v1::TraceService::StubInterface *stub,
grpc::ClientContext *context,
const proto::collector::trace::v1::ExportTraceServiceRequest &request,
proto::collector::trace::v1::ExportTraceServiceResponse *response)
{
return stub->Export(context, request, response);
}

grpc::Status OtlpGrpcClient::DelegateExport(
proto::collector::metrics::v1::MetricsService::StubInterface *stub,
grpc::ClientContext *context,
const proto::collector::metrics::v1::ExportMetricsServiceRequest &request,
proto::collector::metrics::v1::ExportMetricsServiceResponse *response)
{
return stub->Export(context, request, response);
}

#ifdef ENABLE_LOGS_PREVIEW
grpc::Status OtlpGrpcClient::DelegateExport(
proto::collector::logs::v1::LogsService::StubInterface *stub,
grpc::ClientContext *context,
const proto::collector::logs::v1::ExportLogsServiceRequest &request,
proto::collector::logs::v1::ExportLogsServiceResponse *response)
{
return stub->Export(context, request, response);
}
#endif

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
7 changes: 3 additions & 4 deletions exporters/otlp/src/otlp_grpc_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ namespace otlp
OtlpGrpcExporter::OtlpGrpcExporter() : OtlpGrpcExporter(OtlpGrpcExporterOptions()) {}

OtlpGrpcExporter::OtlpGrpcExporter(const OtlpGrpcExporterOptions &options)
: options_(options),
trace_service_stub_(
OtlpGrpcClient::MakeServiceStub<proto::collector::trace::v1::TraceService>(options))
: options_(options), trace_service_stub_(OtlpGrpcClient::MakeTraceServiceStub(options))
{}

OtlpGrpcExporter::OtlpGrpcExporter(
Expand Down Expand Up @@ -60,7 +58,8 @@ sdk::common::ExportResult OtlpGrpcExporter::Export(
auto context = OtlpGrpcClient::MakeClientContext(options_);
proto::collector::trace::v1::ExportTraceServiceResponse response;

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

if (!status.ok())
{
Expand Down
7 changes: 3 additions & 4 deletions exporters/otlp/src/otlp_grpc_log_record_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ OtlpGrpcLogRecordExporter::OtlpGrpcLogRecordExporter()
{}

OtlpGrpcLogRecordExporter::OtlpGrpcLogRecordExporter(const OtlpGrpcExporterOptions &options)
: options_(options),
log_service_stub_(
OtlpGrpcClient::MakeServiceStub<proto::collector::logs::v1::LogsService>(options))
: options_(options), log_service_stub_(OtlpGrpcClient::MakeLogsServiceStub(options))
{}

OtlpGrpcLogRecordExporter::OtlpGrpcLogRecordExporter(
Expand Down Expand Up @@ -74,7 +72,8 @@ opentelemetry::sdk::common::ExportResult OtlpGrpcLogRecordExporter::Export(
auto context = OtlpGrpcClient::MakeClientContext(options_);
proto::collector::logs::v1::ExportLogsServiceResponse response;

grpc::Status status = log_service_stub_->Export(context.get(), request, &response);
grpc::Status status =
OtlpGrpcClient::DelegateExport(log_service_stub_.get(), context.get(), request, &response);

if (!status.ok())
{
Expand Down
6 changes: 3 additions & 3 deletions exporters/otlp/src/otlp_grpc_metric_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ OtlpGrpcMetricExporter::OtlpGrpcMetricExporter(const OtlpGrpcMetricExporterOptio
: options_(options),
aggregation_temporality_selector_{
OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)},
metrics_service_stub_(
OtlpGrpcClient::MakeServiceStub<proto::collector::metrics::v1::MetricsService>(options))
metrics_service_stub_(OtlpGrpcClient::MakeMetricsServiceStub(options))
{}

OtlpGrpcMetricExporter::OtlpGrpcMetricExporter(
Expand Down Expand Up @@ -67,7 +66,8 @@ opentelemetry::sdk::common::ExportResult OtlpGrpcMetricExporter::Export(
auto context = OtlpGrpcClient::MakeClientContext(options_);
proto::collector::metrics::v1::ExportMetricsServiceResponse response;

grpc::Status status = metrics_service_stub_->Export(context.get(), request, &response);
grpc::Status status = OtlpGrpcClient::DelegateExport(metrics_service_stub_.get(), context.get(),
request, &response);

if (!status.ok())
{
Expand Down

0 comments on commit 399ed00

Please sign in to comment.