-
Notifications
You must be signed in to change notification settings - Fork 417
/
otlp_grpc_exporter.cc
153 lines (134 loc) · 5.17 KB
/
otlp_grpc_exporter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include <memory>
#include <mutex>
#include "opentelemetry/common/macros.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_client.h"
#include "opentelemetry/exporters/otlp/otlp_recordable.h"
#include "opentelemetry/exporters/otlp/otlp_recordable_utils.h"
#include "opentelemetry/sdk_config.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_utils.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{
// -------------------------------- Constructors --------------------------------
OtlpGrpcExporter::OtlpGrpcExporter() : OtlpGrpcExporter(OtlpGrpcExporterOptions()) {}
OtlpGrpcExporter::OtlpGrpcExporter(const OtlpGrpcExporterOptions &options)
: options_(options),
#ifdef ENABLE_ASYNC_EXPORT
client_(std::make_shared<OtlpGrpcClient>()),
#endif
trace_service_stub_(OtlpGrpcClient::MakeTraceServiceStub(options))
{}
OtlpGrpcExporter::OtlpGrpcExporter(
std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> stub)
: options_(OtlpGrpcExporterOptions()),
#ifdef ENABLE_ASYNC_EXPORT
client_(std::make_shared<OtlpGrpcClient>()),
#endif
trace_service_stub_(std::move(stub))
{}
// ----------------------------- Exporter methods ------------------------------
std::unique_ptr<sdk::trace::Recordable> OtlpGrpcExporter::MakeRecordable() noexcept
{
return std::unique_ptr<sdk::trace::Recordable>(new OtlpRecordable);
}
sdk::common::ExportResult OtlpGrpcExporter::Export(
const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &spans) noexcept
{
if (isShutdown())
{
OTEL_INTERNAL_LOG_ERROR("[OTLP gRPC] Exporting " << spans.size()
<< " span(s) failed, exporter is shutdown");
return sdk::common::ExportResult::kFailure;
}
if (spans.empty())
{
return sdk::common::ExportResult::kSuccess;
}
google::protobuf::ArenaOptions arena_options;
// It's easy to allocate datas larger than 1024 when we populate basic resource and attributes
arena_options.initial_block_size = 1024;
// When in batch mode, it's easy to export a large number of spans at once, we can alloc a lager
// block to reduce memory fragments.
arena_options.max_block_size = 65536;
std::unique_ptr<google::protobuf::Arena> arena{new google::protobuf::Arena{arena_options}};
proto::collector::trace::v1::ExportTraceServiceRequest *request =
google::protobuf::Arena::Create<proto::collector::trace::v1::ExportTraceServiceRequest>(
arena.get());
OtlpRecordableUtils::PopulateRequest(spans, request);
auto context = OtlpGrpcClient::MakeClientContext(options_);
proto::collector::trace::v1::ExportTraceServiceResponse *response =
google::protobuf::Arena::Create<proto::collector::trace::v1::ExportTraceServiceResponse>(
arena.get());
#ifdef ENABLE_ASYNC_EXPORT
if (options_.max_concurrent_requests > 1)
{
return client_->DelegateAsyncExport(
options_, trace_service_stub_.get(), std::move(context), std::move(arena),
std::move(*request),
[](opentelemetry::sdk::common::ExportResult result,
std::unique_ptr<google::protobuf::Arena> &&,
const proto::collector::trace::v1::ExportTraceServiceRequest &request,
proto::collector::trace::v1::ExportTraceServiceResponse *) {
if (result != opentelemetry::sdk::common::ExportResult::kSuccess)
{
OTEL_INTERNAL_LOG_ERROR("[OTLP TRACE GRPC Exporter] ERROR: Export "
<< request.resource_spans_size()
<< " trace span(s) error: " << static_cast<int>(result));
}
else
{
OTEL_INTERNAL_LOG_DEBUG("[OTLP TRACE GRPC Exporter] Export "
<< request.resource_spans_size() << " trace span(s) success");
}
return true;
});
}
else
{
#endif
grpc::Status status =
OtlpGrpcClient::DelegateExport(trace_service_stub_.get(), std::move(context),
std::move(arena), std::move(*request), response);
if (!status.ok())
{
OTEL_INTERNAL_LOG_ERROR("[OTLP TRACE GRPC Exporter] Export() failed with status_code: \""
<< grpc_utils::grpc_status_code_to_string(status.error_code())
<< "\" error_message: \"" << status.error_message() << "\"");
return sdk::common::ExportResult::kFailure;
}
#ifdef ENABLE_ASYNC_EXPORT
}
#endif
return sdk::common::ExportResult::kSuccess;
}
bool OtlpGrpcExporter::ForceFlush(
OPENTELEMETRY_MAYBE_UNUSED std::chrono::microseconds timeout) noexcept
{
#ifdef ENABLE_ASYNC_EXPORT
return client_->ForceFlush(timeout);
#else
return true;
#endif
}
bool OtlpGrpcExporter::Shutdown(
OPENTELEMETRY_MAYBE_UNUSED std::chrono::microseconds timeout) noexcept
{
is_shutdown_ = true;
#ifdef ENABLE_ASYNC_EXPORT
return client_->Shutdown(timeout);
#else
return true;
#endif
}
bool OtlpGrpcExporter::isShutdown() const noexcept
{
return is_shutdown_;
}
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE